Logitech Harmony Integration with @iandday's component

You should be able to use the event trigger. You may have to delay the action by 30s or so to ensure that pyharmony has pulled the current activity first, but that’s not a huge issue.

automation:
  trigger:
    platform: event
    event_type: homeassistant_start
1 Like

Yeah as @Bahnburner says use the start event with a delay, this works for me.

automation:
  - alias: Harmony Set Input Select On Start
    hide_entity: true
    trigger:
      platform: event
      event_type: homeassistant_start
    action:
      - delay:
          seconds: 30
      - service: input_select.select_option
        data_template:
          entity_id: input_select.living_room_tv
          option: '{{ states.remote.homehub.attributes.current_activity }}'
1 Like

Doesn’t seem to be working for me. Do I need to change homehub to the name of my hub?

Also, let me ask a stupid question. :slight_smile: Currently my state automations look like this:

- alias: "State - Powered off"
  trigger:
    platform: state
    entity_id: remote.freds_harmony_hub
  condition:
    condition: template
    value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'
  action:
    service: input_select.select_option
    entity_id: input_select.freds_harmony_hub
    data:
      option: "Powered Off"

Why does your code have those dashes in the items under action:?

The dashes denote a list in yaml, so it’s telling home assistant to do more than one action for the single trigger. It does them in order so it’ll wait for 30 seconds then update the input_select.

you will need the change the input_select name and the homehub name. Based on your code it should be like this i think:

automation:
  - alias: Harmony Set Input Select On Start
    hide_entity: true
    trigger:
      platform: event
      event_type: homeassistant_start
    action:
      - delay:
          seconds: 30
      - service: input_select.select_option
        data_template:
          entity_id: input_select.freds_harmony_hub
          option: '{{ states.remote.freds_harmony_hub.attributes.current_activity }}'

Yeah, I was just coming back to edit that I discovered this, and now it works! So the reason mine doesn’t have dashes is because it’s only the one action (the service call) and the other items are just arguments for that action?

(Next step is going to be to redo these using the templating code. :))

Yeah, that’s correct. The trigger and condition dictate when the automation is started and the action part is what happens after. The action section is basically the same as a script.

Okay, so in this:

  sequence:
    - service: remote.send_command
      entity_id: remote.freds_harmony_hub
      data:
        device: 34450182
        command: LiveTV

…is the hyphen superfluous, since there is just the one call?

Yes superfluous, but it doesn’t break syntax.

Only benefit would be to save you a few seconds adding the hyphen if you ever added any more steps in that sequence. I don’t know of any drawbacks.

Hi @iandday,

I have strange but reproducible side-effects using your script and automation examples. My Harmony Ultimate remote now (after configuring it in HASS) often performs synchronization without apparent reason, does not enter screensaver after changing activities, starts a single device after PowerOff activity etc.

Has someone had similar problems?

As soon as I remove the ‘Remote external update living room’ automation from your example, the Harmony (outside of HASS) works as expected again.

Maybe my Hub is unable to cope with two activity commands at the same time:
When I hit the PowerOff button on the actual Ultimate remote, the HASS remote component state is updated. Then the ‘Remote external update living room’ automation sets the new value in the input select and the ‘Remote start activity from input select living room tv’ automation immediately triggers the very same activity as received from the state update (which might still be in progress)…?

I wondered about that as I was reading this. My first thought was to put a condition on the update automation so that it doesn’t send the command to the Harmony unless the new state (from the input_select) is different than the current state (from the remote). I haven’t actually tested that yet, but I’m guessing it should be possible.

I have been trying to set this new component up but it is not working correctly so looking for some help. The input select is displayed in the UI with all activities but they do not update when changing an activity on the remote and also do not change the activity if I select the activity from the HA input select. Here is my config.

configuration.yaml

remote:
  - platform: harmony
    name: Living Room Hub
    host: 192.168.1.xxx
    port: 5222
    activity: Watch TV

input_select:
  harmony_remote:
    name: Harmony Remote
    options:
      - PowerOff
      - Watch Amazon
      - Watch Plex
      - Watch Smart TV
      - Watch DVD
      - Watch PC
      - Watch TV
    initial: PowerOff
    icon: mdi:television

scripts.yaml file

input_select_harmony:
    alias: "Start activity from input select"
    sequence:
      - service: remote.turn_on
        data_template:
          entity_id: remote.living_room_hub
          activity: >
            {% if is_state("input_select.living_room_hub", "PowerOff") %}
                -1
            {% elif is_state("input_select.living_room_hub", "Watch Amazon") %}
                16657231
            {% elif is_state("input_select.living_room_hub", "Watch Plex") %}
                16537386
            {% elif is_state("input_select.living_room_hub", "Watch Smart TV") %}
                16657221
            {% elif is_state("input_select.living_room_hub", "Watch DVD") %}
                16537438
            {% elif is_state("input_select.living_room_hub", "Watch PC") %}
                22222835
            {% elif is_state("input_select.living_room_hub", "Watch TV") %}
                16537371
            {% endif %}

and my automations.yaml file

- alias: 'Remote external update living room'
  trigger:
    platform: state
    entity_id: remote.living_room_hub
  action:
  - service: input_select.select_option
    data_template:
      entity_id: input_select.living_room_hub
      option: >
        {{ states.remote.living_room_hub.attributes.current_activity }}

- alias: 'Remote start activity from input select living room hub'
  trigger:
    platform: state
    entity_id: input_select.living_room_hub
  action:
  - service: script.turn_on
    entity_id: script.input_select_harmony

I am seeing the following error in the log if this helps
18:55:38 homeassistant.components.sensor.template: UndefinedError: ‘None’ has no attribute ‘attributes’

Would appreciate any help anyone could give.

Your input select is called harmony_remote but you call input_select.living_room_hub in the entity_id part of both of your automations

Tried adding a condition to the script. But from the HASS log I can see it is still executed if I switch activities from the Ultimate. The problem with my PS3 switched on when I PowerOff still persists… :smiley:

script:
  input_select_harmony:
    alias: 'Logitech Harmony (Wohnzimmer): Input Select - Ausgewählte Aktivität starten (Script)'
    sequence:
      - condition: template
        value_template: '{{ states.remote.wohnzimmer.attributes.current_activity != states.input_select.wohnzimmer }}'
      - service: remote.turn_on
        data_template:
          entity_id: remote.wohnzimmer
          activity: >
            {% if is_state("input_select.wohnzimmer", "PowerOff") %}
                -1
            {% elif is_state("input_select.wohnzimmer", "Apple TV sehen") %}
                14431475
            {% elif is_state("input_select.wohnzimmer", "Fernsehen") %}
                14431436
            {% elif is_state("input_select.wohnzimmer", "Wiedergabe PS3") %}
                14431521
            {% elif is_state("input_select.wohnzimmer", "Musik hören") %}
                14431578
            {% elif is_state("input_select.wohnzimmer", "Spotify") %}
                16371284
            {% elif is_state("input_select.wohnzimmer", "iPod hören") %}
                14431550
            {% elif is_state("input_select.wohnzimmer", "Fire TV sehen") %}
                14431499
            {% endif %}

Thanks @mrtips sometimes you can’t see the obvious so appreciate your help with this. It is working OK now on an initial test but I will test some more later. Many thanks.

No worries, I actually did the same thing with the script part of the config and scratched my head for a day before seeing it.

Setting this up is actually non-trivial, could be good to have the default component make all the activities available as individual switches. I wonder if that’s possible, @iandday may know?

Perhaps that’ll be my Christmas project and first home assistant pull request if people think it would be a good idea?

Is there anything else on your network that can interact with the harmony hub or is it just HASS and the hub? This sounds like a feedback loop.

Also try putting the condition on the ‘Remote start activity from input select’ automation rather then the script itself so the script never gets launched if the input select is the same as the current activity.

It’s completely possible, during component setup you can loop through the list of detected activities and create a switch for each one. I didn’t want to clutter up the front-end with a large number of switches when they might not all be used. It’s on my to-do list and to create a variable that will be placed in the harmony section of the config.yaml to control if the individual switches are created

1 Like

I moved the condition to the automation. And then I found an error in my condition… :blush: I compared to the input_select state object instead of the state string. Here is the correct condition:

- condition: template
  value_template: '{{ states.remote.wohnzimmer.attributes.current_activity != states.input_select.wohnzimmer.state }}'

Now my strange behaviors disappeared. All working as expected. So if someone else also encounters problems, here is the complete automation (as per iandday’s example) including the condition suppressing the feedback loop:

- alias: 'Remote start activity from input select living room tv'
  trigger:
    platform: state
    entity_id: input_select.living_room_tv
  condition:
    condition: template
    value_template: '{{ states.remote.living_room_tv.attributes.current_activity != states.input_select.living_room_tv.state }}'
  action:
    service: script.turn_on
    entity_id: script.input_select_harmony
2 Likes

I had feedback loop issues on my RPi3 with the auto-correcting input_select and ended up going another route. Here’s what I have:

The majority of the code is the same, but rather than trying to correct the input_select anytime it changes, I instead directly display the current activity using a template sensor and have the input_select revert to Select Input after sending the start activity command to the harmony hub.

input_select:
  harmony:
    name: Harmony Control
    options:
### DO NOT REMOVE 'SELECT INPUT'
      - Select Input
      - Watch DirecTV
      - Watch Plex
      - Watch Netflix
      - Watch Hulu
      - Watch Prime Video
      - Watch Chromecast
      - Watch Smart TV
      - Power Off
    icon: mdi:monitor

sensor:
  - platform: template
    sensors:
      current_activity:
        friendly_name: 'Current Activity'
        value_template: '{{ states.remote.living_room.attributes.current_activity }}'

- alias: Harmony
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmony
    from: 'Select Input'
  action:
    - service: remote.turn_on
      entity_id: remote.living_room
      data_template:
        activity: >
          {% if is_state("input_select.harmony", "Watch DirecTV") %}
            19594181
          {% elif is_state("input_select.harmony", "Watch Plex") %}
            19755764
          {% elif is_state("input_select.harmony", "Watch Netflix") %}
            23422788
          {% elif is_state("input_select.harmony", "Watch Hulu") %}
            20619552
          {% elif is_state("input_select.harmony", "Watch Prime Video") %}
            19752038
          {% elif is_state("input_select.harmony", "Watch Chromecast") %}
            19938341
          {% else %}
          {% endif %}
    - service: input_select.select_option
      entity_id: input_select.harmony
      data_template:
        option: "Select Input"

- alias: Harmony Off
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmony
    to: 'Power Off'
  action:
    - service: remote.turn_off
      entity_id: remote.living_room
    - service: input_select.select_option
      entity_id: input_select.harmony
      data_template:
        option: "Select Input"
5 Likes

Yeah that’s what I had in mind.

Be aware that that code will only work if your input select state names are identical to your harmony remote activity names. If you customise them you will have to do a bit more logic in that template.