Utility Meter - keep values after restart

I have experienced this problem with “blips” on a HA restart (my sensor is a NodeMCU with a Tasmota Counter linked to an optical sensor reading electricity meter LED pulses). The graphs and data below show how my daily usage surged from 55.3 to 77283.3 during an update of HA to version 104.1 earlier today.

I have attempted to work around the problem with the following sensor template config which only accepts a new meter reading if it is greater then the previous reading (this should hopefully prevent the meter from going to zero momentarily and then returning to normal which causes the spike):

meter_reading:
  friendly_name: Electricity Meter Reading
  unit_of_measurement: kWh
  value_template: >
    {% set current = states('sensor.meter_reading') | float | round(1) %}
    {% set new = (states('sensor.nodemcu01_counter_c1') | float / 1000.0) | round(1) %}
    {% macro max(X, Y) -%} {{X|float if X|float > Y|float else Y|float }} {%- endmacro %}
    {{ max(current, new) }}

The reason for the divide by 1000 on the counter is to convert the pulses into units (i.e. kWh).

My Utility Meter config is:

utility_meter:
  energy_usage_hour:
    source: sensor.meter_reading
    cycle: hourly
    tariffs:
      - std
  energy_usage_day:
    source: sensor.meter_reading
    cycle: daily
    tariffs:
      - std
  energy_usage_month:
    source: sensor.meter_reading
    cycle: monthly
    tariffs:
      - std

Tried to do that, by calling “hassio homeassistant stop”, but HA keeps RESTARTING all the time. It won’t stay stopped. This gives me no chance to alter the db file.

Well, that’s not utility meter issue :wink:

1 Like

bumping this thread

I’m, also suffering from weird numbers upon a hassio restart. it’s okay for the daily meters but the monthly?? Not quite tolerable since I happen to make changes from time to time and loosing a whole month of counting feels not so okay? What am I missing? :slight_smile:

I fixed it by replacing the utility meter template source with an input_number, and then running an automation every second to update the utility_number with the original source entity.

Seems it fixed my issue, since after restart the source (input_number) remains the same as before the reset, so no strange additions take place.

In the developer tools, you can manually call the service “utility_meter.calibrate”. This will also restart paused meters.

1 Like

did you have an example code?

i have the same problem. Sometimes, not everytime, the values are complete wrong from the utility_meter.

That makes everything broken in the statistics…

1 Like