Automation - Wait for Trigger. Am I Doing This Wrong?

Trying to get a simple automation up and running, sure I’m either misunderstanding the Wait for Trigger, or something else small I’m overlooking. The first trigger & action happens, but stops there. The light never turns off after the motion goes back to cleared.

Here is the desired basic outcome (eventually when tested, will move it to be after sunset/before sunrise and probably restart instead of single mode):

  • Motion detected in kitchen, turn on kitchen lights
  • When motion clears, turn off light 5 mins later

I’m doing this in UI - but here is the yaml it spits out.

- id: '1601067028729'
  alias: Kitchen - Lighting w Motion
  description:
  trigger:
  - type: motion
    platform: device
    device_id: a04404d353f445b4b78b838cc103efaa
    entity_id: binary_sensor.presence_8
    domain: binary_sensor
  condition: []
  action:
  - type: turn_on
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
    brightness_pct: 85
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.presence_8
      to: 'off'
      for: 00:05:00
    timeout: ''
  - type: turn_off
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
  mode: single

Thanks!

The example in the docs has the for: time in quotes “00:00:00”, give that a go.

1 Like

The way the automation is written, it is triggered by the motion sensor changing state to either on or off. So even when the motion sensor changes to off it triggers the automation which dutifully sends a command to turn on the light. Effectively, the wait_for_trigger never gets the opportunity to do its part.

Change the trigger so that it triggers only when the motion sensor changes state to on. I’m more familiar with a State Trigger (than the Device Trigger you are using) and for that you would simply add:
to: 'on'

Thanks. Tried no quotes, single quotes and double quotes. No go on all of them.

Thanks. I’ve tried your suggestion with State instead of Device. Still the same result. It’s like the automation doesn’t recognize either the state change on the motion sensor, or the timer of 5 mins.
Note, I’ve tried 00:05:00, ‘00:05:00’ and “00:05:00” in the for: section.

- id: '1601067028729'
  alias: Kitchen - Lighting w Motion
  description: Turns on Kitchen Pots for 5 mins when motion detected after sunset
  trigger:
  - platform: state
    entity_id: binary_sensor.presence_8
    from: 'off'
    to: 'on'
  condition: []
  action:
  - type: turn_on
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
    brightness_pct: 85
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.presence_8
      to: 'off'
      for: 00:05:00
      from: 'on'
    timeout: ''
  - type: turn_off
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
  mode: single

I assume you are aware that the for: 00:05:00 means the motion sensor’s state must remain off for a continuous 5 minutes in order to qualify. If it changes state to on, even momentarily, the 5-minute countdown is reset (and the automation is triggered again).

As an experiment, remove the from: 'on' in wait_for_trigger. Just the to: 'off' should be sufficient.

Thanks. Yes, the 00:05:00 is intended. I’m actually testing with 00:01:00 to make the tests quicker :slight_smile: I’m looking for lights to turn off after there has been no motion detected for 5 mins. My sensors take 90 seconds exactly to go from motion detected to cleared, and I’m testing by watching logs/lights after this 90 second period.

I tried putting just a straight up 5 instead of 00:05:00 in the for: field. No dice. I can’t imagine what I’m trying to do here is hard… It seems pretty basic stuff - motion detected, lights on. No motion, lights off… I’d have to assume this is common use case.

Edit: Forgot to mention, the test you recommended results in the same.

I agree that it is with the qualifier that you are using new functionality in 0.115 to achieve it. The documentation shows an example where wait_for_trigger waits for a different entity to change state (not the one triggering the automation). Theoretically it should work with the same entity but it’s possible there’s a bug.

I see… So prior to the 115 functionality, I would need to do this in 2 automations correct? Maybe that is why even this basic automation didn’t turn off (only on) last night…

- id: '1599856483022'
  alias: Driveway Lights ON at Sunset OFF at Midnight
  description: ''
  trigger:
  - event: sunset
    platform: sun
  condition: []
  action:
  - device_id: 644c905bcbc44478b1e2b5f0bc0a45fe
    domain: switch
    entity_id: switch.light_driveway_switch
    type: turn_on
  - wait_for_trigger:
    - platform: time
      at: '23:59:59'
    timeout: ''
  - type: turn_off
    device_id: 644c905bcbc44478b1e2b5f0bc0a45fe
    entity_id: switch.light_driveway_switch
    domain: switch
  mode: single

Add a timeout. I’ve found that without it, it does not work the way I’ve expected it to.

Interesting. This actually worked in my quick tests. I needed to ensure that the timeout entered was longer than the whole automation. For example:

  • Motion detected = Light On
  • Motion cleared takes 90 seconds
  • Wait for Trigger is set to 1 minute
    Therefore, timeout needed to be longer than 2:30.

I’m wondering though… How will a timeout impact this if/when outside of my isolated testing, when motion is constantly being detected in the room? Ie. if the initial trigger of “motion detected” does not cease while people are in the room, will it just timeout?

That’s based on the mode you set. If you have the mode set to single, the previous runs will cease to exist and only the current one will be in effect.

I’m trying to understand the interplay between for: HH:MM:SS and timeout: HH:MM:SS. I imagine timeout ought to be longer than for. The idea is if for is not satisfied within the first 5 minutes after the automation is triggered, the timeout value comes into effect.

However, wouldn’t supplying a timeout mean the light will be turned off even if the motion sensor never returns to off for 5 minutes?

well, there’s other stuff they added. Like stop on timeout, and the whole wait variable that will allow you to see what happened during the wait.

Ok. I think I’ve got the actual automation ready for testing tonight. The wife spends a lot of time in the Kitchen, so fingers crossed this passes the WAF test. I’ve changed the times to be for: 00:05:00 and the timeout: 00:10:00. Also changed mode from single to restart, and added in the after sunset (-30 min offset). Only thing I’m not very solid on, what happens if/when someone turns the switch (inovelli red) on manually?
Here’s the yaml (done in UI).

- id: '1601067028729'
  alias: Kitchen - Lighting w Motion
  description: Turns on Kitchen Pots for 5 mins when motion detected after sunset
  trigger:
  - platform: state
    entity_id: binary_sensor.presence_8
    to: 'on'
  condition:
  - condition: sun
    after: sunset
    after_offset: -00:30:00
  action:
  - type: turn_on
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
    brightness_pct: 85
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.presence_8
      to: 'off'
      for: 00:05:00
    timeout: 00:10:00
    continue_on_timeout: true
  - type: turn_off
    device_id: ae3a20c96ad443f5a714db86b62eb933
    entity_id: light.light_kitchen_pots_level
    domain: light
  mode: restart

You might be turning the light on a lot there. Might want a separate automation that turns on the light with a stipulation that the light is off. And then this automation that just starts the ‘off timer’.

Man. This is tough, even though the use case sounds easy :confused:
I’ll try this tonight, and if not I’ll take the 2-automation path. I really hoped to attempt to get this into 1 automation, which I thought was the point of Wait for Trigger…
Thanks for your help!

I tried using yaml and seem to get stuck. I ended up using node red from something similar.

Below is whats been working for me so far.

Thanks, maybe it’s time to try node red… What exactly is this doing? I had some challenges with my automation last night. For example:

  • If sunset comes and motion is already detected in the room, lights won’t come on as the automation is looking for the state change to ‘on’.
  • Timeout set to 10 mins, this is no good… If we are active in Kitchen, automation times out waiting for the trigger that is looking for state to go to ‘off’. So it turns the lights off, and then they don’t come back on as motion is already ‘on’ in kitchen…

The timeout behavior you experienced is what I had theorized; it’s a ‘hard limit’ and incompatible with what you are trying to achieve.

Just use the old-school technique of two automations: one to control turning on the light and another for turning it off. Lots of flexibility and it just works (speaking from experience).