-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
possible solution for issue #26028 #26324
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Made some investigation: - as written in my post, energy has a device_class and state_class power and current don't. - searched for the reason why energy behaves different to power and current - in [homeassistant.ts](https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/extension/homeassistant.ts) I found this code in line 1018: ``` // If a variable includes Wh, mark it as energy if (firstExpose.unit && ['Wh', 'kWh'].includes(firstExpose.unit)) { Object.assign(extraAttrs, {device_class: 'energy', state_class: 'total_increasing'}); } ``` I think a possible solution could be to add following ``` // If a variable includes A or mA, mark it as energy if (firstExpose.unit && ['A', 'mA'].includes(firstExpose.unit)) { Object.assign(extraAttrs, {device_class: 'current', state_class: 'measurement'}); } // If a variable includes mW, W, kW, MW, GW, TW, mark it as energy if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) { Object.assign(extraAttrs, {device_class: 'power', state_class: 'measurement'}); } ``` But I don't know how to test the changes in my homeassistant if that would fix the problem. Didn't found homeassistant.ts in my installation (using VS-Code-extension). Perhaps it's also way for other sensors with other units, e.g. Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s or GiB/s (see [here](https://www.home-assistant.io/integrations/sensor#device-class)).
Just saw, I forgot to change "mark it as power" and "mark it as current" instead of "mark it as energy" in the comments. Don't know how to edit the pr. |
Koenkk
reviewed
Feb 12, 2025
lib/extension/homeassistant.ts
Outdated
} | ||
|
||
// If a variable includes mW, W, kW, MW, GW, TW, mark it as energy | ||
if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change
if (firstExpose.unit && ['mW', 'W', 'kW', 'MW', 'GW', 'TW'].includes(firstExpose.unit)) { | |
if (firstExpose.unit && ['mW', 'W', 'kW'].includes(firstExpose.unit)) { |
It's unlikely that these will ever be there. Also could you make it and if/else if
statement?
implemented the suggestion of @Koenkk
Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Made some investigation:
I think a possible solution could be to add following
But I don't know how to test the changes in my homeassistant if that would fix the problem. Didn't found homeassistant.ts in my installation (using VS-Code-extension). Perhaps it's also way for other sensors with other units, e.g. Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s or GiB/s (see here).