Finally we’re getting back to the original purpose of this whole thread, lol.
Touchdown Detector:
This is definitely not the only way to do this. (I noticed there is a ‘scoreValue’ variable in the ESPN api, which identifies how much the score has just changed.) The method I use below is basically a holdover from how we in this thread were doing it when using the NFL.com api.
First, I created a helper input boolean in the UI and called it “NFL Touchdown Detector”
Here’s the automation that turns on the boolean when a touchdown is scored by my team:
alias: NFL - Input Boolean Control - Turn On/Off Touchdown Tracker Input Boolean
description: Turns on input boolean for one minute if team score changes by 6+ points
trigger:
- platform: state
entity_id: sensor.nfl_score
condition:
- condition: template
value_template: |-
{{ trigger.to_state is not none and trigger.from_state is not none and
trigger.to_state.state|int > (trigger.from_state.state|int + 5)
}}
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.nfl_touchdown_detector
- delay: '00:03:00'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.nfl_touchdown_detector
mode: single
Win Detector:
I also have a “win detector,” which uses another helper input boolean called “NFL Win Detector”:
alias: >-
NFL - Input Boolean Control - Turn On/Off Win Input Boolean
description: Turns on input boolean for five minutes when team wins
trigger:
- platform: state
entity_id: sensor.nfl_game_status
to: Final
condition:
- condition: template
value_template: >-
{{ states("sensor.nfl_score")|int >
states("sensor.nfl_opponent_score")|int }}
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.nfl_win_detector
- delay: '00:05:00'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.nfl_win_detector
mode: single
I use the on/off state of these input boolean helpers to trigger any automations.
Input Boolean Reset Automation:
**It doesn’t hurt to also go ahead and make a built-in reset function to make sure your input booleans are off when they should be. This one below just turns them all off when HA starts.
alias: NFL - Input Boolean Control - All NFL Booleans Reset on HA Restart
description: ''
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.nfl_touchdown_detector
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.nfl_win_detector
- delay: '00:00:30'
- service: homeassistant.update_entity
data: {}
entity_id: sensor.nfl_game_status
mode: single
Update Automation for During Game
Just a reminder, in order for these to actually be live, you need to be forcing the rest sensors to update. I have mine set to update every 3 seconds during the game, which is determined by the status of the google calendar @D34DC3N73R and @djbrooks022 mentioned above. You don’t want the rest sensors taking up resources by updating all of the time while games aren’t active.
Here’s how I have set up my automation:
alias: Saints - NFL Scores - Update JSON/XML Every 3 Seconds During Game Only
description: ''
trigger:
- platform: state
entity_id: sensor.nfl_game_status
from: Pregame
- platform: state
entity_id: calendar.new_orleans_saints
to: 'on'
from: 'off'
- platform: template
value_template: >-
{{ (state_attr('calendar.new_orleans_saints', 'start_time') | as_timestamp
- now() | as_timestamp) < 300 and
(state_attr('calendar.new_orleans_saints', 'start_time') | as_timestamp -
now() | as_timestamp) > 0 }}
condition:
- condition: or
conditions:
- condition: state
entity_id: calendar.new_orleans_saints
state: 'on'
- condition: not
conditions:
- condition: state
entity_id: sensor.nfl_game_status
state: Final
- condition: template
value_template: >-
{{ (state_attr('calendar.new_orleans_saints', 'start_time') |
as_timestamp - now() | as_timestamp) < 300 and
(state_attr('calendar.new_orleans_saints', 'start_time') |
as_timestamp - now() | as_timestamp) > 0 }}
action:
- repeat:
until:
- condition: or
conditions:
- condition: state
entity_id: sensor.nfl_game_status
state: Final
- condition: state
entity_id: calendar.new_orleans_saints
state: 'off'
for: '01:00:00'
sequence:
- service: homeassistant.update_entity
data: {}
target:
entity_id:
- sensor.nfl_game_status
- sensor.nfl_score
- sensor.nfl_opponent_score
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
mode: single
Hope this helps!