Use trigger timeout to turn off switch after time elapsed or at certain time (whichever is sooner)

Do I have this automation configured correctly to work as expected (according to the description)? I’ve never used trigger timeouts before. I did this in the Automations UI since YAML is a copy-and-paste kind of thing for me.

alias: Banister Lights on in Evening
description: >-
  Turns on the banister lights at sunset and then turns them off 5h45m later or
  at 10:30pm (whichever is sooner). 
trigger:
  - platform: sun
    event: sunset
    offset: '-00:00:00'
condition: []
action:
  - type: turn_on
    device_id: 994d492fdbae803341f36b0680164bba
    entity_id: switch.banister_lights_socket
    domain: switch
  - wait_for_trigger:
      - platform: time
        at: '22:30:00'
    timeout: '05:45:00'
  - type: turn_off
    device_id: 994d492fdbae803341f36b0680164bba
    entity_id: switch.banister_lights_socket
    domain: switch
mode: single

The syntax may be correct but this is very bad practice. You should never wait in an automation for longer than a few 10s of seconds at most. It is very likely that this automation could be interrupted while waiting, either because of a restart or reload of your automations.

The simplest method is to use two automations, one for on, one for off.

1 Like

I see. Thank you. I appreciate the guidance on best practices. I will split this into two separate automations. I wish the UI had folders or tags for automations to help keep them all organized.

Should the Wait for time to pass action also be limited to <~60 seconds? I have a separate automation set up this way:

Or you could keep it as 1 automation and use trigger id. Then use a choose in the action

alias: Banister Lights on-off in Evening
description: 'Turn on banister lights at sunset. Turn off 5h45m later or at 10:30pm'
trigger:
  - platform: sun
    id: 'blight_on'
    event: sunset
    offset: '-00:00:00'
  - platform: sun
    id: 'blight_off'
    event: sunset
    offset: '05:45:00'
  - platform: time
    id: 'blight_off'
    at: '22:30:00'
condition: []
action:
  - choose:
    - conditions:
      - condition: trigger
        id: blight_on
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.banister_lights_socket
   default:
     - service: switch.turn_off
       target:
         entity_id: switch.banister_lights_socket
mode: single

I have not checked the automation for spacing. You can do this in the GUI

1 Like