If no motion for 4 minutes turn off, but if motion, reset the 4min timer

Hi guys
I’m struggling, but I think this should be possible with the regular HA tools, no?

So, I have the bathroom light turn on, upon philips motion sensor “becom occupied”.
Now of course I could have it wait 4mins, then switch it off. But reality showed that it isnt really practical. Sometimes people move, but when the 4 mins are over, they haven’t moved a while and the state of the philips motion sensor says: unoccupied.

So the light will turn off, even if somebody is sitting on the toilet haha.

=> so what I want to poll for is 4 minute “uninterrupted” non-occupancy. My idea was: Everytime the sensor senses occupancy, it resets a helper (4min timer) and starts it fresh. The timer ticks down, and always gets reset back to 4min, if there is movement.

If there is no movement for the entire 4mins, switch off the lights.

Can I get this to work, using a helper?

No need for a timer. This requires no motion for 2 minutes before the light is turned off. Adjust it to 4 minutes and it will do what you want:

1 Like

thanks tom
So you are saying, that in that script, if during these 2 minutes, there is motion, it will be set back to 2mins again. and again etc… until there has been 2 minutes for uninterrupted no-motion?

Correct.

In order for the trigger to trigger the sensor needs to be off for two minutes.

So yes, if the sensor turns on before that two minute expires the trigger does not trigger.

1 Like

It’s magic! Thanks for the additional info Pete, now I finally got it. It was too clever for me.

I still need a bit of help with the logic.

the “on” and “off” in this script, in the first part, what does that refer to?
Motion On / off, so is this for the sensor stater?

or does that say what “order” is being sent to the light service, so on/off?

I’m not sure. I got positive traces for both motion on (occupied) as well as the trace for 10 seconds of unoccupied.

But the light part I got no trace for.

Here’s what I did so far (I’m switching on/off the light switch, not the bulb):

alias: Bathroom switch on off
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 5398f8e759e8c73728e3760be72b080b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
  - type: not_occupied
    platform: device
    device_id: 5398f8e759e8c73728e3760be72b080b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
    for:
      seconds: 10
action:
  - service: light.turn_{{ trigger.to_state.state }}
    target:
      entity_id: light.lightswitch_toilet_eg_light
mode: single
1 Like

for some reason I think it just started to work now.
I’ll observe and then mark the issue as resolved.

There was a reason I used state triggers.