Best way to connect switch with light

I currently have a switch (integration: Tuya) and 2 lamps connected in a group (integration: ZHA). Plus simple automation:

trigger:
  - platform: device
    type: changed_states
    device_id: a72ccdeec00700acbf1ccd34d271daf6
    entity_id: d62f8856751f080ecf8b897205ffdfac
    domain: switch
  - platform: state
    entity_id:
      - light.salon
condition:
  - condition: template
    value_template: "{{ states('light.salon') != states('switch.wlacznik_salon_salon') }}"
action:
  - if:
      - condition: template
        value_template: "{{ trigger.to_state.state == 'on' }}"
    then:
      - service: light.turn_on
        metadata: {}
        data:
          kelvin: 2500
          brightness_pct: 50
        target:
          entity_id:
            - light.salon
      - type: turn_on
        device_id: a72ccdeec00700acbf1ccd34d271daf6
        entity_id: d62f8856751f080ecf8b897205ffdfac
        domain: switch
    else:
      - service: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.salon
      - type: turn_off
        device_id: a72ccdeec00700acbf1ccd34d271daf6
        entity_id: d62f8856751f080ecf8b897205ffdfac
        domain: switch
mode: single

Is there a simpler way to BIND one with the other?
The current solution has the disadvantage that if I use the switch, the automation turns on the light, and this triggers the automation again.
On the other hand, if I remove light.salon from the triggers, the light control will not switch switch to proper state.

You can probably use a group.
Disable the automation and try create a group with the two entities.

Even if you create a group with 2 lights, turning on one will not turn on the other, that is, using the switch will not turn on the light even if it could be grouped together.

Hi Marcin Bielecki,
Mix-using States and devices and templates is all a very bad idea. Devices can’t use templates, and using a state trigger that turns on a device trying to use that triggered entity, well, let’s just say this is not the easiest way to do this.
I HIGHLY suggest sticking to just state triggers if you are using templates, or use just devices and don’t use templates at all.

Why and how to avoid device_ids in automations and scripts.

2 Likes

you could use this blueprint:

and if you want the default on value for the light, instead of hard coding it here, you may be better to set the default light profile value:

1 Like

works like a charm

1 Like