Automation Loop with Harmony remote

Hey guys,

I have an interesting situation and was hoping someone could help out here. I use a harmony remote but also use voice controls and a chromecast. My wife also uses the TV and the cable remote. The issue I was having was that the harmony remote would go out of sync due to all the other HDMI CEC (alexa for fire tv and chromecast controls) and my wife using other remotes. I got into Home Assistant to try to solve this issue.

In Home Assistant, I have created activities to set the harmony remote’s activity based on a state change of the TV’s (LG OLED C8) input e.g. if the TV input is changed to Xbox, then change the harmony’s remote activity to “Play Xbox”. This way, the Harmony remote doesn’t go out of sync. If I tell Alexa to play Ozark, the TV’s input changes to “Fire TV” and HomeAssistant runs an automation to turn on the Harmony remote’s “Watch Fire TV” activity.

It almost works fine. However, the BIG issue is I run into loops. As an example, if the TV’s last input before it got turned off was the Xbox and I use Alexa to play a show on the Fire TV,

  1. The TV first turns on on its last input (“Xbox”)
  2. The TV then switches to the “Fire TV”
  3. The initial automation for the Xbox input then runs and turns on the “Play Xbox” activity on Harmony remote which switches the TV back to the Xbox input
  4. The automation for the Fire TV input runs and turns on the “Watch Fire TV” activity on Harmony remote which switches the TV back to the Fire TV input
  5. Steps 3 and 4 continue indefinitely

Also, the same effect occurs when we switch inputs between the time the automation is triggered and when it actually runs. I added delay conditions to the “trigger” component of the automation try to remedy this i.e. only run automation if TV has been on input for x seconds. However, that has 2 issues: a) if input is switched just after the delay but before automation is done running, the loop still occurs; b) if delay is too long, i can’t use harmony remote for a while till automation triggers.

The big issue is really that there’s a lag between when the automation is triggered and when it’s run. Does anyone have a better way to prevent this loop? I’m trying to prevent home assistant from running an automation if the TV input has changed from the input that triggered the automation.

Thanks

Actually, I just added a condition that made sure the TV’s input was still on the same state that caused the trigger and that might have fixed the issues. It’s performed well so far *fingers crossed

The method above didn’t fix the issue. Please I’d love to hear suggestions if anyone has any.

Essentially, the problem is that during the time it takes for the automation (harmony activity) to run, the TV input might have changed and that ends up causing a loop. I thought about adding a condition in the “action” part of the automation to ensure the TV input is still the same input that caused the trigger before the trigger is completed but thinking about it, that is likely to also have issues. If it says it’s the same input but the input gets changed right after the condition is validated, I’d think the same issue would occur.

I solved this issue by programming the harmony remote to do everything, using voice commands to access the activities instead of individual devices, and hiding all the other remotes from my wife forcing her to learn the extremely easy to use harmony remote. We pretty much only use voice commands at this point, but it’s always an activity. And my wife now uses and learned the remote.

Thanks but there are some really cool voice commands that the Harmony can’t handle. Perfect example is “Alexa, watch Friends” and having the TV switch to the Fire TV input and start playing Friends where we stopped. Essentially, any form of searching is where the Harmony remote would be limited.

My approach is very similar to what you’re doing only with CEC disabled.

I don’t have a Fire TV, but have Chromecast Video and Chromecast Audio.

So I have an automation that notices that Chromecast has gone from idle to playing or whatever and then it starts the corresponding Harmony Activity.

I use the same approach without the Harmony in another room where the Chromecast Audio turns on an Onkyo Stereo and sets the correct input using an automation.

There is no chance for a loop since it doesn’t start anything that can cascade trigger – sometimes there is a bit of a delay since the Harmony can be slow sometimes.

Not sure that you can catch the FireTV state changes like with Chromecast, but if so then you might be able to do the same thing.

Also I suppose if you are making use of ARC then my approach won’t work since ARC requires CEC enabled as far I know.

Thanks. I think I found Home Assistant from a post by you on Reddit actually detailing what you were doing with Chromecast!

I’m doing the exact same thing with the Chromecast actually. The Fire TV has similar states but they’re very unreliable. Most of the time, it’s actually in an “unknown” state.

I do use ARC and my TV has lossy Atmos with ARC so don’t plan to disable CEC.

So I noticed that the delay I was using was applied to the “trigger” i.e. trigger: - for: 8s. Now I’m thinking I should instead do
trigger:

action:

  • delay: 10s
  • condition:
    for: 8s
  • service: start Harmony activity

This way, it would wait about 10s after the input changes to see if the input has been on the new input for up to 8s before turning in the Harmony activity. I think this might prevent race conditions or at least, reduce the occurrence.

Wdyt?

Right, thats great. I similar things. I have a routine that starts the harmony activity and I use other automation techniques via the harmony remote & home assistant that resume at that state.

I also use similar automations as @a3a, but with my receiver and harmony commands. Even with my current setup, the source is sometimes incorrectly selected and my automations correct that. The big takeaway here is that I always have the harmony activity start first. Otherwise you will get into an endless loop.

I guess the first thing I need to ask is, how are you setting your harmony activities in HA?

Sorry, are you saying you’re able to replicate this in some way that also ensures the harmony activity starts first? If you are, I’d love to know how you have it implemented.

My harmony activities are gotten from the conf file that homeassistant downloads when connected to the harmony device

Well yes, but what are you using to detect the change in activity? an automation? a series of switches? A input select? I have an idea that I want to throw by but I need a little more input. If you aren’t detecting at all, then even better.

I’m using a state change in the input of the TV. I made the input a virtual device and every time the input changes, it starts an activity. An example is if the input state changes to HDMI2, it starts the “Watch For TV” activity. The only exception to this is with the Chromecast. For that, I’m using a state change to “playing” of the Chromecast itself.

Then with that automation check the activity status. If the activity status is equal to the one that you are changing to, don’t fire it.

You’ll probably have to do the same for your corrections. I.e. when your activity is set and you verify that the input is correct. Don’t set it if it is correct.

condition: template
value_template: "{{ not is_state_attr('remote.living_room','TV') }}"

Let me share what I currently do. Here’s an example with changing to the Xbox input and calling the “Watch Xbox” activity.

- id: '1540083738055'
  alias: Harmony Xbox
  trigger:
  - entity_id: sensor.lg_tv_source
    platform: state
    to: Xbox
    for: 00:00:08
  condition:
  - condition: state
    entity_id: sensor.lg_tv_source
    state: Xbox
  action:
  - data:
      activity: 30320764
    entity_id: remote.livingroom_hub
    service: remote.turn_on

So essentially, whenever there’s an input change to “Xbox”, if the input is still on “Xbox” 8s later, it turns on the “Watch Xbox” activity.

I’ve also toyed with:

- id: '1540083738053'
  alias: Harmony Tivo
  trigger:
  - entity_id: sensor.lg_tv_source
    platform: state
    to: Tivo
  action:
  - delay: 00:00:08
  - condition: state
    entity_id: sensor.lg_tv_source
    state: Tivo
    for: 00:00:08
  - data:
      activity: 30320584
    entity_id: remote.livingroom_hub
    service: remote.turn_on

Seems I’m doing the same thing. The issue is that if the TV’s input is changed after 8s but before the “Watch Xbox” activity is completed, I get into the loop. As an example, if everything is off and I tell Alexa to “Watch Bosch”,

  1. My Fire TV would turn the TV on through HDMI-CEC
  2. Initially, the TV’s input would be on whatever the last input was (let’s imagine that’s TiVo)
  3. If it takes more than 8s to switch to the Fire TV’s input through but less than the time the harmony activity is completed:
    a) The “Harmony Tivo” HA trigger would activate
    b) The TV’s input would switch to “Fire TV Input”
    c) The “Watch Tivo” Harmony activity would then complete and switch the TV back to the Tivo input
    d) The “Harmony Fire TV” HA trigger would then activate (this is really because it takes Harmony a while to complete especially after starting from power off)
    e) The “Watch Fire TV” Harmony activity would then complete and switch the TV back to the Fire TV input
    f) Once again, if it takes more than 8s for Harmony to switch to “Fire TV” input, “Harmony Tivo” HA would trigger and say, we were on Tivo for more than 8s
    g) “Watch Tivo” Harmony activity would run and switch back to the “Tivo” input

Typing it all out, it does appear the loop is caused because the delay I’m giving isn’t enough time for the Harmony Activity to get completed. Maybe if I made it 20s, I won’t run into this loop. Of course, I could still have issues of someone picking up the remote and changing the input right after 20s but before the harmony activity has been completed so the input of the TV changing back on them to the previous input but that’d be a less frequent issue and shouldn’t result in a loop.

Ok, so you didnt’ name your activity

- id: '1540083738055'
  alias: Harmony Xbox
  trigger:
  - entity_id: sensor.lg_tv_source
    platform: state
    to: Xbox
    for: 00:00:08
  condition:
  - condition: state
    entity_id: sensor.lg_tv_source
    state: Xbox
  - condition: template
    value_template: "{{ not is_state_attr('remote.living_room','activity','30320764') }}"
  action:
  - data:
      activity: 30320764
    entity_id: remote.livingroom_hub
    service: remote.turn_on

Thanks. So this would ensure that before it changes to an activity, it’s not already on that activity. Harmony has a built-in check for that so I’m not sure it makes a difference i.e. if you call “Watch Tivo” when the Harmony is already on “Watch Tivo”, the Harmony remote simply ignores the request.