How create switch to run one script for on and a different script for off?

Turns right back on

GAH!

The only thing I can think of is ugly and certainly seems like a Rube Goldberg: Have the switch change the state of an input boolean and create automations that trigger on that state change.

But that makes me feel bad just suggesting it. Sorry I can’t think of a more-efficient solution.

Ha, no worries, appreciate the help!

I want to but only to say your idea of a generic switch that works like the command line or template switch but allows use of scripts, services, scenes etc to be used for off or on would be extremely useful, particularly with the emulated hue component. It seems like something that should be easy to implement?

I use this method of two different scripts with Ha-Bridge to an “off” so there is a workaround for voice control if needed.

I was thinking about this post while I was at work today trying to link a combination of maybe 3 input booleans and either an automation or script with conditions but cant quite resolve it.

Edit: took too long typing and @ih8gates jumped in with the boolean already.

Yeah, I assumed such a function already existed but I understand that it probably wasn’t really necessary until alexa really caught on. Hopefully someone can implement it, I will give a look into feature requests to see if such a thing has already been suggested.

Edit : added a feature request here: Generic switch

Feel free to chime in or add any amplifying info

I think I found a working solution. Instead of making a script for 'on' and one for 'off', I noticed there is a PowerToggle command as well. I changed it to use one script with the PowerToggle command called ‘tv’ and in emulated hue, I set the script domain as off_maps_to_on_domains:

going to test it out. This may solve my problem for the tv power but there are still many other uses to make a generic switch a worthy feature.

Edit: so that worked! but… after alexa runs the script she says “sorry, the device blahblahblah is not responding (or disconnected, or something)”
How do I get alexa to ignore that?

I found this: https://github.com/home-assistant/home-assistant/issues/5054

It describes the exact problem but it says it has been fixed. I am on latest HA 35.3. Anyone know how I can fix it?

This the the fix:

I am having the same issue. Looks like the fix was merged 11 days ago into the dev branch. The last release was 15 days ago. Hopefully this will filter through in the next version or two.

Hi Geoff, would you be able to post your config with “PowerToggle” example?

# Upstairs TV power on
  togglepower_upstairs_tv:
    alias: "Toggle TV Power"
    sequence:
      service: remote.send_command
      data_template:
        command: PowerToggle
        device: 39592860
        entity_id: remote.home_theatre

Thank you,

How is that “PowerToggle” command defined? Basically I’m having the same problem as you, I want to have a switch that runs two different scripts when turned on and off…

If you guys really want the feature I could make the value_template for the template switch optional. I just issued a pull request for a template light so I can control on/off/level via google home. I made the template light such that if a value_template isn’t defined it defaults to optimistic operation

my TV has discrete commands for power on, power off, and power toggle. What I wanted to do originally was one switch that performed two commands - turning on the switch would power on the tv and turning the switch off would power off the tv off - but I couldn’t get it to work. Then I realized I can just use the power toggle command and get essentially the same functionality.

Works for me

- platform: template
  switches:
    roku:
      value_template: "{{is_state('media_player.receiver', 'on') and states.media_player.receiver.attributes.source == 'Roku'}}"
      turn_on:
        service: script.turn_on
        entity_id: script.watch_roku
      turn_off:
        service: script.turn_on
        entity_id: script.all_off

Unfortunately, my TV that I use this on does not report its state back to HA and it is in a separate room from my main entertainment center that is controlled by my Harmony hub. I ran an IR extender through my existing telephone lines to get it in line of sight to my upstairs tv and then use this script to turn it on and off without having to use a harmony activity (as that would interfere with an activity that was being run downstairs).

My TV doesn’t report state either (that I’m aware of). You could set up optimistic state switching using hidden input_boolean and have your scripts turn this on/off and have your value_template return its value.

Optional would be nice.
I also am looking to create a virtual switch with which I can run a turn on script and a turn off script.

If you’ve got an mqtt sever set up, an mqtt switch is just a virtual switch if nothing else is monitoring the subject to which it publishes.

It’s not as elegant as a straight up virtual switch, but it works.

This is exactly what I would like to do. I have two scripts to turn on and off my air conditioning using the Harmony exactly like Geoff_Brown is using for his TV. Have set up IFTTT rules to be able to trigger via Google Home, but would be great to use the Emulated Hue for speed and simplicity.

Did anyone come up with an elegant solution I could copy ?

any plans to make value_template optional?

I worked around this before by using input boolean but its ugly.