Template sensor resetting. Is my logic correct?

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

template:  
  # Electricity Profit and Cost Template Sensors
  # Today's Export Compensation      
  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_export_daily_compensation
        to:
    sensor:
      - name: "TS Todays Electricity Export Compensation"
        unique_id: ts_electricity_export_compensation
        state: "{{ states('sensor.electricity_export_daily_compensation') | float(0) }}"
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
    
    # Today's Import costs
  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_import_daily_cost
        to:
    sensor:
      - name: "TS Todays Electricity Import Costs"
        unique_id: ts_todays_electricity_import_costs
        state: "{{ states('sensor.electricity_import_daily_cost') | float(0) }}"
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
    
    # Standing Charge
  - trigger:
      - platform: state
        entity_id:
          - sensor.smart_meter_electricity_import_standing_charge
        to:
    sensor:
      - name: "TS Todays Electricity Standing Charge"
        unique_id: ts_todays_electricity_standing_charge
        state: "{{ states('sensor.smart_meter_electricity_import_standing_charge') | float(0) }}"
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
        
    # Today's Electricity Import Cost with Standing Charge
  - trigger:
      - platform: state
        entity_id:
          - sensor.ts_todays_electricity_import_costs
          - sensor.ts_todays_electricity_standing_charge
        to:
    sensor:
      - name: "TS Todays Total Electricity Cost"
        unique_id: ts_todays_total_electricity_cost
        state: >-
           {{ states('sensor.ts_todays_electricity_import_costs') | float(0) + states('sensor.ts_todays_electricity_standing_charge') | float(0) }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
        
     # Today's Electricity Credit or Debit
  - trigger:
      - platform: state
        entity_id:
          - sensor.ts_todays_total_electricity_cost
          - sensor.ts_electricity_export_compensation
        to:
    sensor:
      - name: "TS Todays Electricity Credit or Debit"
        unique_id: ts_todays_electricity_credit_or_debit
        state: >-
           {{ states('sensor.ts_todays_total_electricity_cost') | float(0) - states('sensor.ts_electricity_export_compensation') | float(0) }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"

These are the value graphs. You can see the rest reset on the 9.50pm reboot.



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.

That makes sense and annoying :slight_smile: Anything I can do to work around this?

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.

  - trigger:
      - platform: state
        entity_id:
          - sensor.electricity_export_daily_compensation
        to:
      - platform: time
        at: '00:00:00'
    sensor:
      - name: "TS Todays Electricity Export Compensation"
        unique_id: ts_electricity_export_compensation
        state: >
          {{ [this.state | float(0), states('sensor.electricity_export_daily_compensation') | float(0)] | max
            if trigger.platform == 'state' else 0 }}
        unit_of_measurement: 'GBP' 
        device_class: "monetary"
1 Like

Thank you. I will give that a try. Might take a couple of days.

Thanks, works also for me