Decluttering Card Plus Contents ↓ GitHub ↗

Sharing Templates Between Dashboards

Templates normally belong to the dashboard that defines them. If you keep several dashboards — a main one, a tablet one, a phone one — you would otherwise have to paste the same decluttering_templates block into each, and keep all the copies in step.

decluttering_templates_from lets one dashboard borrow another’s templates.


Setting it up

1. Put the templates on one dashboard

Pick (or create) a dashboard to hold the library. Its URL path is what you will refer to — find it under Settings → Dashboards. Here it is dcp-shared.

# dashboard: dcp-shared
decluttering_templates:
  house_room:
    card:
      type: tile
      entity: '[[light]]'
      name: '[[room]]'
      icon: '[[icon]]'
      features:
        - type: toggle
    default:
      - icon: mdi:lightbulb

views:
  - title: Shared templates
    cards: []

A dashboard used only as a library can be hidden from the sidebar — the templates are still readable.

2. Borrow them

On the other dashboard, add decluttering_templates_from at the root, alongside views::

# dashboard: dcp-borrow
decluttering_templates_from:
  - dcp-shared

views:
  - title: Borrowed
    cards:
      - type: custom:decluttering-card-plus
        template: house_room
        variables:
          - light: light.kitchen
          - room: Kitchen
          - icon: mdi:silverware-fork-knife

That dashboard defines no templates of its own, yet house_room works:

A dashboard with no templates of its own rendering three cards from a borrowed template

Borrowing from every dashboard: '*' (v1.8.0+)

decluttering_templates_from: '*'

'*' stands for every dashboard there is, so a library spread across several of them is available everywhere without listing each one. Named sources can sit alongside it, before or after: in ['dcp-shared', '*'], a template dcp-shared defines beats one of the same name from any dashboard the star brings in, and nothing is fetched twice. Between two dashboards the star brings in, the one whose path sorts later wins, and the original dashboard beats them all, so name the dashboard you mean whenever two might define the same template.

A library every dashboard gets: decluttering-templates (v1.13.0+)

Create a dashboard with the URL path decluttering-templates (Settings → Dashboards → Add dashboard, then set the URL to decluttering-templates) and put your templates on it. Every other dashboard borrows from it without naming it — no decluttering_templates_from needed.

# dashboard: decluttering-templates
decluttering_defaults:
  icon: mdi:ceiling-light
decluttering_templates:
  house_light:
    description: A light tile in the house style
    card:
      type: tile
      entity: '[[entity]]'
      icon: '[[icon]]'
      features:
        - type: toggle
# any other dashboard
type: custom:decluttering-card-plus
template: house_light
variables:
  - entity: light.living_room

The card editor says where the template came from:

The card editor showing a template borrowed from the library dashboard

The library is read first, so anything else wins a name clash: a dashboard’s own templates, and any dashboard it names in decluttering_templates_from. When there is no such dashboard nothing is said about it; the card only looks for it.

A template here can be built on with extends: from any other dashboard. Before v2.6.0 a dashboard’s own template that extended a library one sometimes built on its own, as if the parent were not there, until the page was reloaded.

The library’s values reach every dashboard (v2.6.0+)

The library’s decluttering_defaults and decluttering_variables now go to every template on every dashboard, not only to the library’s own templates. Set a house colour or an icon once on the library, and a template written on any dashboard can use it without saying so. In the example above, a template on another dashboard that uses [[icon]] and gets no icon from anywhere else gets mdi:ceiling-light.

They sit under everything else: the card, the view and section, the template, the template’s own dashboard, and any other dashboard it was borrowed from all win. So they only ever fill a name nothing else does, and nothing that already works changes. A declaration in the library’s decluttering_variables likewise only describes a variable that no template or dashboard nearer to the card describes.

The library’s decluttering_let is not shared this way. A let beats even the card’s own values, so a library let reaching every dashboard would force a value onto every card everywhere, with no way to override it from the card. It still reaches the library’s own templates, as any lender’s does.

A card that builds before the library has been read builds with what it has and builds again once the library arrives, only if the library gives it something.


Item templates come along too (v2.7.0+)

The item templates that with: and repeat: fill in are found in the same places as card templates: on the library dashboard, on the dashboards named in decluttering_templates_from, and on the card’s own dashboard, which wins. Before v2.7.0 only the card’s own dashboard was searched, so an item template kept on the library was reported as missing. A card whose sources are still loading builds again once they arrive.

A template that extends: one borrowed from another dashboard also keeps its parent’s own defaults ahead of a dashboard default with the same name (v2.7.0+).

Page templates and versions (v2.9.0+)

A page template, a whole dashboard under dashboard:, is borrowed like any other. Keep it on the library dashboard, or on one a strategy names in decluttering_templates_from, and each dashboard that uses it is a strategy of a few lines:

strategy:
  type: custom:decluttering-card-plus
  decluttering_templates_from: house-templates
  page:
    template: house_page
    variables:
      house: The Granary

A section strategy reads borrowed templates too, and the add-section dialog previews with one when the dashboard has none of its own.

When a dashboard keeps its own copy of a template it also borrows, and the borrowed copy has a lower version:, Dashboard health points it out: “room_light_tile” on house-templates is version 2; this dashboard’s is version 3. Both copies need a version: for that.

Rules

Your own templates win. A dashboard’s own decluttering_templates (and its decluttering-template-plus cards) take precedence over anything borrowed, so borrowing can never silently change a template you already have. Use that to override one template from the library while borrowing the rest.

Several sources are allowed, listed in order:

decluttering_templates_from:
  - shared-core
  - shared-experimental

Later sources override earlier ones; your own dashboard still beats all of them.

A single string works too, for the common one-source case:

decluttering_templates_from: shared-core

Borrowing is not recursive. If A borrows from B, and B borrows from C, then A gets B’s own templates but not C’s. List C in A as well if you need it.

The default dashboard — the original one with no URL path of its own — can be named as lovelace, default, or an empty string.


Shared default values (v1.1.0+)

A dashboard can set values its templates fall back on, so a colour or a size shared by a library of them is written once instead of repeated in every template’s default: list:

decluttering_defaults:
  colour: amber
  icon_size: 32px
decluttering_templates:
  room_tile:
    card:
      type: tile
      entity: '[[entity]]'
      icon_color: '[[colour]]'
views: []

They sit at the bottom of the order, so nothing you already have changes: a value on the card wins, then a declaration’s default, then the template’s default: list, and these last.

In YAML mode a yaml anchor already does this. In storage mode there are no anchors, which is the whole reason for it.

A borrowed template gets both dashboards’ values, yours first. The lender’s are still underneath, so a template goes on working where it lives — and borrowing a library never means giving up what you set here. The same goes for the lender’s decluttering_variables and decluttering_let: a borrowed template gets them, with this dashboard’s ahead. Under both come the library dashboard’s (v2.6.0+), last of all, for every template wherever it came from; the library’s decluttering_let is not among them.

decluttering_defaults can be written as a mapping, as above, or as a list (- colour: amber); both are read the same.

A template card’s preview uses them too (v1.10.0+), so a template can leave a value to the dashboard and still preview properly in edit mode. That makes this the place for a value that should differ between dashboards sharing one template — a [[dashboard]] in a navigation path, say: leave it out of the template, set it here on each dashboard, and each one’s own value wins for the cards on it.

Defaults for one view (v1.8.0+)

A view can carry its own decluttering_defaults, for the cards on that view only:

decluttering_defaults:
  colour: amber            # the whole dashboard
views:
  - title: Bedroom
    decluttering_defaults:
      colour: slate        # just this view
    cards: []

They come straight after the card: a value on the card wins, then the view’s, then a declaration’s default, then the template’s default: list, then the dashboard’s. So one view can look different without touching a single card or template, even where the template gives the same name a default of its own.

Changed in v1.9.0: in v1.8.0 a view’s values sat underneath the template’s defaults and only filled names the template left empty. The dashboard-wide ones have not moved.

Defaults for one section (v1.10.0+)

Two sections of the same lamp tiles, amber downstairs and indigo upstairs

In a sections view, a section can carry its own decluttering_defaults too, so the same templated cards can sit in several sections of one page with different values:

decluttering_defaults:
  colour: amber            # the whole dashboard
views:
  - title: House
    type: sections
    sections:
      - type: grid
        cards:
          - type: heading
            heading: Downstairs
          - type: custom:decluttering-card-plus
            template: lamp_tile
            variables:
              - entity: light.living_room
          - type: custom:decluttering-card-plus
            template: lamp_tile
            variables:
              - entity: light.kitchen
      - type: grid
        decluttering_defaults:
          colour: indigo       # just this section
        cards:
          - type: heading
            heading: Upstairs
          - type: custom:decluttering-card-plus
            template: lamp_tile
            variables:
              - entity: light.bedroom
          - type: custom:decluttering-card-plus
            template: lamp_tile
            variables:
              - entity: light.hallway

A section’s values beat its view’s, so the full order is: the card, the section, the view, a declaration’s default, the template’s default: list, the dashboard. Cards inside a stack or grid within the section count as in it.

To add them from the UI, edit the section and switch its editor to YAML. Editing the section’s other settings afterwards keeps them.


Caching and refreshes

Reading another dashboard is a round trip to Home Assistant, so the result is cached for the life of the page. One fetch per source dashboard, however many templated cards are on screen.

The consequence: after editing a template on the source dashboard, refresh the browser on the dashboards that borrow it. They will not pick the change up on their own. Editing a template on the dashboard that owns it behaves as normal.

Timing

A borrowed template has to be fetched, which cannot happen while the card is first being configured. Cards that use one therefore resolve a moment after the page loads. You may see them appear a beat behind the rest of the dashboard on a slow connection. Nothing is wrong.

The template dropdown in the card editor behaves the same way — it starts with the local templates and fills in the borrowed ones once they arrive, so it will not accuse you of a bad template name while it is still loading.

When it goes wrong

If the template cannot be found anywhere, the card shows:

The template “…” doesn’t exist in decluttering_templates, in a custom:decluttering-template card, or on any dashboard listed in decluttering_templates_from

If the source dashboard cannot be read at all — wrong URL path, or no permission — a warning goes to the browser console naming the dashboard:

decluttering-card-plus: could not read the dashboard "shared-core": …

Check the URL path against Settings → Dashboards. It is the path segment in the address bar, not the dashboard’s title.


→ Defining Templates · Troubleshooting

To hand a template to another person rather than another dashboard, see Sharing a Template.