Can I make a template Switch turn itself off after being turned on?

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?

The trick is to use a value template on the template switch . This can set the switch without triggering the payload. So you get switch to turn itself off by setting the switch to turn off when it is on !

platform: template
    switches: 
      test:
        value_template: > 
          {% if is_state('switch.test','on') %}
            false
          {% endif %}
        turn_on:
          
        turn_off:

This will give you a momentary switch , so it will never trigger off because the value template will switch it off when it’s on and never trigger.

Have a look at the Momentary Switch Component in HACS which may help simplify things for you. I currently use this for this like a heating boost function which turns on my heating for a set time when clicked if the heating timer is off.

Hi, I’m a Home Assistant newbie, and installed the momentary switch you recommended.

However, when writing the code in configuration.yaml as described on the website i get error message when I come to the line name:

bad indentation of a mapping entry at line 30, column 5:
name:
^

This is how I’ve written the code:

momentary:

switch:
- platform: momentary
name:

Then the error indicator shows.

Did you write the code as described and made it work?

Best regards,
Sandeidsbuen

Looks like your indentation is off, see mine below:

switch:
  - platform: momentary
    name: Z1 Boost
    mode: on
    toggle_for: 5400
    cancellable: True
1 Like

Thanks, now I got it.
Just need to find out how to link it to my PLC Memory bit address :slight_smile:

So I can fill in whatever actions I need to trigger in the “turn_on:” section and they will trigger and the switch will automatically flip back to off but actions in the “turn_off:” section won’t be triggered.
Have I understood that correctly?

Thanks for taking the time to write a great answer.

Yes, I have actually argued for the opposite so the template would trigger the actions , as this would make a great intelligent switch

1 Like