Skip to content

Instantly share code, notes, and snippets.

@pathsny
Created May 7, 2024 19:17
Show Gist options
  • Save pathsny/181203bc61eb9566b1aa36a376ba3551 to your computer and use it in GitHub Desktop.
Save pathsny/181203bc61eb9566b1aa36a376ba3551 to your computer and use it in GitHub Desktop.
home_assistant_sync_switch_and_light
blueprint:
name: Link On/Off State of Light and Switch
description: |
## Link On/Off State of Light and Switch v1.0.1
Synchronize the on/off state between a specific light and a specific switch. When either the light or the switch is turned on or off, the other will match its state.
### Requirements
* The light and switch entities MUST support `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
* Adapted from a blueprint by [@adchevrier](https://community.home-assistant.io/u/adchevrier) and [@hebus](https://community.home-assistant.io/u/hebus).
domain: automation
homeassistant:
min_version: 2022.5.0
input:
light:
name: Light Entity
selector:
entity:
domain: light
switch:
name: Switch Entity
selector:
entity:
domain:
- switch
- light
mode: queued
max_exceeded: silent
variables:
light_entity: !input 'light'
switch_entity: !input 'switch'
trigger:
- platform: state
entity_id: !input 'light'
- platform: state
entity_id: !input 'switch'
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) }}'
action:
- choose:
- conditions: '{{ trigger.entity_id == light_entity }}'
sequence:
- service: homeassistant.turn_{{ trigger.to_state.state }}
target:
entity_id: '{{ switch_entity }}'
- conditions: '{{ trigger.entity_id == switch_entity }}'
sequence:
- service: homeassistant.turn_{{ trigger.to_state.state }}
target:
entity_id: '{{ light_entity }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment