Using timer vs state with duration in a trigger

Hello, I have a following use case: switch on a device if it has been off for 8 hours. I see 3 approaches:

Option 1:
Create a timer for 8 hours and create automation that starts it once the device is switched off.
Create another automation start switches the device on once timer is finished.

Option 2:
Create a single automation with trigger being “device in state ‘off’ for 8 hours” and action “switch on the device”

trigger:
  - platform: state
    entity_id:
      - switch.x
    to: "off"
    for:
      hours: 8
      minutes: 0
      seconds: 0

action:
  - type: turn_on
    device_id: ...
    entity_id: ...
    domain: switch

Option 3:
Same as option 2, but use platform “device” instead of state:

trigger:
  - platform: device
    type: turned_off
    device_id: ...
    entity_id: ...
    domain: switch
    for:
      hours: 8
      minutes: 0
      seconds: 0

Is there any benefit of using the timer approach over the state duration in terms of performance (i.e. HA app performance) or anything else? I suppose both options should be surviving the HA restart too?
And what exactly is the difference between using platform state vs device?

1 Like

Do option 1. It can be restored after a restart.

Do not do option 3. See: Why and how to avoid device_ids in automations and scripts

Thanks a lot! Do I get it right, that options 2 & 3 don’t survive restart?

What would be the use-case for those duration settings then? Are they useful for shorter periods of time, like < 5 minutes or should not be used altogether?

Yes. Even more so now that reloading automations (instead of restarting) works well and only reloads the automations that have changed.

And shorter times are not as likely to be interrupted by a restart. I use them all the time for things like this:

- id: 17b8be51-e6b1-482d-979b-0313f277274f
  alias: 'Entry Light Auto Off'
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_entry
    to: 'off'
    for:
      minutes: 3
  condition:
  - condition: state
    entity_id: input_select.hallways_scene
    state: 'Automatic'
  - condition: state
    entity_id: light.entry
    state: 'on'
  action:
  - service: light.turn_off
    entity_id: light.entry
1 Like