I am using template switches to control some things. In this case I am using them to control things with a Logitech Harmony Hub.
What I want to achieve is switches that when turned on will do their thing and then turn themselves off. The reason is so that visibly in the Home Assistant Interface they appear off, when pressed they show on for a moment, then return to showing off, so it is clear they can be triggered again if desired.
Here is an example of a template switch which when turned on sends the “VolumeUp” command to my soundbar twice and then when turned off sends the “VolumeUp” command again.
I did it this way because the soundbar has many sound levels so three times per activation is good, and I made one of them as the turn off action so that the switch has a turn off action because it must have a turn off action (if anyone knows of a turn on/off action that effectively does nothing, that would be cool to know about).
The switch should also turn itself off, as you can see in the code I set a turn on action to turn off the switch itself but it doesn’t work.
switch:
- platform: template
switches:
tv_volume_up:
friendly_name: TV Volume Up
turn_on:
- service: remote.send_command
data:
entity_id: remote.universal_remote
device: Soundbar
command:
- VolumeUp
- VolumeUp
- service: switch.turn_off
entity_id: switch.tv_volume_up
turn_off:
service: remote.send_command
data:
entity_id: remote.universal_remote
device: Soundbar
command:
- VolumeUp
When I turn on this switch it turns on, sends the commands to increase my volume, but then the switch stays on, it does not turn itself off as I’d hoped to achieve. I thought maybe the commands for the switch can’t be self-referential so I also tried making it call a script which runs the switch.turn_off for that switch but that also fails. The script runs but the switch stays on. If I manually trigger the script, it does turn the switch off, so I’m not sure what the problem is here.
It is possible there is a better way to achieve what I’m trying to do, I am open to suggestions.
I’ve tried a few ways but I keep hitting roadblocks.
*Extra Info: I kept playing around with it, if the switch is told to call a script it does, and it waits for the script to run and complete before it actually reports the state of the script as “on” the the system. So the actions are happening before the switch is officially “on” and the command to turn it off happens, but after that it becomes on.
How can I overcome this and achieve the switch behaviour I’m looking for?