How does Automation Wait for trigger works?

I want the following to happened in my Automation. When the automation has been triggered (time), I want the automation action only to run when a binary sensor is “off”. If its “on” the the action should wait to the binary sensor is “off”.

action:
  - wait_for_trigger:
      platform: state
      entity_id: binary_sensor.window5_open
      to: 'off'
  - choose:

With this code the automation stops at the wait_for_trigger when the binary_sensor is “off”.
If I understand correctly the action should run without waiting since the binary sensor is “off”. If the binary_sensor would be “on” the action should wait until its “off”?

That is because it does not change to off. Triggers only happen when state changes. If you do not want to wait when it is already off, place it under an if.

Aha, got it.
wait_template: "{{ is_state('binary_sensor.window5_open', 'off') ' }}"

This action evaluates the template, and if true, the script will continue. If not, then it will wait until it is true.

  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: binary_sensor.window5_open
            state: 'off'
    then:
      - wait_for_trigger:
          - platform: state
            entity_id: binary_sensor.window5_open
            to: 'off'

1 Like

I personally do not use wait_for or delay unless I know it will be s short wait. Otherwise I use a separate automation with a normal trigger. The reason being wait is aborted if HA restarts or automations are reloaded.

1 Like

Exactly. Personally, i’d add a trigger to the automation

trigger:
  - platform: state
    entity_id: binary_sensor.upstairs_motion
    from: 'off'
    to: 'on'
    id: nowoff
  - platform: state
    entity_id: binary_sensor.other
    from: 'off'
    to: 'on'
    id: regular
action:
  - if:
      - condition: trigger
        id: regular
    then:
      - asfsfsaasfasfsadfa
  - if:
      - condition: trigger
        id: nowoff
    then:
      - sdfasfasfasfasdfas

Or you can put these in separate automations. The caveat with the same automation is you’d need to set the automation mode to either queued or parallel depending on the logic.

You beat me to the punch with a combination. However, your example does not check for both conditions to be true, so it won’t do as asked. I had this example ready for how I would normally do this:

alias: Close curtain in evening if/when window closed
description: ''
mode: single
trigger:
  - platform: time
    at: '20:00:00'
  - platform: state
    entity_id: binary_sensor.openclose_bedroom
    to: 'off'
condition:
  - condition: time
    after: '19:59:59'
  - condition: state
    entity_id: binary_sensor.openclose_bedroom
    state: 'off'
action:
  - service: cover.close_cover
    data: {}
    target:
      entity_id: cover.cover_bedroom

Can I tag onto this, this seems to be the closest post to what I’m trying to accomplish.

So I have a motion control for my bathroom light. I have a separate motion sensor for the main room, and another motion sensor in the shower. Normally, the bathroom sensor senses motion, I wait for the trigger for motion to stop and the bathroom light turns off. What I want it to do is to also wait for the motion in the shower to stop as well. But, the kicker is that I want it to be a perpetual wait, not just a one time check. In other words, if the shower is detecting motion when the bathroom stops detecting motion, it will wait UNTIL the shower stops detecting motion and then still turn off the bathroom light. Rather than what seems to be happening which is when the bathroom stops detecting motion, it checks if the shower is detecting motion, and if it is, it makes the decision to not turn off the bathroom light and that’s it.

Here’s the yaml:

alias: Upstairs Bathroom Sink Light Motion (Night)
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 40d7e7188614674e7956b25eda87bcc1
    entity_id: binary_sensor.upstairs_bathroom_motion_sensor_motion
    domain: binary_sensor
condition:
  - condition: state
    entity_id: input_text.mode_helper
    state: Night
action:
  - service: light.turn_on
    data:
      kelvin: 3500
      brightness_pct: 10
    target:
      entity_id: light.upstairs_bathroom_ceiling_light
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: 40d7e7188614674e7956b25eda87bcc1
        entity_id: binary_sensor.upstairs_bathroom_motion_sensor_motion
        domain: binary_sensor
        for:
          hours: 0
          minutes: 2
          seconds: 0
    timeout: "60:00"
    continue_on_timeout: true
  - condition: state
    entity_id: binary_sensor.upstairs_shower_sensor_motion
    state: "off"
  - type: turn_off
    device_id: 24e895b9d6b59c6dfbbe1e46f0760791
    entity_id: switch.upstairs_bathroom_sink_light
    domain: switch
  - type: turn_off
    device_id: 563d0221e4f92116c62721f80c6b4303
    entity_id: light.upstairs_bathroom_ceiling_light
    domain: light
mode: single

I would not use a wait for trigger in this case for two reasons:

  1. It does not survive a restart or automations reload, so I never used it, maybe some day for very short waits.
  2. It is much easier to implement with an extra automation, triggered by both motion sensors, and with two conditions that both motion sensors must be off. I’d probably also put in a for: x seconds clause in both the triggers and the conditions, just to make sure light does not flicker when moving from one to the other.