Logitech harmony command as a switch


#5

Hi @Jer78

I am trying to convert my functionally Harmony scripts to switches (I think Homekit need the state for on and off) Just running the scripts kind of works but the “tiles” of Homekit keeps spinning…

So I used the scripts for activities: (THEY WORK!)

# Harmoney scripts
  tv_stuen_activity_on:
    alias: "Tænd se Film og TV"
    sequence:
      - service: remote.turn_on
        entity_id: remote.HUB_STUEN
        data:
          activity: 'Se Film og TV'

  tv_stuen_activity_off:
    alias: "Sluk se Film & TV"
    sequence:
      - service: remote.turn_off
        entity_id: remote.HUB_STUEN
        data:
          activity: 'Se Film og TV'

And the switch setup for ON/OFF

  tv_stuen:
    value_template: "{{ is_state('script.tv_stuen_activity_on', 'on')}}"
    turn_on:
      service: script.turn_on
      entity_id: script.tv_stuen_activity_on
    turn_off:
      service: script.turn_on
      entity_id: script.tv_stuen_activity_off

When I switch the switch it starts the TV and flips back on the UI? making it impossible to turn it off?
Shouldn’t it keep the state ON for the on script?


#6

Hi there, I have the exact same configuration as @Jer78, 2 scripts for on and off combined with a switch in the config file. It works to a certain extent, in my case I use it to turn on and off zones on my music system. My issue is that with this process the switch does not “remember” the state and HA status and my Amp status get out of sync. Same problem as what @casperse highlights. For what it’s worth, my post is here : Template switches and scripts to control an IR device.
Also, as a beginner, I do not understand what the value template line in the code does. Can someone explain its purpose ?


#7

@casperse @mviamin

The value template is a template that gets the value of the current state. So if you run the script, the script is on until it finished then turns off, which would incorrectly turn your switch off. What you want to do is use the match the value of the current activity of your remote (which is an attribute of remote component) to the switch.

Here’s an example…You’ll need to change REMOTENAME to whatever your remote is called. And the ACTIVITY NAME to an activity such as “Watch TV” that you want the switch to match the state of.

  tv_stuen:
    value_template: "{{ is_state_attr('remote.REMOTENAME', 'current_activity', 'ACTIVITY NAME')}}"
    turn_on:
      service: script.turn_on
      entity_id: script.tv_stuen_activity_on
    turn_off:
      service: script.turn_on
      entity_id: script.tv_stuen_activity_off

#8

Thanks @Jer78 I tried the attr like this

switch:
      tv_stuen:
        value_template: "{{ is_state_attr('remote.hub_stuen', 'current_activity', 'Se Film og TV')}}"
        turn_on:
          service: script.turn_on
          entity_id: script.tv_stuen_activity_on
        turn_off:
          service: script.turn_on
          entity_id: script.tv_stuen_activity_off 

And like before the switch turns on TV and flickers back to off again?
my hub is named remote.hub_stuen and the activity is from the Harmony.conf file generated by HA


#9

It should temporarily flicker off because the remote hasn’t updated the activity right away but it should turn itself back on within seconds. Can you see if that works after a few seconds?


#10

This is what I use without scripts:

specifically:

        xbox_one:
          value_template: "{{ is_state_attr('remote.living_room', 'current_activity', 'Xbox One') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.living_room
              activity: 'Xbox One'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.living_room
              activity: 'PowerOff'

You can treat the turn_on and turn_off sections as scripts themselves. Like this one:

        audio_1:
          value_template: "{{ is_state_attr('media_player.yamaha_receiver', 'source', 'Echo') and is_state('switch.floating_outlet_switch', 'on') }}"
          turn_on:
            - service: switch.turn_on
              entity_id: switch.floating_outlet_switch
            - service: media_player.turn_on
              entity_id: media_player.yamaha_receiver
            - service: media_player.select_source
              data:
                entity_id: media_player.yamaha_receiver
                source: Echo
            - service: media_player.volume_set
              data:
                entity_id: media_player.yamaha_receiver
                volume_level: 0.7
          turn_off:
            - service: switch.turn_off
              entity_id: switch.floating_outlet_switch
            - service: media_player.turn_off
              entity_id: media_player.yamaha_receiver

#11

@petro looks interesting! but I havent started to use lovelace, to much time getting it just working! :slight_smile:
@Jer78 YOUR RIGHT! - THANKS ALLOT - I Just needed a little patience! It goes back after some seconds!

Thanks again @Jer78 I have been reading allot of your post and I found your input to using a “input_select”
(I would still need the above switch setup with Home kit and Google assistant! ) does it still work?

If I can bother you with one more question, using this input select like this (I have two Hubs):

input_select:
  hub_stuen:
    name: TV stuen
    options:
      - 'Select Activity'
      - 'Se Film og TV'
      - 'Lyt til Sonos'
      - 'PowerOff'
    initial: 'Select Activity'
    icon: mdi:television
  hub_aktivitet:
    name: TV Aktivitetsrum
    options:
       - 'Select Activity'
       - 'Play a PS4 Game'
       - 'Play a Wii Game'
       - 'Chromecast'
       - 'PowerOff'
    initial: 'Select Activity'
    icon: mdi:monitor

I would need the my activities, I found different examples one that used automation scripts and another I dont really know what is? script? - I haven’t been able to find out how to insert this to my configuration file?
Do you still use this?
automation: ???

- alias: 'tv stue controls'
  initial_state: true
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.hub_stuen
  action:
    - service: remote.turn_on
      data_template:  
        entity_id: remote.hub_stuen
        activity: >-
          {% if is_state("input_select.hub_stuen", "PowerOff") %}-1
          {% elif is_state("input_select.hub_stuen", "Se Film og TV") %}25355972
          {% elif is_state("input_select.hub_stuen", "Lyt til Sonos") %}25359950
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.hub_stuen
      data_template:
        option: "Select Activity"

I also found other setup like the above but that had 2 automation (Allot of code to get this working!)
1) # Will update the input select when the activity is changed using the harmony remote or other device
2) # will start an Harmony activity once it’s selected from the input select in the frontend

But if I am correct the above code doesn’t need this?
Still new to this but I am a quick learner (At least that’s what I tell myself LOL)
Again thanks this forum is a lifesaver!
Best regards
Casperse


#12

Ignore the lovelace bits. I just linked that post because it contains many variations of switch templates using the harmony hub.

If you are using the switch templates you don’t need the input_select.

Your attached automation will not update the input_select. A second automation is needed for that.

Yes, that’s what the automation does.

If you are referring to the Switch templates, yes you are correct. You don’t need any of this.


#13

Thanks @petro !!!

For voice I really need the switch but for the hass.io UI the selection would be nice LOL
(Google assistant can only pair with one hub, creating switches solves that limitations!)
Seems the second part above is also automation: ? and not a script:

- alias: 'tv stue controls'
  initial_state: true
  hide_entity: true
  trigger:
    - platform: state
...etc

But according to you I would still need the above extra automations 2 hubs x 2 automations like this?:

automation:

# Will update the input select when the activity is changed using the harmony remote or other device

  - alias: 'Remote external update stuen'
    trigger:
      platform: state
      entity_id: remote.hub_stuen
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.hub_stuen
        option: >
          {{ states.remote.hub_stuen.attributes.current_activity }}
        
  - alias: 'Remote external update aktivitetsrum'
    trigger:
      platform: state
      entity_id: remote.hub_aktivitet
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.hub_aktivitet
        option: >
          {{ states.remote.hub_aktivitet.attributes.current_activity }}
  

# will start an Harmony activity once it’s selected from the input select in the frontend

  - alias: 'Remote start activity from input select stuen'
    trigger:
      platform: state
      entity_id: input_select.hub_stuen
    action:
      service: script.turn_on
      entity_id: script.input_select_harmony_stuen

  - alias: 'Remote start activity from input select aktivitetsrum'
    trigger:
      platform: state
      entity_id: input_select.hub_aktivitet
    action:
      service: script.turn_on
      entity_id: script.input_select_harmony_aktivitetsrum

#14

Yes, those automations are needed for the input_selects to work. One automation that changes the remote via the interface. And one automation that changes the interface via the remote.


#15

@petro YES! its actually working, the missing bit was that I couldn’t tell that it as automation and not a script

One thing, when I select the drop down and an activity, it goes back to the standard “Select Activity” I would like it to stay on the selected activity? - I have initial: Select Activity but that shouldn’t be the problem?

input_select:
  hub_stuen:
    name: TV stuen
    options:
      - 'Select Activity'
      - 'Se Film og TV'
      - 'Lyt til Sonos'
      - 'PowerOff'
    initial: 'Select Activity'
    icon: mdi:television

#16

I’m guessing that one of your scripts handles that for you. Check these 2 scripts and make sure they aren’t setting back the input_select to defualts: input_select_harmony_aktivitetsrum, input_select_harmony_stuen


#17
automation:

  - alias: 'tv stue controls'
    initial_state: true
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_select.hub_stuen
    action:
      - service: remote.turn_on
        data_template:  
          entity_id: remote.hub_stuen
          activity: >-
            {% if is_state("input_select.hub_stuen", "PowerOff") %}-1
            {% elif is_state("input_select.hub_stuen", "Se Film og TV") %}25355972
            {% elif is_state("input_select.hub_stuen", "Lyt til Sonos") %}25359950
            {% endif %}
      - service: input_select.select_option
        entity_id: input_select.hub_stuen
        data_template:                    <--------This &
          option: "Select Activity"     <-------- This line?

@petro Also found this but doesn’t seem to be working:

  data_template:
    option: "Select Activity"
    option: option: '{{ states.remote.hub_stuen.attributes.current_activity }}'

#18

just remove all of this:

      - service: input_select.select_option
        entity_id: input_select.hub_stuen
        data_template:                    <--------This &
          option: "Select Activity"     <-------- This line?

#19

Thanks @petro everything works now!

Looking at your setup and icons it looks really nice! - is all the plugins native to hass.io?
I have everything in switches or light now (Except my Zones are all Sonos but that have so far been to complicated to get working, just wish there was a easy way to get it working with google assistant (Sonos should come out with something soon?)… not to impressed by it - it was just released last week supporting danish…I can see you have a ECHO to bad it doesn’t support danish yet
Thanks again this has been a great learning experience for me


#20

Yes, everything in that is native to home assistant, it just requires you to enable Lovelace.


#21

@casperse glad you got it all working and @petro was able to help. Sorry I had been away all day and couldn’t jump in but looks like you are up and running.

I find I don’t use the input_select ever since Lovelace because the button cards do a better job. I also combine them with conditional cards to only show when the tv is on or if the room sensors are active. Just a few ideas for you. Let me know if you’re interested and I can post my lovelace code.


#22

@Jer78 Yes I would be very happy to also look at your setup!

I am so happy the Google assistant now finally understand “Turn on TV” as a switch through Harmony, the native Harmony link never worked and only supported one HUB, and now I keep getting synch. errors even if I removed the link - What a Sh*tty way of doing a commercial integration

Anyway some quick question to both of you, would help me do things the right way :wink:

  • How do you control voice commands like sound levels on your amplifier?

  • Through Harmony remote as a switch? level 10, 20,30…

  • I can see that @petro have a HA supported receiver, and I believe I also might have one a “Pioneer” would that then be able to be controlled directly?

  • What about pause, Play, Mute do you have all of these as specific switch for each Harmony command? do they need to be linked to a specific activity?

  • I have been putting off Sonos integration for a very long time (I know @Jer78 have had allot of experience with that) so far I have been waiting for Sonos to come through with their voice integration for all platform (I am not a fan of IFTTT so I really don’t want to go down that road) would just Mute all Sonos speakers and Stop all playback be simple to do?

Oh another separate thing (For a separate thread) my configuration file is all in one file and it is truly getting really big. I hesitate to split it up in the already made sections, because I feel I would lack the big picture. Best option would be to put anything related to a specific task in one file “!include harmony.yaml” , but I don’t see that option available I would end up with pieces of code in multiple files like scripts, switch, automation, configuration right?

Again thanks for your valuable input spending your time to help me out, much appreciated :+1:


#23

I don’t know about google assistant but i’ll answer these as if you are using alexa.

I used haaska, but currently I use nothing. You could fake out a volume slider as a template light and use alexa that way. I helped a guy do that here, but the code would be different for the level_template and set_level sections.

Pretty much you would treat it like a light: “Alexa, turn on Volume to 50%”.

That depends, can this component be used with your receiver? If yes, then use this.

Play would be best handled as a script, even pause. But it still won’t be phonetic because you’d have to treat it like a light or switch. The main issue here is that alexa doesn’t support these commands. The play, pause, and change volume all directly change alexa’s media (I’d expect the same behavior from GA).


#24

Or the simplest and best solution… just use the proper buttons on the Harmony remote, the always work first time every time, don’t need to shout to compete when you have the volume to loud, don’t get Alexa doing random things because she couldn’t hear what you said or was more interested in following whatever was said on the telly than you etc etc.

Voice commands are great for a lot of things, as are screen based remotes, but for remote media consumption you still can’t beat a real button :slight_smile: