I would like to provide blueprints keeping switches in sync. The switches to sync can be selected on the UI.
There are templates for 2, 3 and 4 switches. Place them to a subfolder under config/homeassistant/blueprints/automation and after calling the service automation.reload
on the UI the templates are selectable.
sync_2switch.yaml
blueprint:
name: Sync two switches
description: Turn on a switch when other one is turend on and vice versa
domain: automation
input:
switch_entity1:
name: Switch 1
selector:
entity:
domain: switch
switch_entity2:
name: Switch 2
selector:
entity:
domain: switch
variables:
switch_entity1: !input switch_entity1
switch_entity2: !input switch_entity2
trigger:
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
from: 'on'
to: 'off'
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
from: 'off'
to: 'on'
condition: "{{ trigger.to_state.context.parent_id == none }}"
action:
- wait_template: "{{ (as_timestamp(now()) - as_timestamp(this.attributes.last_triggered | default(0)) | int > 3) }}"
timeout: 3
- service_template: >
{% if trigger.to_state.state == 'on' %}
switch.turn_on
{% elif trigger.to_state.state == 'off' %}
switch.turn_off
{% endif %}
data_template:
entity_id: >
{% if trigger.entity_id == switch_entity1 %}
{{ switch_entity2 }}
{% elif trigger.entity_id == switch_entity2 %}
{{ switch_entity1 }}
{% endif %}
sync_3switch.yaml
blueprint:
name: Sync three switches
description: Turn on a switch when other one is turend on and vice versa
domain: automation
input:
switch_entity1:
name: Switch 1
selector:
entity:
domain: switch
switch_entity2:
name: Switch 2
selector:
entity:
domain: switch
switch_entity3:
name: Switch 3
selector:
entity:
domain: switch
variables:
switch_entity1: !input switch_entity1
switch_entity2: !input switch_entity2
switch_entity3: !input switch_entity3
switch_entity12:
- !input switch_entity1
- !input switch_entity2
switch_entity13:
- !input switch_entity1
- !input switch_entity3
switch_entity23:
- !input switch_entity2
- !input switch_entity3
trigger:
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity3
from: 'on'
to: 'off'
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity3
from: 'off'
to: 'on'
condition: "{{ trigger.to_state.context.parent_id == none }}"
action:
- wait_template: "{{ (as_timestamp(now()) - as_timestamp(this.attributes.last_triggered | default(0)) | int > 3) }}"
timeout: 3
- service_template: >
{% if trigger.to_state.state == 'on' %}
switch.turn_on
{% elif trigger.to_state.state == 'off' %}
switch.turn_off
{% endif %}
data_template:
entity_id: >
{% if trigger.entity_id == switch_entity1 %}
{{ switch_entity23 }}
{% elif trigger.entity_id == switch_entity2 %}
{{ switch_entity13 }}
{% elif trigger.entity_id == switch_entity3 %}
{{ switch_entity12 }}
{% endif %}
sync_4switch.yaml
blueprint:
name: Sync four switches
description: Turn on a switch when other one is turend on and vice versa
domain: automation
input:
switch_entity1:
name: Switch 1
selector:
entity:
domain: switch
switch_entity2:
name: Switch 2
selector:
entity:
domain: switch
switch_entity3:
name: Switch 3
selector:
entity:
domain: switch
switch_entity4:
name: Switch 4
selector:
entity:
domain: switch
variables:
switch_entity1: !input switch_entity1
switch_entity2: !input switch_entity2
switch_entity3: !input switch_entity3
switch_entity4: !input switch_entity4
switch_entity123:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity3
switch_entity134:
- !input switch_entity1
- !input switch_entity3
- !input switch_entity4
switch_entity234:
- !input switch_entity2
- !input switch_entity3
- !input switch_entity4
switch_entity124:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity4
trigger:
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity3
- !input switch_entity4
from: 'on'
to: 'off'
- platform: state
entity_id:
- !input switch_entity1
- !input switch_entity2
- !input switch_entity3
- !input switch_entity4
from: 'off'
to: 'on'
condition: "{{ trigger.to_state.context.parent_id == none }}"
action:
- wait_template: "{{ (as_timestamp(now()) - as_timestamp(this.attributes.last_triggered | default(0)) | int > 3) }}"
timeout: 3
- service_template: >
{% if trigger.to_state.state == 'on' %}
switch.turn_on
{% elif trigger.to_state.state == 'off' %}
switch.turn_off
{% endif %}
data_template:
entity_id: >
{% if trigger.entity_id == switch_entity1 %}
{{ switch_entity234 }}
{% elif trigger.entity_id == switch_entity2 %}
{{ switch_entity134 }}
{% elif trigger.entity_id == switch_entity3 %}
{{ switch_entity124 }}
{% elif trigger.entity_id == switch_entity4 %}
{{ switch_entity123 }}
{% endif %}
EDIT [9th of Feb 2022]: Updated/Debounced blueprints to fix race condition when turning on and off within a very short time