zacs / ha-nfl

NFL game scores custom component for Home Assistant
43 stars 32 forks source link

Integration Update Speed #46

Open Spiffieman opened 1 year ago

Spiffieman commented 1 year ago

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 1 year ago

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.

Spiffieman commented 1 year ago

Thanks for responding. I only have the Seahawks configured and game is on right now. It is updating every 10ish seconds currently. I'll have to wait until the next game and start the debug logs starting around the 25 minute mark and see if it changes to 10 seconds when it hits 20 minutes. Next Seahawks game is 1:05 on 10/22 so I'll turn it on around 12:40. Thanks!

On Sun, Oct 15, 2023 at 12:10 PM Zac @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/zacs/ha-nfl/issues/46#issuecomment-1763478249, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASH5EGIXUOEHLKYUJMCHQODX7QYLLAVCNFSM6AAAAAA6BEJO4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRTGQ3TQMRUHE . You are receiving this because you authored the thread.Message ID: @.***>

--

zacs commented 1 year ago

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 😎.

Spiffieman commented 12 months ago

Ok so here are some things I found out.

My 10 minute updates were currently at 12:44 and 12:54. Once the 12:54 update happened (since it was within 20 minutes of the game), it changed to update every 5 seconds.

I have an automation to alert me that kickoff is "in 15 minutes" and it didn't trigger, which is weird.

So then, kickoff happened and the sensor didn't flip from PRE to IN. A few seconds after actual kickoff, the sensor went unavailable and stayed unavailable for a couple of minutes. Then became available again and the sensor then changed from unavailable to IN.

Logs attached.

Hope you are having fun at the game!

On Sun, Oct 15, 2023 at 1:13 PM Zac @.***> wrote:

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 😎.

— Reply to this email directly, view it on GitHub https://github.com/zacs/ha-nfl/issues/46#issuecomment-1763492978, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASH5EGN72ZTVGNCFDNQ7IGDX7Q7WVAVCNFSM6AAAAAA6BEJO4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRTGQ4TEOJXHA . You are receiving this because you authored the thread.Message ID: @.***>

--

D34DC3N73R commented 12 months ago

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.

Spiffieman commented 11 months ago

Thanks for the info and template suggestions. I very much appreciate it.

I didn't copy the attribute portion of that trigger so it was working some of the time:

platform: state entity_id:

I'll try out your template for sure. Templating is something I want to get more proficient at. That looks way better and more reliable than what I was doing!

On Tue, Oct 24, 2023, 12:27 D34DC3N73R @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/zacs/ha-nfl/issues/46#issuecomment-1777889499, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASH5EGP4BNVUDMCCYE6MP5TYBAJAPAVCNFSM6AAAAAA6BEJO4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZXHA4DSNBZHE . You are receiving this because you authored the thread.Message ID: @.***>

D34DC3N73R commented 11 months ago

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

Spiffieman commented 11 months ago

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 11 months ago

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.

Spiffieman commented 1 month ago

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)