Switch as Light - from yaml (Change device type - switch_as_x)

Shortly:
I want to do this (change from switch to light) :

But in yaml !

I found on WebArchive from a coupple of years ago (https://web.archive.org/web/20211207144015mp_/https://www.home-assistant.io/integrations/light.switch ) that there was something like:

light:
  - platform: switch
    name: Christmas Tree Lights
    entity_id: switch.christmas_tree_lights

I think this still works but … now … questions:

  1. Is this (old?) yaml code still ok to use?
  2. Any other better (actual/officially supported) solution ? (e.g.: template light ??)
  3. Any solution to set the entity I want? (in the small (old?) yaml variant the entity is set by HA with the same name as the switch, and using the “name” as a FriendlyName only
1 Like

It does still work, as I just tested it. However, I’m not sure how long it will continue to work.

Please vote here:

Using the newer syntax, you can now achieve the same thing as ‘change the device type of a switch’ using a template light in configuration.yaml or a file included from it. (You should use a UUID generator to create a unique ID for each template entity you create of course. And you probably want to set the switch as ‘hidden’ to stop it showing up in automated dashboards.)

template:
  - light:
      - name: My Light
        unique_id: 8185fd21-3bd1-4743-81a2-dc18e41eda8f
        availability: "{{ has_value('switch.my_switch') }}"
        state: "{{ is_state('switch.my_switch', 'on') }}"
        turn_on:
          - action: switch.turn_on
            target:
              entity_id: switch.my_switch
        turn_off:
          - action: switch.turn_off
            target:
              entity_id: switch.my_switch

You can of course do the same in reverse to change an on/off light to a switch.

1 Like