I have written a template sensor which increments itself together with a source sensor, skipping any resets and unknown/unavailable states. This is to decouple my e.g. cumulative energy sensors from the states of the source devices which can get reset or go bad, throwing my energy monitoring setup into disarray every so often.
- trigger:
- platform: state
entity_id:
- sensor.electricity_a_energy
not_to:
- "unknown"
- "unavailable"
variables:
to_state: "{{trigger.to_state.state}}"
sensor:
- name: Electricity A Energy cumulative
icon: mdi:lightning-bolt
device_class: energy
unit_of_measurement: kWh
state_class: total_increasing
unique_id: "roigjoreoifnofnvewwefwgeo"
state: >-
{% set current = this.state|float(0)%}
{% set from_state = this.attributes.last_good|float(0)%}
{% if to_state | is_number %}
{% set to_state = to_state|float%}
{{(current + to_state - from_state)|round(3) if to_state > from_state else current}}
{% else %}
{{current}}
{% endif %}
attributes:
last_good: "{{to_state if to_state|is_number else this.attributes.last_good}}"
My aim is that the sensor stores the last known good value of the source sensor in the last_good attribute and uses that to fidn the difference between the current source sensor value and the last good one, in case something went wrong with the sensor in the mean time.
(Morally, the utility_meter seems to achieve the same thing, but it resets and it can’t be used with the energy dashboard. But maybe there is a workaround to get a non-reseting utilitility_meter that always increases?)
I would now like to extend this template to work with multiple source sensors, and e.g. return their sum, while storing the last good state for each of the source sensors individually. I can do this by hand, creating a separate trigger for each of the entities and then create a separate attribute for each of them. But maybe there is a better way?
I was hoping to just list the multiple entities together in the state trigger
- platform: state
entity_id:
- sensor.object_id1
- sensor.object_id2
I can then get the name of the triggering entity with something like
{% set trigent = trigger.to_state.object_id%}
and then I would like to write the state as an individual key into the attributes. I thought maybe I could just make the last_good attribute a dictionary, something like
{% set last_good = { 'object_id1': 100, 'object_id2': 200} %}
which is allowed and it seems like I can actually retrieve the values from this dictionary using last_good[trigent]
but I cannot figure out how to update them, i.e. last_good.update({trigent: to_state})
is not allowed and I cannot see any other way of updating the dictionary value. I’ve also tried putting the dictionary in a namespace, setting the key value through set.
Is there some way? Or is it possible to achieve what I want differently?