What is a better solution to not have a threshold sensor reset on restarts?

I have a temp sensor for my freezer that I have to monitor temps. I created a threshold sensor with an upper limit of 10F and then used that sensor to feed into a template binary sensor sensor that triggers when the threshold has been met for 30mins continuously.

{{
is_state('binary_sensor.small_freezer_temperature_threshold_reached', 'on') and (now() > states.binary_sensor.small_freezer_temperature_threshold_reached.last_changed + timedelta(minutes=30))
}}

Then this template is what triggers an alert integration for my freezer. However, this template sensor doesn’t stay ON after restarts so it causes the alert to stay idle again until it triggers after 30mins. See the screenshot below that idle time was after a restart and 30mins later it triggered. How do I prevent this?

Looking at this more zoomed in it seems the threshold sensor is the issue here as it briefly changes value i presume.

like 10s.

There is no need for the second sensor. You can do that in your automation trigger:

triggers:
  - trigger: state
    entity_id: binary_sensor.small_freezer_temperature_threshold_reached
    to: 'on'
    for:
      minutes: 30

Well that is a different approach. I am not using an automation i’m using Alert the integration. I purposely kept it out of an automation bc i like its functionality. I just need a way to have a binary sensor persist across restarts. The issue I’m seeing tho is this threshold sensor tho that is the issue. It seems it changes values during restart.

Every entity is set to unknown as HA starts. They are then either restored or are updated as new values arrive.

Template sensors are not restored, they wait for source sensor updates. Triggered template sensors are restored after restart, so you could do this:

template:
  - trigger:
      - trigger: state
        entity_id: binary_sensor.small_freezer_temperature_threshold_reached
        to:
          - 'on'
          - 'off'
    binary_sensor:
      - name: Name Here
        state: >
          {{
            is_state('binary_sensor.small_freezer_temperature_threshold_reached', 'on') and (now() > states.binary_sensor.small_freezer_temperature_threshold_reached.last_changed + timedelta(minutes=30))
          }}
        device_class: etc...

It should operate exactly the same as your current sensor except that it will be restored after a restart.

EDIT: Hmm. Or maybe not. last_changed will definitely reset to to the restart time, so that my stuff this method up. Give it a try and see what happens.

This seems to be working with restarts, so far.

- trigger:
    - trigger: state
      id: plug
      entity_id: switch.christmas_staircase_light
    - trigger: numeric_state
      id: threshold_reached
      entity_id: input_number.test_temperature_sensor
      above: 10
      for:
        seconds: 10
    - trigger: numeric_state
      entity_id: input_number.test_temperature_sensor
      below: 10
  binary_sensor:
    - name: "Small Freezer Issue"
      unique_id: ca34b70c-8882-496d-be47-ca51a242ff95
      state: >-
        {% if trigger.id == 'plug' %}
          {% if is_state('switch.christmas_staircase_light', 'unavailable') or is_state('switch.christmas_staircase_light', 'off') %}
          true
          {% else %}
          false
          {% endif %}
        {% elif trigger.id == 'threshold_reached' %}
        true
        {% else %}
        false
        {% endif %}

I want the sensor to work off of the temperature but also off of the plug

One more final comment here. I fixed it so far with this.


- trigger:
    - trigger: state
      id: switch_on
      entity_id: switch.small_freezer_plug_green
      to:
        - 'on'
    - trigger: state
      id: switch_off
      entity_id: switch.small_freezer_plug_green
      to:
        - 'unavailable'
        - 'off'
    - trigger: numeric_state
      id: high_temp
      entity_id: sensor.small_freezer_temperature
      above: 10
      for:
        seconds: 10
    - trigger: numeric_state
      id: low_temp
      entity_id: sensor.small_freezer_temperature
      below: 10
    - trigger: state
      id: temperature_malfunction
      entity_id: sensor.small_freezer_temperature
      to: "unavailable"
    - trigger: homeassistant
      id: ha_start
      event: start
  binary_sensor:
    - name: "Small Freezer Alert"
      unique_id: 7288f7f6-b585-4cb8-bf14-2a603c1a19e9
      state: >-
        {% if trigger.id == 'switch_off' %}
          true
        {% elif trigger.id == 'low_temp' and (is_state('switch.small_freezer_plug_green', 'unavailable') or is_state('switch.small_freezer_plug_green', 'off'))%}
          true
        {% elif trigger.id == 'high_temp' or trigger.id == "temperature_malfunction" %}
          true
        {% elif trigger.id == 'ha_start' and (is_state('switch.small_freezer_plug_green', 'unavailable') or is_state('switch.small_freezer_plug_green', 'off') or is_state('sensor.small_freezer_temperature', 'unavailable') or this.state == 'on') %}
          true
        {% elif trigger.id =='switch_on' and states('sensor.small_freezer_temperature')|float > 10 %}
          true
        {% else %}
          false
        {% endif %}

This is the final trigger based template sensor. It does retain its value across restarts so far.