home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.76k stars 30.47k forks source link

Wrong unit of measurement kkWh with Riemann sum helper #115077

Closed bramstroker closed 6 months ago

bramstroker commented 6 months ago

The problem

I have got multiple complaints of users of my custom component Powercalc last days which are unable to setup energy sensors anymore. Errors appear in the log regarding invalid unit of measurement kkWh Essentially Powercalc reuses the integration sensor code provided by HA core, so I have asked users to setup the energy sensor using Riemann sum helper directly to rule out Powercalc bridge code to be the problem.

We can confirm the problem is actually in the core helper code, which also seems to be refactored a lot lately so probably a regression.

Reproduction steps

Observe the following error in log:

Entity sensor.kilowatt_energy (<class 'homeassistant.components.integration.sensor.IntegrationSensor'>) is using native unit of measurement 'kkWh' which is not a valid unit for the device class ('energy') it is using; expected one of ['MWh', 'MJ', 'Wh', 'kWh', 'GJ']; Please update your configuration if your entity is manually configured

And the energy sensor being unavailable.

What version of Home Assistant Core has the issue?

core-2024.4.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

integration

Link to integration documentation on our website

https://www.home-assistant.io/integrations/integration/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 6 months ago

Hey there @dgomes, mind taking a look at this issue as it has been labeled with an integration (integration) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `integration` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign integration` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


integration documentation integration source (message by IssueLinks)

dgomes commented 6 months ago

When creating the Riemann Sum Integration you should not choose ANY metric_prefix as you already have 'k'

bigbadoooff commented 6 months ago

How do I do this with the GUI? There is no option to ommit the prefix and I end up with sensors of kkWh.

dgomes commented 6 months ago

please open a new issue on the frontend repository. There should be the possibility to remove the selection of the unit.

If you never click any of the units, everything is OK (tested)

sangvikh commented 6 months ago

Hi. I am having a similar issue. I made a sensor with the k prefix, causing the unit to be kkWh, but whenever i delete and recreate the sensor, it always ends up on kkWh even with no prefix selected. A new sensor created gets the correct unit.

Do I have to wait until the old sensor data is purged from the history? Tried calling the recorder:purge service, but old data still shows up when recreating the sensor.

dgomes commented 6 months ago

same answer as above, it needs fixing on the frontend. If you move the sensor to YAML all should be fixed

bramstroker commented 6 months ago

@dgomes I think there are still issues with the unit of measurement. I also get complaints from powercalc users about the unit of measurements being wrongly set to kkWh, and I have double checked powercalc is also creating the Riemann sensor with setting the unit prefix to None, similarly as you would with omiting it in the GUI.

kkWh is just a wrong uom and imo we should prevent that from happening in any case. When a user wants a kWh most of them will also set the unit of prefix to k, which is logical. And currently this will fully break the sensor, even when the user later corrects it. As @sangvikh mentioned. I think this can easily be fixed in the integration helper code by setting unit of measurement to a valid UnitOfEnergy. When source entity is in kW and unit prefix is k it can just set unit prefix to None to prevent any issues by users misconfiguring. And this will result in a valid unit of measurement kWh.