Synchronize the on/off state of 2 entities

I tested this one extensively this morning, and worked very well for me.
I have versions of this with up to 5 switches and fast clicking did not resulted in flickering.

Please let me know if it works for you.

blueprint:
  name: Synchronize 2 States
  description: Bind two switches together to act in unison
  domain: automation
  input:
    switch_1:
      name: Switch 1
      selector:
        entity:
          domain: switch
    switch_2:
      name: Switch 2
      selector:
        entity:
          domain: switch

variables:
  switches: 
    - !input switch_1
    - !input switch_2

trigger:
  - platform: state
    entity_id: 
      - !input switch_1
      - !input switch_2


mode: queued
max_exceeded: silent

condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'

  - condition: template
    value_template: >
      {{ trigger.to_state.context.parent_id is none or 
      (trigger.to_state.context.id         != this.context.id and 
       trigger.to_state.context.parent_id  != this.context.id) }}

action:

  # Set to target value
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    target: 
      entity_id: '{{ expand(switches) | selectattr("entity_id", "!=", trigger.entity_id) | map(attribute="entity_id") | list }}'  

3 Likes