Variables in template for lovelace cards

Hi,
I recently started building a dashboard for following NFL football games. That’s using custom components ha-nfl sensor and ha-nfl-card. For this purpose I’m currently using the custom state-switch card. This is where I’m not sure whether this is optimal.
So initially, I figured I’d just display all “away” games per division. That results in 8 vertical stack cards and that’s somewhat ok.

type: vertical-stack
title: AFC East
cards:
  - type: custom:state-switch
    entity: template
    template: >-
      {% if is_state_attr('sensor.nfl_nyj', 'team_homeaway', 'away') %} true {%
      else %} false {% endif %}
    states:
      'true':
        type: custom:nfl-card
        entity: sensor.nfl_nyj
        outline: true
        outline_color: deeppink
  - type: custom:state-switch
    entity: template
    template: >-
      {% if is_state_attr('sensor.nfl_buf', 'team_homeaway', 'away') %} true {%
      else %} false {% endif %}
    states:
      'true':
        type: custom:nfl-card
        entity: sensor.nfl_buf
        outline: true
        outline_color: deeppink
  - type: custom:state-switch
    entity: template
    template: >-
      {% if is_state_attr('sensor.nfl_mia', 'team_homeaway', 'away') %} true {%
      else %} false {% endif %}
    states:
      'true':
        type: custom:nfl-card
        entity: sensor.nfl_mia
        outline: true
        outline_color: deeppink
  - type: custom:state-switch
    entity: template
    template: >-
      {% if is_state_attr('sensor.nfl_ne', 'team_homeaway', 'away') %} true {%
      else %} false {% endif %}
    states:
      'true':
        type: custom:nfl-card
        entity: sensor.nfl_ne
        outline: true
        outline_color: deeppink

That’s what’s shown.

Now I was thinking: let’s instead pair them by game time and this is where issues come in and I can’t figure out the solution I need. The template below results in the card showing up conditional to the sensor for NYJ reporting an “away” game at 1pm eastern time (17UTC).

type: vertical-stack
title: Games at 1 PM
cards:
  - type: custom:state-switch
    entity: template
    template: >-
      {% if ( (is_state_attr('sensor.nfl_nyj', 'team_homeaway', 'away')) and
      ('T17' in state_attr('sensor.nfl_nyj','date')) ) %} true {% else %} false
      {% endif %}
    states:
      'true':
        type: custom:nfl-card
        entity: sensor.nfl_nyj
        outline: true
        outline_color: deeppink

My issue is that if using this format, I have to template the whole thing for each of the 32 teams multiple times. Thursday night, Sunday 1pm, Sunday 4pm, Sunday 8pm, Monday 8pm and that doesn’t even take into account when a game is played for example in London earlier on Sunday for example.
Would there be a way to do that more dynamically and declare variables in the templates or use a different card? For example have a template look at the stated attributes in all 32 team sensors and show cards accordingly?

Hi MrNickll,

did you try the [Auto Entity Card?]

Hi sagresbier,

I did and my concern with it is that while it works perfectly with standard cards like the entities card, it can’t populate the parameter for the custom:NFL-card.

I’ve tried it with the custom-bar-card:

type: custom:auto-entities
filter:
  include:
    - entity_id: '*temp*'
  exclude: []
card:
  type: custom:bar-card

It’s only an example. Did you find the sheet “Card” and “change card type”?

type: custom:auto-entities
show_empty: false
card:
  title: 4PM Games
  type: entities
filter:
  include:
    - attributes:
        team_homeaway: "away"
        date: "*T20*"

This does filter entities correctly, showing only those with an “Away” game played at 4:xxPM. But that’s using the entities card.
Results in this:
Screenshot 2023-09-22 at 12.29.30 PM

If I try to use the custom:nfl-card, I end up with an empty card. That’s whether or not I add an entities list, use the parameter for include/entity_id like your example or add the card_param option. Nothing I tried results in the auto entities filling the entity parameter of the NFL-card.

type: custom:auto-entities
show_empty: false
card:
  title: 4PM Games
  type: custom:nfl-card
  entity: []
  outline: true
  outline_color: deeppink
card_param: entity
filter:
  include:
    - attributes:
        team_homeaway: away
        date: '*T20*'

What happens if you try like this:

type: custom:auto-entities
show_empty: false
card:
  title: 4PM Games
  type: custom:nfl-card
filter:
  include:
    - attributes:
        team_homeaway: away
        date: '*T20*'

That also stays empty. But wait, is it possible the auto-entities card can only result in 1 card being shown? I.e. it works in our attempts with the entities or bar card because those can show more than 1 entity at once? I tried using the vertical stack as card type for auto-entities but then I don’t think there’s a way.
Actually while typing, just found out you can combine the auto-entities card with another one called layout-card. However I can’t seem to make the layout-card display the custom: nfl-card.

I have a new idea:
maybe you can use an array an loop for each club:

type: vertical-stack
title: AFC East
variables:
  array: ['nfl_nyj', 'nfl_buf', 'nfl_mia', 'nfl_ne']
cards:
  {% for club in array %}
    {% if is_state_attr('sensor.{{club}}', 'team_homeaway', 'away') %}
        - type: custom:nfl-card
          entity: sensor.{{club}}
          outline: true
          outline_color: deeppink
    {%endif%}
  {%endfor%}

That looked promising and would be exactly what I’m trying to do, however it returns a syntax error with “missed comma between flow collection entries (6:4)”. If I add a comma as it’s requesting, I’m either getting an error for bad indentation later at ‘sensor’ or it complains about double quotes.

I modified the code above from my automation. Maybe it doesn’t work in cards. what about:

type: vertical-stack
title: AFC East
cards:
  {% set  my_array as [ 'nfl_nyj', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
  {% for club in my_array %}
    {% if is_state_attr('sensor.{{club}}', 'team_homeaway', 'away') %}
        - type: custom:nfl-card
          entity: sensor.{{club}}
          outline: true
          outline_color: deeppink
    {%endif%}
  {%endfor%}

Edit: I changed the array to my_array. Maybe the word is the problem.

Trying something else to confirm whether that’s working at all. So I just pasted this in the developer tools template editor. The state attributes given should return nfl_pit as it’s the only one “away”, played at UTC 00:xx and broadcasted on NBC. I.e. the Sunday Night game from yesterday.
It returns no result.
How do we define that the array options are set as “club” in this template?

{% set array = [ 'nfl_pit', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
  {% for club in array %}
    {% if ( is_state_attr('sensor.{{club}}', 'team_homeaway', 'away') and is_state_attr('sensor.{{club}}', 'tv_network', 'nbc') and ('T00'
  in state_attr('sensor.{{club}}','date')) ) %}
    {%- endif %}
  {%- endfor %}

I don’t know, if we may use the word “array”, so i changed to my_array. Could you try this?

You didn’t give him anything to do. For testing you should insert {{ club }} between the if statement:

{% set array = [ 'nfl_pit', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
  {% for club in array %}
    {% if ( is_state_attr('sensor.{{club}}', 'team_homeaway', 'away') and is_state_attr('sensor.{{club}}', 'tv_network', 'nbc') and ('T00'
  in state_attr('sensor.{{club}}','date')) ) %}
      {{ club }}
    {%- endif %}
  {%- endfor %}

Now it should return the club which agrees to your template.

Maybe I forgot the quotes:

type: vertical-stack
title: AFC East
cards:
  {% set  my_array as [ 'nfl_nyj', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
  {% for club in my_array %}
    {% if is_state_attr('sensor.{{club}}', 'team_homeaway', 'away') %}
        - type: custom:nfl-card
          entity: "sensor.{{club}}"
          outline: true
          outline_color: deeppink
    {%endif%}
  {%endfor%}

Try this. I think, we are getting nearer. :smiley:

You cannot actively build YAML configuration with templates. Think of YAML as a form with various fields (item, quantity, size, color, etc). You can use templates to dynamically fill in the value for a field, but the structure of the form is static.

But how to get this work? The auto-entity card doesn’t work for him.

type: custom:auto-entities
card:
  type: vertical-stack
  title: AFC East
card_param: cards
filter:
  template: >-
    {%- set object_ids = [ 'nfl_nyj', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
    {%- set ns = namespace(cards=[]) %}
    {%- for object_id in object_ids %}
      {%- set entity_id = 'sensor.' ~ object_id %}
      {%- if is_state(entity_id, 'team_homeaway', 'away') %}
        {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.cards }}
1 Like

Thanks @petro for chiming in!
That’s pretty much the solution. Only difference being that we’re looking at an attribute in the if statement. I also added ‘T17’ in the if statement for future reference in case someone else wants the example. That filters games from sensors in the object_ids list per team Away, played at T17 UTC.

type: custom:auto-entities
card:
  type: vertical-stack
  title: AFC East
card_param: cards
filter:
  template: >-
    {%- set object_ids = [ 'nfl_nyj', 'nfl_buf', 'nfl_mia', 'nfl_ne' ] %}
    {%- set ns = namespace(cards=[]) %}
    {%- for object_id in object_ids %}
      {%- set entity_id = 'sensor.' ~ object_id %}
      {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T17'in state_attr(entity_id,'date')) ) %}
        {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.cards }}

In case anyone’s interested (or should I post this somewhere else?), complete dashboard with all teams, games split per time.
Custom components needed: ha-nfl sensor, ha-nfl-card, auto-entities card.

  - icon: mdi:football
    title: NFL
    path: nfl
    badges: []
    cards:
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Thursday Night Football
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T00'in state_attr(entity_id,'date')) and ('PRIME' in state_attr(entity_id, 'tv_network')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: International Series
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T13'in state_attr(entity_id,'date')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Sunday 1PM
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T17'in state_attr(entity_id,'date')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Sunday 4PM
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T20'in state_attr(entity_id,'date')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Sunday Night Football
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T00'in state_attr(entity_id,'date')) and (is_state_attr(entity_id, 'tv_network', 'NBC')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Monday Night Football
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state_attr(entity_id, 'team_homeaway', 'away') and ('T00'in state_attr(entity_id,'date')) and ('PRIME' not in state_attr(entity_id, 'tv_network')) and ('NBC' not in state_attr(entity_id, 'tv_network')) ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
      - type: custom:auto-entities
        card:
          type: vertical-stack
          title: Bye Week
        card_param: cards
        filter:
          template: >-
            {%- set object_ids = [ 'nfl_ari', 'nfl_atl', 'nfl_bal', 'nfl_buf',
            'nfl_car', 'nfl_chi', 'nfl_cin', 'nfl_cle', 'nfl_dal', 'nfl_den',
            'nfl_det', 'nfl_gb', 'nfl_hou', 'nfl_ind', 'nfl_jax', 'nfl_kc',
            'nfl_lac', 'nfl_lar', 'nfl_lv', 'nfl_mia', 'nfl_min', 'nfl_ne',
            'nfl_no', 'nfl_nyg', 'nfl_nyj', 'nfl_phi', 'nfl_pit', 'nfl_sea',
            'nfl_sf', 'nfl_tb', 'nfl_ten', 'nfl_wsh', ] %} {%- set ns =
            namespace(cards=[]) %} {%- for object_id in object_ids %}
              {%- set entity_id = 'sensor.' ~ object_id %}
              {%- if ( is_state(entity_id, 'BYE') ) %}
                {%- set ns.cards = ns.cards + [{'type': 'custom:nfl-card', 'entity': entity_id, 'outline': True, 'outline_color': 'deeppink'}] %}
              {%- endif %}
            {%- endfor %} {{ ns.cards }}
2 Likes

Thank you Petro. You are a real M A S T E R !!!

Excuse my ignorance here, but this doesn’t work. A simple copy/paste of this has a ton of formatting issues and missing items. Can you go into the code editor and copy/paste EXACTLY what should be pasted in our auto-entities card? Seems there is missing a few options above the - icon.