Turn light off if another sensor had movement

Hey guys!

There is hue motion sensor in my bathroom (BR) and my corridor ©. Both are working fine turning lights on/off.
From the bathroom you have to pass the corridor.

Now, my idea is the following:
When BR-sensor states changes to off, but the C to on and BR sensor stays off for another 15 seconds, BR lights should turn off.

That should prevent two things, having lights on, even if there is no one in BR and switching lights off faster and not wait another minute or what ever.
Since that was my previous solution, to wait a minute after sensor switches to off.

Till now, I tried the following:

 trigger:
  - entity_id: binary_sensor.presence_8    #<- BR-sensor
platform: state
    to: 'off'
  condition:
  - condition: template
    value_template: '"{{ (as_timestamp(now())-as_timestamp(states.binary_sensor.presence_5.last_updated))
      < 30 }}"'    #<-- C-sensor
  action:
  - delay: 00:00:10
  - entity_id: light.licht_decke_badezimmer
    service: light.turn_off

The light wont switch off with this automation.

Does anyone has an idea what I’m doing wrong?
Also, how do I know all possible attributes from a device/entity? like last_updated (which i have from another thread)

Thanks a lot !

A few things. First, the spacing on the line ‘platform’ is wrong. Could just be a copy/paste error, but if not, this will never trigger because of syntax.

Next, too many quotes on the value_template. Just use the double quotes.

value_template: "{{ (as_timestamp(now()) - is_timestamp(states.binary_sensor.presence_5.last_updated)) < 30 }}"

So this says, when the bathroom presence sensor goes to off, only turn the lights off if the hallway sensor changed in the last 30 seconds.

You better hope the presence sensor turns to ‘off’ within those 30 seconds. If its a normal motion switch, it will turn off pretty much right away. But because it’s labeled as “presence_8”, it could be a presence detector which turns on with motion, but stays on for some amount of minutes. If this is the case, you could leave the bathroom, walk through the hall…then a minute later the presence_8 would turn to ‘off’. Suddenly, your condition is false, so lights would never turn off.

Also, I have to assume you live alone with no pets? If not, if someone walks through the hall, your lights are going to turn off once the presense goes to off :stuck_out_tongue:. If this is a standard motion sensor, it will be off for probably a minute until it will wake up to detect another motion event.

Why not just trigger on the hallway sensor, and add a wait_template?

trigger:
  # Trigger when there is motion in the hallway
  platform: state
  entity_id: binary_sensor.presense_5
  to: 'on'
condition:
  # Only do this if the lights are on and someone is in the bathroom.
  - condition: state
    entity_id: light.licht_decke_badezimmer
    state: 'on'
  - condition: state
    entity_id: binary_sensor.presence_8
    state: 'on'
action:
  # Wait for the BR sensor to report 'off'
  - wait_template: "{{ is_state('binary_sensor.presence_8', 'off') }}"
  # Wait 10 more seconds
  - delay: "00:00:10"
  # Last second sanity check....are they really out of the bathroom?
  - condition: state
    entity_id: binary_sensor.presence_8
    state: 'off'
  # Turn off the lights.
  - service: light.turn_off
    entity_id: light.licht_decke_badezimmer

Thanks you @jocnnor.

I guess the wrong quotes came from working with lovelace and switching between yaml and gui editor. Same happened when using your code first time. I switched using the config file then.

Yeah, no pets here :smiley:
And regarding other ppl walking the hallway is the reason why I tried to add this value_template checking whether there is still someone in the bathroom. (not using it now anymore)

Your option works very fine for me! I will see, if i might have to adjust the delay because ppl not moving while sitting on the toilette :smiley:

The only thing i discovered is that when i move through the hallway to the bathroom and leave it quickly (like 2-3 seconds) lights stays on.

Ah, good catch. This wont work unless you are in the bathroom for long enough that the presense_5 motion detector resets and is able to capture the next state.

Change the trigger

trigger:
  # Trigger when there is motion in the hallway
  platform: state
  entity_id: binary_sensor.presense_5
  to: 'off'
  for: 
    seconds: 10

This gives you a 10 second window to get from the first motion event into the bathroom until it will wait for you. As long as you make it into the bathroom before that timer, it should work…until the next corner case is found lol

Good morning @jocnnor :wink:

Good idea Jim! It just also means that when I enter the bathroom, I have to move after 10 seconds otherwise the light will turn off :wink:
So, I will stay with it as it is now.

May I just ask if I understand the wait_template correctly?
Does
wait_template: "{{ is_state('binary_sensor.presence_8', 'off') }}"

mean that the automation waits so long till the BR sensor switches to off? Even if it takes 5 minutes and than waits for the time set in delay before switching off the light?

Thank you!

yes.

it will wait forever unless you tell it otherwise.