Decluttering Card Plus Contents ↓ GitHub ↗

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.living_room
    name: Living Room
  - entity: light.kitchen
    name: Kitchen
    colour: blue
  - entity: light.bedroom
    name: Bedroom
  - entity: light.hallway
    name: Hallway

Four tiles, two columns, from one card.

Four room tiles in two columns, all from one card

Kitchen sets its own colour; the other three take the one set on the card.


On this page:

Items

An item is a set of values for one copy. It can carry a nested mapping, and each piece of it can be reached on its own (v1.2.0+) — which is what lets one item hold a room’s light, its sensor and its icon together:

for_each:
  - room: { name: Hall, light: light.hall, icon: 'mdi:lamp' }
content: '[[room.name]] uses [[room.light]]'   # and [[room]] is still the whole mapping

Three levels deep. A list is reached whole or not at all.

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 with variables:

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 Kitchen.

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

The same order holds for a repeat over what Home Assistant knows, and there it catches people out: each copy carries its own name, entity, area and so on, and those beat the card’s variables:. A card-level name: Lamp therefore renames nothing. To change what one copy carries, use overrides:; to give the template a value of your own, pick a name the copies do not carry, such as label.

Repeating over what Home Assistant knows (v1.1.0+)

Writing the list out means the dashboard stops being true the moment a lamp is added. for_each_from asks Home Assistant instead, so the card grows by itself:

type: custom:decluttering-card-plus
template: room_light
columns: 2
for_each_from:
  domain: light
  area: Kitchen

What to repeat over is the source. Entities are the default; the others each have a section below:

Source One copy per
entities (v1.1.0+) entity. Used whenever none of the keys below is given
areas (v1.1.0+) area
devices, floors, labels (v1.8.0+) device, floor or label
views (v2.0.0+) view of the dashboard the card is on
template (v2.0.0+) item of a list a Jinja template renders
range (v1.2.0+) number, up to the one you give

Then narrow the copies with filters such as domain, area, floor or label. Each copy is given the things it needs — the entity id, its name, the area it is in — and the list is worked out again when the registry changes, not on every state change.

Give one source per repeat. If a mapping holds more than one, the first of range, template, views, areas, devices, floors and labels wins and the rest are ignored.

for_each_from only repeats when it names a source or a filter. sort:, limit:, offset:, reverse:, require:, overrides:, group_by:, with:, include_hidden: and entity_category: shape a repeat but do not start one, so a mapping holding only those renders the template once, without a word. To shape every entity, say so:

for_each_from:
  entities: true        # not just group_by on its own
  group_by: domain

A written-out for_each wins over for_each_from if a card somehow has both: it is the more particular of the two.

An entity id written out in full under entities: is always kept (v2.0.0+), even one with a state but no registry entry, like sun.sun. See Hidden, diagnostic and config entities.

Filters built from the card’s variables (v2.4.0+)

for_each_from can hold placeholders, filled in from the card’s variables: and the template’s defaults before anything is looked up:

type: custom:decluttering-card-plus
template: tile
for_each_from:
  area: '[[room]]'
variables:
  - room: Kitchen

Two tiles, the kitchen light and the kitchen temperature sensor

That is mostly useful when the card is itself written by a template or a strategy, where the room arrives as a variable. The match count in the card editor fills it in the same way, and the health panel does not call room unread. Nor does the card editor’s own note about unused variables (v2.4.1+), which on v2.4.0 still said room was set but never used.

Patterns

Every source and filter takes true (anything), a pattern, or a list of patterns, any of which may match:

for_each_from:
  entities: ['light.kitchen_*', 'light.*_lamp']
  area: [Kitchen, living_room]      # a name or an id

* is the only wildcard, and it matches any run of characters, including none. A pattern has to match the whole text, so kitchen does not match kitchen_ceiling but kitchen* does. Case does not matter. There is no ? and no regular expression: light.a+b means exactly that text.

Each key matches against something particular. Entities match by entity id, never by friendly name. Areas, floors and labels match by id or by name. Devices match by id or by the name Home Assistant shows, which is the one you gave it if you renamed it. Views match by title or path. unit: is the exception to all of this: it is compared exactly, case and all, with no wildcard. The full list is in Repeat Reference.

What each copy is given

A copy from an entity sweep, and from an area, carries these. Every copy also gets the position values.

Entity copy Is
entity The entity id
name Its friendly name, else its registry name, else the id
domain The part of the id before the dot
area The name of its area, or of its device’s area. Empty when it has none
area_id That area’s id. Empty when it has none
category (v2.0.0+) The category it matched, only when category: narrows the repeat
total How many copies matched, before limit:
Area copy Is
area_id The area’s id
area Its name
area_icon Its icon. Empty when it has none
floor The name of its floor. Empty when it is on none
total How many copies matched, before limit:

An entity copy has no floor. Ask Home Assistant for it inside the template with [[entity|floor]] (see Placeholder Reference). An area grouped with with: gets three more values, and the other sources are listed in their own sections below and in Repeat Reference.


Repeating over devices, floors and labels: devices:, floors:, labels: (v1.8.0+)

One copy per floor, per label and per device, each named after what it repeats over

Entities and areas are not the only things worth a card each. The same for_each_from can repeat over the devices, the floors or the labels Home Assistant knows about:

for_each_from:
  floors: true          # every floor
for_each_from:
  labels: 'outside*'    # labels matching a pattern, by id or by name
for_each_from:
  devices: true
  area: Kitchen         # the devices in the kitchen

These sources read fewer filters than an entity sweep, and ignore the rest without a word:

Source Reads
devices: area (the device’s own area), floor, label (the device’s own labels), exclude patterns
floors: exclude patterns only
labels: exclude patterns only

So devices: true with domain: light still gives every device, and floors: true with area: Kitchen still gives every floor. To narrow what a device, floor or label holds, put the filters inside with: instead. Every source’s list is in Repeat Reference.

What each copy is given:

Source Variables
devices: device_id, device, name, area, area_id, manufacturer, model
floors: floor_id, floor, name, level
labels: label_id, label, name

A device’s device and name are the name Home Assistant shows, so a device you renamed carries your name, not the maker’s, and a devices: pattern has to match that name too. Anything a device or floor does not have, such as a model or a level, comes out empty.

Floors come in name order, like everything else. sort: level puts them in the order they sit in the house, comparing the levels as numbers, so a basement at -1 comes first:

for_each_from:
  floors: true
  sort: level

All three take with:, the same as an area does, so a copy can carry the entities that belong to it — a card per floor listing that floor’s lights, say. See A card per area, listing what is in it for how with: works.

Home Assistant does not hand the label registry to cards the way it does the others, so the card fetches it once per page to show a label’s name rather than its id (v1.8.0+). Until it arrives, a labels: repeat has only the labels some entity or device carries, named by their ids, and a pattern written against a label’s name matches nothing. Once it lands, a moment later, every label gets a copy, including one nothing carries yet.

Repeating over the dashboard’s views: views: (v2.0.0+)

A row of four buttons, one per view of the dashboard, matching the view tabs above them

views: true makes one copy per view of the dashboard the card is on, in the dashboard’s own order. That is the makings of a navigation bar that stays true when a view is added, renamed or moved:

type: custom:decluttering-card-plus
template: view_button
columns: 4
for_each_from:
  views: true
decluttering_templates:
  view_button:
    card:
      type: button
      name: '[[name]]'
      icon: '[[icon]]'
      tap_action:
        action: navigate
        navigation_path: '[[navigation_path]]'

Each copy is given:

Name Is
name The view’s title, or its path when it has none
path The view’s path, or its position when it has none
navigation_path /<dashboard>/<path>, ready for a navigate action
icon The view’s icon, empty when it has none
view_index Its position among all the views, counting from 0

Subviews are left out, since they are reached from a card rather than from a bar; subviews: true brings them in. views: can also be a pattern or a list of them, matched against a view’s title or path, and exclude: patterns match the same way. sort:, limit:, offset:, require: and overrides: (keyed by path or title) work as they do for any other source, except that with no sort: the dashboard’s order is kept rather than sorting by name.

The views come from the dashboard’s configuration, so editing the dashboard rebuilds the card.

Repeating over a list a Jinja template works out: template: (v2.0.0+)

Three tiles for the lights that were on, and four markdown cards each naming a room and its temperature

Some lists no filter here can describe. template: hands the job to Home Assistant’s own templating and repeats over the list it renders:

for_each_from:
  template: >-
    {{ states.light | selectattr('state', 'eq', 'on')
       | map(attribute='entity_id') | list }}

The template is rendered once, when the card is built, and not again as states change. “The lights that are on” means the lights that were on when the dashboard was opened; reload it to render the list afresh. That is deliberate, since following every state change would rebuild the card each time, which is what this card is built to avoid. Until the first render arrives the card shows nothing rather than flashing its empty: card.

What the list holds decides what each copy is given:

The list holds Each copy gets
Entity ids, every one of them known The same values an entities: sweep gives: entity, name, domain, area, area_id
Mappings That mapping, as its variables
Anything else The value, as item
for_each_from:
  template: >-
    {% set ns = namespace(rooms=[]) %}
    {% for s in states.sensor if s.attributes.device_class == 'temperature' %}
    {% set ns.rooms = ns.rooms + [{'name': s.name | replace(' Temperature', ''), 'temp': s.state}] %}
    {% endfor %}{{ ns.rooms }}

gives each copy a name and a temp.

A list of entities can still be narrowed by domain, area, exclude and the rest, but hidden and diagnostic entities are kept: the template picked them. The template’s own order is kept unless there is a sort:; limit:, offset:, reverse:, require: and overrides: work as usual. A list that arrives as text, in JSON or with Python’s quotes, is read as a list. A template that fails, or renders something that is not a list, says so in the browser console and repeats nothing, which shows the empty: card if there is one. A url carrying an access token, such as a camera’s proxied picture, is dropped from the copies.

The card editor’s match preview renders the template too, and shows what it listed or why it listed nothing.

Narrowing what it matches (v1.1.0+)

A sweep that takes everything is rarely what you want. These narrow it, and can be combined; a copy has to pass every one you give:

Key Narrows to Matches by
domain entities of a domain light, binary_sensor, …
device_class (v1.2.0+) entities reporting a device class motion, door, temperature, …
integration (v1.2.0+) entities from one integration hue, zha, mqtt, …
area entities in an area its name or its id
floor entities on a floor its name or its id
label things carrying a label its name or its id
unit (v2.0.0+) entities measured in a unit kWh, °C, %, … exactly as the state reports it
state_class (v2.0.0+) entities with a state class measurement, total, total_increasing
category (v2.0.0+) entities filed under a category its name or its id

A domain on its own is usually too coarse — nobody wants motion, door and smoke sensors on the same card:

for_each_from:
  domain: binary_sensor
  device_class: motion

A label put on a device counts for all of that device’s entities, which is how labels are normally used. A label on an area does not count for the entities in it.

These are for entity sweeps. An areas: repeat reads area, floor and label (the area’s own labels) and ignores the rest; devices, floors and labels read fewer still, as their section says.

Narrowing by unit and state class (v2.0.0+)

Four temperature tiles picked by unit, and two humidity tiles picked by unit and state class

unit: keeps entities whose state reports that unit_of_measurement, and state_class: those with that state class. Either takes a list:

for_each_from:
  unit: '%'
  state_class: measurement
for_each_from:
  unit: [kWh, Wh]
  state_class: total_increasing     # every energy meter

A unit is compared exactly, case and all, because mW and MW are a milliwatt and a megawatt. state_class: takes the usual patterns. Both are read when the copies are worked out, not watched, since neither changes while an entity exists. They work wherever the other filters do: an entities: sweep, an area’s with:, group_by: and an exclude: mapping.

Narrowing by category (v2.0.0+)

Three button tiles, the helpers filed under the Lighting category

Home Assistant lets you file automations, scripts, scenes and helpers into categories on their settings pages. category: keeps what is filed under one, by its name or its id, so one card can carry a button per automation in your Lighting folder and keep up with the folder:

type: custom:decluttering-card-plus
template: entity_tile
columns: 3
for_each_from:
  category: Lighting

This is Home Assistant’s category on an entity, not the category: a template can be filed under in the library.

Each list has categories of its own; any of them counts, so a category called Lighting among the automations and one among the helpers both match. Patterns and lists work as they do for label:. Each copy is also given category, the name of the category it matched.

Cards are not handed the categories the way they are handed areas and labels, so the card fetches them once per page, and only when something on the page narrows by category. Until they arrive a category matches nothing; the card fills in a moment later. Filing an entity under a category, or renaming one, reaches the card straight away.

Leaving things out with exclude:

for_each_from:
  domain: light
  exclude: light.bedside            # a pattern, or a list of them

Written as patterns, exclude means entity ids — which is what it is nearly always for. On an areas: repeat the patterns mean areas, by id or name; on devices, floors, labels and views they mean those, the same way the source itself matches them.

Written as a mapping, exclude is a set of filters, and it drops only what matches every filter in it:

for_each_from:
  domain: [light, sensor]
  exclude:
    area: Kitchen
    domain: sensor        # drops the kitchen's sensors; the kitchen's lights stay

So a mapping cannot leave out two unrelated things at once, such as everything in the bedroom or everything labelled hidden: written together, those drop only the bedroom’s labelled entities. List the unwanted entity ids as patterns instead, or narrow the sweep with a positive filter (floor: Ground floor) so the bedroom never comes in.

Only entity sweeps and areas: read the mapping form. Devices, floors, labels and views read just the entities: patterns out of a mapping and ignore its other filters.

What is left out wins over what is taken in, so “all of these except those” is one line rather than a list of everything you did want.

Hidden, diagnostic and config entities (v2.0.0+)

Three columns: a sweep of the sun integration finds nothing, the same sweep with entity_category diagnostic finds its sensors, and sun.sun named outright is kept

This changed in v2.0.0. A repeat over the registry (an entities: sweep, an area’s or device’s with:, group_by:) now leaves out the entities Home Assistant hides and its diagnostic and config entities: the signal-strength sensor, the identify button, the firmware switch that integrations hang off a device. Home Assistant’s own automatic dashboards leave those out too. Before v2.0.0 only hidden entities were left out, so a sweep that used to show diagnostic or config entities shows fewer copies after the update.

Two options bring them back:

for_each_from:
  integration: sun
  entity_category: diagnostic    # or config, [diagnostic, config], or all
  include_hidden: true           # hidden entities too

entity_category: adds those entities to the sweep alongside the ordinary ones; it does not narrow it to only them. entity_category: true means the same as all.

An area’s, device’s, floor’s or label’s with: gathers its entities in a sweep of its own, so for those the two options go inside with:; written beside it they do nothing:

for_each_from:
  devices: 'Bedside*'
  with:
    entity_category: all     # the lamp's signal-strength sensor and identify button too
    include_hidden: true

An entity id written out in full under entities: is never dropped by either rule, because somebody typed that id and so wants it. That includes an entity with a state but no registry entry, like sun.sun, which a sweep never found before; those come after the registry’s, in the order written, and the other filters still apply to them. A pattern (sun.*) still only sweeps the registry.

for_each_from:
  entities: [sun.sun, sensor.sun_next_dawn]   # both kept

Order, and how many: sort:, reverse:, limit: (v1.2.0+)

for_each_from:
  domain: light
  sort: area        # name (the default), entity, id, area, domain, floor
  reverse: true
  limit: 5

sort: orders the copies, and without it they come in name order (views and Jinja lists keep their own order instead). entity and id are the same thing: the entity id, or the area id on an area copy. sort: floor only does anything on area copies, because an entity copy carries no floor; on an entity sweep every copy ties and the name order decides. reverse: turns whichever order you chose around.

sort: none (v1.3.0+) keeps the registry’s own order, which is how you ask for “however Home Assistant listed them”.

Ordering by what the copies carry (v1.3.0+)

A sort: that is not one of those names is read as a key on the copies themselves, so anything a repeat hands its template can also order it — including the values a grouped repeat works out:

for_each_from:
  areas: true
  with:
    domain: light
  sort: entity_count      # the emptiest rooms first

Numbers are compared as numbers, so 2 comes before 10 rather than after it. A copy that does not carry the key at all counts as empty and sorts to the front, and copies that tie fall back to the usual name order.

Backwards, and breaking ties (v1.5.0+)

A leading minus turns one key around, and a list of keys sorts by the first and breaks ties with the next:

for_each_from:
  areas: true
  with:
    domain: light
  sort: [floor, -entity_count]   # floor by floor, the busiest room in each one first

reverse: still flips the whole order at the end, tiebreaks and all. The minus is for turning one key around while the others stay as they are.

Ordering by an attribute with sort: attr: (v1.5.0+)

attr: reads an attribute off the entity’s state rather than a key the copy carries:

for_each_from:
  domain: climate
  sort: -attr:current_temperature      # warmest room first

It reads attributes only, not the state, so it suits a thermostat’s current_temperature but cannot order temperature sensors, whose reading is their state.

This is a snapshot taken when the card is built, not a live order — the copies will not rearrange themselves as the readings change. An entity without the attribute counts as empty and sorts to the front.

limit: caps how many copies are built. Every copy still carries total — what matched before the limit — so a card can be honest about what it is not showing:

card:
  type: markdown
  content: 'Showing [[count]] of [[total]] lights'

count is the number of copies actually built: after limit:, and also after the template has dropped copies that leave a required: true variable empty. So total can be higher than count even with no limit. Every for_each_from source gives total; a written-out for_each does not, since its count already says it.

Splitting one list across two cards with offset: (v1.5.0+)

Two cards, the first showing two lights and the second the next two from the same list

offset: skips the first few copies, so two cards side by side can show different parts of the same list:

# the left card
for_each_from:
  domain: light
  limit: 10

# the right card
for_each_from:
  domain: light
  offset: 10
  limit: 10

total ignores the window on purpose: every page of a split list says the same “of 23”, which is the point of splitting it.

Copies that must have a value: require: (v1.5.0+)

Four light tiles above, and three below with the hallway light dropped

require: names keys a copy has to have filled in. An entity in no area, or an area on no floor, is dropped:

for_each_from:
  domain: light
  require: area          # or a list - [area, floor]

It runs before anything is counted, so total says what is actually shown rather than what matched. A key the copies never carry empties the repeat, since no copy has it filled in: check the name against what each copy is given. This is about the values the repeat works out for itself; to drop a copy because a variable you pass it came out empty, see Leaving out a copy that has nothing to show.

Different variables for one copy: overrides: (v1.5.0+)

Four light tiles, one of them renamed by an override and re-sorted to the end

overrides: gives particular copies extra variables without excluding them. Each key is a pattern, matched against:

Copy Matched by
An entity its entity id
An area its area id, or its name (v2.4.0+)
A device, a floor or a label (v2.4.0+) its id or its name
A group_by group (v2.4.0+) the group: light, an area’s id…
A view (v2.0.0+) its path or its title
A plain value from a Jinja list (v2.0.0+) the value

range copies have nothing an override can match, so overrides: does nothing on those. Before v2.4.0 only entity, area, view and Jinja-list copies could be overridden.

for_each_from:
  domain: light
  overrides:
    'light.*':
      row: compact
    light.kitchen_ceiling:
      icon: mdi:ceiling-light
      name: The big one

Every pattern that matches is applied in turn, so a specific id can build on a wildcard above it. Overrides are applied before require: and before sorting, so one that changes name also changes where that copy lands in the order.

This is the way to change a value a copy carries. A name: in the card’s variables: loses to each copy’s own name, as Sharing values between copies explains.

Repeating a fixed number of times with range: (v1.2.0+)

for_each_from:
  range: 4

Four copies, with nothing but the position and total to go on. Useful for a row of slots, or for laying something out before the entities behind it exist. Asking for range: 0 gives no copies — not, as you might fear, every entity in the house. The most it gives is 1000 (v1.4.0+); a bigger number is cut to that.

range: takes nothing else. sort:, reverse:, limit:, offset:, require: and overrides: are all ignored, so range: 10 with limit: 3 still gives ten copies: ask for three.

When nothing matches: empty: (v1.2.0+)

A repeat that matches nothing renders nothing. That is deliberate — a dashboard should not break because a room has no motion sensor yet — but silence looks the same as breakage, so a card can say so on purpose:

type: custom:decluttering-card-plus
template: motion_tile
for_each_from:
  domain: binary_sensor
  device_class: motion
empty:
  type: markdown
  content: No motion sensors set up yet.

The empty: card shows when no copies are left, including after the template has dropped the ones that leave a required: true variable empty.

Why a repeat can come out empty

When a repeat you expected to fill shows nothing, it is usually one of these:

Repeat Reference has the full list. The card editor’s match preview shows what the repeat matches right now, which narrows it down quickly.

A card per area, listing what is in it (v1.2.0+)

This is the shape people build most: a tile per room, each showing that room’s lights. with: gathers the entities for each area and hands them to the copy.

Three room cards, each listing the light in that room

One card. Each copy is an area, and the tiles under it come from a second template repeated over that area’s own entities.

# The outer card - one copy per area.
type: custom:decluttering-card-plus
template: room_summary
for_each_from:
  areas: true
  with:
    domain: light
columns: 3
min_column_width: 240
# The template, which repeats again over what its area holds.
type: custom:decluttering-template-plus
template: room_summary
card:
  type: vertical-stack
  cards:
    - type: markdown
      content: '### [[area]] — [[entity_count]] lights'
    - type: custom:decluttering-card-plus
      template: light_tile
      for_each: '[[items]]'

Each area copy is given, on top of the usual area values:

Name Is
items The whole mappings, ready to hand to a nested for_each
entities Just the entity ids, as a list
entity_count How many there are

The area being grouped always wins over any area: inside with: — that is the point of grouping.

with: takes the same filters as an entity sweep, and include_hidden: and entity_category: belong in it too. What it gathers always comes in name order: a sort: or limit: written inside with: is ignored. A sort: beside it orders the areas themselves, which is how sort: -entity_count puts the busiest room first.

with: works on areas:, devices:, floors: and labels: repeats. A device gathers the entities that belong to it, and a floor or label those on it or carrying it. On an entity sweep, views:, a Jinja list or range: it is ignored.

An area with nothing in it is left out, because a card for a room with no lights is noise rather than news. with: { keep_empty: true } keeps it, which is how you get to say “nothing in here” on purpose.

One copy per group: group_by: (v1.8.0+)

One copy per domain, each showing how many entities fell into it

group_by: folds a repeat over entities into one copy per distinct value — per domain, area, floor or label:

for_each_from:
  entities: ['light.*', 'sensor.*_temperature']
  group_by: domain

Each copy is given group (the value it groups on), name, entity_count, and the members as entities and items — the same shape with: gives an area, so a template written for one works for the other. name is the domain, or the area’s, floor’s or label’s name. An entity with nothing to group on, like one on no floor, is left out rather than lumped into a group of its own. Grouping by label puts an entity in every group whose label it carries.

Those four are the only values group_by: takes; anything else, such as integration, gives no copies at all. It groups entity sweeps only, and is ignored on areas, devices and the other sources. The members of each group come in name order, and sort:, limit: and require: then apply to the groups, not the members: sort: -entity_count puts the biggest group first, and require: area empties the repeat, since a group carries no area.

Numbering the copies (v1.1.0+)

Every copy is also given [[index]], counting from one, and [[count]], the number of copies — so a template can number itself without you writing the number into each item:

card:
  type: markdown
  content: 'Room [[index]] of [[count]]'

Three more (v1.2.0+) help with the ends of the list:

Name Is
index Its place, counting from 1
index0 (v1.2.0+) Its place, counting from 0
count How many copies were built
first (v1.2.0+) true for the first copy
last (v1.2.0+) true for the last copy

first and last are real booleans, so they suit if:, say to drop a divider after the last copy.

An item that sets any of these names itself wins, and so does the card’s own variables:, so a template already using those names for something else keeps working.

(v2.9.0+) Every kind of repeat gives these five: a card’s copies, the rows and picture elements below, repeated sections, badges and views, and the copies repeat: puts in a list. A template’s own visibility: can read them too, so variable: first with equals: true keeps only the first copy. Visibility sees each copy’s place among all the candidates; the copies that are built are then numbered again, so the numbers on the page always run 1, 2, 3 with no gaps.


Leaving out a copy that has nothing to show (v1.1.0+)

for_each skips an item that leaves a required: true variable empty, which is what lets one template serve a room with four lights and a room with one:

type: custom:decluttering-template-plus
template: room_light
variables:
  - name: entity
    required: true
card:
  type: tile
  entity: '[[entity]]'
type: custom:decluttering-card-plus
template: room_light
columns: 4
for_each:
  - entity: light.kitchen
  - entity: light.hall
  - entity: ''
  - {}

That renders two cards, not four, with no gaps where the other two would have been. A template that requires nothing renders every item as before. See Describing Variables for required.

(v2.9.0+) For more than “this one is set”, a template can say which copies are worth building with visibility:, and edit mode then shows a dashed slot naming the reason for each one left out. See Visibility.


Rows and picture elements repeat too (v2.9.0+)

An entities card with a row per light between the Sun row and a switch, and a floor plan with a light icon in each room

A repeat used to need a card template. A row: template given for_each or for_each_from now becomes a row per item, in place, inside an ordinary entities card. The entries written before and after it stay where they were:

decluttering_templates:
  light_row:
    row:
      entity: '[[entity]]'
      secondary_info: last-changed
type: entities
title: Lights
entities:
  - sun.sun
  - type: custom:decluttering-card-plus
    template: light_row
    for_each_from:
      domain: light
      sort: name
  - type: divider
  - switch.heater

An element: template does the same inside a picture-elements card: one entry becomes an element per item, each placed by its own values, so a floor plan gets an icon per light from one entry:

decluttering_templates:
  light_marker:
    element:
      type: state-icon
      entity: '[[entity]]'
      tap_action:
        action: toggle
      style:
        left: '[[x]]%'
        top: '[[y]]%'
type: picture-elements
image: /local/floorplan.svg
elements:
  - type: custom:decluttering-card-plus
    template: light_marker
    for_each:
      - entity: light.living_room
        x: 26
        y: 36
      - entity: light.kitchen
        x: 73
        y: 36
      - entity: light.bedroom
        x: 26
        y: 80
      - entity: light.hallway
        x: 73
        y: 80

The position goes in the template’s style: here, filled from each item, rather than in a style: on the entry, since one entry now stands for several elements.

Every repeat option works as it does for cards: the sources and filters, sort, limit, exclude, require, overrides, template_by_domain (every template it picks has to be a row, or every one an element), empty, and the template’s own visibility:. An empty: here is a row or an element, such as {type: section, label: No fans}.

The options that lay copies out in a grid, columns, min_column_width, gap, lazy and lazy_eager, mean nothing in a list of rows or on a picture. They are ignored, and the console says so:

columns mean nothing for a repeated row (“numbered_row”) and are ignored - rows run down the card and elements sit where their style puts them.

Badges still cannot repeat on their own, since Home Assistant gives a badge nowhere to put a second copy; a view can repeat them, see Repeating Sections and Badges.


Layout

columns Result
absent, or 1 The copies stack vertically
2 or more The copies are laid out in that many columns
with min_column_width columns becomes the most it will use; a column is dropped rather than going narrower

Set min_column_width whenever you set columns. Without it the copies keep every column on a phone, each one squeezed; the card editor points this out (v1.11.0+).

The copies all live inside the one card, so they cannot reflow across a sections view or have a heading and background each. For that, repeat whole sections instead: see Repeating Sections and Badges (v1.11.0+).

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.


Long repeats: lazy: (v2.0.0+)

The card editor's two lazy settings: the switch to build copies as they scroll into view, and the number built at once

A repeat over half the house can be a hundred copies, and each one is a real card Home Assistant creates when the page opens, most of them far below the fold. lazy: true builds the first few straight away and the rest as they come within a few hundred pixels of the screen:

type: custom:decluttering-card-plus
template: entity_tile
lazy: true
lazy_eager: 4         # built at once; 6 if left out
for_each_from:
  domain: sensor

A copy that is waiting holds its place at the height of the first copy, so the page does not jump as they fill in, and once built it stays built. Which copies there are is still worked out up front; only creating the cards waits.

The editor’s preview and any debug setting build every copy, since both are there to show everything. A group template is never lazy. The card editor offers both settings with the other repeat options.

A repeat of more than 50 copies gets a note in the browser console: one that is not lazy is pointed at lazy: true, and one that already is is told the list is still a long way to scroll. See also Troubleshooting.


What it needs

A repeat needs a template that defines a card, a row (v2.9.0+) or a picture element (v2.9.0+). Home Assistant gives a badge a single slot, so there is nowhere for a second copy to go, and asking for one is an error:

for_each needs a template that defines a card, a row or a picture element. “porch_badge” defines a badge, which Home Assistant gives one slot - put what you want repeated inside a card template instead.

Badges can still be repeated by a view: see Repeating Sections and Badges. Before v2.9.0 rows and elements were refused the same way.

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

The keys a repeating card takes. The keys that go inside for_each_from — every source, filter and shaping key, with an example each — are in Repeat Reference, along with which value wins when a copy, the card and the position all set the same name.

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. The template can build a card, or (v2.9.0+) a row or a picture element
columns number How many copies sit side by side. Defaults to 1
min_column_width (v1.1.0+) number Pixels. Drops a column rather than going narrower, so one card suits a phone too
for_each_from (v1.1.0+) mapping What to repeat over, read from Home Assistant instead of written out. See its keys
empty (v1.2.0+) card What to render when the repeat produces no copies, or every copy was dropped
gap (v1.2.0+) number Pixels between the copies. Leave it out for Home Assistant’s own spacing
lazy (v2.0.0+) boolean Build copies only as they come near the screen
lazy_eager (v2.0.0+) number How many copies a lazy repeat builds straight away. Defaults to 6

Next

→ Using Templates · Describing Variables → Repeating Sections and Badges for a section or badge per item → Recipes for finished examples