Stairwell automation doesn't work as planned, help needed

I tried to set up an automation of my stairwell with 4 motion sensors and 3 lights.

What I expected:

Lights turn on when motion on any of the motion sensors is detected and light switches off after there is no motion for 30 seconds on all motion sensors.

What actually happens:

Lights turn on when motion is detected and turn off instantly, like on and off in one second. When I look into my automation’s log I can see the “wait” action is triggered after 30 seconds but at that point lights are already off.

How I did it:

I grouped the motion sensors in an HA binary group and made sure they’re not on “and” but “or”. Now I use them as a trigger to turn on the lights. After a short period of time the lights should turn off again. I tried to do that with a “wait for trigger” action. This is my YAML so far:

alias: "Stairwell light automation"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.stairwell_motion_sensors
    from: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: Motion detected
    to: "on"
    alias: Motion sensor detects motion
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.stairwell_lights
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.stairwell_1st_floor_occupancy
        from: "on"
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30
    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    enabled: true
    target:
      entity_id: light.stairwell_lights
mode: restart

In the last part I switched out the binary group with the exact motion sensor I’m experimenting with because I realized ALL of the motion sensors shouldn’t have detected motion for 30 seconds.

Well that automation is doing exactly what you told it to.
Sense a trigger, turn on a light, start a wait for that is set to time out immediately (and does do), then turn the light off.
So adjust the timeout on the wait for. It should be longer that the one you are actually waiting for.

1 Like

Thank you, I followed a tutorial where timeout was also set to 0 (that part is at 10:12 min.):

I don’t understand the logic behind it. Why is “wait for trigger” not set to 30 seconds? It says:

  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.stairwell_1st_floor_occupancy
        from: "on"
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30

Why even have a timeout here when there is a specified waiting time? And should timeout be just +1 on the “for” time (31 seconds in this case) or what would be the difference if it would be a larger number like 300 seconds?

The timeout is there in case whatever you are waiting for fails. Then it will not just hang forever.

If this has fixed your problem, would you perhaps consider selecting solution on one of the answers?

1 Like