Harmony Hub Sleep Timer Automation - action syntax quirk (bug?)

Hi all,

I’m new to Home Assistant, and have been working on setting it up for my home over the last month. Overall it’s gone smoothly, and it’s been really impressive how easy it is to implement everything. But I hit a snag recently working on an automation around the Logitech Harmony Hub component, and I wanted to share it in case it’s a legitimate bug, or to help others who may encounter the same issue.

Context

In my bedroom I have a lamp with a Philips Hue bulb, plus a Harmony Hub remote for the TV. I wanted to set up a sleep timer so that I could watch TV while falling asleep, and after a specified amount of time, the TV and lamp would both turn off. I attempted to accomplish this by using the Timer component, a switch, and an automation:

Timer

timer:
  bedroom_sleep:
    name: Bedroom Sleep Timer
    icon: mdi:sleep

Sample Switch

# Note that timers can have custom durations, so it's possible to have
# multiple switches with different duration values

switch:
  platform: template
  switches:
    bedroom_sleep_30_minutes:
      friendly_name: 'Bedroom Sleep 30 Minutes'
      icon_template: mdi:sleep
      value_template: "{{
        is_state('timer.bedroom_sleep', 'active') and
        states.timer.bedroom_sleep.attributes.duration == '0:30:00' }}"
      turn_on:
        service: timer.start
        data:
          entity_id: timer.bedroom_sleep
          duration: '00:30:00'
      turn_off:
        service: timer.cancel
        data:
          entity_id: timer.bedroom_sleep

Automation

automation:
- alias: 'Bedroom TV Sleep Timer'
  id: bedroom_tv_sleep_timer
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bedroom_sleep
  action:
    # note the use of the 'data' key for all 3 elements here; this is how I
    # originally wrote it. Spoiler alert: this turned out to be the problem
    - service: remote.turn_off
      data:
        entity_id: remote.bedroom_harmony
    - service: light.turn_off
      data:
        entity_id: light.bedroom_lamp
    - service: notify.ios_my_device
      data:
        title: 'Sleep Timer Finished'
        message: 'Goodinght!'

Problem & Solution

When I first added the configuration above, the timer and switch worked as expected, but the automation never fired. Through troubleshooting I learned that the notification and light actions worked fine, but the Harmony remote action did not. Additionally, if the Harmony remote action was first in the list, then subsequent actions wouldn’t fire, and the Home Assistant log contained no clues as to why.

Update: I thought I had figured out the solution, but the problem persists in subsequent testing. I’m striking out the original hypothesis but preserving the text for continuity’s sake. Thanks to @pnbruckner for providing evidence to debunk it below.

Update 2: The problem was that the automation was somehow turned off. Once I turned it back on, everything worked as expected. Thanks again to @pnbruckner for the troubleshooting help.

After racking my brain for a while, and searching the forums for similar issues, I realized that the problem was rooted in a quirk of syntax. In the “action:” section of the automation, the service call for the Harmony remote needs to look like this:

    - service: remote.turn_off
      entity_id: remote.bedroom_harmony

Note the lack of a “data:” key above “entity_id”. If written as it is here, the service call succeeds and everything works fine. But if written as I originally wrote it above, there’s no indication of a syntax error, and the service call fails silently, taking the automation down with it.

Indeed, the Harmony Hub Remote documentation contains an example that shows the correct syntax, and I had overlooked that when I first wrote this. Still, the lack of consistency in the syntax strikes me as a bug. I would expect the action syntax for Harmony remote service calls to work if written the same as the others (i.e. with the data key) as in my original example above. Barring that, the incorrect syntax should at least trigger an error or warning instead of failing silently.

Can anyone back me up on this? If so I’ll happily file and issue on GitHub. I’d just like to get a second opinion before cluttering the HA GitHub issues.

Thanks!

I’m not sure your conclusion is correct. I’m pretty sure that it doesn’t matter (for any service) if you do:

- service: x.y
  data:
    entity_id: a.b

or

- service: x.y
  entity_id: a.b

Because before the service is actually called, the latter will effectively be automatically converted to the former. See:

Sigh, you’re right, and it looks like I posted this too soon. I thought had the automation working in testing last night, but I’m having no such luck with it today. Something is still preventing the automation from firing when the timer finishes, and I’m still not sure what. I’m going to keep troubleshooting, and I’ll try to update my post above (preserving as much of the original content as possible) to correct my assertions.

Do you have logger set to debug, and have you looked in home-assistant.log? When the timer finishes the event should be logged. Also, if the automation is triggering, then the steps of the action should be logged. It will even show if the automation is on. (Is the automation on? Have you tried manually triggering it to see if the actions run successfully?)

1 Like

@pnbruckner Thanks for the tips. It turns out the automation was likely turned off – TIL automations have “on/off” state, and won’t run if that state is not “on”. I must’ve somehow disabled it at some point. To prevent that from happening again, I added initial_state: 'on' to the automation config, and also added an Automations card to my UI so I can confirm that they’re running.

For the record, I also verified that either syntax for the service call is indeed valid, as you pointed out above. Thanks again!

The plot thickens: In further testing, I’ve noticed that occasionally the automation will turn off seemingly at random. At first I thought it was turning off after every time it ran (i.e. when the timer finished), but I can’t reproduce that consistently. I have yet to find a concrete reason why this might be happening, but I’ll update if I find one.

Has anyone seen this behavior before? I’m considering writing a script to ensure the automation is on each time the timer is started.

Again, look in the logs. If something is turning the script off, then there should be a call_service entry showing that, as well as a state_changed event for the automation with a new_state of off.

What I’ve seen in my logs is, all my automations start off when HA starts, then the ones that were on before restarting get turned on. But that happens very early. (Also I do not have initial_state for any of my automations.)