I want to achieve to play my Sonos speaker when I turn on a switch. Using an automation is working fine, but I don’t understand why it’s not working with a template switch. So my automation which works looks:
Using the developer tools I can see that the state of the new template switch is evaluated correct using the value template. But for some reason my turn_on and turn_off actions are not working.
Look carefully at how you’ve constructed the Template Switch:
When you turn it on it will call the media_play service for media_player.kuche.
The Template Switch will now expect feedback (acknowledgement) via its value_template.
However, it won’t get the acknowledgement it requires because the value_template is monitoring the state of another switch called switch.sonos_kuche_switch.
As a result, the Template Switch will automatically set its state back to off.
A Template Switch’s value_template needs to report the state of whatever you are controlling via its turn_on and turn_off services. What I suggest is you create a template that monitors the state of the media_player.kuche. Try this:
as there is no more connection to my physical switch switch.sonos_kuche_switch anymore. The idea is, that I’m using my physical KNX switch to trigger the Sonos speaker to play. When looking at the developer tools the state of my template switch is shown correct with “on” and “off” dependent on the state of my KNX switch.
If you don’t understand it then the explanation I provided has failed to help you.
Your mission is to create a value_template that reports true when media_player.kuche is playing. Otherwise, it should report false. Your current value_template doesn’t do that.
EDIT
I suggest you review the documentation for Template Switch. The value_template’s purpose is to report the Template Switch’s state immediately after either turn_on or turn_off are executed. The value_template does not behave like an automation’s trigger, serving to execute turn_on and turn_off. If that’s how you think it works, it does not (and you should review the documentation).
The turn_on and turn_off sections of the template switch get run when the template switch itself is turned on or off. They won’t get run just because the value_template changes. This is what an automation is for.
Seems like you want an extension of what you originally had:
Hi all,
first of all I have to thank you for your work and help -that’s really great!!
BUT, I read the docu and it is NOT explaining at all that behavior! It’s the other way around! I also understood it like michelde and it took me about 2 days to get here and understand it! The HA docu is quite often really short or not explaining at all, just giving a a bunch of examples that are also NOT explained.
During my 2 days I also tried to implement the template switch in my packages whitch is also noch working because only the old style of configuration is supported (and this is also not explained in the docu). This were really two annoying days, just because a simple explaining sentence is missing!!
I will try to give that feedback also to the docu itself…
But anyway, as I said in the beginning - thanks for the Help and that great project HA!!
You should know that you just exhumed a topic that has been dead for three years. In other words, it’s highly unlikely the documentation should be “the other way around”. Nevertheless, what exactly do you mean should be “the other way around”.
Yes I know. But I was again so frustrated about that documentation!
The documentation for the template switch is just saying “The template platform creates switches that combines components.” and then it lists some examples WITHOUT explanation! The first example is: “Copy switch”. For me it was clear this example shows how to copy the input of a “source switch” to a “target switch” (and obviously it was the same for the creator of that topic).
BUT this is NOT how the template switch is working. In fact I have still no idea what this copy example is good for!
What is missing in the documentation is simply any kind of an explanation what the template switch is doing!!
I would like to find something like this:
“When the template switch is actively turned on it is executing the actions unter “turn_on”, and vice versa it is executing the actions unter “turn_off”, when it is actively turned off. The “value_template” defines the state of the switch that is returned continously all the time. The value_template is NOT triggering any action it is just providing the state of the “template switch” (e.g. “on”, “off”, “unavailable”)”
Ok, thanks for the answer, you are right, the docs probably explain what the variables do.
But for some reason I am VERY certain, that I am by far not the only one that is confused by that documentation!
Maybe just a second try: What about making at least the point for the value_template that it is ONLY setting the state WITHOUT turning the switch?!
Maybe there are other out there as stupid as me thinking that “setting the state” may mean “turning the switch”.
If I set the state of a switch to “on” the switch is certainly not expected to be “on” afterwards - you are right!! No need to explain all the things that will not happen!
I think it would make some sense to take care of all the not mathematicans that are not aware of all exact definitions all the time when trying to get their house working!
Sorry, but I am really a bit frustrated about the time that it takes again and again to get things working.
But thanks again - I’m happy to get support!
FWIW, I have been helping thousands of users in this community forum since 2018. To my recollection, I have encountered maybe three instances where someone misunderstood the purpose of a Template Switch’s value_template. Even if I have misremembered and underestimated by 200%, that’s still very few cases.
According to the documentation for the Template Switch integration, it is used by nearly 27% of all installations of Home Assistant. According to Analytics, there are currently at least 230000 installations of Home Assistant. Twenty-seven percent of 230000 represents over 60000 users who didn’t report difficulty understanding the purpose of Template Switch’s value_template.