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:
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?
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.
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.
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.
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.
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.
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?