Wait for trigger doesn't seem to work the way i expect it to

Hello everyone.
Trying to turn off my lights after a set time using the “Wait for trigger” and specifying a time. But they never turn off. I can use delay but that seemed odd (why no start and stop time?)

What would be a better way of doing this ?

- id: '1638049352323'
  alias: Garage Light
  description: Turning on and off the garage light at a set time
  trigger:
  - platform: time
    at: '16:00'
  condition: []
  action:
  - service: light.turn_on
    target:
      area_id: garage
  - wait_for_trigger:
    - platform: time
      at: '23:59'
  - service: light.turn_off
    target:
      area_id: garage
  mode: single

Thank you,

the issue you could have is if you restart HA or reload automations the running automation (the one above waiting for the trigger) will be cancelled and it won’t re-start until 16:00 the next day.

it would be best if you made two automations - one for on and one for off - then use the time above in the wait for trigger as the trigger for the off automation.

Delays also suffer the same limitation.

1 Like

just identify the triggers using id and use a service template instead. Makes for simple automations that use the trigger id without needing choose.

- id: '1638049352323'
  alias: Garage Light
  description: Turning on and off the garage light at a set time
  trigger:
  - id: 'on'
    platform: time
    at: '16:00'
  - id: 'off'
    platform: time
    at: '23:59'
  condition: []
  action:
  - service: light.turn_{{ trigger.id }}
    target:
      area_id: garage
  mode: single
3 Likes

Thanks @petro. So what does this code do different from mine? Also does it address the problems the poster before you mentioned?

Thanks again.

Good to know. I think i might have restarted the server somewhere in between the time of the automation. I’ll test it again tonight, so far i haven’t restarted the HA so it should work. I didn’t want to many automations (on, off etc) but it might be worthwhile, as you say it cuts the problem in half.

1 Like

The automation I posted also avoids the issue you are mentioning with restarting.

1 Like

Thanks @petro, much obliged. Could you expand a little on how it avoids that issue? Does using a id somehow circumvent the issue of a canceled automation because of say, a server restart?

I’ll use this instead.

You’re not using wait_for_trigger so the automation is not in a ‘already running and waiting for the trigger’ state. It’s just like any other automation. You restart, the automation is still on, but not running. It only runs when it triggers and it’ll be instant.

1 Like

If you interrupt an automation, either by restarting Home Assistant or Reloading Automations, that is busy waiting for one of the following:
delay to expire
wait_for_trigger to trigger
wait_template to get a match for its conditions
repeat to complete its iterations
for to countdown to its target value
etc

its operations are cancelled and the entire automation is reset.

1 Like

@petro
can i use ‘on’ and ‘off’ for every automation or do i need to use different trigger id’s ?
and, can i use the {{ }} notation for devices as well ?

thanks,
atv