Harmony Hub Automation

I am wanting to make an automation for my Harmony Hub. I have made an input_boolean so I can expose it to Google Assistant. Google Assistant natively supports the hub but it’s too ‘specific’ If I want to turn everything off at night as part of my bedtime routine, I have to say 'Turn off xxxx" and specify the activity.
If I make an input_boolean, I can just turn that on or off and it can trigger the turn_off service for the hub.

But I’m losing my mind with the automation. I have 2 automations that are working but it occurs to me I should be able to use only 1.
The input_boolean needs to turn on/off if I am operating the hub (so the boolean and the hub are always in sync)
Here is what I have…

- id: 'harmonyon'
  alias: Harmony Remote On
  trigger:
  - platform: state
    from: 'off'
    to: 'on'
    entity_id: input_boolean.harmonyremote
  - platform: state
    from: 'off'
    to: 'on'
    entity_id: remote.harmony_hub
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.harmonyremote
  - service: remote.turn_on
    entity_id: remote.harmony_hub
    data:
      activity: "Watch DTV"
- id: 'harmonyoff'
  alias: Harmony Remote Off
  trigger:
  - platform: state
    from: 'on'
    to: 'off'
    entity_id: input_boolean.harmonyremote
  - platform: state
    from: 'on'
    to: 'off'
    entity_id: remote.harmony_hub
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.harmonyremote
  - service: remote.turn_off
    entity_id: remote.harmony_hub

any help appreciated… @petro ?

@DavidFW1960, you’ve inadvertently answered questions I’ve had in the past so it’s an honor to reply to a question of yours.

Why not create a template switch? I do this to control my vacuums with Alexa. In your configuration.yaml file you can define what the switch does when turned on and off and what the switch mirrors.

2 Likes

As Derek pointed out, a template switch would be the solution. It’s a little tricky to do this with a single automation since the remote.turn_off service call doesn’t support the activity parameter. You could make both actions into scripts and call the appropriate script based on the state changes though.

- alias: Toggle Harmony
  trigger:
  - platform: state
    entity_id: 
    - remote.harmony_hub
    - input_boolean.harmonyremote
  action:
  - service_template: "script.harmony_{{ trigger.to_state.state }}"

In this case, the scripts would be named script.harmony_on and script.harmony_off. This also assumes the only states for the remote would be on or off. You could always add

to:
- 'on'
- 'off'

to the trigger if the remote could be in a different state. Yes, to: and from: support lists even though it’s undocumented!

But again, template switch FTW.

Thanks both for the suggestion. I’ll have a play with a Template switch as suggested by @dmo012 Derek. Using 2 scripts isn’t much of an improvement on what I have now. It’s a pity I can’t just do one command in Google Assistant to just shut it all down… then I wouldn’t need to bother with this at all.

A job for tomorrow.

Oh yeah, I’d use a template switch. Here is a whole guide on the process, template switches are in there:

switch:
- platform: template
  switches:
    xbox_one:
      value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Watch DTV') }}"
      turn_on:
        service: remote.turn_on
        data:
          entity_id: remote.harmony_hub
          activity: 'Watch DTV'
      turn_off:
        service: remote.turn_on
        data:
          entity_id: remote.harmony_hub
          activity: 'PowerOff'
1 Like

So I tried this:

# Switch to copy harmony remote state
  - platform: template
    switches:
      copy:
        value_template: "{{ is_state('input_boolean.harmonyremote', 'on') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony_hub
            activity: "Watch DTV"
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony_hub

But it doesn’t work…
I also think if I use the remote to turn on/off then the input_boolean which I can control with Google Assistant isn’t going to work either.

So I think my working automations might be best unless anyone has any other ideas?

Because that’s not the correct way to do it.

  1. You need to use activities for the turn on and turn off.
  2. You have to use the state of the activity for the switch, otherwise it will never change.
  3. You don’t need anything but the switch. No automations, just a single switch.
  4. There will be a delay for the switch turning on or off in the interface but the hardware will react immediately. This is because harmony is shitty and you can only poll it for a state.

This switch should work for you right out of the gate. Nothing else required. The turn on will turn on, the turn off will turn off. There is no mistake in this template (remote.turn_on in the turn off is 100% correct and not a typo).

switch:
- platform: template
  switches:
    xbox_one:
      value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Watch DTV') }}"
      turn_on:
        service: remote.turn_on
        data:
          entity_id: remote.harmony_hub
          activity: 'Watch DTV'
      turn_off:
        service: remote.turn_on
        data:
          entity_id: remote.harmony_hub
          activity: 'PowerOff'

So close… But if I am using any activity other than ‘Watch DTV’ then the switch turns off.

So I’m trying this now for the value template…

value_template: "{{ not is_state_attr('remote.harmony_hub', 'current_activity', 'PowerOff') }}"

Finally success!

Thanks everyone for the suggestions and @petro for the solution

2 Likes

Yeah harmony only has 1 activity at a time. The remote only controls devices that are attached to the current activity.

Yep. With the changed template which checks the harmony is not off then it works for any activity.

Ah I didn’t realize that’s what you wanted. Sorry bout that.

Yeah… I only wanted/needed a switch I can expose to Google Assistant so that I can include turning everything off in my bedtime routine. When I am in the lounge using my TV etc I can just use the remote so all this drama was merely so I can use a voice command to turn it all off.

Thanks again.

@ DavidFW1960 @ petro Please excuse my ignorance and/or noobie understanding of this, and sorry for posting on such an old thread, I can’t find info on this anywhere else.

I too want to have the ability to tell google to turn of my Harmony as if the “power off” button was pressed. I have everything setup and working with google assistant integration etc. already. The thing I can’t figure out is exactly where I plug in the code that has been discussed here, and what it should look like.

Do I add the code to automation under “triggers” and make a template there? Should I be adding an “action” at the end of the automation to tell the hub to “poweroff”? Or do I just grab this code and paste it into the configuration.yaml file directly? I’m new to Home Assistant but I do have an understanding of what may have to be done.

I’m trying to piece together how the code should look from the posts here in this thread. Does this look correct to you?

switch:
  - platform: template
    switches:
      copy:
        value_template: "{{ not is_state_attr('remote.harmony_hub', 'current_activity', 'PowerOff') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony_hub
            activity: "Computer"
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony_hub

Under “turn_on” I just added ‘Computer’ as is one of my activities in this code, But I don’t know what I’m doing here lol. Can you post your exact completed code in full that you used so that I may have a look at it and try to add it to my Home Assistant? I just can’t get this to work :frowning: And exactly where to add it would be a great too! Thank you for your patience with my ultra noobness. I am trying over here lol.

Once I have this working, I will have fully finished my setup on Home Assistant, it is the only thing that is missing to my setup at this point. One more thing, I also would like to know what voice command to say to google to actually turn the Harmony off lol. I would assume “Hey google, turn off harmony hub”… As is the name given to the device. Thanks again!