Sports Standings and Scores

So we can get this:

team_abbr: SF
opponent_abbr: LV
down_distance_text: 4th & 2 at LV 40
possession: '13'
opponent_id: '13'
team_id: '25'

That is every thing we need.

who has the ball? possession=‘13’
Which is? opponent_id
Which is? opponent_abbr: LV

So the statement cut at the end is “LV 40”

So that means LV had the ball at the LV 40 …
So if changed to SF 20 would mean “Red Zone”

Just goofing around:

{% set team = 'sensor.san_francisco_49ers' %}
{% set possession = state_attr(team,'possession') %}
{% set team_id = state_attr(team,'team_id') %}
{% set opponent_id = state_attr(team,'opponent_id') %}
{% set team_abbr = state_attr(team,'opponent_abbr') %}
{% set opponent_abbr = state_attr(team,'opponent_abbr') %}
{% set down_distance_text = state_attr(team,'down_distance_text') %}
{% set down_distance_endzone = state_attr(team,'down_distance_text').split(' ')[4] %}
{% set down_distance_yardline = state_attr(team,'down_distance_text').split(' ')[5] %}

{{ team }} is the team entity
{%- if opponent_id == possession %}
{{ opponent_abbr }} 
{%- else %}
{{ team_abbr }}
{%- endif %} has the ball
{{ down_distance_text }} is the down distance text
{%- if opponent_id == possession %}
{{ opponent_abbr }} 
{%- else %}
{{ team_abbr }}
{%- endif %} has ball toward endzone {{ down_distance_endzone}} at {{ down_distance_yardline }} yardline

Yields:

Result type: string
sensor.san_francisco_49ers is the team entity
LV has the ball
1st & 10 at LV 35 is the down distance text
LV has ball toward endzone LV at 35 yardline

So I think its only a matter of if the possession team != endzone team and the yardline <= 20 … Red Zone.

I think this just may do it …

{% set team = 'sensor.san_francisco_49ers' %}
{% set possession = state_attr(team,'possession') %}
{% set team_id = state_attr(team,'team_id') %}
{% set opponent_id = state_attr(team,'opponent_id') %}
{% set team_abbr = state_attr(team,'team_abbr') %}
{% set opponent_abbr = state_attr(team,'opponent_abbr') %}
{% set down_distance_text = state_attr(team,'down_distance_text') %}
{% set down_distance_endzone = state_attr(team,'down_distance_text').split(' ')[4] %}
{% set down_distance_yardline = state_attr(team,'down_distance_text').split(' ')[5] %}
{% set possession_abbr = opponent_abbr if opponent_id == possession else team_abbr %}
{% set non_possession_abbr = opponent_abbr if opponent_id != possession else team_abbr %}
{{ team }} is the team entity
{{ possession_abbr}} has the ball, {{ down_distance_text }}
{{ possession_abbr }} has ball closer to {{ down_distance_endzone}} endzone at {{ down_distance_yardline }} yard line
{%- if (possession_abbr != down_distance_endzone) and (down_distance_yardline | int) <= 20 %}
REDZONE!!!!!!
{% else %}
Not in REDZONE
{% endif %}

Just need some team to get in the REDZONE!
And of course no one gets in the Redzone after I wrote it.
Next game I guess.
THen we turn that into a template in the NFL stats … Red Zone tab or something!

If anyone wants to test and help, games start tonight and tomorrow even better. Here’s what I am testing with auto-entities:

      - type: custom:auto-entities
        unique: true
        show_empty: true
        card:
          title: Red Zone
          type: custom:layout-card
          layout_type: masonry
        card_param: cards
        filter:
          template: >
            {%- for team in integration_entities("teamtracker") -%}
            {%- if state_attr(team, "league") == "NFL" -%}
            {%- if states(team) == "IN" -%}
            {%- if state_attr(team, "team_homeaway") == "home" -%}
            {% set possession = state_attr(team,'possession') %}
            {% set team_id = state_attr(team,'team_id') %}
            {% set opponent_id = state_attr(team,'opponent_id') %}
            {% set team_abbr = state_attr(team,'team_abbr') %}
            {% set opponent_abbr = state_attr(team,'opponent_abbr') %}
            {% set down_distance_text = state_attr(team,'down_distance_text') %}
            {% set down_distance_endzone = state_attr(team,'down_distance_text').split(' ')[4] %}
            {% set down_distance_yardline = state_attr(team,'down_distance_text').split(' ')[5] %}
            {% set possession_abbr = opponent_abbr if opponent_id == possession else team_abbr %}
            {% set non_possession_abbr = opponent_abbr if opponent_id != possession else team_abbr %}
            {%- if (possession_abbr != down_distance_endzone) and (down_distance_yardline | int) <= 20 %}
            {{{"type": "custom:teamtracker-card",
               "entity": team,
               "home_side": "right"}}},
            {% endif %}
            {% endif %}
            {% endif %}
            {% endif %}
            {% endfor %}
          exclude:
            - entity_id: '*team_tracker*'
        sort:
          method: attribute
          attribute: date

Logic in above:

Loop through all teamtracker entities
If it is NFL…
If the game is “IN” (LIve)
If the team is the home team (eliminate duplicates)
Get the info on who has the ball on whose yard line
If it is opposing team is at or within the 20 yard line of non-opposing team
Create an auto-entity of the team tracker for that team

Won’t be too interesting with one game tonight, two games tomorrow running concurrently … but Saturday late games will be the test with probably 6 or 7 concurrent games.

1 Like

All of this was a mess. So, cleaning it up by deletion :slight_smile:

See below for better answers.

1 Like

Duh on me! I just grabbed my standard view which eliminates duplicates (by showing only home team). Of course there is no need here because both teams can’t be in the Red Zone at once!

Good catch @jeffcrum !

Well, you do need to remove duplicates or you’ll get two cards.

Another mess. Fixed below :slight_smile:

I did not have a chance to watch it but I think I know what you are saying … since I have the whole NFL (and not a subset of teams) my example would work as it was removing the away team. You are still getting every game that way (which means every team that is playing).

Also, if you only want a subset of the teams then you cannot just assume eliminating the away team because your watched/favorite team could be the away team.

Another way would be to create a list of them all. Then pass in that list and inside the loop over them, look them up in the full list for both home and away … then if just home is in favs or just away is pass on that team, if both are use either one, eliminate the other.

Right. I have a way to eliminate duplicates for those not using the full list I have posted before. I’ll apply it to this later tonight and post.
*** FIXED NOW ***

NOTE Like @kbrown01 does, I pulled in this template from my ‘standard’ duplicate removal. This does NOT need the event_name else team_abbr part. But, it won’t hurt anything. It just keeps teams not playing in the list. Of course, that is not necessary for a red zone routine.

Here is my fix to the template for people not watching every team. Currently untested. Just pulled in from another template. I’ll play over the weekend and make sure I like it and fix if necessary.

                {%- set ns = namespace(items=[]) -%}
                {%- for team in integration_entities("teamtracker") -%}
                  {%- if state_attr(team, "league") == "NFL" -%}
                    {%- if states(team) =="IN" -%}
                      {%- set event_name = state_attr(team, "event_name") if state_attr(team, "event_name") else state_attr(team, "team_abbr") -%}
                      {%- set ns.items = ns.items + [{"team_sensor": team, "event_name": event_name}] -%}
                    {%- endif -%}
                  {%- endif -%}
                {%- endfor -%}
                {%- for team_ns in ns.items|unique(attribute="event_name") -%} 
                  {% set team = team_ns.team_sensor %}
                  {% set possession = state_attr(team,'possession') %}
                  {% set team_id = state_attr(team,'team_id') %}
                  {% set opponent_id = state_attr(team,'opponent_id') %}
                  {% set team_abbr = state_attr(team,'team_abbr') %}
                  {% set opponent_abbr = state_attr(team,'opponent_abbr') %}
                  {% set down_distance_text = state_attr(team,'down_distance_text') %}
                  {% set down_distance_endzone = state_attr(team,'down_distance_text').split(' ')[4] %}
                  {% set down_distance_yardline = state_attr(team,'down_distance_text').split(' ')[5] %}
                  {% set possession_abbr = opponent_abbr if opponent_id == possession else team_abbr %}
                  {% set non_possession_abbr = opponent_abbr if opponent_id != possession else team_abbr %}
                  {%- if (possession_abbr != down_distance_endzone) and (down_distance_yardline | int) <= 20 %}
                    {{{"type": "custom:teamtracker-card",
                       "entity": team,
                       "home_side": "right"}}},
                  {% endif %}
                {% endfor %}```

So, tonight’s games are Panthers @ Giants and Bengals @ Falcons.

I have added three of the four to my list and will test my code above. Will report back.

But got an error when both were in. Need to watch in developer tools. NOte this was my version, not yours … every team is in my list of teams.

One thing I noticed that I am not sure why. One team enters “Red Zone” territory … is not removed until someone else does. If I leave my Red Zone tab open it continues to show the game and does not remove them. Have to think about that one.

Has to be caching and decluttering card because if I leave the tab and come back, they are gone.

I did not get a chance to watch any games last night. Hope I can get some testing on this today.

I am running this in just a test dashboard in a vertical-stack. So, we can either prove or disprove that the decluttering card is causing the caching.

But, I did notice that I left the check for home in my code above. DOH!

Below is the fixed code. I also moved the check for IN to the top part. No need to pass those that are not IN to your code.

                {%- set ns = namespace(items=[]) -%} {%- for team in integration_entities("teamtracker") -%}
                  {%- if state_attr(team, "league") == "NFL" -%}
                    {%- if states(team) =="IN" -%}
                      {%- set event_name = state_attr(team, "event_name") if state_attr(team, "event_name") else state_attr(team, "team_abbr") -%}
                      {%- set ns.items = ns.items + [{"team_sensor": team, "event_name": event_name}] -%}
                    {%- endif -%}
                  {%- endif -%}
                {%- endfor -%}
                {%- for team_ns in ns.items|unique(attribute="event_name") -%} 
                  {% set team = team_ns.team_sensor %}
                  {% set possession = state_attr(team,'possession') %}
                  {% set team_id = state_attr(team,'team_id') %}
                  {% set opponent_id = state_attr(team,'opponent_id') %}
                  {% set team_abbr = state_attr(team,'team_abbr') %}
                  {% set opponent_abbr = state_attr(team,'opponent_abbr') %}
                  {% set down_distance_text = state_attr(team,'down_distance_text') %}
                  {% set down_distance_endzone = state_attr(team,'down_distance_text').split(' ')[4] %}
                  {% set down_distance_yardline = state_attr(team,'down_distance_text').split(' ')[5] %}
                  {% set possession_abbr = opponent_abbr if opponent_id == possession else team_abbr %}
                  {% set non_possession_abbr = opponent_abbr if opponent_id != possession else team_abbr %}
                  {%- if (possession_abbr != down_distance_endzone) and (down_distance_yardline | int) <= 20 %}
                    {{{"type": "custom:teamtracker-card",
                       "entity": team,
                       "home_side": "right"}}},
                  {% endif %}
                {% endfor %}

I was looking at your github for this. Your favorites example is losing the away teams that are not playing another of your favorites.

You might want to change it to include my part.

type: custom:auto-entities
unique: true
show_empty: false
card:
  type: custom:layout-card
  layout_type: masonry
card_param: cards
filter:
  template: |
    {%- set ns = namespace(items=[]) -%}
    {%- for team in state_attr('group.team_favorities','entity_id') -%}
      {%- set event_name = state_attr(team, "event_name") if state_attr(team, "event_name") else state_attr(team, "team_abbr") -%}
      {%- set ns.items = ns.items + [{"team_sensor": team, "event_name": event_name}] -%}
    {%- endfor -%}
    {%- for team_ns in ns.items|unique(attribute="event_name") -%} 
      {%- set team = team_ns.team_sensor -%}
      {{{"type": "custom:teamtracker-card",
         "entity": team,
         "home_side": "right"}}},
    {%- endfor -%}
  exclude:
    - entity_id: '*team_tracker*'
sort:
  method: attribute
  attribute: date

Yes I posted that assuming all teams in NFL like a scoreboard. Should be changed as yours works either way.

Right. I get it.

But, once you did the Favorite Group, it needed mine.

For the whole Teamtracker when someone has all teams, I’d stick with yours. It keeps a single pass over the data. Might save a half second :slight_smile:

Okay. Mine also does not add or remove cards as they go in and out of the red zone unless I leave the dashboard and go back.

It looks like any card that is on the dashboard changes fine as things in the background change. But, the template does not re-evaluate to change the cards that are displayed on the dashboard.

Agreed. I know how to kinda fix it.
I would guess the best solution is a template sensor for “Red Zone” which will be updated and then just use that to display the teams.

Just walked in the door from dropping grandbabies off for sleepovers. I will work on that for next games maybe if I can. Fantasy draft is tomorrow so I need some prep time for that!!!

Just playing and my editor shows this:

But now Jags scored and it is still the same.
Which makes me believe that the issue resides in the update of the teamtracker sensor.
I left the developer tab and returned and it updated.
Might need some automation that calls update entity for teams that are “IN” every 5 seconds or something.
It is like it does not change when attributes change as it should

That is looking good.

I thought I read somewhere that the Dev Tools | Template updates every minute unless edited, then it updates immediately.

I might be making that up too. Not sure.

OK what screws it up (so far) is this:

possession: null

At half and between TDs and such possession changes to null.
This needs to be accounted for in the template.

So, at those times, they will not be in the red zone.

What about?

{% set possession = state_attr(team,'possession') if state_attr(team,'possession') else state_attr(team,'team_abbr')%}

Basically, if it is null, make it the home team.

Testing a few things now.
Could be simple with another if:

{%- if possession -%}

I say that because I have put this into a template sensor now for red zone whose state is the number of teams in the red zone. This would not add a team so it would be “0”

At any rate, fun stuff and the evening games tonight will be the real test

I have brought all teams into my system so I can stop throwing that at you :slight_smile:

I’ll use a group and cut them down where I need. I think I might want all Red Zone anyway and only list the teams we want in the NFL/MLB/etc tabs.

1 Like