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:
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
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…
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.
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 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
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.
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.