Logitech harmony command as a switch

Hi,

I have various non “AV” devices being controlled by my harmony hub. One of them is a fan that I can turn on, off. Is it possible to have a switch in the HA front end that simply send a command to the Harmony ?

I know how to send a command through an automation, but not add a switch that sends a command.

Thanks

You should be able to do this with a template switch. The turn on and turn off actions would be done calling the Harmony service and sending the appropriate commands.

Here’s what I do. Just look up your device is in the harmony conf file and replace mine.

Here’s the switch:

dyson_fan_bedroom:
  value_template: "{{ is_state('script.dyson_on', 'on')}}"
  turn_on:
    service: script.turn_on
    entity_id: script.dyson_on
  turn_off:
    service: script.turn_on
    entity_id: script.dyson_off

And the script:

dyson_on:
  sequence:
    - service: remote.send_command
      entity_id: remote.bedroom
      data:
        device: "37710632"
        command: "PowerToggle"
    - service: remote.send_command
      entity_id: remote.bedroom
      data:
        device: "37710632"
        command: "Oscillate"

dyson_off:
  sequence:
    - service: remote.send_command
      entity_id: remote.bedroom
      data:
        device: "37710632"
        command: "PowerToggle"
6 Likes

I’m definitely going to try this. Thank you for sharing. :+1:

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?

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 ?

@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

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

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?

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

@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

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.

1 Like

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

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.

@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

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

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 }}'

just remove all of this:

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

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

1 Like

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