NHL API Custom Component - Track your favorite hockey team in Home Assistant!

Based on your configuration i would try,
sensor.bruins_home_team and sensor.bruins_away_team

You have the heading sensor defined twice under the template section of your configuration. Remove the second one. And your template sensors are named sensor.home_team and sensor.away_team.

I updated configuration.yanl to:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

#NHL API Sensor for Bruins... GO BRUINS!
sensor:
  - platform: nhl_api
    team_abbrev: BOS
    name: Bruins
    scan_interval: 6

template:
  - unique_id: away_team
    name: '{{ states.sensor.bruins.attributes.get("away_name", "") }}'
    state: '{{ states.sensor.bruins.attributes.get("away_score", "") }}'
    picture: '{{ states.sensor.bruins.attributes.get("away_logo", "") }}'
  - unique_id: home_team
    name: '{{ states.sensor.bruins.attributes.get("home_name", "") }}'
    state: '{{ states.sensor.bruins.attributes.get("home_score", "") }}'
    picture: '{{ states.sensor.bruins.attributes.get("home_logo", "") }}'

And still get the same error “Entity not available…”
I also tried with
sensor.home_team and sensor.bruins_away_team and sensor.bruins_home_team and sensor.away_team.

So after some trial and error, I discovered there were several errors in the error log, I continued to correct the code (following other examples) and finally got this configuration which didn’t cause any errors… but still it shows the entities are not available on the cards:


sensor:
  #ControlByWeb Generator Monitors
  #shop 468
  - platform: snmp
    accept_errors: true
    name: "Shop 468"
    unique_id: 2024468
    host: !secret shop468_IP
    port: !secret x410_port
    community: !secret x410_community
    baseoid: 1.3.6.1.4.1.30586.46.0.10.0
    unit_of_measurement: "Hours"
    version: 2c

  #NHL API Sensor for Bruins... GO BRUINS!
  - platform: nhl_api
    team_abbrev: BOS
    name: Bruins
    scan_interval: 6

template:
  - sensor:
    - unique_id: away_team
      name: '{{ states.sensor.bruins.attributes.get("away_name", "") }}'
      state: '{{ states.sensor.bruins.attributes.get("away_score", "") }}'
      picture: '{{ states.sensor.bruins.attributes.get("away_logo", "") }}'
  - sensor:
    - unique_id: home_team
      name: '{{ states.sensor.bruins.attributes.get("home_name", "") }}'
      state: '{{ states.sensor.bruins.attributes.get("home_score", "") }}'
      picture: '{{ states.sensor.bruins.attributes.get("home_logo", "") }}'


the configuration should be:

template:
  - sensor:
    - unique_id: away_team
      ...
    - unique_id: home_team
      ...

You only need to have one sensor heading under the template heading. I bet there’s an error in your log for invalid configuration for template with the sensor heading twice.

I changed the configuration to remove the second sensor… but its still not working. I feel like I have it character for character as per the examples and everyone’s recommendations, but its not working.

Here is what I changed it to:


template:
  - sensor:
      - unique_id: away_team
        name: '{{ states.sensor.bruins.attributes.get("away_name", "") }}'
        state: '{{ states.sensor.bruins.attributes.get("away_score", "") }}'
        picture: '{{ states.sensor.bruins.attributes.get("away_logo", "") }}'
      - unique_id: home_team
        name: '{{ states.sensor.bruins.attributes.get("home_name", "") }}'
        state: '{{ states.sensor.bruins.attributes.get("home_score", "") }}'
        picture: '{{ states.sensor.bruins.attributes.get("home_logo", "") }}'

Here is the whole configuration file for reference…

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
  #ControlByWeb Generator Monitors
  #shop 468
  - platform: snmp
    accept_errors: true
    name: "Shop 468"
    unique_id: 2024468
    host: !secret shop468_IP
    port: !secret x410_port
    community: !secret x410_community
    baseoid: 1.3.6.1.4.1.30586.46.0.10.0
    unit_of_measurement: "Hours"
    version: 2c

  #NHL API Sensor for Bruins... GO BRUINS!
  - platform: nhl_api
    team_abbrev: BOS
    name: Bruins
    scan_interval: 6

template:
  - sensor:
      - unique_id: away_team
        name: '{{ states.sensor.bruins.attributes.get("away_name", "") }}'
        state: '{{ states.sensor.bruins.attributes.get("away_score", "") }}'
        picture: '{{ states.sensor.bruins.attributes.get("away_logo", "") }}'
      - unique_id: home_team
        name: '{{ states.sensor.bruins.attributes.get("home_name", "") }}'
        state: '{{ states.sensor.bruins.attributes.get("home_score", "") }}'
        picture: '{{ states.sensor.bruins.attributes.get("home_logo", "") }}'

Here is the home-assistant-log:

2024-01-09 21:18:23.935 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 21:18:23.937 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration nhl_api which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 21:18:23.939 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration alexa_media which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 21:18:23.941 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration teamtracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 21:18:44.203 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 21:18:44.206 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 21:18:44.207 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 21:18:44.212 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation

And home-assistant-log-1:

2024-01-09 04:33:56.503 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 04:33:56.507 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration nhl_api which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 04:33:56.510 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration alexa_media which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 04:33:56.515 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration teamtracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-01-09 04:34:02.280 WARNING (Recorder) [homeassistant.components.recorder.util] The system could not validate that the sqlite3 database at //config/home-assistant_v2.db was shutdown cleanly
2024-01-09 04:34:02.482 WARNING (Recorder) [homeassistant.components.recorder.util] Ended unfinished session (id=18 from 2024-01-09 07:39:57.242405)
2024-01-09 04:34:27.960 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 04:34:27.962 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 04:34:27.965 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 04:34:27.967 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'custom_components.hacs.update.HacsRepositoryUpdateEntity'>) is using deprecated supported features values which will be removed in HA Core 2025.1. Instead it should use <UpdateEntityFeature.INSTALL|PROGRESS|RELEASE_NOTES: 21>, please create a bug report at https://github.com/hacs/integration/issues and reference https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation
2024-01-09 04:45:53.704 ERROR (MainThread) [homeassistant.components.roku.coordinator] Error fetching roku data: Invalid response from API: Timeout occurred while connecting to device
2024-01-09 11:13:26.111 WARNING (MainThread) [homeassistant.components.websocket_api.http.connection] [546970150080] from 127.0.0.1 (Mozilla/5.0 (Linux; Android 14; Pixel 6 Build/UQ1A.231205.015; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/120.0.6099.144 Mobile Safari/537.36 Home Assistant/2023.12.4-11898 (Android 14; Pixel 6)): Disconnected: Did not receive auth message within 10 seconds
2024-01-09 11:38:15.345 WARNING (MainThread) [homeassistant.components.websocket_api.http.connection] [546970312896] from 127.0.0.1 (Mozilla/5.0 (Linux; Android 14; Pixel 6 Build/UQ1A.231205.015; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/120.0.6099.144 Mobile Safari/537.36 Home Assistant/2023.12.4-11898 (Android 14; Pixel 6)): Disconnected: Did not receive auth message within 10 seconds
2024-01-09 14:46:34.444 ERROR (MainThread) [smart_meter_texas] Unknown meter reading status: FAILED
2024-01-09 14:46:34.445 ERROR (MainThread) [homeassistant.components.smart_meter_texas] Error fetching Smart Meter Texas data: Unknown meter status: FAILED
2024-01-09 19:52:08.935 ERROR (MainThread) [homeassistant.components.smart_meter_texas] Error requesting Smart Meter Texas data: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://smartmetertexas.com/api/ondemandread')

Ok I see what’s possibly going on. First, you have an indentation problem in the template/sensor section. move the 8 lines under the - sensor heading 2 spaces to the left. then, search your list of entities from the frontend for a sensor that says “sensor.vegas_golden_knights”. Second, what’s happening is as of right now (1/11) Boston is playing Vegas. The home_name attribute of your sensor.bruins sensor is populated with “Vegas Golden Knights”. This value is entered into the name: field for you template sensor. That means the name of the sensor is sensor.vegas_golden_knights and the name will change every game. the same is true for the away team sensor which should be sensor.boston_bruins right now and will change accordingly for their next game. The only way I have been able to get around this is doing the old template sensor format (you are currently using the new template format). See my config example below for the old template format. Notice how instead of the template: section, I’m creating a sensor of the platform template:

sensor:
  - platform: nhl_api
    team_abbrev: NSH
    name: 'Nashville'
  - platform: template
    sensors:
      away_team:
        friendly_name_template: '{{ state_attr("sensor.nashville", "away_name") }}'
        value_template: '{{ state_attr("sensor.nashville", "away_score") }}'
        entity_picture_template: '{{ state_attr("sensor.nashville", "away_logo") }}'
      home_team:
        friendly_name_template: '{{ state_attr("sensor.nashville", "home_name") }}'
        value_template: '{{ state_attr("sensor.nashville", "home_score") }}'
        entity_picture_template: '{{ state_attr("sensor.nashville", "home_logo") }}'

now, my sensor names will always be sensor.away_team and sensor.home_team. The friendly name is what changes every game but the actual sensor id stays the same. Additionally, using state_attr("...") allows the sensor to properly handle the unavailable state in the event the template renders whenever the sensor.nashville doesn’t exist like on HA startup and not everything is loaded, or in the off season, or if the integration cannot fetch data.

Thank you so much for the help with this, that got it working… at least I got one victory tonight… too bad the Bruins didn’t…
Screen Shot 2024-01-12 at 12.14.28 AM

1 Like

Currently I’ve got an issue with the API showing the incorrect data for tonights game. It’s stuck showing the Oilers as OFF after Colorado last night. Tried restarting etc. no change.

Happening on two seperate HA installs on very different machines.

Found some logs:

Update for sensor.oilers fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 951, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1270, in async_device_update
    await hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nhl_api/sensor.py", line 204, in update
    self.set_state()
  File "/config/custom_components/nhl_api/sensor.py", line 164, in set_state
    all_attr = self.get_game_data()[0]
               ^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nhl_api/sensor.py", line 108, in get_game_data
    if Plays(game_id).scoring_info() is not None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/.local/lib/python3.12/site-packages/pynhl/classes.py", line 298, in scoring_info
    event_type = plays[i]["typeDescKey"]
                 ~~~~~^^^
IndexError: list index out of range

I saw you submitted this issue on Github as well. I will see if I can get to the bottom of it. Usually this happens when the NHL tweaks their API endpoints.

Is there any way to create a helper template as binary sensor for when a goal is scored? I tried the below but the state doesn’t change.

platform: event
event_type: nhl_goal
event_data:
  goal_tracked_team: true

It just changes the state to off but nothing in the logbook for it turning on. I haven’t tested it live yet, but should it work? I can test again for tomorrow’s game.

Great work by the way, the reaction is instant. Much better than team tracker.

You can create an automation using the event as a trigger to turn the binary sensor on, add a delay and then turn it off.

What are you trying to accomplish with the binary sensor though?

Please forgive this newbie question but can someone provide an example of how to add ‘next_game_datetime’ to configuration.yaml for the NHL API? I have the following in my current config as a placeholder:

sensor:

  • platform: nhl_api
    team_abbrev: BOS
    name: Bruins
    scan_interval: 60
    template:
  • sensor:
    • unique_id: away_team
      name: >
      {{state_attr(‘sensor.bruins’,‘away_name’)}}
      state: >
      {{state_attr(‘sensor.bruins’, ‘away_score’)}}
      picture: >
      {{state_attr(‘sensor.bruins’,‘away_logo’) }}
  • sensor:
    • unique_id: home_team
      name: >
      {{state_attr(‘sensor.bruins’,‘home_name’)}}
      state: >
      {{state_attr(‘sensor.bruins’, ‘home_score’)}}
      picture: >
      {{state_attr(‘sensor.bruins’,‘home_logo’) }}

next_game_datetime should already be exposed as an attribute of your sensor.bruins sensor. Make sure you restart Home Assistant after adding the sensor to your config.

Thank you. I hadn’t created a sensor for next_game_datetime, which I think was my problem. My config looks as follows. I hope I’m doing this correctly.

sensor:

  • platform: nhl_api
    team_abbrev: BOS
    name: Bruins
    scan_interval: 60

template:

  • sensor:
    • unique_id: away_team
      name: >
      {{state_attr(‘sensor.bruins’,‘away_name’)}}
      state: >
      {{state_attr(‘sensor.bruins’, ‘away_score’)}}
      picture: >
      {{state_attr(‘sensor.bruins’,‘away_logo’) }}
  • sensor:
    • unique_id: home_team
      name: >
      {{state_attr(‘sensor.bruins’,‘home_name’)}}
      state: >
      {{state_attr(‘sensor.bruins’, ‘home_score’)}}
      picture: >
      {{state_attr(‘sensor.bruins’,‘home_logo’) }}
  • sensor:
    • unique_id: next_game
      state: >
      {{state_attr(‘sensor.bruins’,‘next_game_datetime’)}}

Yes, that should work if you’re looking to create a distinct sensor that will display the next game’s date and time as a state.

Jay,

I was looking for a way to display the next game date/time for an area I named “Hockey” but I don’t know where to read up on how to do this.

Thank you for your feedback.

Hello Jay,
First fantastic work to bring those nhl updates. I’m very new with Home-Assistant so I’m discovering and I have a question for you regarding updates of the API after a reboot.

So during a game, I had to restart Home Assistant (was doing some tweaks and modifications live to test) and I realized that the card updates the score right after the reboot (at the initialization I presume) and doesn’t update anymore until the next game.

Have you ever encounter this? Or did I misconfigure something?

Thanks for your help,
Cheers!

This has to do with how the sensor is programmed to update. Fixing it would require a full rewrite which isn’t really in the cards for me right now. I am accepting PRs if someone would like to give it a go.

1 Like

Once set up, the sensor will have an attribute called next_game_datetime that you can use.