Light on when door opens, light off when door closes

Hi all. I’m quickly learning HA and porting all my devices and automations over from SmartThings. What a fantastic platform. Why did I take so long to come over??

Anyway, I’ve got an automation that toggles a light when a door sensor is opened.

1st time - door open -> light on -> door closed -> light stays on (enter room)
2nd time - door open -> light off -> door closed -> light stays off (leave room)

This works, but I’d like the light to go off when the door is shut, not opened when leaving the room.

1st time - door open -> light on -> door closed -> light stays on (enter room)
2nd time - door open -> light stays on -> door closed -> light off (leave room)

I can’t figure out how to achieve this. I can’t find a solution searching the forum or online. Ideally, I’d like to do this using the UI, as my YAML skills are sorely lacking. But, I am learning!

Current automation:

alias: Toilet door open / closed
description: ‘’
trigger:

  • type: opened
    platform: device
    device_id: 069100a1f71175ca0533b76de8157541
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_7575a904_on_off
    domain: binary_sensor
    condition: []
    action:
  • type: toggle
    device_id: 1898fa3f02609717292baeef46221c05
    entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
    domain: light
  • delay:
    hours: 0
    minutes: 40
    seconds: 0
    milliseconds: 0
  • type: turn_off
    device_id: 1898fa3f02609717292baeef46221c05
    entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
    domain: light
    mode: restart

The bit at the end is to turn the light off if the door is accidently left open.

Any ideas?

Many thanks

1 Like

I think what I might do is have an input_booleans to act as a variable.
So when the door is opened:
check the “first” boolean, if it’s false, this is the first time the door is opened, so turn on the light
When the door is closed:
if the boolean is true, don’t turn off the light, else do turn off the light

Something like that anyway. If you really need more help, I’ll have a go at it myself and see if I can get it working

Thanks.
Can I set booleans in the UI?
I’ll try and read up on it and have a go.

I don’t actually know, I do most of this in the yaml files.
I couldn’t resist trying out your problem though. Here’s what I hacked together:

input_boolean:
  # this is a stand-in for an actual door, I couldn't bothered to get up and down opening
  # and closing the door:
  test_door:
  test_door_first:
  test_door_light:

automation:
# door opens for the first time
  - alias: 'test_door1'
    trigger:
      - platform: state
        entity_id: input_boolean.test_door
        to: 'on'
    condition:
      - condition: state
        entity_id: input_boolean.test_door_first
        state: 'off'
    action:
      - service: homeassistant.turn_on
        entity_id: input_boolean.test_door_first
      - service: homeassistant.turn_on
        entity_id: input_boolean.test_door_light

# door opens again
  - alias: 'test_door2'
    trigger:
      - platform: state
        entity_id: input_boolean.test_door
        to: 'on'
    condition:
      - condition: state
        entity_id: input_boolean.test_door_first
        state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id: input_boolean.test_door_first

# door closes 2
  - alias: 'test_door3'
    trigger:
      - platform: state
        entity_id: input_boolean.test_door
        to: 'off'
    condition:
      - condition: state
        entity_id: input_boolean.test_door_first
        state: 'off'
    action:
      - service: homeassistant.turn_off
        entity_id: input_boolean.test_door_first
      - service: homeassistant.turn_off
        entity_id: input_boolean.test_door_light

I’m sure there’s a slicker way of doing it, but it seems to work…

The answer to that is “yes” :slight_smile:
go to Configuration > Helpers > add Helper > Toggle

Wow, thanks for this.

However, when I try to save this automation, I get:

Message malformed: extra keys not allowed @ data['input_boolean']

Yeah, you can’t put the “input_boolean:” section into the automation directly. Add those in Config/Helpers/etc and put the part under “automation:” into the, er, automation.
I haven’t used the GUI to do this stuff before, and I have separate yaml files for all things, so I was able to bung both bits into a file called test.yaml, but you will have to separate them out.

In fact I think you’ll have to input each of the three automations I pasted above into three automations in the GUI, doesn’t like you can put 3 into 1

OK.
Excuse my noobness!

No, don’t worry, I’m learning about the GUI now too. My setup won’t let me enter any automations at all via the GUI, so now I’ve gone down a rabbit hole trying to work out why! Every day’s a school day!

ok, I’ve managed to add an automation via the GUI, and I think I’m right in saying that you need to enter each automation individually. So If you go into “edit as YAML” under the three dots at the top right, you can enter each automation, starting at “alias”, but miss off the “-”, like so:

I’ll try to get my head around this!

Thanks again.

1 Like

I managed to achieve something like you want with the following.

alias: Bathroom - Door Opened
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_door
    from: 'off'
    to: 'on'
condition:
  - condition: state
    entity_id: binary_sensor.bathroom_is_dark
    state:  "on"
  - condition: state
    entity_id: light.bathroom_ceiling_lights
    state: "off"
action:
  - service: scene.turn_on
    data:
      entity_id: >-
        {% if is_state('input_select.house_mode','Sleeping') %}
          scene.bathroom_sleeping
        {% else %}
          scene.bathroom_awake
        {% endif %}

alias: Bathroom - Door Closed
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_door
    from: 'on'
    to: 'off'
condition:
#
# If lights have been on for more than 5 seconds assume the bathroom has been occupied 
# and that the door has been closed after someone has left.
#
  - condition: state
    entity_id: light.bathroom_ceiling_lights
    state:  "on"
    for:
      seconds: 5
action:
  - service: light.turn_off
    data:
      entity_id: light.bathroom_ceiling_lights


Thank you.

What is ‘binary_sensor.bathroom_is_dark’? Is this a light level sensor?

That’s just a template binary sensor derived from a xiaomi aqara light level sensor. It is set to true when the light level is below a certain level and false otherwise.

not that automation cannot be done, but is effectively not practical (you will see). This approach makes use of assumption that everytime you enter or leave toilet the door are being closed. Which in real life is simply not true. If not during common usage then when cleaning.

So if you are doing it as an exercise then ok. If you expect it useful, it probably won’t

I take your point, but I think it will work for me.

I am the person who uses that toilet so I will be well aware of the logic involved. I also have built in a failsafe that will turn off the light if the door is accidently left open.

Sorry to bump this old thread, this is exactly what i want to do but being new to Home Assistant I have no idea where to put this code. I have tried automation.yaml and scene.yaml errors get thrown up everywhere. Please can Glyn or someone explain in plain English what I need to do to get this working.

Thank you

Hi,

My original solution was not complete as it referenced another binary sensor that indicated if the bathroom was in darkness or not. Here is a simplified version. It consists of two automations, one for when the door opens and the other for when it closes.

alias: Bathroom - Door - Opened
mode: restart
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_door
    from: 'off'
    to: 'on'
condition: 
  - condition: state
    entity_id: light.bathroom_ceiling
    state: 'off' 
action:
#
# Turn Light
#
  - service: light.turn_on
    data:
      entity_id: light.bathroom_ceiling
alias: Bathroom - Door - Closed
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_door
    from: 'on'
    to: 'off'
mode: restart
condition:
#
# If lights have been on for more than 5 seconds assume the bathroom has been occupied 
# and that the door has been closed after someone has left so turn lights off.
#
  - condition: state
    entity_id: light.bathroom_ceiling
    state:  "on"
    for:
      seconds: 5
action:
  - service: light.turn_off
    data:
      entity_id: light.bathroom_ceiling

binary_sensor.bathroom_door is the binary sensor for the door/window sensor on the bathroom door and light.bathroom_ceiling is the light I want to control. Both of these will have to be changed to reflect the appropriate entity ids in your home.

Hope this helps,

Glyn

2 Likes

Thanks for this.

Works a treat!

I added this at the end of the Open automation to turn the light off after 1 hour if the door is left open.

  • delay:
    hours: 1
    minutes: 0
    seconds: 0
    milliseconds: 0
  • service: light.turn_off
    target:
    entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off