[solved] Automation with motion sensor & light: sometimes in the dark. Why?

Can anyone please tell me what is wrong with the following automation that, even being close/in front of the motion sensor and moving around, sometimes the light goes out?

alias: "Kitchen: motion"
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 89c5a9c26431823543a610e05923512a
    entity_id: binary_sensor.kitchen_motion
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.sleeping
        state: "off"
      - condition: state
        entity_id: input_boolean.somebody_is_home
        state: "on"
action:
  - service: light.turn_on
    data:
      transition: 2
      brightness_pct: >-
        {{ 1 if is_state('input_boolean.bedtime', 'on') or
        is_state('input_boolean.sleeping', 'on') else 90 }}
    target:
      entity_id: light.kitchen
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion
        to: "off"
        for:
          hours: 0
          minutes: 2
          seconds: 0
  - service: light.turn_off
    data:
      transition: 5
    target:
      entity_id: light.kitchen
mode: restart

Thanks a lot for helping!

EDIT: it’s a Philips Hue motion sensor, so the cooldown time should be 10s

See if this works any better. If it doesn’t then the motion sensor’s cooldown period might be in question.

alias: "Kitchen: motion"
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: 'on'
    from: 'off'
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    to: 'off'
    from: 'on'
    for:
      minutes: 2
condition:
  - condition: state
    entity_id: input_boolean.sleeping
    state: "off"
  - condition: state
    entity_id: input_boolean.somebody_is_home
    state: "on"
action:
  - service: light.turn_on
    data:
      transition: 2
      brightness_pct: >-
        {{ 0 if trigger.to_state.state == 'off' else 1 if is_state('input_boolean.bedtime', 'on') or
        is_state('input_boolean.sleeping', 'on') else 90 }}
    target:
      entity_id: light.kitchen
mode: single 
1 Like

Hi Taras, thank you for your help!
I’m testing it out and will let you know how it goes.

The strange thing with the way I had done it: most of the time it worked flawlessly and occasionally you would be in the dark.
I didn’t know why/where it went wrong but the most important is that it works now.

TBH: your solution, I get every step separately but not the logic behind the whole.
Would you mind elaborating a little please because I like to understand/learn?

  1. why the platform: state as a trigger?
  2. what does that 2 minute timer really do?
  3. why the conditions not with a logical AND?
  1. State Trigger is less verbose than a Device Trigger.
  2. Same as the State Trigger you created for your wait_for_trigger.
  3. A list of conditions is logically ANDed by default.
1 Like

Hi Taras, or anyone else, there seems to be some issue with this automation.

It has happened again, maybe 3 times, since: that the light turns off, even being in front at about 1,5 meter of the sensor and moving all the time in the detection field.
It just happened again right now, from the history:

detected
cleared: 6 minutes, 59 secs later
detected: 2 minutes, 58 secs later
cleared: 14 secs later

I don’t understand the effect of the 2 minutes in this all, what does it do in simple words.
On top of that, I don’t understand why/where it goes wrong so any help is highly appreciated!

As mentioned in my previous post, if the suggested automation fails to work reliably, it’s not the automation’s fault but the motion detector. Check its motion sensitivity and cooldown period (if it’s adjustable).

You should be able to check the cooldown time in the logs.
Also not sure which zigbee you use, but with zigbee2mqtt i can change the cooldown timer on my ikea motion sensors :wink:

It takes 10 secs for the sensor to clear and immediately it can detect motion again.
Is that 10s cooldown then?

There is something with ‘Delay’ but it’s not clear to me what it means/does (have not touched it yet):

Yea, that is the cooldown.
Before I found out that i could change my cooldown (180 s) on the ikea motion sensor, i used to make sure the light would be on for at least 4-5 minutes, so the sensor would have 1-2 minutes to re-activate :wink:

Just happened again: light was on for almost 10 minutes and then it switched off.

That 2 minute timeframe: is that the time in which the light should stay on anyway and when it ends, the state of the sensor is evaluated?

No, my 2 minute time frame ensures the lite would not switch off during the cooldown time.
But within that 2 minutes the motion sensor should be triggered again for the light to stay on.

I don’t get it…
Do you mean that the sensor has to be triggered IN the 2 minute timeframe so the light stays on AFTER the 2 minutes have passed?
This seems to me the obvious course of events, the way a light should work with a motion sensor and from what I understand, this automation works that way, no?

Yesterday, again light off, even being close to the sensor (about 1,5m) and moving.
After 30s, the light switched on again.
That doesn’t make sense with a sensor that has 10s cooldown, no?

after which the new cooldown starts….and the automation restarts
That is exactly when i ment

I also just noticed

Shouldn’t that be restart???

If you’re referring to the example I posted, no.

1 Like