Help understanding template switches

I’m trying to understand template switches and am trying to get an example to work.

Here is my simulation example::

You have a door that’s closed (off).
You have a ceiling light that’s off.
template switch:
Its value_template reports the status of the door.
Its turn_on and turn_off portions control a ceiling light switch.

Here is my non-working code to play with the simulation:

switch:
  - platform: template
    switches:
      ceiling_light_template:
        value_template: "{{ is_state('input_text.door_status', 'open') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.ceiling_light_on
        turn_off:
          service: script.turn_on
          entity_id: script.ceiling_light_off

input_text:
  door_status:
    initial: closed
 
  ceiling_light:
    initial: initial

script:
  'ceiling_light_on':
    sequence:
    - data:
        entity_id: input_text.ceiling_light
        value: is on
      service: input_text.set_value
  'ceiling_light_off':
    sequence:
    - data:
        entity_id: input_text.ceiling_light
        value: is off
      service: input_text.set_value

I know I can simplify without the scripts; I did that just to make sure the code to change text worked.
If anyone has a free moment, could you adjust the code to get it to work?

not sure you indeed understand template switches.
The idea is generally to have a switch that will issue on and off commands and use a template/sensor to retrieve the switch’s state.
It seems that you’re trying to turn a light on when your door is open and turn the light off when the door closes right? If so you can have a much simpler setup by using 1 automation:

- alias: Control Ceiling Light with Door
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.door
  action:
  - service_template: >
    {% if states('binary_sensor.door') == 'on' %}
      switch.turn_on
    {% else %}
      switch.turn_off
    {% endif %}
  entity_id: light.ceiling_light

Check that your door sensor shows ‘on’ when it’s open. Otherwise amend the automation above to reflect this

Actually, this process began when I ‘discovered’ template switches and tried to convert a functioning automation. Someone else on the forum tried to explain to me how a template switch works but I just didn’t get it. This was the example he used, but didn’t give me a working sample of how his example would be coded.

I appreciate the automation example, I learned something more from that.

But back to the template,
If I understand your description, the door doesn’t turn on the template switch but rather I must turn on the template switch to activate the ‘turn on’ and ‘turn off’ values; and the displayed state of the template switch is a mirror of its ‘value template’.
But that would mean his example was poorly written; the "value_template_ should be the state of the light switch and the door should have nothing to do with it. Am I getting closer?

You should probably read through this thread to understand how much effort has already been expended in explaining template switches.

False. That’s your own code you posted here.

I did my level best to explain to you that you have a preconceived idea of how a template switch works and it’s incorrect. I then explained how it works. Nevertheless, my explanation didn’t produce the desired outcome because here we are again.

Good luck.

You only use a template switch when you want to take a device that’s not a switch and turn it into a switch. They have a very niche use case. What you want is an automation.

Here’s an example switch template:

        xbox_one:
          value_template: "{{ is_state_attr('remote.living_room', 'current_activity', 'Xbox One') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.living_room
              activity: 'Xbox One'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.living_room
              activity: 'PowerOff'

It takes a harmony activity (which is normally turned on through a script) and turns it into a switch. So when I toggle the on button in the interface, it turns on the activity. When I turn it off it powers off the activity. The state of the device in the interface is driven by the value template. In this case, the switch will only be on when the current harmony activity is set to ‘Xbox One’. You then end up with a switch in the states page that you can place anywhere in your interface. It will behave like a hardware switch and look like a hardware switch. Hence the name “template switch”. It creates a templated switch.

image

3 Likes

My bathroom has a hue bulb and a hue motion sensor in it. I use normal hue rules (independent of homeassistant) that when there has been no motion for a few minutes the light switches off.

This is no use for people in the bath as they would find themselves sat in the dark.

I made a ‘bathtime mode’ template switch.

Turn the switch on, and it switches the lights on full in the bathroom and deactivates the motion sensor. Turn it off and it reactivates the sensor and switches the light off. The value template that defines whether the switch is reported on or off is the ‘opposite’ of whether the motion sensor is on/off.

switch:
  - platform: template
    switches:
      bathtime_mode:
        icon_template: mdi:hot-tub
        value_template: "{{ is_state('switch.bathroom_motion_status' , 'off') }}"
        turn_on:
          - service: homeassistant.turn_off
            entity_id: switch.bathroom_motion_status
          - service: homeassistant.turn_on
            entity_id: light.bathroom
            data:
              brightness_pct: 100
        turn_off:
          - service: homeassistant.turn_on
            entity_id: switch.bathroom_motion_status
          - service: homeassistant.turn_off
            entity_id: light.bathroom

Hope that helps.

1 Like

FWIW, in the previous thread, Sid started with perfectly good automations then tried to replace them with a template switch.

Based on the previous thread, Sid’s understanding is that if switch.bathroom_motion_status changes to on it will turn on the template switch. I explained that value_template is for reporting the template switch’s status, it does not control it. It’s this persistent misconception that is leading him astray.

There are only so many ways to explain the value_template simple purpose and I think I covered them all. It’s not a difficult concept. Nevertheless, perhaps you gentleman can succeed where I failed.

Petro and mf_social,
between the 2 of you, I now understand the purpose and function of the template switch. I hope that HA will incorporate your explanations into the “// Template Switch” help file. It would have saved myself and 123 much grief. I could not see the forest from the trees. Sooo simple…
When you said they are a “very niche use case”, that explained why there were so very few examples in the github. (Sorry mf_ social, I selected Petro as solved because he was first and I don’t think I can select both.)

123, thank you for your attempts. Your example with the open door and light is what threw me off. Now that I understand, I see what you were trying to show me. I just needed a fresh approach.

2 Likes

It caused me no grief only surprise that you persisted to misunderstand Template Switches despite it being “Sooo simple”.

When you said they are a “very niche use case”, that explained why there were so very few examples in the github.

Github? Why not look for examples right here in the community forum? There’s no shortage here:
https://community.home-assistant.io/search?q=template%20switch

Now that I understand, I see what you were trying to show me

Call me cynical but I continue to doubt that. I would welcome being proved wrong. Post the code that you’re now using to solve the original problem.

I’m sticking with the automatons until I have a real need for template switches. Here’s the code I used to demo your ‘door/ceiling fan’ relationship:

switch:
  - platform: template
    switches:
      ceiling_light_template:
        value_template: "{{ is_state('input_text.ceiling_light', 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.ceiling_light_on
        turn_off:
          service: script.turn_on
          entity_id: script.ceiling_light_off

input_text:
  door_status:
    initial: closed
 
  ceiling_light:
    initial: initial

script:
  'ceiling_light_on':
    sequence:
    - data:
        entity_id: input_text.ceiling_light
        value: 'on'
      service: input_text.set_value
  'ceiling_light_off':
    sequence:
    - data:
        entity_id: input_text.ceiling_light
        value: 'off'
      service: input_text.set_value

Good choice.

If I want to use a template switch, where do I put this code to make it appear on a view? Do I have to add something to the view and replace its text with this code, or do I just place it in my configuration.yaml? Once there, how does HA know which view the switch belongs on?