Direct hard link between switch toggles

Hi community.

I tried I suppose every solution from this forum but none of them works as I expect. I simply want to hard link 2 toggles in Hass. I have following setup: There are 2 lights in the room and one RF wall switch. I put two lights into one group.

  kitchen_card:
    name: Kitchen Lighting
    entities:
      - light.kitchen_ceiling
      - light.kitchen_worktop

In this way switches can function regardless of group toggle but also with the group toggle I can switch on/off both of them together. The behaviour is exactly what I want.
Recently I added RF wall switch and simply want to control group toggle with it.

light.kitchen_wall_switch

But unfortunately all solutions I found here are suggesting to use service/data templates. In my case it looks as follows:

- alias: Kitchen Light On Wall Switch    
  trigger:
    - platform: state
      entity_id: group.kitchen_card, light.kitchen_wall_switch
  action:
    - service_template: >
        {% if trigger.to_state.state == "on" %}
        light.turn_on
        {% elif trigger.to_state.state == "off" %}
        light.turn_off
        {% endif %}
      data_template: 
        entity_id: >
          {% if trigger.from_state.entity_id == "group.kitchen_card" %}
          light.kitchen_wall_switch
          {% elif trigger.from_state.entity_id == "light.kitchen_wall_switch" %}
          group.kitchen_card
          {% endif %}

But this automation doesn’t work as desired in case when I want to turn on only one of the lights. It changes state of the wall switch which triggers this automation once again setting all lights on. What I want is simply link a group toggle to a wall switch toggle without any workarounds and delays etc.: group.kitchen_card <==> light.kitchen_wall_switch.
I made an illustration for easier understanding what I need:


Maybe someone have already solved this problem?

Thank you for any help in advance.

Then that is all you need to do:

- alias: Kitchen Light On Wall Switch    
  trigger:
    - platform: state
      entity_id: light.kitchen_wall_switch
  action:
    - service_template: >
        {% if trigger.to_state.state == "on" %}
          homeassistant.turn_on
        {% elif trigger.to_state.state == "off" %}
          homeassistant.turn_off
        {% endif %}
      entity_id:  group.kitchen_card

Errrr elif ?
That should be an else - what ‘else’ can it be ? :rofl:

Or

  action: 
    - service_template: homeassistant.turn_{{trigger.to_state.state}}
      entity_id: group.kitchen_card

Thank you Tom for your reply. Not exactly what I want to achieve.

In this case if I turn the lights or at least one light On from UI the status of RF switch doesn’t reflect the status of the group and I need to turn On the wall switch just to set status of the wall switch in order to turn Off the lights.

It looks like very simple use case but to be honest I can’t find the solution for a long time. :slightly_frowning_face:

I’ve read this thread 4 times and I just don’t get it.
You have one switch and two lights but you don’t want to switch them together as Tom suggests ?
Can you draw a truth table of device settings (on/off for L1, L2) and Sw1 then what you change and what you want the outcome to be ?

That was my code he just quoted. But doesn’t matter logically it works the same as just else, right? :wink: Anyways it doesn’t solve the issue unfortunately.

No, that’s bad programming practice and will cause the action to crash if an unexpected condition (not a problem here because there is no elif) but then any subsequent action would not occur. So just try to stay out of bad habits. :man_shrugging:

Edit; ‘if’ there’s an ‘if’ there’s ‘always’ an ‘else’.

Yeah I did not pay attention to what I was copying. It should be else.

Ah I see. Tricky one. Let me think about it,

Thank you Mutt for reply.

I want RF switch to work exactly as Group of lights. I want to switch all lights On/Off together with RF button. Also I want to switch lights On/Off individually from UI and able to turn them Off with RF button.

Use cases:

  • I turn Switch On, L1 and L2 are switched On
  • I turn Switch Off, L1 and L2 are switched Off
  • I turn L1 On from UI, L2 is Off, switch turned On
  • I turn L2 On from UI, L1 is Off, switch turned On
  • I turn L1 and L2 On from UI, switch turned On

I hope this is better explanation. :slightly_smiling_face: Please let me know if it’s still not clear for you.

Two automations should do it:

- alias: Kitchen Light On Wall Switch    
  trigger:
    - platform: state
      entity_id: light.kitchen_wall_switch
  action:
    - service_template: >
        {% if trigger.to_state.state == "on" %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      entity_id:  group.kitchen_card

and:

- alias: Kitchen RF Switch On Group
  trigger:
    - platform: state
      entity_id: group.kitchen_card
  action:
    - service_template: >
        {% if trigger.to_state.state == "on" %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
      entity_id:  light.kitchen_wall_switch

I could do this but I’d need to create an intermediary blocking boolean to stop the changing of the light switch from the trigger of the light to turn on the other light. Tricky as Tom says.
Let’s see what he comes back with.

Edit: I wasn’t quick enough

But won’t the second trigger the first ?

Unfortunately same as I mentioned before but in two separate rules. :slightly_frowning_face:

Exactly, Tom’s solution is the same as I mentioned in the first message. If I turn at least one light On then it triggers second rule Kitchen RF Switch On Group which triggers the first Kitchen Light On Wall Switch because of light.kitchen_wall_switch is On :upside_down_face:

So, create an input boolean switched to on when you switch one of the lights (double trigger, double action)
The input boolean is first
The input boolean ‘on’ triggers timer to switch it off (2 secs ?) third automation
The switch operation has a condition that the boolean must be off

- alias: Kitchen Light On Wall Switch    
  trigger:
    - platform: state
      entity_id: light.kitchen_wall_switch
  action:
    - service_template: >
        {% if trigger.to_state.state == "on" %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      entity_id:  group.kitchen_card

and:

- alias: Kitchen RF Switch On Group
  trigger:
    - platform: state
      entity_id: group.kitchen_card
  action:
    - service: homeassistant.turn_off  # prevent group change automation
      entity_id: automation.kitchen.light.on.wall.switch  
    - service_template: >
        {% if trigger.to_state.state == "on" %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
      entity_id:  light.kitchen_wall_switch
    - service: homeassistant.turn_on # re-enable group change automation
      entity_id: automation.kitchen.light.on.wall.switch  

Ah ! … Cheating ! … Me likey ! :rofl:

Thank you Tom! Works!
But that’s wow! I supposed something more simple for wiring 2 toggles together.

Thanks again.

Thank you guys! Very appreciated!