Make 7 automations into 1

Hello there!

I am looking for a convenient way to reduce 7 automations into one. They are all doing more or less the same, only the room changes.
This is one of the mentioned automations:

alias: Office Light Sync
description: Sync lights in the office
trigger:
  - entity_id:
      - switch.office_passive
      - light.office_passive
    platform: state
    to: 'on'
    from: 'off'
  - platform: state
    entity_id: 'switch.office_passive,light.office_passive'
    to: 'off'
    from: 'on'
condition:
  - condition: template
    value_template: "{{ states('light.office_passive') != states('switch.office_passive') }}"
action:
  - service_template: "homeassistant.turn_{{trigger.to_state.state}}"
    data:
      entity_id: >-
        {% if trigger.entity_id == "light.office_passive" %} switch.office_passive
        {% else %} light.office_passive {% endif %} 
mode: single

So the automation synchronises the wall-switch with my zigbee lights.
I have similar automations for 6 other rooms. Has anyone an idea on how to merge those automations? I aim on reducing the number of automations and the amount of maintenance.
I am thinking about * and variables, but would be interested, if anyone has done something similar and can give a working option.

Thanks in advance!

They are all doing more or less the same, only the room changes.

As far as I know the blueprint just helps me creating several automations. I would like to reduce the number of automations as well as reducing amount of maintenance.
But if blueprints are the only way to solve this I will try them out. Thanks for your suggestion.

With using a single, parametrized blueprint, you will still have 7 automations, but they only define the parameter values and benefit from every improvement you apply to the blueprint.

Personally (despite being a “programmer”) prefer easy to read, YaML only automations over heavily-scripted onces.

True, but can’t you make one automation with all 7 triggers that uses the specific trigger device name to do the rest of the automation? 1 automation, 7 results depending on the trigger. The problem is “parameterizing” the trigger device name so the rest of the automation would have switch.[room]_passive and light.[room]_passive.

I think something like this might work for you. If it does work, I believe it will trigger twice without the condition but I’ll leave that to you to fix.

alias: Sync
description: Sync
trigger:
  - platform: state
    entity_id:
      - switch.room1_passive
      - switch.room2_passive
      - switch.room3_passive
      - switch.room4_passive
      - switch.room5_passive
      - switch.room6_passive
      - light.room1_passive
      - light.room2_passive
      - light.room3_passive
      - light.room4_passive
      - light.room5_passive
      - light.room6_passive
condition:
action:
  - service: "homeassistant.turn_{{trigger.to_state.state}}"
    data:
      entity_id: >-
        {% if trigger.to_state.domain == "light" %}
           switch.{{trigger.entity_id.split('.')[1]}}
        {% else %}
           light.{{trigger.entity_id.split('.')[1]}}
        {% endif %}
mode: single
1 Like

I tested and confirmed the following ‘all-in-one’ automation works per your requirements.

alias: Office Light Sync
description: Sync lights in the office
trigger:
  - platform: state
    entity_id:
      - switch.office_passive
      - light.office_passive
      - switch.kitchen_passive
      - light.kitchen_passive
action:
  - variables:
      light: 'light.{{ trigger.to_state.object_id }}'
      switch: 'switch.{{ trigger.to_state.object_id }}'
  - condition: template
    value_template: '{{ states(light) != states(switch) }}'
  - service: 'homeassistant.turn_{{ trigger.to_state.state }}'
    data:
      entity_id: "{{ switch if trigger.to_state.domain == 'light' else light }}"
mode: single
max_exceeded: silent

All you need to do is modify the trigger’s list of entity_ids.

2 Likes

nice improvement

FWIW, although it may appear to be an improved version of your suggested automation it’s a clean-sheet design.

Now that I’ve looked at your automation, I see it contains trigger.domain which isn’t available in a State Trigger (use trigger.to_state.domain). In addition, the double-quotes wrapping the two statements in the if-else will be passed literally to the entity_id (unnecessary; remove them). Although not critical, service_template was deprecated in favor of service a few versions ago.

2 Likes

Thanks for the input. It works like a charm.
Short, simple, efficient - love this community!
And thank you for comments on my depricated and unnecessary code-parts. I try to improve with every automation.

1 Like