Please help with making a set of automations better - templates?

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).

1 Like

The following solution requires three simple things:

  1. Group
  2. Template Switch
  3. Automation

Group

Create a group containing all doors and windows to be monitored.

group:
  doors_windows:
    entities:
      - binary_sensor.front_door
      - binary_sensor.kitchen_door
      - binary_sensor.family_room_door
      - binary_sensor.bathroom_window
      - binary_sensor.bedroom_window

Template Switch

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.

switch:
  - platform: template
    switches:
      indicator:
        value_template: "{{ is_state('light.status_led', 'on') }}"
        turn_on:
          service: light.turn_on
          data: 
            entity_id: light.status_led
            brightness: 125
            rgb_color: [80, 72, 244]
        turn_off:
          service: light.turn_off
          entity_id: light.status_led

Automation

Create an automation that is triggered by group.doors_windows.
It turns switch.indicator on or off depending on the group’s state.

- alias: 'LED indicator'
  trigger:
    platform: state
    entity_id: group.doors_windows
  action:
    service_template: switch.turn_{{trigger.to_state.state | lower }}
    entity_id: switch.indicator

EDIT
Corrected Template Switch. Forgot to include service: light.turn_on

2 Likes

@TD22057 Someday I will learn Python. It’s on my bucket list. I just need more hours in a day… :grin:

@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

- platform: template
  switches:
    indicator:
      value_template: "{{ is_state('light.status_led', 'on') }}"
      turn_on:
        service: light.turn_on
        data:
          entity_id: light.status_led
          brightness: 125
          rgb_color: [80, 72, 244]
      turn_off:
        service: light.turn_off
        data:
          entity_id: light.status_led

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 :slight_smile:

2 Likes

I often try to test my solutions before posting them but this time I didn’t and overlooked to include the service! :man_facepalming: Good job zeroing in on the oversight and correcting it.

Glad to hear it’s working well for you.

1 Like

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.

1 Like