I have a problem I can’t figure out with a utility_meter which resets daily:
I calculate in a template the cumulative total amount to date of energy saved by using PV panels (ie total produced PV energy - total exported PV energy)
using a utility_meter I want to see the daily energy saved amount (same for generated and exported energy)
However, the utility_meter shows the PV energy generated values rather than Generated - Export. I’m puzzled how to solve this.
Configuration:
template:
- sensor:
- name: "Electricity Meter Energy Saved Total"
unique_id: "electricity_meter_energy_saved_total"
state_class: total_increasing
device_class: energy
unit_of_measurement: "kWh"
state: >
{% set generated = states('sensor.zp_mb_total_export_kwh') | float(0.0) %}
{% set export = states('sensor.electricity_meter_energy_export_total') | float(0.0) %}
{% if ((generated > 0) and (export > 0)) %}
{{ generated - export }}
{% endif %}
utility_meter:
energy_saved_today:
name: "Electricity Meter Energy Saved Today"
unique_id: "electricity_meter_energy_saved_today"
source: sensor.electricity_meter_energy_saved_total
cycle: daily
always_available: true
Graphical representation of Generated, Exported and Saved energy values cycled daily. NOTE: as Generated (blue) and Saved (orange) have the same value so only one is visible! Wat you should see is the difference between the blue (=orange) and yellow lines.
Maybe not the problem you report here: There is no value here when generated or exported is 0 or undefined. So it is not a total to date, it too wil reset to 0. Put an availability template in there, and maybe an else too. Why is the if there? it will be 0, that is true if the other two are 0?
Check the values of the template above in developer tools. Maybe you made a typo in entity ids so one of the helper variables is 0.
I used to work with availability but was advised by one of the moderators this wasn’t necessary. As the export value is not immediately available after a HA restart and I get incorrect results when I omit the ‘>0’ test.
All cumulative total values (generated, export and saved) are calculated and reported correctly. These are the 3 graphs showing the cumulatieve totals:
Generated (don’t let the name of the sensor fool you … it does represent the PV panel production):
The value of ‘saved energy’ at the start of 13 March is: production - export = 8745 - 3239 = 5506. Likewise for the end of the day: 8760 - 3249 = 5511. The daily utility_meter value should therefor be 5511 - 5506 = 5. However, the value reported for ‘saved’ in the first graph above is 15kWh (which happens to be the same as the value for ‘generated’ on 13 March).
If the source is correct, there is no way the utility meter could have known the increase from the other sensor. So I cannot come to any other conclusion than that the utility meter is linked to the wrong sensor. Are there any errors in the log? Are you sure you restarted using the definition you show us?
Thanks for thinking with me. Your suggestion of another sensor used as source for the utility_meter also occurred to me but the definition is as it is. This is what I copied just now from my configuration.yaml:
- sensor:
- name: "Electricity Meter Energy Saved Total"
unique_id: "electricity_meter_energy_saved_total"
state_class: total_increasing
device_class: energy
unit_of_measurement: "kWh"
state: >
{% set generated = states('sensor.zp_mb_total_export_kwh') | float(0.0) %}
{% set export = states('sensor.electricity_meter_energy_export_total') | float(0.0) %}
{% if ((generated > 0) and (export > 0)) %}
{{ generated - export }}
{% endif %}
I restart HA 3 times a week after HA finishes its housekeeping and a backup has been made. The last one was yesterday morning.
All mentioned utility_meters in this thread are defined in my configuration.yaml and there have been no changes since yesterday morning. I have tried to find a place in HA where the yaml-based utility_meter definitions can be found but did not succeed. I there a way to look deeper under the HA ‘bonnet’?
This is what the helper page shows for the utility_meter:
I see one error involving sensor.electricity_meter_energy_saved_...:
2025-03-14 08:05:10.478 WARNING (Recorder) [homeassistant.components.sensor.recorder] Entity sensor.electricity_meter_energy_saved_total from integration template has state class total_increasing, but its state is not strictly increasing. Triggered by state 5511.887 (5511.888) with last_updated set to 2025-03-14T07:02:24.434043+00:00
This must be due to a slight timing difference between the moments when values for generated and exported energy become available.
Could making the utility_meter not strictly increasing have an effect (ie net_consumption: true)?
By adding net_consumption: true the problem has disappeared (for now).
However, I cannot explain this because export will always be less than or equal production so in reality the value of saved can only increase and can never go negative!
Perhaps it is a timing issue if production value always updates before export gets updated. In that case production will always be added first and subsequently subtracting export from saved will have no effect as it cannot decrease in value …