Utility meter sensor acting weird showing wrong unit of measurement, I don't understand why?

Ok, so I have a sensor from fritz_box that shows the current power usage:

  - platform: template
    sensors:
      serverwattage:
        friendly_name: "Server forbrug"
        unit_of_measurement: "kW"
        device_class: power
        value_template: "{{ state_attr('switch.server', 'current_power_w') | multiply(0.001) | float }}"

This shows the current power usage for the server, and it works nicely, currently showing: 0.21278 kW

I put that in an integration sensor to have it calculated in energy consumption over time:

  - platform: integration
    source: sensor.serverwattage
    name: server_energy
    unit_time: h

This looks correct as well, currently showing: 0.163 kWh

That’s all well and good. I then need for this to reset every night at midnight, to have a daily usage, so I’ve made a utility meter sensor:

  server_daily_energy:
    source: sensor.server_energy
    cycle: daily

But that is really odd, it shows: 2.542 W

How can it show W, when the source sensor is in kWh?

I don’t use these integrations, but I did look at the code a bit. As far as I can tell, that shouldn’t happen.

Take a look at sensor.server_energy and sensor.server_daily_energy on the STATES page. What are their unit_of_measurement attributes?

Ok, the unit’s shown above is taken from the developer pages.
First is the sensor from fritz.box


Then I extract the current power usage in a sensor:

  - platform: template
    sensors:
      serverwattage:
        friendly_name: "Server forbrug"
        unit_of_measurement: "kW"
        device_class: power
        value_template: "{{ state_attr('switch.server', 'current_power_w') | multiply(0.001) | float }}"


I then integrate this value over time:

  - platform: integration
    source: sensor.serverwattage
    name: server_energy
    unit_time: h


And finally I want it recycle daily:

  server_daily_energy:
    source: sensor.server_energy
    cycle: daily

Thanks for the details. Well, based on the code I can’t explain why the unit_of_measurement attribute for sensor.server_daily_energy is W. Clearly it should be kWh.

Is there any chance that there is a customization entry for sensor.server_daily_energy that is changing its unit_of_measurement attribute?

1 Like

Hmm, I hadn’t thought of that, but good point. No, nothing is customized for it.

Well then I’m stumped. You could try enabling debug but I didn’t see any log messages that would help explain why this is happening. You could always modify the code to add some. :slightly_smiling_face:

1 Like

:laughing:At least I’m not the only one, thankyou for the effort, I’l keep digging into it.

Same problem, I still haven’t found a solution

1 Like

I found onew with the help of the developer. THe problem is apparently that the unit is defined first time the sensor is created, so if you have done something wrong there, it will never change. So I got a suggestion with creating a new integration sensor and a new utility meter sensor called ‘test’, and that changed the unit, so I just created a new pair for them (https://github.com/home-assistant/home-assistant/issues/30233)

1 Like

Another easy fix:

  1. Stop HA
  2. Find unit_of_measurement in .storage/core.restore_state for the sensor with problem.
  3. Rename the unit for correct unit
  4. Restart HA
5 Likes

This is the best solution for this issue :smiley:

That’s not a great solution because it does not recalculate the history of the utility meter. So from 1500Wh, you will have 1500kWh.

Have you solved this?

go to develop tools -->Services:
service: utility_meter.calibrate

data:
value: “1.5”
target:
entity_id:
- sensor.XXXXXXXXXX