Light automation on door sensor and detection

It didn’t work correct:(
I open the door - lights turn on. Then I close the door, open, close (nothing happens) and then I open it again and lights instantly turn off

- id: '1530647280357'
  alias: WC light on
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'off'
  action:
  - data:
      entity_id: switch.a15_key
    service: switch.turn_on


- id: '2845647836457'
  alias: WC light off
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001e5d691
      to: 'on'
  condition:
    condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'on'
  action:
    - wait_template: "{{ is_state('binary_sensor.motion_sensor_158d000224aa83', 'off') }}"
    - service: switch.turn_off
      entity_id: switch.a15_key

If I read your question correctly, you want to use the door sensor as a switch, where the motion sensor determines whether or not to switch on/off. Assuming that the door is always closed either way. My example automation would be (untested)

automation:
  - alias: Toggle Bathroom Lights
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001e5d691
        from: 'off'
        to: 'on'
    action:
      - service_template: >
          {% if is_state('binary_sensor.motion_sensor_158d000224aa83', 'on') %}
            switch.turn_off
          {% else %}
            switch.turn_on
          {% elif %}
        entity_id: switch.a15_key

Hey, thank you for your time
Sorry for my english
Not really like a switch in a case of turning off

I want lights not to to turn off, when detection sensor is off, but door was not opened second time (So if sensor does not see movement, but I am still in the bathroom). And I want light not to turn off, when door sensor is on, but motion sensor is still on
So, to turn off the light I need two conditions:
-door was opened and closed,
-motion sensor turns off.

Are you sure, that ‘if’, ‘elif’, ‘else’ are working in yaml? I just tried to copy your example:

Invalid config for [automation]: invalid template (TemplateSyntaxError: Encountered unknown tag "elif". Jinja was looking for the following tags: "endif". The innermost block that needs to be closed is "if".) for dictionary value @ data["action"][0]['service_template']. Got None. (See /config/configuration.yaml, line 73). Please check the docs at https://home-assistant.io/components/automation/

It is elseif :man_facepalming:

My automation turns on the light when the door is opened and closed AND there is NO motion. The light is turned off when the door is opened and closed AND there is motion.

Your automation should be just fine I think
but for now I get this issue:

found character '%' that cannot start any token

Assumption: When someone enters the bathroom the motion detector will sense it and the corresponding HA entity (like @sparkydave, I’ll call it binary_sensor.bathroom_motion, since you didn’t say what it was named) will go from ‘off’ to ‘on’.

It would seem to me you want:

  1. When someone enters the bathroom (and the motion detectors senses it), the light (I’ll call it light.bathroom) should go on. This should not depend on the state of the door.
  2. When the motion sensor stops sensing motion, and if the door is open (I’ll call it binary_sensor.bathroom_door, again, because you didn’t say what it was named, and 'on' means open), the light should go off. This is the case where someone goes into the bathroom, doesn’t close the door, and then leaves.
  3. When the door opens, the light should go off. This is the case where someone goes in the bathroom, closes the door, stays a while, then opens the door and leaves. In this case, it doesn’t matter if/when the motion sensor stops sensing motion.

If you think these events/conditions will work for you, then you could automate it like this:

automation:
  - alias: Turn bathroom light on when someone enters
    trigger:
      # Motion detector sensed motion
      platform: state
      entity_id: binary_sensor.bathroom_motion
      to: 'on'
    action:
      service: light.turn_on
      entity_id: light.bathroom

  - alias: Turn bathroom light off when no motion and door open, or door opens
    trigger:
      # Motion detector stopped sensing motion
      - platform: state
        entity_id: binary_sensor.bathroom_motion
        to: 'off'
      # Door opened
      - platform: state
        entity_id: binary_sensor.bathroom_door
        to: 'on'
    condition:
      # Door is open
      condition: state
      entity_id: binary_sensor.bathroom_door
      state: 'on'
    action:
      service: light.turn_off
      entity_id: light.bathroom

The only likely scenario this doesn’t cover is if someone goes into the bathroom, doesn’t close the door, stays, but the motion sensor eventually stops detecting motion. In this case the light would go off, but I don’t see how that could be avoided. Except, maybe, you could add a timer to the mix and use it to make sure the light always stays on for a minimum amount of time.

Actually, you might want a small delay before turning the light off. This could, at least partially, address the “unhandled” scenario. So maybe something like this instead:

script:
  turn_off_bathroom_light:
    sequence:
      - delay: '00:00:05'
      - service: light.turn_off
        entity_id: light.bathroom_light

automation:
  - alias: Turn bathroom light on when someone enters
    trigger:
      # Motion detector sensed motion
      platform: state
      entity_id: binary_sensor.bathroom_motion
      to: 'on'
    action:
      - service: script.turn_off
        entity_id: script.turn_off_bathroom_light
      - service: light.turn_on
        entity_id: light.bathroom

  - alias: Turn bathroom light off when no motion and door open, or door opens
    trigger:
      # Motion detector stopped sensing motion
      - platform: state
        entity_id: binary_sensor.bathroom_motion
        to: 'off'
      # Door opened
      - platform: state
        entity_id: binary_sensor.bathroom_door
        to: 'on'
    condition:
      # Door is open
      condition: state
      entity_id: binary_sensor.bathroom_door
      state: 'on'
    action:
      service: script.turn_off_bathroom_light

Thank you. Will try it tonight. Now I read your code, seems to me, that it is not going to work, because motion sensor is still “on” after living me the bathroom for two minutes. And I will open the door, and then close, so your condition will not be true.
Correct me if I got code wrong

This is likely to never work, no matter the piece of code. A movement sensor won’t always report movement, so you’ll have a lot of instance where you close the door bit the movement sensor is not anymore or has not yet reported your movement.

It could maybe work if you always leave the door open when not in, and close when in.

Or simply turn on the light for 30 minutes, one would have to stay still 30 minutes to have an issue.
Or buy a microwave or radar sensor, it will detect a human even if not moving or in a shower →

Right now I turn on the light by door sensor + no motion in bathroom and turn off when there is no motion for 12 minutes, but again, it does not work with taking shower. That is why I wanted two conditions: secon time open close door (not first, when I enter the bathroom) and no motion
I use aqara motion sensors, not sure there are your sensors on zig bee in nice box. Would like everything to be pretty, not DIY

Although fun to try I agree with @touliloup it’s never going to work reliable without proper sensors to detect if somebody is actually in the bathroom or taking a shower.
As you’re using xiaomi sensors maybe you can use the water leak sensor to detect if the shower is running?

I have one, but really? How do you picture that?:slight_smile:


You want me to put it insede the bath? It is really srtange, it will swim under my feet:)
But thank you for advice. Maybe humidity sensor will work

Actually a temperature sensor might also work, depending on your shower/tub, you might be able to stick the temperature sensor under it, and detect presence if temperature increase 5°c in the last 10 minutes. The only problem is that it might take some time before it goes down in temperature again.

Water flow sensor also exist, but would require some pipe work. And doesn’t work if you just chill in your bathtub for 20 minutes :slight_smile:

:joy: ok that might not work.
Humidity could do the trick but the xiaomi temperature/humidity sensors are not updating at a regualr interval as far as I know. There has been some discussion on this.

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