I am trying to track my daily electricity spend. This is easy to do with the sensors I have, but they will reset on a reboot. I know this by design and I don’t reboot very often, but it is annoying.
I have made the following Template Sensors. Last night I rebooted around 9.20pm and the sensor.ts_electricity_export_compensation reset to zero. I am not sure why. I then rebooted again around 9.45pm and they all reset. It is unlikely I would reboot twice so quickly normally. I just wanted to check if my logic was correct and why only the sensor.ts_electricity_export_compensation reset the first time
If your sensor.electricity_export_daily_compensation (and others) reset on a reboot, all of the template sensors you have created will also do the same. I think they are the issue that needs adressing. Why do they reset on a reboot? Can this be prevented?
EDIT: would also add that none of your template sensors need the trigger section as they will all update based on the sensors they reference changing state. Not an issue but makes your template much simpler.
Thanks for the reply. I thought all sensor values reset on reboot, but Template Sensor were meant to remember values. I was basing that off this Template Sensor I made. This sensor will remember the highest value on a reboot.
- trigger:
- platform: state
entity_id:
- sensor.lux_solar_output_live
- sensor.maximum_incoming_watts_combined
- platform: time
at: input_datetime.solar_panel_watt_reset # Specify the desired date and time
sensor:
- name: "Maximum Incoming Watts Combined"
unique_id: maximum_incoming_watts_combined
state: >-
{% if trigger.platform == 'time' %}
0
{% else %}
{{ [states('sensor.maximum_incoming_watts_combined')|float(0), states('sensor.lux_solar_output_live')|float(0)] | max }}
{% endif %}
A Template Sensor’s state value is not stored. Its state value is computed by its template which is evaluated when one of the entities it references changes state. On restart, the Template Sensor may have no value until its template is evaluated.
In contrast, a Trigger-based Template Sensor’s state value is stored. Its state value is computed by its template when one of its triggers is triggered. On restart, the Template Sensor will report its stored value, unless one of its triggers is triggered causing it to re-evaluate its template.
In your case, it’s likely that the value of sensor.electricity_export_daily_compensation changes on restart thereby triggering your Trigger-based Template Sensor and causing it to replace its stored value with a computed new one.
It depends on whether you have any control over the behavior of sensor.electricity_export_daily_compensation. It appears to be changing to 0 on startup which is sufficient to trigger the re-calculation of sensor.ts_electricity_export_compensation.
If you have no control over it then I’m not sure what you can do other than make sensor.ts_electricity_export_compensation reject a received value of 0. In other words, if it gets 0 from sensor.electricity_export_daily_compensation then it just reports its existing value as opposed to 0.
However, this idea is unusable if there are times when you legitimately need the sensor to report 0.
I don’t have control of that sensor. The sensor will reset to 0 every night or a reboot. I would say my Template Sensor should ignore 0 and reset itself a midnight. The same will happen with sensor import cost. The standing charge will always be static. I might change once a year.