Variables in template for lovelace cards

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.

That is 7 auto-entities cards inside a view. You can 100% copy each card yourself.

Copy what’s in the red box, as that’s the first auto-entities card. The second starts the line below it. Remove the leading spaces but maintain interior indentation.

EDIT: The marked solution contains the individual card.

Thanks!! I figured I was missing something simple here lol. I’ve only been using HA for a couple weeks and still learning how to make things. Couldn’t wrap my head around why I kept having a bunch of errors.

Been using this now since I got it all configured last month but noticed that during the last week or so, saturday games show under monday night football, and I suspect during the playoffs, there may be other instances of this. So to make it simpler, I simply have everything in 1 card that is all the games playing that week without being separated by the T## in the date.

However, I was surprised to see that there was no sort option for date. Is there a way to add that so I can just sort the games by when they are playing?