Use for-loops to create multiple cards for each room

Hey there!
I’m currently struggling with for loops within card arrays.

My goal would be to create three cards per room and group them together like so:

  • Temperature Sensor Card
  • Open Window Sensor Card
  • Thermostat Card

Do make this as dynamic as possible I wanted to provide an array of room names ([‘bathroom’, ‘office’, ‘livingroom’]) and iterate over that like so:

  cards:
      - type: "grid"
        columns: 2
        cards: >-
          {% for room in ['bad', 'buro'] %}
          - type: "custom:button-card"
            template: card_graph
            entity: sensor.{{room}}_temperature
            variables:
              ulm_card_graph_entity: sensor.{{room}}_temperature
          - type: "custom:button-card"
            template: card_binary_sensor
            entity: binary_sensor.{{room}}_open_window
          - type: "custom:button-card"
            template: card_thermostat
            entity: climate.{{room}}
            variables:
              ulm_card_thermostat_enable_controls: true
              ulm_card_thermostat_enable_popup: true
              ulm_card_thermostat_preset_mode: true
          {%- endfor %}

Sadly this won’t work out as excepted. The frontend just gives me an “Invalid configuration” error.
I couldn’t find any other solution so far so I would really appreciate some hints.

Thanks in advance and have a great weekend!
Max

1 Like

The standard cards don’t take any kind of “clever” configuration really. You should take a look at custom components like:

thomasloven/lovelace-auto-entities: :small_blue_diamond:Automatically populate the entities-list of lovelace cards (github.com)

iantrich/config-template-card: :memo: Templatable Lovelace Configurations (github.com)

thomasloven/hass-lovelace_gen: :small_blue_diamond: Improve the lovelace yaml parser for Home Assistant (github.com)

You should be able to achieve something similar to what you want, with one (or more) of these.

2 Likes