Harmony hub returning null on restart of HA

I noticed some oddness between harmony and HA recently and not sure if it’s due to a new TV but I had an automation that set the current activity on my virtual remote such as:

- alias: 'harmony family room setup after startup'
  initial_state: on
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service: input_select.select_option
      entity_id: input_select.harmonytvsource
      data_template:
        option: >
          {% if states.remote.familyroom_hub.attributes.current_activity == "PowerOff" %}
            PowerOff
          {% elif states.remote.familyroom_hub.attributes.current_activity == "XBox 360" %}
            XBox 360
          {% elif states.remote.familyroom_hub.attributes.current_activity == "Listen to Music" %}
            Listen to Music
          {% elif states.remote.familyroom_hub.attributes.current_activity == "NVidia Shield" %}
            NVidia Shield
          {% elif states.remote.familyroom_hub.attributes.current_activity == "Unknown" %}
            Select Activity
          {% endif %}

I have everything off right now and in my harmony config file I see for each activity the following values:

    "Activities": {
        "-1": "PowerOff",
        "45998071": "Watch Smart TV",
        "45998090": "XBox 360",
        "45998723": "Listen to Music",
        "45998747": "NVidia Shield"

However in the logs I get an error:

2020-11-23 08:01:47 WARNING (MainThread) [homeassistant.components.input_select] Invalid option: (possible options: Select Activity, XBox 360, NVidia Shield, Listen to Music, Watch Smart TV, PowerOff)

So I go OK, it’s trying to set blank or null. Odd as per the above activity and possible values it used to be “PowerOff”. So I look at the hub in HA and see:

image

So it does think it’s null and not PowerOff. Any idea why?

At startup the entity probably doesn’t exist and your if statement doesn’t catch that. Also, what is this for? Seems overkill unless you are trying to keep a activity list in sync with the device itself. FWIW, I went that route early on and it was not worth the work. Switching to template switches was a much better route. Anyways, add an else and use the state_attr method. Also, remove unknown as it’s not a possible activity.

your template modified:

        option: >
          {% if is_state_attr('remote.familyroom_hub', 'current_activity', 'PowerOff') %}
            PowerOff
          {% elif is_state_attr('remote.familyroom_hub', 'current_activity', "XBox 360") %}
            XBox 360
          {% elif is_state_attr('remote.familyroom_hub', 'current_activity', "Listen to Music") %}
            Listen to Music
          {% elif is_state_attr('remote.familyroom_hub', 'current_activity', "NVidia Shield") %}
            NVidia Shield
          {% else %}
            Select Activity
          {% endif %}

improved template where you never have to edit anything other than your input select’s options. I.e. if you add an activity, you don’t have to edit the template.

        option: >
          {%- set activity = state_attr('remote.familyroom_hub', 'current_activity' ) %}
          {%- set options = state_attr('input_select.harmonytvsource', 'options') %}
          {%- if activity in options %}
            {{ activity }}
          {%- else %}
            Select Activity
          {%- endif %}

Thanks Petro had already added in an else clause since writing this but good call on the Unknown one. I did look at your more “automatic” option and that made sense and was slick.

I’m basically using this in order to populate my virtual remote current activity pulldown when HA is rebooted. What I did find interesting is I have two copies of my remote as I for now always have a version I keep for any changes and the working on. Oddly they both populate, but only the left one populates the buttons for the current activity even though it shows the actual devices in green.

Whatever it is I copied the left one to the right and it’s populating now so something must be different that this change effects but who knows.

Seeing on my last reboot that it didn’t determine the current activity and later returned:

2020-11-23 12:48:01 ERROR (MainThread) [aioharmony.helpers] get_current_activity was not called due to mismatch in callback type.

So ended up using the Select an Activity.

So it didn’t work?

The change does work. I think the problem is HA like a lot of things in it, takes to long to load or connect. So like for example the below “may” be causing this but not sure:

2020-11-23 12:46:54 ERROR (MainThread) [aioharmony.harmonyclient] 192.168.0.54: Unable to determine if XMPP is enabled: [Errno 113] Connect call failed ('192.168.0.54', 5222)
2020-11-23 12:46:54 WARNING (MainThread) [homeassistant.components.harmony.remote] FamilyRoom-Hub: Unable to connect to HUB

Now the system is up and running, but looks like HA is lagging loading it. See this with this, Audi integration, Unifi, well lots of stuff.

I’d probably need to restart HA multiple times and see if it’s the same. Then change the code back and see how it reacts.

So what I can say is…

Now I no longer have errors in the log on startup but when I startup from reboot and the XBox activity is running it sits on the Select an Activity activity in the pulldown and if I restart thru HA and not a full VM restart it does come up with the right activity.