#Using template to generate Card

1 messages · Page 1 of 1 (latest)

graceful hollow
#

Hi all, I've been working with HA for a year or so and have accomplished a lot with thoughtful contributions from the community. I'm stuck on this one, but think there is an obvious answer I'm missing.

I'd like to take the script below to create a dynamic card for my media player groups. All of the code works perfect in generating an output notification, but I'm not sure how to make it work on the dashboard.

#

{% for entity_id in media_players if state_attr(entity_id, 'group_members')
== [entity_id] %}
- {{ entity_id }}
{% endfor %}

Grouped Speakers:

{% set ns = namespace(unique_groups=[]) %} {% for entity_id in media_players
if (state_attr(entity_id, 'group_members') or []) | length > 1 %}
  {% set sorted_members = (state_attr(entity_id, 'group_members') | sort) %}
  {% if sorted_members not in ns.unique_groups | map(attribute=0) | list %}
    {% set ns.unique_groups = ns.unique_groups + [(sorted_members, entity_id)] %}
  {% endif %}
{% endfor %}

{% set ns.result_array = [] %} {% for group in ns.unique_groups %}
  {% set formatted_names = group[0] | map('replace', 'media_player.', '') | map('replace', '_', ' ') | map('title') | join(' + ') %}
  {% set ns.result_array = ns.result_array + [[formatted_names] + group[0]] %}
{% endfor %}

cards: {% for group in ns.result_array %}
  - type: vertical-stack
    cards:
      - type: heading
        icon: ""
        heading: {{ group[0] }}
        heading_style: subtitle
      - type: custom:mini-media-player
        entity: {{ group[1] }}
        group: true
        info: scroll
        hide:
          progress: true
          name: true
        speaker_group:
          platform: sonos
          show_group_count: false
{% for entity in group[1:] %}
      - type: custom:mini-media-player
        entity: {{ entity }}
        artwork: none
        group: true
        hide:
          progress: true
          controls: true
          power: true
          info: true
        speaker_group:
          platform: sonos
          show_group_count: false
{% endfor %} {% endfor %}
vernal cradle
#

By default, you can't use templating with vertical stack cards (or the other default cards, other than the markdown card).

feral pebble
#

You cannot generate cards dynamically like this, however if you copy this into the template developer, it will give you the yaml to copy into your dashboard (assuming it's all correct)