Light automation on door sensor and detection

Tags: #<Tag:0x00007fc40d879348>

They actually do better then regularly updating, they only update when the temperature or humidity changed. So it will report any change, but stay quiet if none changed.

Thank you for advices again, will try out some examples. And here is another algorithm:

Motion sensor turns to “on”:
Turn on the light
When door goes from closed to open and then again to close, wait for motion sensor to turns off, then turn off the light.

I think this code should be in one cycle, so I am not shure, that I can write it on yaml. How do you think, will it work if I will write a piece of python code? (Or not me, because I know just basic things about programming)

Well, I might have guessed wrong how you’d like this to work. E.g., I assumed the bathroom door would be left open all the time except for when someone is actually in the bathroom. You’ll need to adjust accordingly. I think the main point is that a state trigger using to: will only “fire” (i.e., the actions will only be run) when that entity changes to that state. It doesn’t matter how long it is in that state. It’s the state change that triggers the automation, not the state level.

That is why I say about python code, not yaml, where I should have a trigger like you said. Can not I write a piece of code, which will follow that steps (closed, theb opened, then closed)

It might be possible to do this in YAML. You’d need a counter and possibly a few automations. The basic idea is, when the sensor detects motion, reset the counter and turn on the light. Then when the door opens, increment the counter to one. Then when it closes, increment the counter to two. And when the motion detector goes off and the count is two, turn off the light. Something like that. But you’d have to consider all potential sequences of events to make sure it actually works for you. Not knowing the exact behavior of your sensors and all the potential sequences of events, it’s kind of hard to write something.

1 Like

that’s where you can use the wait_template: in your action. It allows the automation to trigger on whatever you put in the trigger section, but then wait for a second thing to occur (whatever you put in the wait_template:) before the actual action takes place.

1 Like

I ended using that:) Thank you


  alias: Bath light on
  - entity_id: binary_sensor.motion_sensor_158d000224aa83
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  - data:
      entity_id: switch.a15_key
    service: switch.turn_on

- id: '4332954240888'
  alias: Bath light off
  - platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    from: 'off'
    to: 'on'
    condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'on'
    service: script.4234949665413'


  alias: turn_off_bath
  - wait_template: '{{ is_state(''binary_sensor.motion_sensor_158d000224aa83'', ''off'')
  - service: switch.turn_off
    entity_id: switch.a15_key

I used script in the end, because in your example if I open and close the door again after all automation, it was triggering again and lights turn of without waiting motion sensor to turn off.