Noob question. Where does one put this code? Thanks.
This is an automation rule, so you will need to enter it with the other automations, depending on how you configuration.yaml is set, it could be in a dedicated automation.yaml, a subfolfer etc.
Hi I am new to HA, can you tell me where you put this code? In Automations.yaml or Configuration.yaml? How do you get HA to assign an “id” for this automation? thanks,
Thank you for this.
Some observations: if in put this directly in automation.yaml file it’s working OK.
But if I try to edit it later in UI (change alias name for example) it stops working. First switch powers on second one but not vice versa.
UI somehow rewrites this automation.
I got same results using UI and set up this automation from scratch.
Any ideas?
I don’t think you can use anchors with the UI editor.
In case enyone is still looking for this. I am using this blueprint from johanschelin
Works really well.
Sync 2 switches:
blueprint:
name: Synchronize 2 States
description: Bind two switches together to act in unison
domain: automation
input:
switch_1:
name: Switch 1
selector:
entity:
domain: switch
switch_2:
name: Switch 2
selector:
entity:
domain: switch
variables:
switch_1: !input switch_1
switch_2: !input switch_2
trigger:
- platform: state
entity_id:
- !input switch_1
- !input switch_2
mode: single
condition:
'{{ trigger.to_state.state != trigger.from_state.state }}'
action:
# Set to target value
- choose:
- conditions:
- '{{ trigger.entity_id != switch_1 }}'
- '{{ trigger.to_state.state != states(switch_1) }}'
sequence:
- service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: !input switch_1
- choose:
- conditions:
- '{{ trigger.entity_id != switch_2 }}'
- '{{ trigger.to_state.state != states(switch_2) }}'
sequence:
- service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: !input switch_2
Sync 3 switches:
blueprint:
name: Synchronize 3 States
description: Bind Three switches together to act in unison
domain: automation
input:
switch_1:
name: Switch 1
selector:
entity:
domain: switch
switch_2:
name: Switch 2
selector:
entity:
domain: switch
switch_3:
name: Switch 3
selector:
entity:
domain: switch
variables:
switch_1: !input switch_1
switch_2: !input switch_2
switch_3: !input switch_3
trigger:
- platform: state
entity_id:
- !input switch_1
- !input switch_2
- !input switch_3
mode: single
condition:
'{{ trigger.to_state.state != trigger.from_state.state }}'
action:
# Set to target value
- choose:
- conditions:
- '{{ trigger.entity_id != switch_1 }}'
- '{{ trigger.to_state.state != states(switch_1) }}'
sequence:
- service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: !input switch_1
- choose:
- conditions:
- '{{ trigger.entity_id != switch_2 }}'
- '{{ trigger.to_state.state != states(switch_2) }}'
sequence:
- service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: !input switch_2
- choose:
- conditions:
- '{{ trigger.entity_id != switch_3 }}'
- '{{ trigger.to_state.state != states(switch_3) }}'
sequence:
- service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: !input switch_3
This works great, but how can I apply brightness to the template? I know I’m close, but it’s still not working - here’s what I’ve got:
- id: mirror_kitchen_lights
alias: Mirror Kitchen Lights
trigger:
- entity_id: &entities
- light.kitchen_fridge
- light.kitchen_sink
platform: state
to:
- 'on'
- 'off'
action:
- service_template: homeassistant.turn_{{trigger.to_state.state}}
data:
entity_id: *entities
brightness: "{{ trigger.to_state.attributes.brightness }}"
mode: single
This will only work with lights, so you need to change the service to light.turn_{{…
Also, you should update this and remove the deprecated yaml.
- id: mirror_kitchen_lights
alias: Mirror Kitchen Lights
trigger:
- entity_id: &entities
- light.kitchen_fridge
- light.kitchen_sink
platform: state
to:
- 'on'
- 'off'
action:
- service: light.turn_{{trigger.to_state.state}}
target:
entity_id: *entities
data:
brightness: "{{ trigger.to_state.attributes.brightness }}"
mode: single
Got one set of lights working but not the other and I can’t figure out why.
I have two different sets of lights in my kitchen, both sets have paddle switches. I also have two different sets of lights in my bedroom, and both of these switches only have one button to toggle on and off. All of these switches are dimmable.
The kitchen lights with the paddle switches work flawlessly, but the automation for the single button toggle switches in the bedroom only work from the Home Assistant UI - not from the switches on the walls. Why would these need a different different coding?
- id: mirror_kitchen_lights
alias: Mirror Kitchen Lights
trigger:
- entity_id:
- light.kitchen_fridge
- light.kitchen_sink
platform: state
action:
- delay: 2
- data_template:
brightness: "{{ state_attr(trigger.entity_id, 'brightness') | int }}"
entity_id: light.group_kitchen_lights
service: homeassistant.turn_on
mode: single
################################################################################
- id: mirror_master_bedroom_lights
alias: Mirror Master Bedroom Lights
trigger:
- entity_id:
- light.master_bedroom_lights
- light.master_hall_light
platform: state
action:
- delay: 2
- data_template:
brightness: "{{ state_attr(trigger.entity_id, 'brightness') | int }}"
entity_id: light.group_master_bedroom_lights
service: homeassistant.turn_on
mode: single
Why would the kitchen lights automation work great while the bedroom automation only works from the developer UI? The switches may be different, but the operation is virtually the same.
I would love to know if with this automation template would be possible to link to a Wemo Dimmer Switch. I have a three pole setup on two switches and want to replace them with dimmer switches that will update based on the automation.
I am not sure this will work.
This works without (& *) anchors
makes it easier to have multiple sync automations in the same file
alias: Sync Switches
description: ""
trigger:
- platform: state
entity_id:
- switch.device_1
- switch.device_2
to:
- "on"
- "off"
condition: []
action:
- service_template: homeassistant.turn_{{trigger.to_state.state}}
data:
entity_id:
- switch.device_1
- switch.device_2
mode: single
It is a long thread and I didn’t read it all, but why don’t you just use helper and create light group, put all switches in it and volla.
You can use an entity to turn lights on/off or automation.
because you cant group a switch and an input_boolean like i do in my automation.
tying a virtual switch to a real switch. for reasons
trigger:
- platform: state
entity_id:
- input_boolean.movie_mode
- switch.virtual_switch
to:
- "on"
- "off"
Occasionally if a switch is turned on/off too fast this gets in a loop and turns the lights into a disco mode where they are chasing each other.
Is there a way to put a delay or test condition in there to stop this?
I’m using this in may automation for stairs light. I have 3 switches, one main and two dumb switches to turn on light. I use it only for manual mode. In automation I don’t have problem with loop
trigger:
- platform: device
type: turned_on
device_id: 042f0d9d2f9ec22c99781c6315889c31
entity_id: light.switch_1_5
domain: light
for:
hours: 0
minutes: 0
seconds: 1
Hi Taras.
I need the same solution for two sonoff stair lights. One at the bottom of the stairs the other at the top. If I used the solution you kindly provided, would I just swap the entity_id’s for the sonoff one’s in my home assistant? Also where would I add the code to groups or automations YAML
That post of mine is over four years old. There have been dozens of new versions of Home Assistant released since then providing many new features, including Blueprints.
I suggest you use a ready-made blueprint for this application. Here are two examples but there are others in the Blueprint Exchange section of the community forum.
Hi Taras,
Thanks for the info. Im new to HA so still finding my feet. As you say many post seem to be quite old. I will look through the blueprints you suggested first.
Thanks again.
5 minutes and its done.
Thanks