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

- id: final_score
  alias: final_score
  initial_state: true
  trigger:
    platform: state
    entity_id: sensor.nhl_hockey
    from: "Game Over"
    to: "Final"
  action:
  - service: notify.alexa_media
    data:
      target: 
        - "Living room Dot"
      message: "Final Score: {{ state_attr( 'sensor.away_team', 'friendly_name') }} {{ states('sensor.away_team') }},, {{ state_attr( 'sensor.home_team', 'friendly_name') }}{{ states('sensor.home_team') }} !"
      data: 
        type: "tts"

This code copied doesn’t work for me as I don’t have a entity_id: sensor.nhl_hockey

What am I missing??
Do I have to change the entity_id: sensor.nhl_hockey to something like this for every team I follow:
entity_id: sensor.nhl_winnipeg_jets

My config is:

  - platform: nhl_api
    team_id: 52
    name: "NHL Winnipeg Jets"
    scan_interval: 1

      nhl_winnipeg_jets_away_team: 
        entity_picture_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.away_logo }}"
        friendly_name_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.away_name }}"
        value_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.away_score }}"
      nhl_winnipeg_jets_home_team: 
        entity_picture_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.home_logo }}"
        friendly_name_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.home_name }}"
        value_template: "{{ states.sensor.nhl_winnipeg_jets.attributes.home_score }}"

Scores are announced, my lovelace reports correctly but the “Final Score” automation does not work.

Because you used the name configuration option for your sensor. your sensor is called sensor.nhl_winnipeg_jets.

Thanks, So if I wanted to have the final score reported on all the teams I follow I would have to make multiple “final score” automations each with the specified team name sensor?

Yes you would need a “base” nhl sensor for each team id, and a home and away team sensor for each team. Through template conditions and actions or the choose statement in the actions you could probably get away with a single game over automation.

Love this stuff!

1 Like

Glad you like it!

1 Like

Would love to put up a goal horn that sounds when the Rangers score, but not sure if it would be that popular in Sweden with a 6 hour time difference… :wink:

Just set it up to only go off when Zibanejad scores and I’m sure they’ll forgive you.

1 Like

So I never managed to get this to work… Goal is to create an automation that triggers when your team’s game is over and it’s a win… Any thoughts on what I may have wrong here?

- id: '1616005296131'
  alias: 'NHL: Game over! Bruins win!'
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.nhl_sensor
    attribute: game_state
    from: In Progress - Critical
    to: Game Over
  condition:
  - condition: or
    conditions:
    - condition: and
      conditions:
      - condition: template
        value_template: "{{ states('sensor.home_team') > states('sensor.away_team') }}"
      - condition: template
        value_template: "{{ is_state_attr('sensor.nhl_sensor', 'home_id', 6) }}"
    - condition: and
      conditions:
      - condition: template
        value_template: "{{ states('sensor.away_team') > states(sensor.home_team)) }}"
      - condition: template
        value_template: "{{ is_state_attr('sensor.nhl_sensor', 'away_id', 6) }}"
  action:
  - service: notify.mobile_app_pixel_3
    data:
      message: Bruins win! Play dirty water!
  - service: media_player.volume_set
    data:
      volume_level: 0.3
    entity_id: media_player.downstairs
  - service: media_player.play_media
    data:
      media_content_id: media-source://media_source/local/bruinswin.mp3
      media_content_type: music
    entity_id: media_player.downstairs
  mode: single

That looks right…

Are there any errors in your log about this automation? If you manually trigger the automation actions, do the actions execute? Keep in mind manually executing an automation bypasses the conditions, so we’re just testing the actions at this point. If the actions actually complete, then the issue is going to be either the trigger or conditions. Try wrapping quotes around your trigger to state, to: "Game Over". You may not need the from: state so you could remove that for troubleshooting.

Also, as another troubleshooting tool, the latest version of HA (2021.4.X) introduced an automation troubleshooter. I have yet to update and check it out, but it might be helpful in this case.

The actions work fine when tested. When I reload this automation, i get an error:

Logger: homeassistant.config
Source: config.py:437

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘condition’][0][‘conditions’][1][‘conditions’][0][‘value_template’]. Got None. (See /config/configuration.yaml, line 10).

My configuration.yaml line 10 is group: !include groups.yaml and the only thing I have in my groups.yaml is a grouping of attic lights. I’m so confused.

I’ll have a second look at the automation in a bit. What’s on line 9 of your configuration? Sometimes that error just states where the validation errors out but it actually broke on the line just before.

Found it. In your second set of “and” conditions, you have an extra closing parentheses ) towards the end. The error was actually useful, it said it found an unexpected ) and sure enough it did, just not in the spot it said it was.

Thanks, that solved the error! Now just need to wait for a win :slight_smile:

This finally worked! Although it seems the game state doesn’t change to “Final” for 10-15 mins or so after the game is over. I see the following definitions for the states:

Game Over The scheduled game has recently ended.
Final The scheduled game is over and the score is final.

Any reason not to consider the score as final when state is “Game Over”?

The states are driven by the API. My component just feeds them into Home Assistant. I assume the NHL uses the slack time between “Game Over” and “Final” to make any last minute adjustments to the linescore for the official records. You could just base your automation around the “Game Over” state instead of “Final”.

Final is determined by the NHL and only when all stats for shots, blocks, assists, goals, etc are recorded. My automations are based on game over and it’s fairly close to immediate.

Thanks for this absolutely fantastic component! Is there any possibility of retrieving the goal scorers name? The idea would be to use it with a TSS to announce the goal scorer, or potentially pre-recorded audio files that could also be sent to a speaker depending on who scored.

Thanks. Glad you’re enjoying it! The last_goal sensor attribute is exactly what you are looking for. You can have your speaker announce it through TTS.

1 Like

Hi, I just integrated your awesome NHL_API component, the event triggers anytime my followed team “or” the opponent team scores. Is there a way to fire the goal event only when my followed team scores?
I am using the HA automation trigger but not the action, I hooked up the HA trigger in Nodered and then I send a cast to google and turn on a TPLink plug.

This is my configuration.yaml:

sensor:
  - platform: nhl_api
    team_id: '8'

I tried the team_id: with double quotes “8” single quotes ‘8’ and no quotes 8, the event always fired on both teams, not that I had a huge chance to test it looking at the results 5-1 for Tampa Bay, so I thought to post the question here to be sure I have it right.

My automations.yaml:

- alias: 'Montreal Goal Announcement'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "8"
      goal_team_id: "8"
  action:

I tried with or without the “goal_team_id:” same result, when Tampa Bay scored I was getting the Horn and Stroboscopic light triggered.

I was looking for a way to filter the team ID that scored from Nodered that is why I added the “goal_team_id:”
Any suggestion?

This is what my NodeRed flow looks like:

Edit: I may have found the issue;

My NHL Events Node in nodered:

I will try these new settings, maybe I’ll be able to get the team_id and goal_team_id in nodered then it would be easy to filter:

This is where I found the initial NodeRed flow:
dennyreiter/GoalHorn: Node Red flow to play team goal horns and turn on a light when a goal is scored (github.com)

Hopefully I will be able to test this tomorrow night… :wink:

Thank you in advance.

Edit: July 1st 2021

It worked!!! whooohooo… and yup bhooooooo the Habs lost another game.

The addition of “msg.event_data” exposed the team_id to NodeRed then I just added a switch node and whoila’ now you can trigger anything you want when the Habs or the team of your choice scores also from NodeRed.

Switch Node:
Switch node

NodeRed Flow:

I hope this helps, and thank you again @jayblackedout !!!

1 Like