Automation not triggering after HA restart when using “for” duration in state trigger

I’ve run into something odd with a simple pair of automations controlling a Tasmota-based switch (via MQTT).

Setup:

  • Automation 1 starts the machine at 12:00 PM
  • Automation 2 is supposed to stop the machine after it’s been ON for 30 minutes

What happened:

  • The machine started at 12:00 PM as expected.
  • Around 12:12 PM, Home Assistant restarted.
  • During restart, the switch entity briefly went unavailableoff (for about 1.26 seconds) → on.
  • After restart, I expected the 30-minute timer to restart from 12:12 PM and turn the machine off around 12:42 PM.
  • But the “off” automation never triggered, and the machine kept running indefinitely.

Is this expected behavior that a for: duration in a state trigger doesn’t persist through restarts? And if so, is there a recommended approach to make it survive a restart?

Here’s the YAML for both automations:

alias: Machine - On
description: ""
triggers:
  - trigger: time
    at: "12:00:00"
conditions: []
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.machine
mode: single
alias: Machine - Off
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.machine
    to: "on"
    for:
      hours: 0
      minutes: 30
      seconds: 0
conditions: []
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.machine
mode: single

Yes. Automations do not persist over restarts. And in this case, when HA does restart your switch is already on, so the trigger in the second automation doesn’t fire at all.

You might try a timer helper as a trigger - there’s a setting to ensure that it will resume after a restart, which would give you the behaviour you expect.

2 Likes