Timers after restart... again

So, the topics of timers vs delay has been long discussed, and with timers able to now survive an HA restart i wanted to clarify something.

I have this automation:

alias: Ensuite Bathroom - Fan On/Off with Humidity Derivative
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.ensuite_bathroom_humidity_derivative
condition:
  - condition: numeric_state
    entity_id: sensor.ensuite_bathroom_humidity_derivative
    above: input_number.ensuite_humidity_threshold
action:
  - type: turn_on
    device_id: 7d74b7c8e812e444474b8c4f46021cde
    entity_id: switch.aqara_switch_module_ensuite_switch
    domain: switch
  - service: timer.start
    data:
      duration: "300"
    target:
      entity_id: timer.ensuite_bathroom_fan_timer
  - if:
      - condition: state
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_6
        state: "off"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_6
            to: "on"
  - wait_template: "{{states('timer.ensuite_bathroom_fan_timer')=='idle'}}"
    continue_on_timeout: true
  - type: turn_off
    device_id: 7d74b7c8e812e444474b8c4f46021cde
    entity_id: switch.aqara_switch_module_ensuite_switch
    domain: switch
  - service: timer.start
    data:
      duration: "1500"
    target:
      entity_id: timer.ensuite_bathroom_fan_timer
  - wait_template: "{{states('timer.ensuite_bathroom_fan_timer')=='idle'}}"
    continue_on_timeout: true
mode: single

Basically if the humidity change in a room is above a certain threshold, a fan comes on, a timer then starts for 5 minutes, then when the timer stops, there is a wait for trigger further down that then turns the fan off. The Timer helper has restore enabled.

I’m assuming, despite the fact that the timer has the restore option enabled, if HA restarts during the automations wait for trigger, the automation in its current state won’t turn off the fan.

My presumption is that in order to make this robust against an HA restart, that I need to refactor this automation to add another trigger at the start, to listen for the idle state of the timer. Then use some condition further down to turn off the fan.

Think about the logic. This automation was sitting in the background waiting for the timer to finish, so essentially you were in the middle of this automation when HA was restarted. HA does not go back top the same state when it is restarted, the automation is no longer running, and the automation only runs when the light is turned on. The light is already turned on when HA restarts so aobviously there is no change in the state of the light toi relaunch the automation.

Automations should be short-lived and only run for a very short time and only do one thing. If you want an automation to do several things then split it into several automations. I this case:

Automation: Light turns on, do NOTHING other than start the trimer.

(separate) Automation: When the time finished, turn off the light.

Automations are not restored to their same state when HA is restored. Only some helpers are able to have their state saved and restored when HA restarts.

Each automation should only do one thing, and then finish. If you want to do serveral things, then make several automations. It may be tedious but it makes debugging and a bunch of other things much easier (and improves the performance).

Thanks @KruseLuds - thats what I thought was the case.

I do think I can combine the on and off in a single automation, which is my preference, but I’ll need to change how I handle triggers for the timer, which should be pretty straightforward.

Thanks for your confirmation.

Be advised that there are a few open Issues for the Timer integration.

This one may affect you because it involves timer behavior after a restart.

timer.finished event cannot be catched at startup of Home Assistant if timer ended while Home Assistant was down. · Issue #79145 · home-assistant/core · GitHub

It’s over a year old without any official response. The bug might still exist or it was fixed (as a side effect of some other correction) but this Issue was never formally closed. You’ll have to test the failure scenario to determine if the bug is still present.

Looks like that might be an issue, but I could at least use the workaround to stop the timers early which wouldn’t be terrible for my use case.

There is a way to have multiple triggers launch the same automation.