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

I have a working script made up that turns on my upstairs tv using the harmony hub. How do I create a switch that will run that script when I turn the switch on and then a different script that turns the tv off when I turn off the switch?

here is the working ‘on’ script:
# 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

You should call the scripts from automation:

automation:
- alias: 'TvON'
  trigger:
    platform: state
    entity_id: switch.Switch_ID
    state: 'on'
  action:
    service: script.turn_on
    entity_id: script.tv_ON

- alias: 'TvOFF'
  trigger:
    platform: state
    entity_id: switch.Switch_ID
    state: 'off'
  action:
    service: script.turn_on
    entity_id: script.tv_OFF

I use such thing for turning my TV ON and OFF and I build the following logic:
Power code switch the Tv to the oposite state: from ON to OFF and from OFF to ON.
In order to be sure about the Tv state set using my system, I build the following rules:
Tv ON: to turn the Tv ON, I send one key code, for example Key 1 which start the Tv on the corresponding channel on key one memory.
Tv OFF: first send a code to start the Tv (for example key 1), then send the code for power whitch will change the Tv state from ON to OFF.
This is implemented in my Arduino:

void TvON() {
  // Send the code for Key 1 to switch on the Tv
  irsend.sendLG(551520375,32);  // Send the code for Key 1
  delay(100);
  irrecv.enableIRIn();
}

void TvOFF() {
  // Send the code for Key 1 to be sure the Tv is switched on, then send code for Power Key to switch off
  irsend.sendLG(551520375,32);  // Key 1
  delay(100);
  irsend.sendLG(551489775,32);  // Power Key
  delay(100);
  irrecv.enableIRIn();
}

The whole project is described here: https://www.openhardware.io/view/55 and detailed code is here: https://github.com/Mihai258/MySensors-HomeAssistant-Live

2 Likes

Thanks, Mihai; how do you build the switch that is specified in the automation? entity_id: switch.Switch_ID

Is the switch created by this automation or do I have to specify it somewhere else in the configuration first? This is the part I’m struggling with because I see documentation for all kinds of switches but I’m not sure how to build one that will simply run scripts.

Edit: the reason for this switch is to tell Alexa to turn the tv on or off without having to say "turn on (script to turn on tv) " and “turn on (script to turn off tv)”. I can just say “turn on the tv” and then “turn off the tv” ;it will be more natural

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.