Motion sensors and stopping the lights going out!

I have this simple automation that detects motion in a room, or if a door into the room has been opened and then turns on 2 lights, both via smart plugs:

alias: Kitchen - Lights On when motion detected or door state changed
description: ""
trigger:
  - type: motion
    platform: device
    device_id: f812b27b7f62a6f29272be68528dea71
    entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
    domain: binary_sensor
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
condition:
  - condition: numeric_state
    entity_id: sensor.lumi_lumi_motion_ac02_illuminance
    below: input_number.kitchen_illuminance_threshold
action:
  - type: turn_on
    device_id: eaa3e410315ce8d9b83da37ea83335a5
    entity_id: switch.tradfri_plug_kitchen_leds_switch
    domain: switch
  - type: turn_on
    device_id: 956725bc947f0414fa6e41ca315ee017
    entity_id: switch.tradfri_plug_kitchen_lamp_switch
    domain: switch
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: eaa3e410315ce8d9b83da44ea83635a5
    entity_id: switch.tradfri_plug_kitchen_leds_switch
    domain: switch
  - type: turn_off
    device_id: 956725bc947f0414fa6e21ca785ee017
    entity_id: switch.tradfri_plug_kitchen_lamp_switch
    domain: switch
mode: restart

There is then a 5 minute delay and the lights go out again.
I was hoping that by making the mode: restart, it would effectively refresh the 5 minute timer if people are moving about in the room, but every now and then, despite being active in the room, the lights go out after 5 minutes.

I think this is because the automation isnt actually triggered again unless the motion sensor returns to a clear state first, which it never is when people are moving around.

Is there any way around this ?

  1. Replace your triggers with a trigger template sensor that uses auto_off by adding this to configuration.yaml:
template:
  - trigger:
      - type: motion
        platform: device
        device_id: f812b27b7f62a6f29272be68528dea71
        entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
        domain: binary_sensor
      - platform: state
        entity_id:
          - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    binary_sensor:
      name: Motion detected
      state: 'on'
      auto_off: '00:05'

This will turn on when motion is detected and turn off after 5 minutes. Unless more motion is detected, then the timer will reset.

  1. Remove the delay from your automation and just look at the binary sensor. When it goes on and its dark - turn on the lights. When it goes off - turn off the lights:
alias: Kitchen - Lights On when motion detected or door state changed
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.motion_detected
    to: ['on', 'off']
    from: ['on', 'off']
action:
  - choose:
      - alias: Motion detected and its dark
        condition:
          - "{{ trigger.to_state.state == 'on' }}"
          - condition: numeric_state
            entity_id: sensor.lumi_lumi_motion_ac02_illuminance
            below: input_number.kitchen_illuminance_threshold
        sequence:
          - type: turn_on
            device_id: eaa3e410315ce8d9b83da37ea83335a5
            entity_id: switch.tradfri_plug_kitchen_leds_switch
            domain: switch
          - type: turn_on
            device_id: 956725bc947f0414fa6e41ca315ee017
            entity_id: switch.tradfri_plug_kitchen_lamp_switch
            domain: switch
      - alias: Motion stopped for 5 min
        condition: "{{ trigger.to_state.state == 'off' }}"
        sequence:
          - type: turn_off
            device_id: eaa3e410315ce8d9b83da37ea83335a5
            entity_id: switch.tradfri_plug_kitchen_leds_switch
            domain: switch
          - type: turn_off
            device_id: 956725bc947f0414fa6e41ca315ee017
            entity_id: switch.tradfri_plug_kitchen_lamp_switch
            domain: switch

As an added bonus, this way it won’t break when you restart HA. Restarting HA (or modifying this particular automation) stops the automation wherever it is. Which means if its running it probably stops during the delay. It doesn’t resume after that so your lights just get left on.

Trigger template sensors survive restarts so this automation and sensor combo works fine in those cases.

I am using timers in this cases, if a new movement is detected while the timer is still active i restart the timer to the origin value (say 10 minutes), so it will start a new count.

Why not another trigger with trigger id in the same automation that triggers if there is no motion for x minutes and then turns of the light?

@CentralCommand, some really interesting stuff there that I will definitely investigate.

@rodak 's idea also seems like a straight forward solution.

Thanks all.

So, ahead of attempting @CentralCommand 's idea, I thought I’d try simply changing the automation to be in line with @rodak 's suggestion.

So rather than wait for 5 minutes, I set a wait for trigger action (no motion) after the plugs are turned on. This is the new automation:

alias: Kitchen - Lights On when motion detected or door state changed
description: ""
trigger:
  - type: motion
    platform: device
    device_id: f812b27b7f62a6f29272be68528dea68
    entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
    domain: binary_sensor
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
condition:
  - condition: numeric_state
    entity_id: sensor.lumi_lumi_motion_ac02_illuminance
    below: input_number.kitchen_illuminance_threshold
action:
  - type: turn_on
    device_id: eaa3e410315ce8d9b83da37ea83635a5
    entity_id: switch.tradfri_plug_kitchen_leds_switch
    domain: switch
  - type: turn_on
    device_id: 956725bc947f0414fa6e41ca785ee017
    entity_id: switch.tradfri_plug_kitchen_lamp_switch
    domain: switch
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: f812b27b7f62a6f29272be68528dea68
        entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
        domain: binary_sensor
        for:
          hours: 0
          minutes: 3
          seconds: 0
    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - type: turn_off
    device_id: eaa3e410315ce8d9b83da37ea83635a5
    entity_id: switch.tradfri_plug_kitchen_leds_switch
    domain: switch
  - type: turn_off
    device_id: 956725bc947f0414fa6e41ca785ee017
    entity_id: switch.tradfri_plug_kitchen_lamp_switch
    domain: switch
mode: restart

Whats interesting is that the automation hangs on the wait for trigger, and my hunch is because of the three minute condition.

Looking at my automation traces, I see the following:-

Automation start at 08:15:23 then since then has paused at 08:15:24 on the wait for trigger.

Looking at the motion sensors updates, I have the following:-

Cleared (no motion detected)
08:17:23 - 21 minutes ago
Detected motion
08:15:23 - 23 minutes ago

So motion was detected and the automation fired, but the motion sensor sent a clear state only 2 minutes later, and nothing after that (everyone left the room). But actually, 21 minutes have now passed since that clear state, and thats what I thought the automation would check against for the 3 minute condition i set. It actually looks like it needs another ‘clear’ message after the three minutes for that condition to be satisfied, which is not what I was expecting.

Can anyone confirm thats the case ?

Whats this for? Maybe try removing this.

That sets a timeout of zero, but not to continue past the trigger if there is a timeout.
So basically… do not pass the wait state until no motion is detected, no matter how long it takes to detect it.
I used the UI to add the wait trigger and its added that part automatically. I don’t think thats the issue.

Turns out it was the timeout!

Not sure whats happening there. If you use the UI to create the wait for trigger action, it automatically adds a timeout section and if you slide the ‘continue on timeout’ to false, but leave the rest as 00:00:00 it clearly confuses it.

Hi @Rofo

My proposal was different that this.

I’m on mobile now but basically you need two triggers in the same automation.
One for motion, and one for no motion for x minutes.
Both need different trigger id’s. “id: motion” and “id: no-motion” for example.
Then you make different “choose” actions where you use the trigger id’s as condition.

Here is an example that triggers both for door open and closed and performs different actions depending on which trigger that fired the automation:

alias: Magnet Light
description: ""
trigger:
  - type: opened
    platform: device
    device_id: xxxxx
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
    domain: binary_sensor
    id: Källare-open
  - type: not_opened
    platform: device
    device_id: xxxxx
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
    domain: binary_sensor
    id: Källare-closed
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Källare-open
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kallare_trappa_tak_1
      - conditions:
          - condition: trigger
            id: Källare-closed
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kallare_trappa_tak_1
mode: single

This means never timeout. By setting all these to zeroes you told the automation to wait for the trigger indefinitely, no limit on how long it should wait.

@CentralCommand - that’s exactly what I thought it would do. But if I remove the entire timeout section from my automation it works as intended. (I assume no timeout section at all is basically the same as what I had with zeroes and false)