Help with automation: turn off lights if no motion for 10 minute (2 motion sensors)

Hi everyone,
I need help setting up an automation that turns off the lights if neither of two motion sensors detects motion for 10 minutes. It’s important that the timer resets if either sensor detects motion again during that time.
Here’s the code I currently have, but the lights never turn off:

description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.czujnik_salon
      - binary_sensor.czujnik_kuchnia
    to: "off"
    for: "00:10:00"
condition:
  - condition: state
    entity_id: binary_sensor.czujnik_ruchu_w_salonie_dodatkowy_occupancy
    state: "off"
  - condition: state
    entity_id: binary_sensor.czujnik_ruchu_w_salonie_occupancy
    state: "off"
action:
  - service: switch.turn_off
    target:
      device_id:
        - af15d55424f2ebb176a67bba70d88ded
        - 0d3ed1bc48d9f2a5b06c21ab23529d8f
        - 72cd6828b65f115e1e4c2944c5adb602
mode: restart

Any ideas why this isn’t working? Or how I can better structure the automation so the timer resets properly when motion is detected again?
Thanks in advance!

Can you be a little bit more specific? You actually reference four binary_sensor entities in your automation, but your post only talks about two.

With respect to just the trigger portion of your automation, it will trigger if either of those two sensors has stayed in the off state for 10 minutes, even if the other one has been on. But that isn’t your immediate problem, since you’re saying that the action block never executes.

I’ve tried different combinations — sometimes the light turns off when just one of the motion sensors hasn’t detected movement for 10 minutes, completely ignoring the other one (usually the first sensor added seems to take priority). But with the version I shared, the light never turns off at all.

I’m using two motion sensors:

  1. binary_sensor.czujnik_ruchu_w_salonie
  2. binary_sensor.czujnik_ruchu_w_salonie_dodatkowy

And three light switches:

  1. af15d55424f2ebb176a67bba70d88ded
  2. 0d3ed1bc48d9f2a5b06c21ab23529d8f
  3. 72cd6828b65f115e1e4c2944c5adb602

I don’t mean to be picky but I’m even more confused now. Your code referenced:

  1. binary_sensor.czujnik_salon
  2. binary_sensor.czujnik_kuchnia
  3. binary_sensor.czujnik_ruchu_w_salonie_dodatkowy_occupancy
  4. binary_sensor.czujnik_ruchu_w_salonie_occupancy

Your most recent post, though, references

Perhaps you just left off the _occupancy on those two when you typed your response?

Either way, are these actual sensors, what is in your trigger?:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.czujnik_salon
      - binary_sensor.czujnik_kuchnia
    to: "off"
    for: "00:10:00"

If those actual entity_ids don’t exist, then of course the automation won’t trigger…

The two separate binary_sensor entities you have in your conditions aren’t going to trigger the automation. Conditions are different than triggers.

here is how I do it, although some don’t like the ‘wait for’ usage, it works reliably for me.

alias: Motion Lights Hallway Down
description: ""
triggers:
  - type: occupied
    device_id: 31b4a852a01d0918f4c722efab915b53
    entity_id: 580234a06249db27247c2c882ea284b7
    domain: binary_sensor
    trigger: device
  - device_id: 275641795ee94571138399b1dca81c5c
    domain: media_player
    entity_id: 911428e036347bbcc20b1b4a518a14cd
    type: turned_off
    trigger: device
conditions: []
actions:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 40
      transition: 10
    target:
      device_id:
        - 4718eb90a08f144806bfc39fdce6cfd6
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 30
      transition: 10
    target:
      device_id:
        - 05da142869cc75b57060c8f83f01889c
  - wait_for_trigger:
      - type: not_occupied
        device_id: a56178d76d61528b08bd491f2bd0b293
        entity_id: a5f5a8b6f63718e7be93fcbf05f7ae3e
        domain: binary_sensor
        trigger: device
        for:
          hours: 0
          minutes: 0
          seconds: 30
      - type: not_occupied
        device_id: 31b4a852a01d0918f4c722efab915b53
        entity_id: 580234a06249db27247c2c882ea284b7
        domain: binary_sensor
        trigger: device
        for:
          hours: 0
          minutes: 0
          seconds: 55
      - type: not_occupied
        device_id: fed16781d915239920f0ab4f6f844d64
        entity_id: 6c627101a1cc895725aa7f17193410f9
        domain: binary_sensor
        trigger: device
        for:
          hours: 0
          minutes: 0
          seconds: 30
    timeout:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - action: light.turn_off
    metadata: {}
    data:
      transition: 15
    target:
      device_id:
        - 2997bc90e8ee1239f734ed5a63926c50
        - 4d35c0302daa915961165be5b23fa937
        - 4718eb90a08f144806bfc39fdce6cfd6
        - 05da142869cc75b57060c8f83f01889c
        - 95793fb806f263e326906ff980b7daf8
mode: restart

this one only has 1 motion sensor, but I have others with multiple, you simply include them in the triggers and wait for sections.

this turns on the lower stair lights when motion, or when I turn off my receiver/tv/etc, then waits until specified motion is cleared for however long, and shuts the lights off.

although lights can be controlled in other automations, the only real down side to this, is it does not easily allow for manual over ride, once motion is triggered and cleared, the lights go on and off in their specified manner, regardless of prior state and intention.

I have a couple of spaces that have multiple presence sensors.

For these I have utilized “groups”

Create a helper from your two binary sensors.



Then you can have that group sensor turn on and off based upon the group.

3 Likes

I created a group, but I dont see how to add it whe ncreating an automation.

Just add it as a state trigger, example:

entity_id:
  - binary_sensor.hall_motion_sensors
to: "off"
for:
  hours: 0
  minutes: 10
  seconds: 0
trigger: state

Also skip the conditions, not needed now when you have a group of sensors, just the trigger and then the action.

Here is a much simplified example off my motion sensor automation that turns on and off the lights with a single automation based on trigger id’s.
I just have one single automation for all my motion sensors.

alias: Motion Light
description: ""
triggers:
  - entity_id:
      - binary_sensor.hall_motion_sensors
    to: "on"
    id: hall-motion
    trigger: state
  - entity_id:
      - binary_sensor.hall_motion_sensors
    to: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: hall-no-motion
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - hall-motion
        sequence:
          - data: {}
            target:
              entity_id: light.hall_tak_light
            action: light.turn_on
      - conditions:
          - condition: trigger
            id:
              - hall-no-motion
        sequence:
          - data: {}
            target:
              entity_id: light.hall_tak_light
            action: light.turn_off
    default: []
mode: single

2 Likes

I have one more problem: I set up this code to turn off the light in the bathroom with 2 motion sensors (I created a group) and everything works fine

description: ""
triggers:
  - entity_id:
      - binary_sensor.czujniki_ruchu_w_lazience_na_parterze
    to:
      - "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: lazienka-parter-wylacz-swiatlo
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lazienka-parter-wylacz-swiatlo
        sequence:
          - data: {}
            action: switch.turn_off
            target:
              entity_id:
                - switch.swiatlo_w_lazience_na_parterze
    default: []
mode: single

I made another one for turning the light on, also with the group of motion sensors, so that it would only turn on when both detect motion. It seemed to work, but for the past few days, no matter which single sensor detects motion, the light turns on.

description: ""
triggers:
  - entity_id:
      - binary_sensor.czujniki_ruchu_w_lazience_na_parterze
    to:
      - "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
    id: lazienka-parter-wlacz-swiatlo
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lazienka-parter-wlacz-swiatlo
        sequence:
          - data: {}
            action: switch.turn_on
            target:
              entity_id:
                - switch.swiatlo_w_lazience_na_parterze
    default: []
mode: single