I’m looking for a nicer, more compact way to do the following:
If any door or window is opened, turn on a status LED.
If all doors and windows are closed, turn off the status LED.
I’ve done it in a very ugly fashion, by turning on the LED for each door/window when it is opened (there is one of these automations for each door and for each window):
- alias: LED on Front Door open
initial_state: true
trigger:
platform: state
entity_id: binary_sensor.front_door
to: 'on'
action:
- service: light.turn_on
entity_id: light.status_led
data:
brightness: 125
rgb_color: [80, 72, 244]
And then to turn off the LED, I have one of these for each door/window - with the sensors changing appropriately:
- alias: LED off Front Door closed
initial_state: true
trigger:
platform: state
entity_id: binary_sensor.front_door
to: 'off'
condition:
- condition: state
entity_id: binary_sensor.bathroom_window
state: 'off'
- condition: state
entity_id: binary_sensor.bedroom_window
state: 'off'
- condition: state
entity_id: binary_sensor.kitchen_door
state: 'off'
- condition: state
entity_id: binary_sensor.family_room_door
state: 'off'
action:
- service: light.turn_off
entity_id: light.status_led
This works, but obviously, it’s hideous - I have 3 doors and two windows at the moment, so there are 10 automations to make the status LED work. As soon as I add more windows, there will be more automations, and EVERY automation will have to be edited to deal with the changes. There must be a better way! I tried putting all the doors and windows in a group, and using the group in the turn-off automations instead of all the individual sensors, but that didn’t work. Booleans? Templates? Unfortunately my templating skills are negligible. Any help would be MUCH appreciated!
If you’re willing to learn a little Python programming, I’d recommend looking at AppDaemon. At least for me, it’s much clearer to express what I want with code vs yaml and it’s much easier to do groups, loops, and multiple conditions. And you can instantiate multiple automations with the same code by passing in different arguments (basically templating).
Create a Template Switch called indicator.
When turned on it sets light.status_led to the desired brightness and color.
When turned off it simply turns off light.status_led.
@TD22057 Someday I will learn Python. It’s on my bucket list. I just need more hours in a day…
@123 THANK YOU! This is brilliant - exactly what I wanted, but didn’t know enough to do myself. I did have to change the switch, because it wouldn’t pass config check. I looked at the template switch docs and figured out that I needed to use
It’s working great! SO many less lines of code and SO much easier to maintain! I love this community - I’ve learned so much from people like you that take the time to help others. Many, many thanks! Next time I’ll be that much closer to figuring out how to do it myself
I often try to test my solutions before posting them but this time I didn’t and overlooked to include the service! Good job zeroing in on the oversight and correcting it.
For me, in order of ease of use, I do any complex automations with these tools:
AppDaemon/NodeRed (pick your poison),
custom_component,
python_script,
yaml
That is to say, if it’s really simple, I use YAML. If it’s really complicated, I use AppDaemon. For your particular problem, I would have used a python_script.
Sadly, not everything can be done easily with just yaml/templates. Thankfully, there are other tools to get the job done, but they all require a bit of programming. If you intend to get really deep in Home Assistant, I’d recommending Python/AppDaemon or NodeJS/NodeRed.