Skip to content

Instantly share code, notes, and snippets.

@dbrand666
Forked from aderusha/link_multiple_devices.yaml
Last active March 20, 2025 11:01
Show Gist options
  • Save dbrand666/b731672b2a282b9afd51a97e6e5c2b2b to your computer and use it in GitHub Desktop.
Save dbrand666/b731672b2a282b9afd51a97e6e5c2b2b to your computer and use it in GitHub Desktop.
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.
@vef445
Copy link

vef445 commented Oct 12, 2024

You're my hero :) Thanks

@Santanachia
Copy link

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.

@Santanachia
Copy link

Santanachia commented Dec 17, 2024

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 }}'

@dbrand666
Copy link
Author

One more quick update: wrap lines for readability.

@martinmts
Copy link

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.

@dbrand666
Copy link
Author

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.

@rubenhak
Copy link

@dbrand666, could you explain which part prevents the infinite loop?

@rubenhak
Copy link

rubenhak commented Mar 14, 2025

@dbrand666

Hi, I'm trying out this blueprint. It works most of the times. Testing out with two switches. The failure scenario:

  1. Manually turn on Light 1
  2. Light 2 turns on
  3. Within 3 seconds turn off Light 2
  4. Light 1 stays turns on.
  5. 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.

@dbrand666
Copy link
Author

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.

@rubenhak
Copy link

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

@Jonsonmon
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment