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

Take a look at the states dev tool. It’ll tell you what entity_ids exist in your system. Some will present as “switch”. Depends on the platform.

You can create an arbitrary switch that’ll call existing services/scripts with a template switch.

Also - If your remote is a Harmony hub, you can skip all of this and connect the hub directly to the Alexa. I just recently picked up a Harmony hub and it’s impressive. The remote doesn’t have an IR blaster - all communication comes from the hub. The remote just tells the hub “I pressed this button” and the hub does the right thing. That means that if you say “Alexa, turn on Netflix”, the remote magically assumes all Netflix controls.

I’m using harmony but it is downstairs in my main theatre. I ran an ir extender upstairs only to control volume and power. Since harmony only lets you specify one activity at a time, I have to run discrete device commands through scripts (as I’ve described above) so that if someone is watching a movie downstairs the person upstairs can still tell Alexa to turn on the tv, etc.

I have looked at switch templates but can’t figure out what to put for template value as there is no state for the switch to check - harmony cannot read the stage of this upstairs tv.

Edit : if possible, I’m hoping to create a switch that toggles two scripts - one for power on and the other for power off of the upstairs tv.

IIRC, the state is optional. That just means that the switch won’t keep track of the state of whatever you’re switching.

Unfortunately, it looks required.
value_template (Required): Defines a template to set the state of the switch.

Indeed it is. What if you just put something in there as a placeholder. “True” or similar.

So, how would you replace the value_template I have here with what you are saying?

# Harmony template switch --- redundant with harmony native functionality
  - platform: template
    switches:
      tvpower:
        value_template: "{% if is_state('remote.home_theatre', 'on') %}on{% else %}off{% endif %}"
        turn_on:
          service: remote.send_command
          entity_id: remote.home_theatre
          data_template:
            command: PowerOn
            device: 39592860          
        turn_off:
          service: remote.send_command
          entity_id: remote.home_theatre
          data_template:
            command: PowerOff
            device: 39592860

Just with:
value_template: "true"?

Edit: Oops, this is the script I am using so far (for power On):

# Upstairs TV power on
  turn_on_upstairs_tv:
    alias: "turn on the upstairs tv"
    sequence:
      service: remote.send_command
      data_template:
        command: PowerOn
        device: 39592860
        entity_id: remote.home_theatre

It would probably be like this:

# Harmony template switch --- redundant with harmony native functionality
  - platform: template
    switches:
      tvpower:
        value_template: "true"
        turn_on:
          service: script.turn_on
          entity_id: script.turn_on_upstairs_tv         
        turn_off:
          service: script.turn_on
          entity_id: script.turn_off_upstairs_tv
1 Like

Yeah - I’m not sure if that’d even work. Just “thinking out loud”.

But I think that’ll make it so that the switch always stays “on”. Hmmm… I wonder why that platform requires a state.

Ideally, you’d want the functionality of a command line switch, with services rather than commands.

Of course, you can expose scripts to Alexa via emulated hue, but the “turn off” command becomes weirdly phrased.

Anyone else wanna tag-in?

That is exactly the issue. I am trying that last example I posted now to see what happens.

Edit: yep, stays on!

But does it let you turn it off via Alexa? Sure, the UI would be off, but do you get voice control?

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…