-
-
Save dbrand666/b731672b2a282b9afd51a97e6e5c2b2b to your computer and use it in GitHub Desktop.
blueprint: | |
name: Link On/Off State of Multiple Devices | |
description: "## Link On/Off State of Multiple Devices v1.0.3 | |
Select multiple entities to link their on/off state. If any selected | |
entity is turned on or off, the other selected entities will be sent | |
a matching on or off command. | |
You can select any entity, but only entities supported by the | |
`homeassistant.turn_on` or `homeassistant.turn_off` service calls | |
will work. | |
### Requirements | |
* All selected entities MUST suport `homeassistant.turn_on` and | |
`homeassistant.turn_off` or errors will be logged and the blueprint | |
will not work. | |
* Requires Home Assistant 2022.5.0 or newer. | |
### Credits | |
* [@adchevrier](https://community.home-assistant.io/u/adchevrier) for the | |
[initial blueprint](https://community.home-assistant.io/t/synchronize-the-on-off-state-of-2-entities/259010) | |
* [@hebus](https://community.home-assistant.io/u/hebus) for | |
[this fantastic template](https://community.home-assistant.io/t/synchronize-the-on-off-state-of-2-entities/259010/38)" | |
domain: automation | |
homeassistant: | |
min_version: 2022.5.0 | |
input: | |
linked_entities: | |
name: Entities to link | |
selector: | |
entity: | |
multiple: true | |
mode: restart | |
max_exceeded: silent | |
variables: | |
linked_entities: !input "linked_entities" | |
triggers: | |
- trigger: state | |
entity_id: !input "linked_entities" | |
condition: | |
- condition: template | |
value_template: > | |
{{ | |
trigger.to_state.state != trigger.from_state.state | |
}} | |
- condition: template | |
value_template: > | |
{{ | |
(trigger.to_state.state == "on") or | |
(trigger.to_state.state == "off") | |
}} | |
- 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) | |
}} | |
actions: | |
- action: homeassistant.turn_{{ trigger.to_state.state }} | |
target: | |
entity_id: > | |
{{ | |
expand(linked_entities) | | |
selectattr("entity_id", "!=", trigger.entity_id) | | |
map(attribute="entity_id") | | |
list | |
}} |
Hi, Visual Studio Code (https://community.home-assistant.io/t/home-assistant-community-add-on-visual-studio-code/107863) shows in lines 46, 59:
String does not match the pattern of "LEGACY_SYNTAX^"
And by the way, it's great. I tried to embrace this before manually with automations and this script is a savior.
triggers:
- trigger: state
entity_id:
- !input linked_entities
actions:
- action: homeassistant.turn_{{ trigger.to_state.state }}
target:
entity_id: '{{ expand(linked_entities) | selectattr("entity_id", "!=", trigger.entity_id) | map(attribute="entity_id") | list }}'
One more quick update: wrap lines for readability.
I have been trying painfully to sync the switches. The infinite cycle of on/off was a big problem. Thank you so much for saving me from the big trouble.
triggers: - trigger: state entity_id: - !input linked_entities actions: - action: homeassistant.turn_{{ trigger.to_state.state }} target: entity_id: '{{ expand(linked_entities) | selectattr("entity_id", "!=", trigger.entity_id) | map(attribute="entity_id") | list }}'
Thank you. I made this change and also did some line wrapping for readability.
@dbrand666, could you explain which part prevents the infinite loop?
Hi, I'm trying out this blueprint. It works most of the times. Testing out with two switches. The failure scenario:
- Manually turn on Light 1
- Light 2 turns on
- Within 3 seconds turn off Light 2
- Light 1 stays turns on.
- HA dashboard always reflects the correct state.
NOTE: If I wait longer then the automations would work.
How can I debug this?
I noticed the same behavior with a very similar automation that uses mode: queue
mode.
Edit: corrected typo in step 4.
When you turn off light 2 (step 3) it doesn't turn off at all or it turns off and back on?
You don't mention whether light 1 changes at all after step 1. Does it remain on throughout?
What do you mean by "correct state"? Does it show both as on?
How are you turning the lights on/off? From the light itself? A separate switch that's linked to the light? The dashboard?
What kind of devices are these lights?
As for troubleshooting, take a look at the traces for the automation. They should show you step by step what's changing and why in a pretty flowchart.
@dbrand666, sorry, I made a typo. Step 4 is for "light 1", not "light 2".
The issue is only observed when using physical switch. When controlling the lights from HA dashboard everything looks ok regardless how quick the lights are toggled.
I'm using Inovelli Smart Dimmers.
I tried traces but they are a bit confusing. I will experiment bit more today, but it looks like the "3rd" condition check fails and that's why the other switch goes out of sync.
@dbrand666, sorry, I made a typo. Step 4 is for "light 1", not "light 2". The issue is only observed when using physical switch. When controlling the lights from HA dashboard everything looks ok regardless how quick the lights are toggled. I'm using Inovelli Smart Dimmers.
I tried traces but they are a bit confusing. I will experiment bit more today, but it looks like the "3rd" condition check fails and that's why the other switch goes out of sync.
Same here...
You're my hero :) Thanks