How to configure automations for Harmony Hub activities?

Hi!

I have a Harmony Hub integrated into Home Assistant and now I want to automate a bit with it. Eg. I want to switch on a light when the activities “Watch TV”, “Apple TV” or “Bluray” are switched on with the companion remote.

So I started to create the automation:

But what should I choose here?
Is it an “Event”, a “State” or something other that I have to choose?
If it is an Event , I have to input Eventtype. What is it?
If it is a State, I get this:
Bildschirmfoto 2020-04-16 um 16.39.45

I can choose my switch “Watch TV” that I defined inside the switch.yaml:

        fernsehen:
          value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Fernsehen') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'Fernsehen'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'PowerOff'

But what should be “From”, “To” and “For”?

I am really lost here!
This is nothing I can find in the documentation to the Harmony Hub integration.
Where can I find information regarding this automations?

Thank you in advance!

I don’t have a Harmony Hub, but can maybe help anyway.

For an automation trigger, you are free to trigger on ANYTHING you can think of. The easiest being a state based trigger.

So lets take a look at your switch.

fernsehen:
   value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Fernsehen') }}"

You have told your switch that it is to be ‘on’ if the remote.harmony_hub has an attribute called ‘current_activity’ which equals ‘Fernsehen’. In other words, you told this switch to say it is “ON” when you are watching TV.

In addition, you told it what to do when you turn the switch ON/OFF. This means, the switch will update its state independently of you using the button (i.e. if you turn on the harmony hub manually). This is good!

So you can simply use this switch state as your trigger. Since we know if the state is ‘on’, you are watching TV. If the state is ‘off’, you are not watching TV.

For a state trigger, “to:” and “from:” are optional. If you don’t specify them, you will trigger on ALL state changes. You only need to specify them if you care about a specific state. If we omitted it, we could have the single automation turn the lights on AND off looking at this state.

But for now, lets only look at the ‘on’ state.

We don’t have to specify the “from” state here for 2 reasons. 1) a switch only has 2 states anyway, so it will ALWAYS go from ‘off’ to ‘on’, and 2) even if it had other states, we don’t care HOW it got to ‘on’. Only care that it is on.

- alias: Watching TV
  trigger:
    platform: state
    entity_id: switch.fernsehen
    to: "on"
  action:
    # Using a template here. trigger.to_state.state will be the new state of the 
    # trigger. In this case, 'on' or 'off'. 
    # If we remove the 'to:' in the trigger, this same automation would turn off the
    # lights when done watching TV. 
    # This might not be desired (what if we start a movie....dont want the lights to go off then back on)
    - service_template: light.turn_"{{ trigger.to_state.state }}"
      # Change this 
      entity_id: light.my_light

The second option is to just trigger on the remote.harmony_hub itself. But keep in mind, the state for the hub is probably only ‘on’ or ‘off’ as well. Instead, we want to follow an attribute of the hub.

This is still a state based trigger. Attribute changes of a sensor still cause a state change, even if the state is the same. This kind of trigger is a little more involved.

- alias: Harmony Hub Update
  trigger:
    # This will fire on ALL changes in the harmony_hub state
    platform: state
    entity_id: remote.harmony_hub
  condition:
    condition: template
    # Only runs if this condition is true. Notice, it's the same condition as your template switch.
    value_template: "{{ is_state_attr(trigger.to_state.entity_id, 'current_activitiy', 'Fernsehen') }}"
  action:
    ...
    ...

Either way is fine. I’d prefer just triggering on the template switch so if anything changed, only the template switch would have to be updated and the automation would still work as is. Otherwise, you’d have to update both the template switch AND the automation.

it’s so useful for me. Thanks for your shares a lot of

I think you did! Now I know how to get further into Home Assistant. This was one of the missing links for me!
Thank you very much!

It is really not so easy to get to know how something can be done in Home Assistant and the docs are not helpful for that. I can learn the syntax from them but bringing things together isn’t described there. And sometimes I don’t know into which yaml file something from the docs has to be written. :slight_smile:

1 Like

Yep. I probably spent months just copy/pasting random code not knowing how/why/if it even worked until one day it finally all made sense. You’ll get there!

Good morning!

I tried to get it to work:

  alias: Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder
  description: ''
  trigger:
  - entity_id: switch.fernsehen
    platform: state
    to: 'on'
  - entity_id: switch.videorecorder
    platform: state
    to: 'on'
  - entity_id: switch.appletv
    platform: state
    to: 'on'
  - entity_id: switch.bluray
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: light.avatar_led_klein_3
    service_template: light.turn_{{ trigger.to_state.state }}

(I had to omit the " because otherwise I would get this error message: ""Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder: Error executing script. Unexpected error for call_service at pos 1: Template rendered invalid service: light.turn_“on”)

This is working fine for switching “on”. For switching “off” I think I’ll have to look for the “off” state in another automation because as you correctly wrote that it is not really good when switching between Watch TV and Bluray that the lights will go out. So it will be better to use the PowerOff action of the Harmony Hub. I think I will get this to work.
So normally I can change the “light.turn_{{ trigger.to_state.state }}” into light.turn_on, as I will not switch it off with this automation. So I don’t have to use a service_template?

This part I don’t understand completely:

What did you mean with this?

(I had to omit the " because otherwise I would get this error message: ""Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder: Error executing script. Unexpected error for call_service at pos 1: Template rendered invalid service: light.turn_“on”)

Yep, that was my bad. The quotes should have gone around the whole thing.

service_template: "light.turn_{{ trigger.to_state.state }}"

Templates MUST always be a string. Single line templates MUST always be wrapped in quotes. Multiline templates (the ones you see with the > or >+ etc) are not wrapped in quotes because that special character already means ‘return a string’

You can change it from the current service_template into simply:

service: light.turn_on

What I meant by that is, I prefer the way you have it right now where we are doing a state based trigger on a template switch.

The template switch is following the attribute we care about already. And, in the future, if for whatever reason the attributes changed names, or you wanted the switch to follow a different attribute, or even follow a new device (you switched to plex or something for example), the automation can remain the same because it’s following that switch state.

If you had it follow the remote.harmony_hub, if you changed any of those things as I mentioned above, you would then also have to go change the automation to do the same thing. It’s just redundant code which may have more maintenance.

Here’s how you should do the lights in a single automation.

  1. Create a group.
# Example configuration.yaml entry
group:
  tv_light_state_group:
    name: TV Lights
    entities:
      - switch.fernsehen
      - switch.videorecorder
      - switch.appletv
      - switch.bluray

A group has a unique property where, if ANY single entity of the group is ‘on’, the entire group is on. Likewise, if all are OFF, the entire group is off.

We can use this as our on/off trigger. And if you add more switches to the group, it will just work without touching the automation.

Oh, and one other thing. I recommend you change the alias to something shorter without special characters. The alias for an automation turns into its name. In your example, you will have an automation.licht_an_bei_fernsehen_apple_tv_blueray_videorecorder. Though, being German, you’re probably used to long long words :slight_smile:

Make the alias shorter and put that in the description part.

- alias: TV Lights Automatic
  description: 'Licht an bei Fernsehen, Apple TV, Bluray, Videorecorder'
  trigger:
    # First trigger. If anything turns to 'on', immediately trigger.
    - platform: state
      entity_id: group.tv_light_state_group
      to: "on"
    # Second trigger. If they all turn off and remain off for 1 minute, trigger.
    - platform: state
      entity_id: group.tv_light_state_group
      to: "off"
      for: 
        minutes: 1
  action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      # Change this 
      entity_id: light.my_light

So this one automation will now turn on the tv lights when anything in the group turns on. And, it will turn them off if the group is off for 1 minute. This means if you change states and for example, the blueray switch turns off, then a few seconds later the tv switch turns on, the lights wont go out. They only go off if ALL switches have been off for 1 whole minute. You can change that delay.

Ok, I understand.

Ok, I see. I will change it!

Hey, that is a nice idea, thank you. This saves me months of study!

:slight_smile: That is possible. English is a lot shorter.

I’m really trying to follow all info and instructions in this thread, but I’m still at a (newby) loss.

What I have is a Harmony Hub, an amplifier and subs, both plugged into a tapo_p100 power switch. All available in UI. (Home Assistant 2022.11.1)

The goal I’m trying to achieve is simple:
If the Harmony Hub in on, I want my amplifier and subs on. If the Harmony Hub is off I want those switched off.

I tried two scripts calling each other, but that only works once. So the solution above seems the better route. But changes I make in the yaml files don’t seem to do anything, for some reason… (running HA in a Docker container on a Synology)

Could someone help me out on how to do this using the UI?

After digging through most of possible options, and more, I finally understood what I was doing wrong. Bu stumbling on this announcement:

I finally understand that using yaml is not going to solve my problem.Since all my entities were discovered through the UI there is nothing in the yaml files… Except for the two scripts that I tried as a solution.
Setting up a simple automation through the UI finally does what I was looking for. Shouldn’t have sent that late night call for help :roll_eyes: