A Tale of Two Switches
Welcome back to my series on the foundational configurations that form the bedrock of my Thinking Home. We have tackled complex issues like network reboots and centralized notifications. Today, we’re focusing on a much smaller, but equally important principle: making your smart home intuitive. A smart home should not only be automated but also operate in a way that feels natural and predictable.
In my home, the hallway lighting is a perfect example of a poor design that Home Assistant gracefully solves. There are two separate lights, each with its own switch at opposite ends of the hall. This means if you turn on the first light, you have to walk back through the dark to turn it off. My solution is a simple automation that synchronizes the two switches. Now, if you change the state of one—manually or from a dashboard—the other instantly matches it, ensuring both lights are always on or off together (spouse approved). Clever, isn’t it?
A Simple and Elegant Sync
The solution is a single, elegant automation that I call the “Two-Way Switch Sync.” The challenge in an automation with two triggers like this, is that when one switch triggers the automation and changes the state of the other, the automation is immediately re-triggered. This can lead to an infinite loop of flickering lights or require very complicated logic to solve.
This automation avoids that trap. It is done very simply by using a clever template that checks the trigger’s context. This allows it to know if a change was made manually, either by a person or by another automation. If the change wasn’t manual, the automation stops, preventing the loop before it ever starts. The result is a clean and simple job: if one switch is changed by a user, the other switch is updated to match its state.
How It Works
The real power of this automation lies in its simplicity and a clever bit of Home Assistant logic to prevent a common pitfall.
- Dual Triggers: The automation is triggered by a state change from either of the two switches. It doesn’t matter which one you flip; the automation will wake up and take a look.
- The “Manual Change” Condition: This is the most critical part. How do we stop the switches from getting into an infinite loop, where one turns on the other, which then tries to turn the first one back on? The answer is context. Every action in Home Assistant has a context. When you physically flip a switch or tap it in the UI, its context.parent_id is none. But when an automation changes a switch, its context is linked to that automation.
This automation’s first condition is {{ trigger.to_state.context.parent_id == none }}. This means it will only proceed if the change was made manually by a person. It completely ignores changes made by itself or any other automation, elegantly preventing a feedback loop. - State Check and Action: Once the automation confirms a manual change, it checks if the other switch already matches the new state. If it doesn’t, it calls the appropriate switch.turn_on or switch.turn_off service to bring it into sync.
How to Implement This in Your Home Assistant
This automation is incredibly easy to adapt for any pair of switches in your home.
You can get the complete, up-to-date code from my GitHub repository:
View the Two-Way Switch Sync Automation on GitHub
To get it running, follow these two simple steps:
1. Configure the Variables: Open the automation’s YAML and find the variables: section. Change the entity IDs to match the two switches you want to synchronize.
variables:
switch_1: "switch.your_first_switch"
switch_2: "switch.your_second_switch"
2. Update the Triggers: In the triggers: section, update the entity_id for each trigger to match the switches you defined in the variables. This is necessary because Home Assistant needs to know which specific entities to monitor to start the automation.
triggers:
- entity_id: switch.your_first_switch
id: switch_1_changed
platform: state
- entity_id: switch.your_second_switch
id: switch_2_changed
platform: state
Sometimes the most impactful automations are the simplest ones. This Two-Way Switch Sync is a perfect example of a small tweak that provides a significant boost in quality of life and makes the home feel more polished and intuitive. It is a foundational piece that solves a common annoyance with grace and efficiency.
Did you find this post helpful? Do you have other small but powerful automations that make your smart home better? Let me know in the comments below or on GitHub!