Template switches and scripts to control an IR device

Hi folks, I have a multi-zone sound system that can only be controlled through IR. I have a Harmony hub configured in HA. My goal is to turn on/off music zones using HA. The way I have it done now is through scripts that are calling on Harmony functions. I have a script to turn the zone on and another one to turn the zone off. All of that works.
Separately, I have set up template switches to be able to toggle on and off the zones. This is in my configuration file :

  - platform: template
    switches:
      sound_fam_room:
        friendly_name: 'Switch Fam room'
        value_template: "{{ is_state('remote.living', 'sound_fam_room') }}"
        turn_on:
          service: script.music_fam
          data: 
            entity_id: remote.living
        turn_off:
          service: script.fam_off
          data:
            entity_id: remote.living

The problem is that the switch does not “remember” the state, it is always on “off”. When I click on the switch it does turn on the source, but then reverts back to an off state (the music stays on though). I am sure it has to do with the syntax I use with value_template, but I cannot figure it out…Thanks for your help !

Looks like remotes are toggle entities, which mean their state is either ‘on’ or ‘off’. Not sure why you’re testing for a state of ‘sound_fam_room’. You probably need to change the value_template to:

"{{ is_state('remote.living', 'on') }}"

Yes, looks like it works. Thank you so much !

Actually, spoke too soon :(. Now the switch stays in an “on” state all the time. I click it, it turns on the music, I click it again, it runs the script to turn it off, but then resets itself to an “on” state" (while the music is now off). The behavior should be that it remembers the state that it was last in, and as long as I do not turn on the zones directly from the Amp they should always be in sync. I guess I just do not understand what the “value_template” statement does…sorry…much to learn…thanks for your help.

Just remove value_template then.

Hi, still struggling to get the desired result. The issue is that a device controlled though IR does not communicate its state back to HomeAssistant (am I correct here ? or is there a way ?). Each time I load the front end, the switch shows as “on”, even if the device is off. How do I keep those in sync ? I tinkered around with the entities dev page and I figured out how to change the state of the entity without controlling the entity, which is fine for my purpose. I could add a line in the script that turns the zone on to always change the state of the entity to “on”, and similarly, add a line to the script that turns the zone off to change the state of the entity to “off”. So they would always be in sync now. However, I was able to do this in the frontend, but do not know the syntax to include in a script. This is only page in the docs that I could find on the subject…

Thanks in advance for your help !

https://www.home-assistant.io/docs/ecosystem/appdaemon/api/#set_state

set_state()

set_state() will make a call back to Home Assistant and make changes to the internal state of Home Assistant. This is not something that you would usually want to do and the applications are limited however the call is included for completeness. Note that for instance, setting the state of a light to on won’t actually switch the device on, it will merely change the state of the device in Home Assistant so that it no longer reflects reality. In most cases, the state will be corrected the next time Home Assistant polls the device or someone causes a state change manually. To effect actual changes of devices use one of the service call functions.

So first, I got the template switch confused with some other switch. I thought I remembered value_template was optional, and when not provided it would just remember which it did last - turn it on or off. I think that would have done what you wanted. But, alas, it appears value_template is required.

And given that, I don’t think that just changing the state manually will work because the switch will just get read again and the value_template re-evaluated, which will write over what you just wrote.

Lastly, what you referenced is only if you’re using AppDaemon, which I’m guessing you are not.

Now, having said all that, it is possible to manually set a state via a python_script. And you could have the value_template just return the current state. Together that might do what you want.

Unfortunately I’m not familiar with the remote component, so I really can’t help there. Still, I feel like this should be easier.

If you’d like to try the option of setting the state via a python_script, let me know and I’ll dig up the details from a previous post I made where I explained how to do this. It’s really pretty easy; I just don’t want to have to reinvent the wheel. :slight_smile:

I found it. Check out this post:

Thank you @pnbruckner!, went through the post you referenced and it looks close to what I am struggling with, except he was dealing with a ZigBee device, I am dealing with an IR-controlled device, but it’s close, how do you check and/or control the state of a device that does not provide feedback into HA. I am traveling this week, but mining the post for clues is on my to do list for the week end ! Thanks so much for the help. Very appreciated :slight_smile: