DEV Community

Cover image for Custom Cards in Home Assistant: Mushroom and button-card
Michael Bernhart
Michael Bernhart

Posted on Edited on Originally published at cloudapp.dev

Custom Cards in Home Assistant: Mushroom and button-card

Correction, November 2026: An earlier version of this post showed translated and simplified versions of the code; the excerpts below are now the real code with its German labels. It also called the VM a demo instance, gave the dashboard 300 instead of 481 lines and the fonts 76 instead of about 64 KB, said the battery sensor updates every few seconds (it changes at most once a minute), pointed to a complete YAML file in the templates post that was never there, and ended with advice for a daily dashboard that my own daily dashboard does not follow. While correcting it I also found that the card-mod typography on the value tiles never took effect, although the post said it did. The first correction on 20 November suggested a fix that I had not tested and that would not have worked either; the section now shows the fix I tested on the VM. Those parts are corrected below.

The stock cards in Home Assistant get you a working dashboard in an afternoon, and I described that one in five build steps. I built it in August 2026 on the same day as a second dashboard with custom cards, so that I could show the two side by side. What the stock cards do not give you is control over typography, over how a number is drawn, or over a ring that shows a battery level. This post is about the four custom cards in the second dashboard, the theme that holds them together, the four things that broke while building it, and one mistake I only found while correcting this post.

Both dashboards run on my new Home Assistant instance, a small Hetzner VM, the same one the theme comparison was shot on. Their sensors come from a demo package that calculates a daily curve from the time of day, so none of the values are from my house. Since the house automation moved onto that VM at the end of September, both demo dashboards are hidden from the sidebar; they still load from their YAML files. The instrument dashboard is 481 lines long, and the templates post explains why I did not publish it in full. The excerpts below are cut from the real files, with the German labels I use, and cover the parts you cannot see in the screenshot.

Four cards, loaded without HACS

The four custom cards are card-mod (CSS injection into any card), Mushroom (the compact tile cards), mini-graph-card (the line charts) and button-card (a card that renders whatever JavaScript you hand it). On a normal installation you install them through HACS, or on Home Assistant 2026.11 or newer through the built-in Marketplace, and it registers the resources for you. On the VM I wanted the setup reproducible from the repository alone, so I downloaded the four bundles into www/cards on 20 August and load them from configuration.yaml:

# configuration.yaml (excerpt)
frontend:
  themes: !include_dir_merge_named themes
  # Custom Cards und die Schrift-Einbindung. extra_module_url laedt ES-Module
  # auf jeder Seite - das ist der Weg, der ohne HACS und ohne die
  # Ressourcen-Verwaltung im Speichermodus auskommt.
  extra_module_url:
    - /local/cards/card-mod.js
    - /local/cards/mushroom.js
    - /local/cards/mini-graph-card-bundle.js
    - /local/cards/button-card.js
    - /local/ha-fonts.js
Enter fullscreen mode Exit fullscreen mode

The file has two more entries today, for cards I wrote later. The fifth entry above is not a card. It is the font loader, which I need because a theme cannot contain @font-face rules.

Themes cannot declare fonts

The design direction for this dashboard was an instrument panel: labels small, uppercase and widely spaced in Archivo, values large and calm in IBM Plex Mono with tabular figures. I wanted tabular figures because with proportional digits a value like 2,449 W changes width whenever a digit changes, and the number jumps sideways once a minute. You only notice that in the running dashboard; a screenshot does not show it.

A Home Assistant theme can only set CSS variables. It cannot declare an @font-face rule, so the fonts have to come from somewhere else. A small ES module loaded through extra_module_url appends the rules to the document head, and because @font-face is document-wide it also applies inside the shadow roots of the cards. This is the whole file, with my German comments:

// www/ha-fonts.js
// ha-fonts.js — selbst gehostete Schriften fuer das Instrument-Theme.
//
// Warum eine JS-Datei und kein Theme-Eintrag: HA-Themes setzen CSS-Variablen,
// sie koennen keine @font-face-Regeln deklarieren. Ueber
// `frontend: extra_module_url` wird dieses Modul auf jeder Seite geladen und
// haengt die Regeln in den Document-Head. @font-face ist dokumentweit gueltig
// und wirkt damit auch in den Shadow-Roots der Karten.
//
// Bewusst NICHT ueber fonts.googleapis.com: die Dateien liegen unter
// /local/fonts/ auf derselben Maschine. Kein externer Request, kein Abfluss
// von Besucher-IPs, funktioniert ohne Internet.

const CSS = `
@font-face {
  font-family: 'Archivo';
  src: url('/local/fonts/archivo-var.woff2') format('woff2-variations');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'IBM Plex Mono';
  src: url('/local/fonts/plexmono-400.woff2') format('woff2');
  font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'IBM Plex Mono';
  src: url('/local/fonts/plexmono-500.woff2') format('woff2');
  font-weight: 500; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'IBM Plex Mono';
  src: url('/local/fonts/plexmono-600.woff2') format('woff2');
  font-weight: 600; font-style: normal; font-display: swap;
}

/* Tabellenziffern global: Messwerte sollen beim Aktualisieren nicht springen.
   Das ist der Unterschied zwischen einer Anzeige und einem Zappeln. */
html {
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
`;

const style = document.createElement('style');
style.id = 'instrument-fonts';
style.textContent = CSS;
if (!document.getElementById('instrument-fonts')) {
  document.head.appendChild(style);
}
Enter fullscreen mode Exit fullscreen mode

The last rule switches tabular figures on for the whole page, which turned out to matter, because the card-mod blocks further down were meant to do the same for the tiles and did not work until I fixed them in November 2026. The font files are served from /local/fonts on the same machine: one Archivo file and three IBM Plex Mono weights, about 64 KB together. The browser loads them from the VM and sends no request to fonts.googleapis.com. One surprise on the way: Google's font API returned the same variable font file for all four Archivo weights I requested. When I compared the checksums, I found that I had downloaded the same 35 KB file four times.

Mushroom template cards as value tiles

The three tiles next to and below the generation graph, consumption (Verbrauch), feed-in (Einspeisung) and grid import (Netzbezug), are Mushroom template cards. The template card takes a primary and a secondary text, both Jinja, an icon and an icon colour that can also be a template. That last part makes the tiles readable at a glance: grid import turns red only while there is any, feed-in turns teal only while there is any, otherwise both are grey. Consumption stays grey. These are the options of the older template card. Current Mushroom releases rebuilt the template card on top of Home Assistant's tile card, with color and vertical instead of icon_color and layout, and keep the old one as mushroom-legacy-template-card. The bundle I downloaded still renders my older options, as the screenshot shows.

# dashboards/instrument.yaml as built in August (excerpt: first and third tile)
- type: custom:mushroom-template-card
  primary: Verbrauch
  secondary: "{{ states('sensor.demo_hausverbrauch') | int }} W"
  icon: mdi:home-lightning-bolt
  icon_color: grey
  layout: vertical
  card_mod: &werteblock
    style: |
      ha-card {
        border: 1px solid var(--ha-card-border-color);
        padding-bottom: 4px;
      }
      mushroom-state-info$: |
        .primary {
          font-family: Archivo, sans-serif !important;
          font-size: 0.68rem !important; font-weight: 600 !important;
          letter-spacing: 0.16em; text-transform: uppercase;
          color: var(--secondary-text-color) !important;
        }
        .secondary {
          font-family: 'IBM Plex Mono', monospace !important;
          font-size: 1.28rem !important; font-weight: 500 !important;
          color: var(--primary-text-color) !important;
          font-variant-numeric: tabular-nums;
          margin-top: 2px;
        }

- type: custom:mushroom-template-card
  primary: Netzbezug
  secondary: "{{ states('sensor.demo_netzbezug') | int }} W"
  icon: mdi:transmission-tower-export
  icon_color: >
    {{ 'red' if states('sensor.demo_netzbezug') | int > 0 else 'grey' }}
  layout: vertical
  card_mod: *werteblock
Enter fullscreen mode Exit fullscreen mode

The card-mod block was meant to set the typography: small uppercase labels in Archivo, values in IBM Plex Mono. The screenshot shows that it never did. The labels are neither uppercase nor spaced, and the values are not in the monospaced font. With style: | the whole block is one CSS string, and the line mushroom-state-info$: | inside it is not CSS. The browser discards the rule it belongs to, and the .secondary rule after it finds no element at that level. Only the border on ha-card applied. The mistake was in both anchors, werteblock for the four value tiles and raumkarte for the seven environment and climate tiles, eleven template cards in all, and I only noticed it while correcting this post. My first correction suggested the mapping form from the card-mod documentation, with mushroom-state-info$ as a key. That would not have helped either. In the browser's developer tools the template card in the Mushroom version on my VM contains no mushroom-state-info element at all. Because it is built on the tile card, its two texts are span elements with slot=primary and slot=secondary inside ha-tile-info, in the same shadow root as the style that card-mod injects. A plain CSS string with direct selectors reaches them. In November 2026 I changed the anchor to this and checked the computed styles on all eleven template cards:

# dashboards/instrument.yaml (excerpt: the fixed anchor, November 2026)
card_mod: &werteblock
  # Template-Card ab Mushroom 5: Text steht in ha-tile-info, im selben
  # Shadow Root wie dieser Style, deshalb ohne $-Pfad.
  style: |
    ha-card {
      border: 1px solid var(--ha-card-border-color);
      padding-bottom: 4px;
    }
    ha-tile-info [slot=primary] {
      font-family: Archivo, sans-serif !important;
      font-size: 0.68rem !important; font-weight: 600 !important;
      letter-spacing: 0.16em !important; text-transform: uppercase;
      color: var(--secondary-text-color) !important;
    }
    ha-tile-info [slot=secondary] {
      font-family: 'IBM Plex Mono', monospace !important;
      font-size: 1.28rem !important; font-weight: 500 !important;
      color: var(--primary-text-color) !important;
      font-variant-numeric: tabular-nums;
      margin-top: 2px;
    }
Enter fullscreen mode Exit fullscreen mode


The raumkarte anchor is also used by three entity cards and a select card in the controls section, and those still render mushroom-state-info with its own shadow root. So that anchor is now a mapping: a dot for the card's own shadow root with the same span selectors, and mushroom-state-info$ for the other cards. The number card for the target temperature had the same mistake and got the same form. On those cards the letter spacing only applied with !important.

The battery ring as an SVG stroke

The storage tile shows the state of charge as a ring. My first version was a button-card with a conic-gradient background and a smaller circle on top of it, whose background was set to var(--ha-card-background) to punch the hole. That produced a full disc. The inner circle stayed transparent and the gradient showed through. At the time I put it down to the variable not resolving inside the shadow DOM of button-card, and I did not look into it further.

The version that works draws the ring as an SVG circle and uses stroke-dasharray to show the filled portion, so there is no inner circle that needs a background. The colour switches at 60 and 25 percent, and the ring is a custom field in button-card, computed in JavaScript from the sensor state. The card also has a second custom field for the charge flow below the ring and a block of styles that sets fill: none and a stroke width of 9 on both circles. Without those styles the circles are drawn as filled black discs. Both are left out of this excerpt:

# dashboards/instrument.yaml (excerpt: the ring field)
- type: custom:button-card
  entity: sensor.demo_batterie_ladestand
  show_name: false
  show_state: false
  show_icon: false
  custom_fields:
    ring: |
      [[[
        const soc = Math.max(0, Math.min(100, Number(states['sensor.demo_batterie_ladestand'].state) || 0));
        const col = soc >= 60 ? '#54cabe' : (soc >= 25 ? '#f0a44a' : '#e0685f');
        const R = 52, C = 2 * Math.PI * R;
        const dash = (soc / 100) * C;
        return `
          <svg class="ring" viewBox="0 0 120 120">
            <circle class="track" cx="60" cy="60" r="${R}"/>
            <circle class="val"   cx="60" cy="60" r="${R}"
                    stroke="${col}"
                    stroke-dasharray="${dash.toFixed(1)} ${(C - dash).toFixed(1)}"
                    transform="rotate(-90 60 60)"/>
            <text class="num" x="60" y="58">${soc}</text>
            <text class="pct" x="60" y="58" dx="${soc >= 100 ? 30 : 22}">%</text>
            <text class="lbl" x="60" y="76">LADESTAND</text>
          </svg>`;
      ]]]
Enter fullscreen mode Exit fullscreen mode

Two more things that broke

Markdown cards as section headings broke the grid. I had used markdown cards for the small uppercase section labels. A markdown card counts as a grid cell, so the tiles below it wrapped onto the wrong columns. In the sections layout the native heading card takes a full-width row of its own, so the cards below it start on the next row. Every section label on the dashboard is now a heading card with card-mod for the letter spacing.

The slider stayed neon yellow. The target temperature control at the bottom right is a Mushroom number card on a demo input_number. I had set Mushroom's own colour variable in the theme, and the slider ignored it. It takes its colour from Home Assistant's --rgb-state-number variable, which I found by reading the computed styles in the running frontend. The theme now sets both, plus the input_number variant:

# themes/instrument.yaml (excerpt)
Instrument:
  modes:
    dark:
      mush-rgb-state-number: "240, 164, 74"
      # HA faerbt input_number-Regler ueber --rgb-state-number, nicht ueber
      # die Mushroom-Variablen. Ohne diese Zeile bleibt der Regler neongelb.
      rgb-state-number: "240, 164, 74"
      rgb-state-input-number: "240, 164, 74"
    light:
      mush-rgb-state-number: "180, 106, 18"
      rgb-state-number: "180, 106, 18"
      rgb-state-input-number: "180, 106, 18"
Enter fullscreen mode Exit fullscreen mode

The fourth one is a testing problem rather than a design problem. Light mode kept showing Home Assistant's default white instead of my theme. The frontend.set_theme service only sets the server default per mode, and as long as a user profile says backend-selected, I could not reliably control in my screenshot runs which mode applies. For reproducible screenshots the theme has to be set explicitly, which my screenshot script does by writing selectedTheme into localStorage before it loads the page.

What it costs, and what my daily dashboard uses

The instrument dashboard needs four custom cards and 481 lines of YAML, comments included, for what the stock version does with 23 cards, no plugins and 172 lines. The two screenshots in the templates post show the difference. After an update of any of the four cards I would have to check whether my styling still works. Each of the four cards is a separate project with its own release cycle, and card-mod reaches into shadow DOM selectors that the Mushroom maintainers can rename in any release. The template card is an example: the mushroom-state-info selector that older card-mod examples use does not match it in the version I installed. The four bundles on the VM are still the ones I downloaded on 20 August, and I have not tested an update yet.

The earlier version of this post ended with what I would use for a daily dashboard: Mushroom alone, without card-mod. When I built the dashboard the house actually runs on, at the end of September, I did something else. It is generated by a script and consists mostly of button-card with seven templates of my own, plus mini-graph-card for the trends and card-mod for details. It uses no Mushroom card and no battery ring. What it took over from this dashboard is the Instrument theme and the two fonts.

Frequently asked questions

Do I need HACS or the Marketplace to use Mushroom or button-card?

No. The cards are plain JavaScript modules. Put the file under config/www, which Home Assistant serves as /local/, and add its /local/ URL either under frontend: extra_module_url in configuration.yaml or as a JavaScript module in the dashboard resources. HACS or the Marketplace only do the download and the registration for you. Without them, nothing tells you about new versions.

Why does card-mod need the dollar sign in the selector?

In a card-mod style mapping, a key ending in a dollar sign enters that element's shadow root, which my Mushroom entity, select and number cards need for their texts. My template cards use direct CSS selectors instead, because their text spans sit in the same shadow root as the injected style.

Does the SVG ring update live?

Yes, button-card updates the ring without a page reload when the bound sensor changes state. My demo sensor calculates its value from the time of day and changes at most once a minute.

Top comments (0)