NFL game sensor (scores, possession, etc)

I haven’t done automation triggers for the NFL yet, but I do have them for hockey. Here is the code for a goal.

id: maple_leafs_goal
alias: Maple Leafs Goal
trigger:
- platform: state
  entity_id: sensor.nhl_home_score
- platform: state
  entity_id: sensor.nhl_away_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 
         and "TOR" in trigger.from_state.attributes.friendly_name
      }}

Probably needs an additional condition in the condition template where the trigger.to state is > 5 from the trigger.from state so that the automation only triggers for a touchdown and not just any score.

This is the win automation for NHL.

id: maple_leafs_win
alias: Maple Leafs Win
trigger:
- platform: state
  entity_id: sensor.nhl_game_status
  from: 'In Progress'
  to: 'Final'
- platform: state
  entity_id: sensor.nhl_game_status
  from: 'In Progress - Critical'
  to: 'Final'
- platform: state
  entity_id: sensor.nhl_game_status
  from: 'In Progress'
  to: 'Game Over'   
- platform: state
  entity_id: sensor.nhl_game_status
  from: 'In Progress - Critical'
  to: 'Game Over'    
condition:
- condition: template
  value_template: >
      {{ (states("sensor.nhl_home_score")|int < states("sensor.nhl_away_score")|int 
         and is_state_attr("sensor.nhl_away_score", "friendly_name", "TOR")) or
         (states("sensor.nhl_home_score")|int > states("sensor.nhl_away_score")|int 
         and is_state_attr("sensor.nhl_home_score", "friendly_name", "TOR"))
      }}

The triggers can probably be simplified for NFL since there aren’t as many game states as the NHL feed I’m using.

1 Like

Here’s what I did with your automation for a touchdown:

id: saints_touchdown
alias: Saints Touchdown
trigger:
- platform: state
  entity_id: sensor.nfl_home_score
- platform: state
  entity_id: sensor.nfl_away_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) 
         and "NO" in trigger.from_state.attributes.friendly_name
      }}

That seems to do the trick in testing. (And saves me a lot of time, thank you again!)

1 Like

Worked really well for today’s game! I’m extremely pleased with the results.

1 Like

I agree the sensors and automations worked great. Too bad my team didn’t.

if anyone is interested. I modified some of the sensors, templates, automations, scripts, and lovelace card. shout out to @BigBlackBear, @yguns31, and @zacs. You guys did awesome work.

#####################

CALENDAR

#####################
I am using google calendar to publish various sports team that I follow. You can add your teams by following this video.

Then bring this calendar into HA by following.

#####################
#			SENSORS				#
#####################
#Dallas Cowboys sensors
  - platform: rest
    resource: https://static.nfl.com/liveupdate/scorestrip/ss.xml
    name: nfl_game_status
    value_template: >
      {% for game in value_json.ss.gms.g %}
        {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
          {% if game['@q'] == 'P' %}
            Pregame
          {% elif game['@q'] == '1' or game['@q'] == '2' or game['@q'] == '3' or game['@q'] == '4' or game['@q'] == 'O' or game['@q'] == 'H' %}
            In Progress
          {% elif game['@q'] == 'FO' %}
            F(OT)
          {% elif game['@q'] == 'F' %}
            Final
          {% elif game['@q'] == 'S' %}
            Suspended      
          {% endif %}        
        {% endif %}
      {% endfor %}
    json_attributes:
      - ss
    scan_interval: 
      hours: 24
  - platform: template
    sensors:       
      cowboys_home_score:
        value_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {{ game['@hs'] }}
            {% endif %}
          {% endfor %}
        friendly_name_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {{ game['@h'] }}
            {% endif %}
          {% endfor %}
        entity_picture_template: >-
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              /local/logos/nfl/{{ game['@h'] }}.svg
            {% endif %}
          {% endfor %}
        entity_id: sensor.nfl_game_status       
      cowboys_away_score:
        value_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {{ game['@vs'] }}
            {% endif %}
          {% endfor %}
        friendly_name_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {{ game['@v'] }}
            {% endif %}
          {% endfor %}
        entity_picture_template: >-
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              /local/logos/nfl/{{ game['@v'] }}.svg
            {% endif %}
          {% endfor %}
        entity_id: sensor.nfl_game_status
  - platform: template
    sensors:    
      nfl_time_status:
        value_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {% if states.sensor.nfl_game_status.state == 'Pregame' %}
                {{ game['@t'] }}
              {% else %}
                {% if game['@q'] == 'H' or game['@q'] == 'F' or game['@q'] == 'FO' %}
                {% else %}
                  {% if game['@k'][:1] == "0" %}
                    {{ game['@k'][1:] }}
                  {% else %}
                    {{ game['@k'] }}
                  {% endif %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endfor %}
        friendly_name_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {{ game['@q'] }}
            {% endif %}
          {% endfor %}
        entity_id: sensor.nfl_game_status
      nfl_period_status:
        value_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {% if game['@q'] == 'P' or game['@q'] == 'S' %}
                0 
              {% elif game['@q'] == 'F' or game['@q'] == 'FO' %}
                -1
              {% elif game['@q'] == 'H' %}
                2          
              {% elif game['@q'] == 'O' %}
                5          
              {% else %}
                {{ game['@q'] }}
              {% endif %}            
            {% endif %}
          {% endfor %}
        friendly_name_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {% if game['@q'] == 'P' %}
                Pregame 
              {% elif game['@q'] == 'F' %}
                Final
              {% elif game['@q'] == '1' %}
                1st
              {% elif game['@q'] == '2' %}
                2nd
              {% elif game['@q'] == '3' %}
                3rd
              {% elif game['@q'] == 'H' %}
                Half          
              {% elif game['@q'] == 'O' %}
                OT          
              {% elif game['@q'] == 'FO' %}
                F(OT)          
              {% else %}
                {{ game['@q'] }}th
              {% endif %}            
            {% endif %}
          {% endfor %}
        entity_id: sensor.nfl_game_status      
      nfl_possession:
        value_template: >
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {% if game['@h'] == game['@p'] %}
                {{ game['@h'] }}
              {% elif game['@v'] == game['@p'] %}
                {{ game['@v'] }}    
              {% endif %}            
            {% endif %}
          {% endfor %}
        friendly_name: NFL Possession
        entity_picture_template: >-
          {% for game in states.sensor.nfl_game_status.attributes['ss']['gms']['g'] %}
            {% if game['@h'] == 'DAL' or game['@v'] == 'DAL' %}
              {% if game['@h'] == game['@p'] %}
                /local/logos/nfl/{{ game['@h'] }}.svg
              {% elif game['@v'] == game['@p'] %}
                /local/logos/nfl/{{ game['@v'] }}.svg
              {% endif %}
            {% endif %}
          {% endfor %}
        entity_id: sensor.nfl_game_status
#########################
#			AUTOMATIONS				#
#########################
- id: dallas_scheduled
  alias: Dallas - Scheduled
  trigger:
  - platform: time_pattern
    minutes: /10
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: sensor.nfl_game_status
      state: Pregame
    - condition: state
      entity_id: sensor.nfl_game_status
      state: Suspended
  action:
  - service: script.turn_on
    entity_id: script.cowboys_update
  mode: single
- id: dallas_in_progress
  alias: Dallas - In Progress
  trigger:
  - entity_id: sensor.nfl_game_status
    platform: state
    to: In Progress
  condition: []
  action:
  - data: {}
    entity_id: automation.dallas_game_on
    service: automation.turn_on
  - data: {}
    entity_id: automation.dallas_game_on_with_lights
    service: automation.turn_on
  mode: single
- id: dallas_game_on
  alias: Dallas - Game On
  trigger:
  - minutes: /1
    platform: time_pattern
  condition:
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Pregame'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''F(OT)'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Final'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Suspended'') }}'
  - condition: state
    entity_id: input_boolean.cowboys
    state: 'off'
  action:
  - entity_id: script.cowboys_update
    service: script.turn_on
  mode: single
- id: dallas_game_on_with_lights
  alias: Dallas - Game On - With Lights
  description: ''
  trigger:
  - platform: time_pattern
    seconds: /2
  condition:
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Pregame'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''F(OT)'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Final'') }}'
  - condition: template
    value_template: '{{ not is_state(''sensor.nfl_game_status'', ''Suspended'') }}'
  - condition: state
    entity_id: input_boolean.cowboys
    state: 'on'
  action:
  - entity_id: script.cowboys_update
    service: script.turn_on
  mode: single
- id: dallas_game_over
  alias: Dallas - Game Over
  trigger:
  - entity_id: sensor.nfl_game_status
    platform: state
    to: Final
  - entity_id: sensor.nfl_game_status
    platform: state
    to: F(OT)
  - entity_id: sensor.nfl_game_status
    platform: state
    to: Suspended
  condition: []
  action:
  - entity_id: input_boolean.cowboys
    service: input_boolean.turn_off
  - data: {}
    entity_id: automation.dallas_game_on
    service: automation.turn_off
  - data: {}
    entity_id: automation.dallas_game_on_with_lights
    service: automation.turn_off
  - delay: 01:00:00
  - data: {}
    entity_id: script.cowboys_update
    service: script.turn_on
  mode: single
- id: dallas_run_celebration
  alias: Dallas - Run Celebration
  trigger:
  - entity_id: sensor.cowboys_home_score
    platform: state
  - entity_id: sensor.cowboys_away_score
    platform: state
  condition:
  - condition: template
    value_template: "{{ trigger.to_state is not none and trigger.from_state is not\
      \ none and\n         trigger.to_state.state|int > (trigger.from_state.state|int\
      \ + 5) \n         and \"DAL\" in trigger.from_state.attributes.friendly_name\n\
      }}\n"
  action:
  - service: script.cowboys
    entity_id: media_player.upstairs_echo_dot
    data: {}
  mode: single
- id: check_cowboys_schedule
  alias: Check Cowboys schedule
  trigger:
  - platform: state
    entity_id: calendar.dallas_cowboys
    to: 'on'
  action:
  - service: script.cowboyskickoff
    data: {}
  - service: script.turn_on
    entity_id: script.cowboys_update
  mode: single
- id: check_cowboys_schedule_and_at_home
  alias: Cowboys Game Start at Home Check
  description: ''
  trigger:
  - platform: state
    entity_id: calendar.dallas_cowboys
    to: 'on'
  condition:
  - condition: state
    entity_id: person.ryan_locey
    state: home
  - condition: device
    device_id: 5361af66347a4b18a2c1f4c6d548debb
    domain: media_player
    entity_id: media_player.ryan_s_fire_tv
    type: is_on
  action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.cowboys
  mode: single
#########################
#			SCRIPTS     			#
#########################
cowboys_update:
  alias: Update Cowboys Game Info
  sequence:
  - service: homeassistant.update_entity
    entity_id: sensor.nfl_game_status
  - service: homeassistant.update_entity
    entity_id: sensor.cowboys_away_score
  - service: homeassistant.update_entity
    entity_id: sensor.cowboys_home_score
  mode: single
cowboys:
  alias: Cowboys TD
  mode: queued
  sequence:
  - service: media_player.play_media
    data:
      entity_id: media_player.upstairs_echo_dot
      media_content_id: Baseball Crowd Cheering
      media_content_type: AMAZON_MUSIC
  - repeat:
      count: '3'
      sequence:
      - service: light.turn_on
        data:
          rgb_color:
          - 134
          - 147
          - 151
        entity_id: light.ryans_light
      - service: light.turn_on
        entity_id: light.monicas_light
        data:
          rgb_color:
          - 0
          - 34
          - 68
      - service: light.turn_on
        data:
          rgb_color:
          - 0
          - 34
          - 68
        entity_id: light.ryans_light
      - service: light.turn_on
        entity_id: light.monicas_light
        data:
          rgb_color:
          - 134
          - 147
          - 151
  - service: light.turn_on
    data:
      rgb_color:
      - 255
      - 255
      - 255
    entity_id: light.ryans_light
  - service: light.turn_on
    data:
      rgb_color:
      - 255
      - 255
      - 255
    entity_id: light.monicas_light
  max: 10
cowboyskickoff:
  alias: Cowboys Kickoff
  mode: queued
  sequence:
  - service: notify.alexa_media_upstairs_echo_dot
    data:
      data:
        type: tts
      message: It's Game Time
  - repeat:
      count: '3'
      sequence:
      - service: light.turn_on
        data:
          rgb_color:
          - 134
          - 147
          - 151
        entity_id: light.ryans_light
      - service: light.turn_on
        entity_id: light.monicas_light
        data:
          rgb_color:
          - 0
          - 34
          - 68
      - service: light.turn_on
        data:
          rgb_color:
          - 0
          - 34
          - 68
        entity_id: light.ryans_light
      - service: light.turn_on
        entity_id: light.monicas_light
        data:
          rgb_color:
          - 134
          - 147
          - 151
  - service: light.turn_on
    data:
      rgb_color:
      - 255
      - 255
      - 255
    entity_id: light.monicas_light
  - service: light.turn_on
    data:
      rgb_color:
      - 255
      - 255
      - 255
    entity_id: light.ryans_light
  max: 10
#########################
#			LOVELACE    			#
#########################
type: entities
entities:
  - entity: sensor.cowboys_away_score
  - entity: sensor.cowboys_home_score
  - entity: sensor.nfl_time_status
  - entity: sensor.nfl_period_status
  - entity: sensor.nfl_possession
state_color: false
1 Like

This is great, I added the calendar sensor for the schedule to my set-up. Assuming the google calendar is regularly updated with schedule changes, it should come in handy with the fact that game times and event days are not necessarily set in stone this year…

This is how I’ve implemented some automations to start updates using the calendar and then continue throughout the game.

#####Automations#####
- id: saints_update_sensor_to_start_game
  alias: Saints - Update to Start Game - Google Calendar
  description: ''
  trigger:
  - platform: state
    entity_id: calendar.new_orleans_saints
    from: 'off'
    to: 'on'
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: sensor.nfl_game_status
      state: In Progress
  action:
  - service: homeassistant.update_entity
    data: {}
    entity_id: sensor.nfl_game_status
  - delay: 00:01:00
  - service: automation.trigger
    data:
      entity_id: automation.saints_update_sensor_to_start_game
  mode: queued
  max: 15

- id: saints_update_during_game
  alias: Saints - Update Every 10 Seconds During Game Only
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.nfl_game_status
    to: In Progress
  condition:
  - condition: state
    entity_id: sensor.nfl_game_status
    state: In Progress
  action:
  - repeat:
      until:
      - condition: state
        entity_id: sensor.nfl_game_status
        state: Final
      sequence:
      - service: homeassistant.update_entity
        data: {}
        entity_id: sensor.nfl_game_status
      - delay: 00:00:10
  mode: single

Sorry formatting is a little sloppy because I (perhaps obviously) made them in the UI.

What are you all using the sensors to do? I see @ryanlocey is setting lights to his team’s colors and playing cheering on an alexa device when they score.

For a touchdown, I am flashing (non-RGB) lights in the Living Room (conditioned by if I’m home and the TV is on) and using the workaround outlined in this thread - Play a local mp3 file on Alexa Echo dot - to play a voice recording from my team’s home in stadium announcer - followed by 1.5 min of on-theme celebratory music randomly from a playlist. I can get into all that if people are interested in discussing? It’s ridiculous, but I’m a season ticket holder and haven’t missed a home game since 2005 until coronavirus came along, so it’s helping me cope!

You might want to update your game[’@q’] value list.

There was a weather delay today in the Texans-Browns game. During the delay, @q showed a value of ‘Suspended’ (not ‘S’).

These NFL sensors are great!

1 Like

Has anyone done anything similar with NCAA Football. I found the following:

http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard

but have no idea how to parse the data.

Thanks for sharing!
I implemented the google calendar for Kansas City and use your initial automations, and it works that I got a notification when the game started (I think it worked, will see on the next game :D). But without a variable and im not sure which I need to display the other team in the notification?

And how do I automate that my lights are flashing if we score a Touchdown?

Although I hope if you implement celebratory automations for Kansas City, none of them are triggered this coming Sunday, here’s what I’ve done to create touchdown automations:

alias: Saints - Touchdown Tracker
description: Turns on input boolean for three minutes if Saints score changes by 6+ points
trigger:
  - platform: state
    entity_id: sensor.nfl_away_score
  - platform: state
    entity_id: sensor.nfl_home_score
condition:
  - condition: and
    conditions:
      - 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)
                   and "NO" in trigger.from_state.attributes.friendly_name
                }}
      - condition: state
        entity_id: remote.living_room_hub
        state: 'on'
      - condition: zone
        entity_id: person.davearneson
        zone: zone.home
action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.saints_touchdown_detector
  - delay: '00:03:00'
  - service: input_boolean.turn_off
    data: {}
    entity_id: input_boolean.saints_touchdown_detector
mode: single

Before setting up this automation, I created an input boolean using helpers to function as a trigger for any other automations I want and called it “Saints Touchdown Detector.” The above automation turns on this input boolean for three minutes before turning it back off, because there has been some instability with the data getting pulled from NFL.com that resulted in multiple triggers, and that seemed to help prevent multiple triggers for one score.

If you have set up the sensors that @yguns31 outlined in their post, this should work and you can just use the input boolean as a trigger for whatever you want. As I think you saw, I outlined in a previous post how I set up the sensors to update every 10 seconds during a game using that google calendar.

I also created a lovelace page that I can use to keep tabs on all the sensors and/or manually refresh or turn on/off the touchdown tracker if I get impatient or want to test automations.

Let me know if any of this helps or if you have other issues. (I’m not sure how to pull the opponent name from the calendar, maybe someone else can chime in on that one.)

1 Like

Oh Boy, thanks a lot!
But I wish you good luck on sunday, u are not always blessed with the right decisions from your referees. :smiley:

I implemented your touchdown tracker and two more automations:
The first one should flash my led red when im at home and the second one send me a push message, if im not at home. (Still not sure how I implement the score and the other team in this. ^^")
Whats the best possibility to test this?

When I first set this up for myself, I tested that I was getting the info pulled correctly from the nfl resource by duplicating all of my sensors and changing the target from “NO” to whatever team was playing the Thursday night game. (This week it’s “LAC” or “LV” for the Thursday game.) Then I just added the sensors to lovelace, ran my “update every 10 seconds” automation, and watched to see if they updated as the game progressed. You could obviously do this by just changing those target values in your regular sensors, but you’ll see there are a number of places where those need to be changed, and I just didn’t want to have to worry about missing one when changing back.

If you want to display who the opponent for the game is from this sensor, it’s going to be the value of sensor.nfl_home_name or sensor.nfl_away_name. (If you used the naming conventions from @yguns31’s sensors from post 31 in this thread.) These sensors don’t parse information in a “Chiefs are playing the Saints away” type syntax, instead (in your case) it would be filtering the data to find the game that has the Chiefs as one of the teams and then telling you the home and away teams of that game. If you want to use the sensors as written to pull both teams to display in a push notification or something, you could use the following template (which I filtered to capitalize the friendly names, because mild OCD):

{{ state_attr('sensor.nfl_home_name', 'friendly_name')|capitalize }} vs. {{state_attr('sensor.nfl_away_name', 'friendly_name')|capitalize}}

This would result in “Saints vs. Chiefs” for the upcoming game. (As a side note, the nfl resource doesn’t update for the upcoming game until Wednesday/Thursday each week, so for me right now this template would show “Eagles vs. Saints” - don’t remind me…)

Or, if you want to show the score in a push:

{{ state_attr('sensor.nfl_home_name', 'friendly_name')|capitalize }} {{ states('sensor.nfl_home_score') }} - {{ state_attr('sensor.nfl_away_name', 'friendly_name')|capitalize }} {{ states('sensor.nfl_away_score') }}

This template, in another painful example, currently shows “Eagles 24 - Saints 21” for me.

You could also test your automations themselves by turning on and off the touchdown input boolean manually.

1 Like

Thanks, I change it to your push notifications. I will see it on sunday. :smiley:

I used the calendar sensor now for my sidebar. Next step is to display it in hours, not in days.

Great work here. Thanks for sharing! I just set this up for my Buffalo Bills!
Going to play the shout song for every score!

Cheers!

LOL. You had a lot of opportunities to see how well it worked today.

1 Like

Folks who are still doing automations for postseason need to pull from:
http://static.nfl.com/liveupdate/scorestrip/postseason/ss.xml

4 Likes

Thank you!

Thanks. Noticed the playoff xml feed now includes more info for each game, like timeouts remaining, down and distance, ball location and score by quarter. Let me know if anyone would like that information in a sensor.

My silly pandemic project was creating this mini scoreboard using some OLED displays. So having the timeouts enhances the look for me.

(Yes, it’s dumb that I have the scoreboard beside the TV that is showing the game with the score on it.)

9 Likes

I love this so much! I’d love to know more. Are you doing this with ESPhome?

Yes. It’s running a ESPhome on a ESP8266 with 4 SPI OLED displays (2 SSD1306 and 2 larger SH1106). I used SPI displays instead of I2C because it’s easier to have multiple displays on one ESP.

The code is a little messy since I use it to display other things when there aren’t games on.