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

Hi all,

My project started to gain some traction on Reddit so I thought I would share with the Home Assistant Community at large.

In essence, it is a custom sensor that allows you to track your favorite NHL team(s) in the front end and/or use the information exposed to trigger an automation. It uses the NHL’s undocumented API so all the information comes straight from the source. Want to be notified when the Habs game is about to start? Easy. Want that LED strip around your TV to flash red when the Blues score? Done. Want the Leafs to win the cup? Well… can’t help you there. :face_with_hand_over_mouth:

The custom component is available for installation through HACS. Manual installation is of course also possible. Check it out here!

I’m currently working on firing a custom event when a goal is scored so that automations can be triggered even more effortlessly. As of version 0.5.0, you can use the nhl_goal event to trigger automations! Looking forward to your feedback!

11 Likes

I’m curious what kind of delay there might be in their API for updates when goals are scored. I can only imagine the lightbulbs start going crazy blinking at a random time because a goal finally updated via API.

After extensive testing, it is usually immediate. For best results, you can set scan_interval to 1 which will poll the API every second. Then, if your automations get triggered early because of stream/broadcast delay, you can adjust the delay in your automations.

Disclaimer: Polling the API is not recommended if running off an SD card or if you have an extremely low data cap with your ISP. Objectively, the sensor does not consume a lot of data but this will definitely add up with a high frequency of updates.

1 Like

@jayblackedout I was wondering where to find you to thank you and call you a wonderful human being for making this work so well. Thank you, you wonderful human being.

When are you doing the PR on the main HA to make this a permanent integration?

2 Likes

@databoy2k Haha, that’s very kind of you. There’s a few things I want to work out before submitting the PR. Hopefully I’ll manage to get everything up and running before Christmas and will submit then.

1 Like

Hi @jayblackedout thank you for putting this together, I was waiting of the nhl_goal feature (which is huge) so now I’m incorporating this into my setup. I’ve installed through HACS and added this to my configuration file but its throwing this error: Platform error sensor.nhl_api - No module named 'pynhl'

sensor:
  - platform: nhl_api
    team_id: 30
    name: Wild

@dusing I moved the API calls to a third-party PyPI package I created to be in line with HASS best practices. This probably means that the package installation failed through PIP. Have you tried restarting Home Assistant?

Hi @jayblackedout it worked after a reinstall. I’ve got it working now, just need to add a delay because its so good!

1 Like

@dusing Haha, I have the same issue. Found out that my broadcast is a whole 2 minutes behind the API updates.

This is very cool, I wish you were into the NFL as much as the NHL I could see a million used for this kind of automation in sports in general.

Best of luck moving forward!

3 Likes

This is cool. I’m trying to get the a goal horn mp3 in an automation using this from your github

- id: predators_score
  alias: predators_score
  initial_state: true
  trigger:
    platform: template
    value_template: "{{ states.sensor.away_team.state |int > states.numeric_input.away_score |int or 
states.sensor.home_team.state |int > states.numeric_input.home_score |int }}"
  condition:
    condition: template
    value_template: "{{ state_attr('sensor.nhl_sensor', 'goal_tracked_team') }}"
  action:
  - service: script.home_wave
    data:
      delay: 00:00:25
      wave: "goal-horns.mp3"
      home_entity: media_player.living_room_speaker

It’s complaining about the script.home_wave in my log. Do you have the code for this service?

@Corey_Maxim Maybe I’ll feel adventurous one day. I used to be way more into the NFL in college.

@gkron I’ll get in touch with the guy who shared these automations and will get back to you. I highly recommend you use the new nhl_goal event as a trigger though. You can find the yaml for that in the very first automation example.

1 Like

Thanks for the good work, well done write up.
Anyone figure out a way to trigger an automation based on whether it is a win or loss. I get colored lights to flash when there is a score but would like a game end win vs game end loss notification.

1 Like

Should look like this, correct? Automation is in a automations.yaml. Animation seems to fire just fine based on the tracked team goal, however the action does not fire. Manually triggering the service in the dev tools to make sure it work, and it seems to be just fine.

Not sure what im missing here.

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    service: media_player.play_media
    data:
      delay: 00:00:01
      entity_id: media_player.living_room
      media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
      media_content_type: 'audio/mp3'

@SirRobin I will add that in a future update. Be sure to star the GitHub repo or check back here for updates.

@THland83 Question to help troubleshoot. When you say you trigger the event in the dev tools, do you mean the actual event or the automation? Also, can you paste what you have in configuration.yaml to set up the sensor?

1 Like

@jayblackedout

Configuration.yaml

input_number:
  away_score:
    min: 0
    max: 100
  home_score:
    min: 0
    max: 100

sensor: !include sensors.yaml

- platform: nhl_api
  team_id: "25"
  name: Dallas Stars
  scan_interval: 1
  
- platform: template
  sensors:
     away_team:
       friendly_name_template: '{{ states.sensor.dallas_stars.attributes.away_name }}'
       value_template: '{{ states.sensor.dallas_stars.attributes.away_score }}'
       entity_picture_template: '{{ states.sensor.dallas_stars.attributes.away_logo }}'
     home_team:
       friendly_name_template: '{{ states.sensor.dallas_stars.attributes.home_name }}'
       value_template: '{{ states.sensor.dallas_stars.attributes.home_score }}'
       entity_picture_template: '{{ states.sensor.dallas_stars.attributes.home_logo }}'

Automation.yaml

- id: reset_score
  alias: reset_score
  initial_state: true
  trigger:
    platform: state
    entity_id: sensor.dallas_stars
    from: "Scheduled"
    to: "Pre-Game"
  action:
  - service: input_number.set_value
    entity_id: input_number.home_score
    data:
      value: 0
  - service: input_number.set_value
    entity_id: input_number.away_score
    data:
      value: 0


- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    service: media_player.play_media
    data:
      delay: 00:00:01
      entity_id: media_player.living_room
      media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
      media_content_type: 'audio/mp3'

If i trigger the automation from the dev tools, i can see it successfully trigger, but the action never happens.

i also know that the following works by manually calling the service using the dev tools.

      entity_id: media_player.living_room
      media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
      media_content_type: 'audio/mp3'

@THland83

I don’t think you can pass delay as a data attribute. Try this:

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    - delay: '1'
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
        media_content_type: 'audio/mp3'

You also don’t need the 2 input_number and the automation to reset their values if you’re using only the nhl_goal event to trigger the automation.

I’ve found it to be really quick. I’ll be watching a game casted to my tv and my goal song starts playing well before the goal is actually scored. Had to turn that automation off to avoid spoilers lol.

2 Likes

@spitfire1337 Add a delay to your automations. The event will fire as soon as the API updates and streams are not exactly real-time.

@jayblackedout thank you for this wonderful component. I’m using it to create personalized alexa alerts for when I’m home but unable to watch the games.

1 Like