RIght on. I consolidated some if’s too with and’s …
- name: NFL Red Zone
unique_id: sensor.nfl_red_zone
state: |
{% set redzone = namespace(teams=[]) %}
{%- for team in integration_entities("teamtracker") -%}
{%- if states(team) == "IN" and state_attr(team, "league") == "NFL" and state_attr(team, "team_homeaway") == "home" and state_attr(team,'possession') is not none -%}
{% 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') %}
{% if state_attr(team,'down_distance_text') is not none %}
{% 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 %}
{% set redzone.teams = redzone.teams + [team] %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{{ redzone.teams | count }}
attributes:
teams: |
{% set redzone = namespace(teams=[]) %}
{%- for team in integration_entities("teamtracker") -%}
{%- if states(team) == "IN" and state_attr(team, "league") == "NFL" and state_attr(team,'possession') is not none -%}
{% 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') %}
{% if state_attr(team,'down_distance_text') is not none %}
{% 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 %}
{% set redzone.teams = redzone.teams + [team] %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{{ redzone.teams }}