Can you pass substitution variable in a Template like Switch

hi in esphome you can use subsitutions and variables… as i wanna be able to change 1 line in switch template and it just used through out… instead of needing to change swtich name 3 4 times
so my example is

  switch:
    - platform: template
      switches:
        dehumidifier_1:
          friendly_name: "Dehumidifier 1"
          value_template: "{{ is_state('switch.mini_plug_1', 'on') }}"
          turn_on:
            action: switch.turn_on
            target:
              entity_id: switch.mini_plug_1
          turn_off:
            action: switch.turn_off
            target:
              entity_id: switch.mini_plug_1
          icon_template: >-
            {% if is_state('switch.dehumidifier_1', 'on') %}
              mdi:air-humidifier
            {% else %}
              mdi:air-humidifier-off
            {% endif %}

and what i wanna do is

  switch:
    - platform: template
      switches:
        dehumidifier_1:
          friendly_name: "Dehumidifier 1"
          switchname: "switch.mini_plug_1"
          value_template: "{{ is_state('{{switchname}}', 'on') }}"
          turn_on:
            action: switch.turn_on
            target:
              entity_id: { { switchname } }
          turn_off:
            action: switch.turn_off
            target:
              entity_id: { { switchname } }
          icon_template: >-
            {% if is_state('switch.dehumidifier_1', 'on') %}
              mdi:air-humidifier
            {% else %}
              mdi:air-humidifier-off
            {% endif %}

but of course it doesnt work that way… but can i do that ? does the template card let you do stuff like this where you can sub 1 line and it gets referenced so your not having to change multiple spots…

also trying to do color icon but it doesnt like it… i tried this

          icon_color: >-
            {% if is_state('switch.dehumidifier_1'), 'on' %} 
              yellow
            {% elif is_state('switch.dehumidifier_1'), 'off' %}
              green
            {% else %}
              blue
            {% endif %}

Although you can use a Jinja template for the entity_id, it isn’t possible to define a variable using the YAML for a template switch, so it is not possible to do a cookie-cutter template switch the way your code suggests. (You also can’t use { { and } } as Jinja print delimiters; there can’t be spaces between the brackets–it needs to be {{ and }}.)

Blueprints support template entities, but I think that technically the template switch is in the switch domain, not template, so you probably can’t use a blueprint for this, although you might look into it further.

(Also, I don’t think icon_color is something you can define in the YAML, Jinja or not…; at least it’s not listed as a valid option in the docs).

ah ok ya so cant do that
i even figured maybe how you can use like Basic or Python
where
switchname: “switch.plug_1”

$switchname or switchname$

to be the place holder for it… so not able to do it… and since the changing of the template icon can be done… i figured i can do the same with the color

i dunno is it worth asking for a feature request if it hasnt been asked for place holder…

so for now just stick to if i have to change it change them all at once… i figured be nice to just fill in 1 spot…

always learning something new everyday

You can use YAML anchors. See here for an example: Way to create multiple sensors per device with template? - #7 by 123

@tom_l cant seem to understand it. from that link with the people having issues and it seems for just mqtt but i trying to add to add like 1 variable to to equal those few spots where i have {{switchname}}

but ill keep re reading it

wasnt able to figure out that anchor stuff for either color or the switch name place holder i was asking for

when i googled about anchor video uses for scene but i couldnt figure out how to work that for what i doing although video was 4 yrs old…

too confusing for me at the moment

No, it is an example of YAML anchors that has been applied to mqtt light configuration.

You can use YAML anchors with any YAML. Think of it like supplying a template then for each thing you want to define all you have to do is tell the template which bits are different.

Here’s a more generic explanation: Misc tricks · thomasloven/hass-config Wiki · GitHub

@tom_l
so i guess i did it wrong i followed the first example but of course i did it wrong in the wrong order… i also wanted to do 3 different icon color changes in the template but it doesnt let me i guess you can only do it in the card? as i figured u could do in the template… since you can change the icon in the template but you cant do it if its unavaliable

but here is what i did with anchor… now im sure its totaly wrong but thats how i figured it worked?

    - platform: template
      switches:
        dehumidifier_test:
          switchname: &anchor "switch.mini_plug2"
          friendly_name: "Dehumidifier test"
          value_template: "{{ is_state('switchname: *anchor', 'on') }}"
          turn_on:
            action: switch.turn_on
            target:
              entity_id: switchname: *anchor
          turn_off:
            action: switch.turn_off
            target:
              entity_id: switchname: *anchor
          icon_template: >-
            {% if is_state('switchname: *anchor', 'on') %}
              mdi:air-humidifier
            {% elif is_state('switchname: *anchor', 'off') %}
                mdi:information-off-outline
            {% else %}
              mdi:air-humidifier-off
            {% endif %}

as im a visual learner is there a good video that explains how anchor works step by step… as i trying to understand the second with key: << etc but i not getting it as i try to learn something new everyday just struggling learning that

@tom_l i dont think these anchors work for switchs … probably just like colors… as i been reading people use it with colors or sensors but not a switch like i trying to do… probably cant be done as i even tried

    - platform: template
      switches:
        dehumidifier_4:
          &my_switch_template:"switch.mini_plug_4"
          friendly_name: "Dehumidifier 4"
          value_template: "{{ is_state('<<: *my_switch_template', 'on') }}"
          turn_on:
            action: switch.turn_on
            target:
              entity_id: <<: *my_switch_template
          turn_off:
            action: switch.turn_off
            target:
              entity_id: switch.<<: *my_switch_template
          icon_template: >-
            {% if is_state('switch.dehumidifier_4', 'on') %}
              mdi:air-humidifier
            {% else %}
              mdi:air-humidifier-off
            {% endif %}

so i going to research more about this anchor thing and see if it works for other things i might be able to do… for now i just have to specficy the switch name multiple times instead of 1 reference

unless you have to specify in different sub headings but guess ill keep trying to learn this anchor stuff

You can not use anchors inside jinja templates. You need to anchor the entire value in the key: value pair.