Remembering sensor value on restart

I have added the following sensors to my configuration.yaml. The idea was, record the highest setting for these sensors and update them if the value increased. This works, but on a restart they are reset. How can I get around this?

  - sensor:
     - name: "Maximum Incoming Watts Combined"
       unique_id: maximum_incoming_watts_combined
       state: >-
          {% set old_value = states('sensor.maximum_incoming_watts_combined')|float(default=0) %}
          {% set new_value = states('sensor.lux_solar_output_live')|float(default=0) %}
          {% if new_value > old_value %}
            {{ new_value }}
          {% else %}
            {{ old_value }}
          {% endif %}
     - name: "Maximum Incoming Watts Front"
       unique_id: maximum_incoming_watts_front
       state: >-
          {% set old_value = states('sensor.maximum_incoming_watts_front')|float(default=0) %}
          {% set new_value = states('sensor.lux_solar_output_array_2_live')|float(default=0) %}
          {% if new_value > old_value %}
            {{ new_value }}
          {% else %}
            {{ old_value }}
          {% endif %}
     - name: "Maximum Incoming Watts Back"
       unique_id: maximum_incoming_watts_back
       state: >-
          {% set old_value = states('sensor.maximum_incoming_watts_back')|float(default=0) %}
          {% set new_value = states('sensor.lux_solar_output_array_1_live')|float(default=0) %}
          {% if new_value > old_value %}
            {{ new_value }}
          {% else %}
            {{ old_value }}
          {% endif %}

The value of a Trigger-based Template Sensor is restored on startup. Convert your three Template Sensors to Trigger-based Template Sensors.

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.lux_solar_output_live
          - sensor.maximum_incoming_watts_combined
        to:
    sensor:
      - name: "Maximum Incoming Watts Combined"
        unique_id: maximum_incoming_watts_combined
        state: >-
           {{ [states('sensor.maximum_incoming_watts_combined')|float(0),
             states('sensor.lux_solar_output_live')|float(0)] | max }}
  - trigger:
      - platform: state
        entity_id:
          - sensor.lux_solar_output_array_2_live
          - sensor.maximum_incoming_watts_front
        to:
    sensor:
      - name: "Maximum Incoming Watts Front"
        unique_id: maximum_incoming_watts_front
        state: >-
           {{ [states('sensor.maximum_incoming_watts_front')|float(0),
             states('sensor.lux_solar_output_array_2_live')|float(0)] | max }}
  - trigger:
      - platform: state
        entity_id:
          - sensor.lux_solar_output_array_1_live
          - sensor.maximum_incoming_watts_back
        to:
    sensor:
      - name: "Maximum Incoming Watts Back"
        unique_id: maximum_incoming_watts_back
        state: >-
           {{ [states('sensor.maximum_incoming_watts_back')|float(0),
             states('sensor.lux_solar_output_array_1_live')|float(0)] | max }}

That has worked a treat. Thanks very much.

1 Like