How to fully synchronize status: smart switch and the smart light it controls

I have a smart light and a smart switch. The switch controls the switching of the smart light using the “lingdong mode/凌动模式”

I don’t know the exact English term for “lingdong mode,” but it refers to a mode where the switch doesn’t directly control the circuit’s on/off state. it sends commands to the smart light through a double-click action to turn it on or off.

The switch can control the light, but the light cannot control the switch. This creates a problem where, in Home Assistant, the light may be on when the switch is off, or the light may be off when the switch is on.I would like to see both devices’ states fully synchronized in Home Assistant.

I have some ideas and tried hard, but they all failed:

  1. Creating automation will lead to an endless loop
  2. Find the light configuration and modify its turn_on and turn_off to smart switches.
  3. Create a custom entity, call the light dimming and switch
  4. Create a light group and disable the turn_on and turn_off of light

You may search for template light which can provide a solution to your scenario.

To leave a reference for future users, I used a template to create a lamp.

Its on/off function is controlled by a switch, and its switch state, color temperature, and brightness are synchronized with the lamp light. This lamp will replace two entities. In theory, this approach can replace any combination of smart light and smart switch.

However, there are still some flaws. When the lamp is turned off, adjusting certain parameters will still trigger the lamp to turn on. In this case, the state of the lamp and the switch will still be opposite.

Here is my code:

light:
  - platform: template
    lights:
      temp_light:
        unique_id: temp_light
        friendly_name: "temp_laight"
        value_template: "{{ is_state('light.Controlled Light', 'on') }}"
        level_template: "{{ state_attr('light.Controlled Light', 'brightness') }}"
        temperature_template: "{{ state_attr('light.Controlled Light', 'color_temp') }}"
        turn_on:
          service: switch.turn_off
          entity_id: switch.Controlled Switch
        turn_off:
          service: switch.turn_off
          entity_id: switch.Controlled Switch
        set_level:
          service: light.turn_on
          target:
            entity_id: light.Controlled Light
          data:
            brightness: "{{ brightness }}"
        set_temperature:
          service: light.turn_on
          target:
            entity_id: light.Controlled Light
          data:
            color_temp: "{{ color_temp }}"