Sorry for the spam, but I think I see what’s going on.
I enabled all logs and it does:
2020-07-24 16:09:42 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on zigbee2mqtt/hallway_2_switch: b'{"state_top":"OFF","linkquality":72,"state_bottom":"OFF"}'
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.hallway_2_switch_1, old_state=<state switch.hallway_2_switch_1=off; state_top=OFF, linkquality=87, state_bottom=ON, friendly_name=hallway_2_switch_1 @ 2020-07-24T15:57:49.422320+10:00>, new_state=<state switch.hallway_2_switch_1=off; state_top=OFF, linkquality=72, state_bottom=OFF, friendly_name=hallway_2_switch_1 @ 2020-07-24T15:57:49.422320+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.hallway_2_switch_2, old_state=<state switch.hallway_2_switch_2=on; state_top=OFF, linkquality=87, state_bottom=ON, friendly_name=hallway_2_switch_2 @ 2020-07-24T16:07:48.325669+10:00>, new_state=<state switch.hallway_2_switch_2=on; state_top=OFF, linkquality=72, state_bottom=OFF, friendly_name=hallway_2_switch_2 @ 2020-07-24T16:07:48.325669+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.0x00158d0004771720_linkquality, old_state=<state sensor.0x00158d0004771720_linkquality=87; state_top=OFF, linkquality=87, state_bottom=ON, unit_of_measurement=lqi, friendly_name=hallway_2_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:07:48.330753+10:00>, new_state=<state sensor.0x00158d0004771720_linkquality=87; state_top=OFF, linkquality=72, state_bottom=OFF, unit_of_measurement=lqi, friendly_name=hallway_2_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:07:48.330753+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.hallway_2_switch_2, old_state=<state switch.hallway_2_switch_2=on; state_top=OFF, linkquality=72, state_bottom=OFF, friendly_name=hallway_2_switch_2 @ 2020-07-24T16:07:48.325669+10:00>, new_state=<state switch.hallway_2_switch_2=off; state_top=OFF, linkquality=72, state_bottom=OFF, friendly_name=hallway_2_switch_2 @ 2020-07-24T16:09:43.019942+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.0x00158d0004771720_linkquality, old_state=<state sensor.0x00158d0004771720_linkquality=87; state_top=OFF, linkquality=72, state_bottom=OFF, unit_of_measurement=lqi, friendly_name=hallway_2_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:07:48.330753+10:00>, new_state=<state sensor.0x00158d0004771720_linkquality=72; state_top=OFF, linkquality=72, state_bottom=OFF, unit_of_measurement=lqi, friendly_name=hallway_2_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:09:43.024074+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.components.homekit.type_switches] switch.hallway_2_switch_2: Set current state to False
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.hallway_2_way, old_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:07:48.840151+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>, new_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.216504+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Hallway 2-way, entity_id=automation.hallway_2_way>
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Executing Hallway 2-way
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.hallway_2_way, old_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.216504+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>, new_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.219197+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Hallway 2-way, entity_id=automation.hallway_2_way>
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Executing Hallway 2-way
2020-07-24 16:09:43 WARNING (MainThread) [homeassistant.components.automation] Hallway 2-way: Already running
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Hallway 2-way: Running script
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Hallway 2-way: Executing step call service
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=switch, service=turn_on, service_data=entity_id=switch.hallway_1_switch>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service=publish, service_data=topic=zigbee2mqtt/hallway_1_switch/set, qos=0, retain=False, payload=ON>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on zigbee2mqtt/hallway_1_switch/set: ON
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Hallway 2-way: Executing step delay 0:00:01
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on zigbee2mqtt/hallway_1_switch: b'{"state":"ON","linkquality":150}'
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.hallway_1_switch, old_state=<state switch.hallway_1_switch=off; state=OFF, linkquality=150, friendly_name=hallway_1_switch @ 2020-07-24T16:07:48.712530+10:00>, new_state=<state switch.hallway_1_switch=off; state=ON, linkquality=150, friendly_name=hallway_1_switch @ 2020-07-24T16:07:48.712530+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.0x00158d0004622393_linkquality, old_state=<state sensor.0x00158d0004622393_linkquality=150; state=OFF, linkquality=150, unit_of_measurement=lqi, friendly_name=hallway_1_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:00:00.897916+10:00>, new_state=<state sensor.0x00158d0004622393_linkquality=150; state=ON, linkquality=150, unit_of_measurement=lqi, friendly_name=hallway_1_switch_linkquality, icon=mdi:signal @ 2020-07-24T16:00:00.897916+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.hallway_1_switch, old_state=<state switch.hallway_1_switch=off; state=ON, linkquality=150, friendly_name=hallway_1_switch @ 2020-07-24T16:07:48.712530+10:00>, new_state=<state switch.hallway_1_switch=on; state=ON, linkquality=150, friendly_name=hallway_1_switch @ 2020-07-24T16:09:43.553837+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.hallway_2_way, old_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.219197+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>, new_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.569952+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Hallway 2-way, entity_id=automation.hallway_2_way>
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Executing Hallway 2-way
2020-07-24 16:09:43 WARNING (MainThread) [homeassistant.components.automation] Hallway 2-way: Already running
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.hallway_2_way, old_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.569952+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>, new_state=<state automation.hallway_2_way=on; last_triggered=2020-07-24T16:09:43.692102+10:00, mode=single, friendly_name=Hallway 2-way @ 2020-07-24T15:57:10.559755+10:00>>
2020-07-24 16:09:43 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Hallway 2-way, entity_id=automation.hallway_2_way>
2020-07-24 16:09:43 INFO (MainThread) [homeassistant.components.automation] Executing Hallway 2-way
2020-07-24 16:09:43 WARNING (MainThread) [homeassistant.components.automation] Hallway 2-way: Already running
you can see the it process the mqtt event that the switch got turned off once it’s done the 1s delay.
And so the switch will be turned off.
Now what is very bizarre, with the following two automation rules; it works perfectly.
- id: '1595578058005'
alias: Sync Hallway - On
description: ''
trigger:
- entity_id: switch.hallway_2_switch_2, switch.hallway_1_switch
platform: state
to: 'on'
condition: []
action:
- service_template: switch.turn_{{trigger.to_state.state}}
data:
entity_id: switch.hallway_1_switch, switch.hallway_2_switch_2
- delay: '1'
mode: single
- id: '1595580244911'
alias: Sync Hallway - Off
description: ''
trigger:
- entity_id: switch.hallway_2_switch_2, switch.hallway_1_switch
platform: state
to: 'off'
condition: []
action:
- service_template: switch.turn_{{trigger.to_state.state}}
data:
entity_id: switch.hallway_1_switch, switch.hallway_2_switch_2
- delay: '1'
mode: single
yet, the functionally equivalent:
- id: '1595578058005'
alias: Sync Hallway
description: ''
trigger:
- entity_id: switch.hallway_2_switch_2, switch.hallway_1_switch
platform: state
condition: []
action:
- service_template: switch.turn_{{trigger.to_state.state}}
data:
entity_id: switch.hallway_1_switch, switch.hallway_2_switch_2
- delay: '1'
mode: single
will cause the two switch to be in the exact opposite state; is it possible for trigger.to_state.state
to be something else than ‘on’ or ‘off’?
Edit:
I found a solution for a single template which works quite nicely.
- alias: Sync Hallway
description: ''
trigger:
- entity_id: switch.hallway_2_switch_2, switch.hallway_1_switch
platform: state
condition:
condition: template
value_template: "{{ 'on' in trigger.to_state.state or 'off' in trigger.to_state.state }}"
condition: template
value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
- service_template: homeassistant.turn_{{trigger.to_state.state}}
data:
entity_id: switch.hallway_2_switch_2, switch.hallway_1_switch
mode: single
Difference to before is in the condition I make sure the state is either on or off; and that the new state must be different to the old one.