zacs/ha-nfl

Integration Update Speed

Spiffieman opened this issue · 10 comments

Hi there - I have noticed this year that when in PRE status (especially when getting close to the game like under an hour), that the card doesn't seem to update very fast (updates every 20 minutes or so). I have some super sweet lighting automations tied to when the status of the game switches from PRE to IN and today for example, the integration didn't update until about 4 minutes after kick off. Is there anyway to increase the update frequency when it gets close to the start of the game? Thanks!

zacs commented

Can you enable logging and paste output? The behavior is to check every 10 minutes, but if the most recent update shows the game starting within 20 minutes, the interval updates to 10 seconds.

Logically this should capture all games and have you in 10s intervals anywhere between 9:59 until kickoff or kickoff itself.

The only thing I can think of is the kickoff time changing close to kickoff.

zacs commented

Appreciate the logs. The logic is pretty basic, but it could even just be a weird race condition and I need to bump the "poll fast" trigger from 20 to 30 min (not a big deal).

As a fellow [tortured] Hawks fan I will be at the clink for next week's game so will check back in afterward 😎.

You're checking the state of the actual sensor, which will never evaluate to "in 15 minutes". The only states for the sensor are "PRE", "IN", "POST" and "BYE".

You could change the automation to work with the 'kickoff_in' attribute, but I think you'd be better off with a template trigger that uses the date attribute and compares it to the current time. I think something like this would work.
{{ not is_state('sensor.nfl', 'unknown') and not is_state('sensor.nfl', 'unavailable') and as_timestamp(state_attr('sensor.nfl', 'date')) <= (as_timestamp(now()) + (15 * 60)) }}
That has the benefit of triggering the automation even if the sensor is unavailable for the 15th minute before the game.

You'd probably want to add a condition that sensor.nfl state is "PRE", otherwise reboots after (up until Wednesday of the following week) or during the game would have the potential to trigger the automation as well.

you can incorporate the PRE condition in the template as well
{{ is_state('sensor.nfl', 'PRE') and as_timestamp(state_attr('sensor.nfl', 'date')) <= (as_timestamp(now()) + (15 * 60)) }}

You can test out any template in developer tools > templates

Fyi - with today's seahawks game, as soon as the kickoff happened, it went from PRE to unavailable. Then about 30 seconds later, it turned to IN. So something seems to be happening right at kickoff to make the integration go unavailable.

Here is the error I see in the Core logs:

This error originated from a custom integration.

Logger: custom_components.nfl
Source: helpers/update_coordinator.py:322
Integration: NFL (documentation, issues)
First occurred: 00:05:23 (2 occurrences)
Last logged: 10:03:08

Error fetching NFL data: 0, message='Attempt to decode JSON with unexpected mimetype: ', url=URL('http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard')
Error fetching NFL data: 'awayTimeouts'

zacs commented

Thanks for the log output. Looks like a race condition where the game's kickoff time has passed, but ESPN has yet to create all the data (probably because in reality the game hasn't actually started). I can probably fix by wrapping a bunch of stuff in try/catch.

Hey there, just an update to this. Same thing occurring this season. As soon as kickoff happened today, the sensor went unavailable and then came back a little later. I didn't have debug on, but I caught this in the logs.

2024-09-15 10:03:10.646 ERROR (MainThread) [custom_components.nfl] Error fetching NFL data: 'awayTimeouts'
2024-09-15 10:03:10.647 WARNING (MainThread) [homeassistant.helpers.script] Error in 'if[0]' evaluation: In 'and' (item 2 of 3):
In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'team_score'
2024-09-15 10:04:00.647 WARNING (MainThread) [homeassistant.helpers.script] Error in 'if[0]' evaluation: In 'and' (item 2 of 3):
In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'team_score'

2024-09-15 10:05:07.658 DEBUG (MainThread) [custom_components.nfl] Getting state for SEA from http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard
2024-09-15 10:05:07.663 DEBUG (MainThread) [custom_components.nfl] Found event; parsing data.
2024-09-15 10:05:07.663 DEBUG (MainThread) [custom_components.nfl] Event in progress, setting refresh rate to 5 seconds.
2024-09-15 10:05:07.663 DEBUG (MainThread) [custom_components.nfl] Finished fetching NFL data in 0.054 seconds (success: True)