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
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();
}
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.
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
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.
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.
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 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.