Hi,
I have a lot of devices which report kWh energy consumption which act as proper “total_increasing” sensor: They reset to 0 at regular ticks and continue counting. I’d like to calculate the sum of these to use in the energy panel as a proxy for a full house energy meter. I’ve used How to sum energy and power sensors? - #6 by chemelli to create a sum of these like this:
template:
- trigger:
- platform: time_pattern
minutes: "*"
sensor:
- name: "Energy cumulative"
unique_id: all_energy_kWh
unit_of_measurement: 'kWh'
device_class: energy
state_class: total_increasing
state: >-
{%set ns = namespace(values=[]) %}
{%set ss = states.sensor|rejectattr('entity_id', 'eq', 'sensor.energy_cumulative')|selectattr('attributes.unit_of_measurement', 'eq', 'kWh')|list%}
{%for s in ss%}
{%set ns.values = ns.values + [ s.state|float ]%}
{%endfor%}
{{ns.values|sum}}
… but when any one of these input sensors reset to zero, of course the sum doesn’t reset to zero, and the total_increasing calculation makes it look like there was a ton of consumption all at once.
Is there a nice way of getting this to work where you sum up a bunch of total_increasing sensors and either reset the sum to zero (by storing some offset somewhere maybe?) or just getting a real total out of it instead of using total_increasing for the end sum?
Thanks for any help, including links to obvious places I should have searched.