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

Thanks :slight_smile: I appreciate it.

I am not sure what is going on, but over the past couple of weeks, my scores card has gone on the fritz. It was displaying the upcoming first game of the season, but now, I’m getting the game date/time, but the away and home teams are showing as ‘unknown’.
I can see that my sensor has the away_name and home_name correctly attributed. I can also pull the template out of my configuration and put it in the templates page in the developer tools and see that it reads the team names as expected.

Is this typical for pre-season functionality? I’m confused because it was working fine a couple of weeks ago.

Here the snippet of my configuration:

sensor:
  #nhl sensors
  - platform: nhl_api
    team_id: 54
    name: VGK NHL
#    scan_interval: 1
  - platform: template
    sensors:
      vgk_away_team:
        friendly_name_template: '{{ states.sensor.vgk_nhl.attributes.away_name }}'
        value_template: '{{ states.sensor.vgk_nhl.attributes.away_score }}'
        entity_picture_template: '{{ states.sensor.vgk_nhl.attributes.away_logo }}'
      vgk_home_team:
        friendly_name_template: '{{ states.sensor.vgk_nhl.attributes.home_name }}'
        value_template: '{{ states.sensor.vgk_nhl.attributes.home_score }}'
        entity_picture_template: '{{ states.sensor.vgk_nhl.attributes.home_logo }}'

I had this issue too. It has to do with the template sensors being rendered or initialized before the NHL integration is initialized. It also has to do with the way the template gathers its data. If you go to the configuration panel from the frontend and under server controls/general you reload the template section you’ll see the sensors update and show correctly, but the next time HA gets restarted you have the same issue. The fix is to change the templates to use the other way of getting the state of an object instead of direct. The other way cleanly handles if a state is invalid or unknown. Instead of using {{ states.sensor.blah.attribute.state }} it gets changed to {{ state_attr('sensor.blah', 'attribute') }}

I had a hunch that was what was causing it. I was unaware of the other method of checking the attribute. This worked like a charm! Thank you!

Has anyone transitioned this from the legacy template to new template config? friendly_name_template is no longer accepted so I’m not sure what to do with those lines.

There’s a discussion here. I haven’t tried the solution mentioned in the thread but maybe it can point you in the right direction.

in the migration you can simply use the new name: field, and keep the old template there.

so

sensor:

  - platform: template
    sensors:
      vgk_away_team:
        friendly_name_template: '{{ states.sensor.vgk_nhl.attributes.away_name }}'
        value_template: '{{ states.sensor.vgk_nhl.attributes.away_score }}'
        entity_picture_template: '{{ states.sensor.vgk_nhl.attributes.away_logo }}'

will become:

template:

  - sensor:
      - unique_id: vgk_away_team
        name: '{{ states.sensor.vgk_nhl.attributes.away_name }}'
        state: '{{ states.sensor.vgk_nhl.attributes.away_score }}'
        picture: '{{ states.sensor.vgk_nhl.attributes.away_logo }}'

you’d need to check the object_id of this sensor after reloading, because it will be using the return of the name template, and you’d probably want it to use the ‘old’ object_id, which I’ve set as unique_id

Id recommend wring that as:

template:

  - sensor:
      - unique_id: vgk_away_team
        name: >
          {{state_attr('sensor.vgk_nhl','away_name')}}
        state: >
          {{state_attr('sensor.vgk_nhl','away_score')}}
        picture: >
          {{state_attr('sensor.vgk_nhl','away_logo')}}

because of these considerations

1 Like

Thanks so much for this! I was on the right track but missing the second part of this to fix the name template itself.

Thanks @Mariusthvdb !

I updated the documentation in the repository to address this issue.

Would any of you like the sensor to show the league logo as the entity picture instead of the material design hockey-sticks icon?

Would look something like this:
image

Also, I’d love to collaborate with a frontend dev to create a dedicated scorecard lovelace card for this. Hit me up if interested.

5 Likes

I’d be glad to help test! Thanks for your efforts!

I’d definitely like the NHL logo and I definitely like a Lovelace card but I have zero to contribute.

I created a sensor for the official game time that grabs from the attribute data. Could probably use that for creating a scoreboard.

official_time:
  friendly_name: "Official Time"
  value_template: "{{ state_attr('sensor.nhl_sensor', 'time_remaining') }} - {{ state_attr('sensor.nhl_sensor', 'current_period') }}"
  icon_template: mdi:clock-outline
1 Like

+1 to show the league logo as the entity picture!

Has anyone been able to get it working with multiple teams since the update? I have one team working fine, but before i could track multiple teams. My config is below. Thanks in advance

    #NHL Sensor#
sensor:
  - platform: nhl_api
    name: nhl_sensor_mtl
    team_id: 8

  - platform: nhl_api
    name: nhl_sensor_tor
    team_id: 10

template:

  - sensor:
      - unique_id: mtl_away_team
        name: >
          {{state_attr('sensor.nhl_sensor_mtl','away_name')}}
        state: >
          {{state_attr('sensor.nhl_sensor_mtl','away_score')}}
        picture: >
          {{state_attr('sensor.nhl_sensor_mtl','away_logo')}}

  - sensor:
      - unique_id: mtl_home_team
        name: >
          {{state_attr('sensor.nhl_sensor_mtl','home_name')}}
        state: >
          {{state_attr('sensor.nhl_sensor_mtl','home_score')}}
        picture: >
          {{state_attr('sensor.nhl_sensor_mtl','home_logo')}}

  - sensor:
      - unique_id: tor_away_team
        name: >
          {{state_attr('sensor.nhl_sensor_tor','away_name')}}
        state: >
          {{state_attr('sensor.nhl_sensor_tor','away_score')}}
        picture: >
          {{state_attr('sensor.nhl_sensor_tor','away_logo')}}

  - sensor:
      - unique_id: tor_home_team
        name: >
          {{state_attr('sensor.nhl_sensor_tor','home_name')}}
        state: >
          {{state_attr('sensor.nhl_sensor_tor','home_score')}}
        picture: >
          {{state_attr('sensor.nhl_sensor_tor','home_logo')}}

What’s not working? Do the 2 sensors render?

Try this:

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

Sorry should’ve been more clear. The trackers for montreal work great but the toronto ones do not show up as entities. Ill give your solution a try

@michaeldooley97 I just set mine up for two teams today with the Kings and Ducks using your code and it seems to be working fine for both teams. For whatever reason, my instance isn’t liking @jayblackedout’s template code (99% sure it’s a user error). But both teams and the respective visitors for the scheduled games this week are showing up fine.

Good to know, thanks for testing! I might just start over and set it up again

1 Like

@michaeldooley97 absolutely! It’s worked great for both games (Ducks and Kings) the last two nights for how you have it formatted. The challenge, like what you’ve already raised, is that when it clears from the previous game it sets the sensors to “unknown” rather than ‘0’ or a ‘-’. So, those I set the state manually for today because I’m in and out of it. I’m not too experienced with templates, so I’m using this to play around and learn it as well.

I also added “Period” and “Time Remaining” (which is for the period, not the whole game) to the card/sensor line up:

template:
  - sensor:
    - unique_id: 'ducks_cur_period'
      state: >
        {{state_attr('sensor.nhl_ducks', 'current_period')}}
    - unique_id: 'ducks_time_remain'
      state: >
        {{state_attr('sensor.nhl_ducks','time_remaining')}}

Notes for reference on the card:
o Period icon is: mdi:hockey-puck and the timer is mdi:timer-outline to save people time in looking for them. I set those on the card itself since those don’t need to change.

o The time and period will automatically set (what you see on my Ducks card) when it recognized that the game is today and then update live when the game is going so that if I check it, it’ll reflect what’s going on.

o For my Kings card, I entered the ‘-’ for time/period and the ‘0’ for score manually in Developer Tools just to clean it up as I was bouncing in and out of it earlier, otherwise it reads “unknown” for all four. I’ll play with it later to see how to change the default to the ‘-’ (time/period) and ‘0’ (team scores) when the game isn’t going.

Hope that helps!

Added: just for clarity and for whoever sees this later, the Ducks are playing the Wild on 10/15/21 and then the Kings are playing them on 10/16/21. Just to avoid confusion, I didn’t do anything special, that’s @jayblackedout’s integration that pulled that data automatically, so that side is definitely functioning right.