Skip to content

Repeating a Template

tempus2016 edited this page Aug 18, 2026 · 6 revisions

Repeating a Template

A template removes the repetition inside a card. for_each removes the repetition around it — one card renders the template once per item in a list, instead of you pasting the same instance block out four times.

type: custom:decluttering-card-plus
template: room_tile
columns: 2
variables:
  - colour: amber
for_each:
  - entity: light.kitchen
    name: Kitchen
  - entity: light.hall
    name: Hall
  - entity: light.shed
    name: Shed
    colour: blue

Three tiles, two columns, from one card.


Items

Each item holds the variables for that copy, written as a mapping — which reads better than a list of one-key entries when every copy sets the same few things:

for_each:
  - entity: light.kitchen
    name: Kitchen

A list of one-key entries works too, if you prefer it to match variables::

for_each:
  - - entity: light.kitchen
    - name: Kitchen

A for_each that is a single mapping rather than a list counts as a list of one, the same forgiveness variables: gets — and an empty mapping counts as no list at all, so a cleared field never leaves a stray single-copy stack behind.

Sharing values between copies

Anything in the card's own variables: is shared by every copy, and an item can override it. Above, colour is amber everywhere except the shed.

That is the usual shape: put what is the same in variables:, and what differs in for_each.

Layout

columns Result
absent, or 1 The copies stack vertically
2 or more The copies are laid out in that many columns

The copies are handed to Home Assistant's own vertical-stack and grid cards, so they behave exactly like any other card in your layout — including in the sections layout, and including visibility conditions on each copy.


What it needs

for_each needs a template that defines a card. A row, badge or element has nothing to stack, and asking for one is an error:

for_each needs a template that defines a card

An empty list renders nothing rather than failing the card, so a list you build up over time can start out empty without breaking the dashboard.

A style on the card resolves against the card's own variables and the template's declared defaults, not any one item's values — per item it would be ambiguous.


In the editor

For a card template, the card editor offers Repeat for each and Columns below the variables.

The missing-variable warning accounts for the items, so a card whose for_each supplies entity and name is not told they are missing. See Describing Variables.


When not to use it

If the copies differ by more than a few values — different card types, different structure — you want separate cards, or a template with visibility conditions inside it. for_each is for the case where the only thing that changes is what you pass in.


Reference

Key Type Description
for_each list One copy of the template per item; each item is that copy's variables. A single mapping counts as a list of one
columns number How many copies sit side by side. Defaults to 1

Next

Using Templates · Describing VariablesRecipes for finished examples

Clone this wiki locally