Automation works but unwanted trigger

I want to switch on a light when I enter my home when it’s dark.

The trigger is ‘binary_sensor.garagepoort_gesloten’, the gate that opens (state on to off) in my garage.
It should also be dark, so I have this as condition (brightness below 20).
In the garage I have a motion sensor.
To determine the direction (the light must not be switche on when I left my home), I have the gate as trigger and the wait_for_trigger for the motion sensor.

alias: Switch on light when entering home
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.garagepoort_gesloten
    from: 'on'
    to: 'off'
condition:
  - condition: numeric_state
    entity_id: sensor.lightsensor_illuminance_lux
    below: '20'
action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.motionsensor_garage_occupancy
        id: binary_sensor.motionsensor_garage_occupancy
        to: 'on'
    continue_on_timeout: true
  - service: switch.turn_on
    target:
      entity_id: switch.light_keuken_indirect
mode: single

When I enter at night, the light switches on, so that works.

Unluckely, this afternoon I came in and the light was also triggered to switch on.
I had a look in the automation and found:


So this morning 8:03 the gate opened, it was dark and 5 hours later, when I came in the motion sensor was triggered and the light was switched on.

Is there a way to limit the ‘wait for state trigger’ for example for 5 minutes? The automation should not wait 5 hours for the next action.

Yes, it’s explained in Wait Timeout. Basically, you need to add a timeout value (there’s none by default).

You will need to change continue_on_timeout to false so that if the waiting time exceeds the timeout value it will not proceed to the service call to turn on the light.

Thanks for the hint.
I changed my automation as per below.
The future will learn if it works but I believe this is the correct solution. Thanks!

alias: Switch on light when entering home
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.garagepoort_gesloten
    from: 'on'
    to: 'off'
condition:
  - condition: numeric_state
    entity_id: sensor.lightsensor_illuminance_lux
    below: '20'
action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.motionsensor_garage_occupancy
        id: binary_sensor.motionsensor_garage_occupancy
        to: 'on'
    timeout: '00:05:00'
    continue_on_timeout: false
  - service: switch.turn_on
    target:
      entity_id: switch.light_keuken_indirect
mode: single

For future reference, should you ever want to do one thing if wait_for_trigger is triggered and something different if it times out, here’s some code that demonstrates how to do that:

action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.motionsensor_garage_occupancy
        to: 'on'
    timeout: '00:05:00'
  - choose:
      - conditions: "{{ wait.trigger == none }}"
        sequence:
          - service: notify.persistent_notification
            data:
              title: 'Wait for motion timed out.'
              message: 'Timed out after 5 minutes.'
    default:
      - service: notify.persistent_notification
        data:
          title: 'Wait for motion triggered.'
          message: 'Motion detected within 5 minutes.'

It sends different notifications depending on whether there was a trigger or a timeout (obviously you would replace the notifications with whatever service calls you require).

1 Like