Building a Custom Card
This guide walks through adding a new card to LCARdS: the class, the schema, the registration in src/lcards-main.js, the editor hook-up, the build and the checks that guard each step. It is the how-to; the base classes themselves (every hook, the HASS flow, rules, preview mode, cleanup) are described in Card Foundation, and the rules card code must follow are in .claude/rules/cards.md.
New cards live in this repository: there is no plugin API for cards shipped outside the LCARdS bundle. Every sample below uses the real base class APIs in src/base/LCARdSCard.js and src/base/LCARdSNativeCard.js.
Checklist
- Create
src/cards/lcards-mycard.jsextendingLCARdSCard(step 1). - Declare
static CARD_TYPEandstatic getStubConfig(), and a lazystatic getConfigElement(). - Write the schema in
src/cards/schemas/mycard-schema.js, with adescriptionon every property (step 2). - Create the editor at
src/editor/cards/lcards-mycard-editor.jsextendingLCARdSBaseEditor; it registers its own element (step 3). - Register the card element, its card picker entry and its schema in
src/lcards-main.js(step 4). - Add the schema to
scripts/lib/card-schemas.js(step 5). - Run
npm run types:elements(step 6). - Override
_getCardSize()for pixel heights (step 7). - Write the docs page and a test, run
npm run buildand test in Home Assistant (step 8).
This is the same list as "Adding new cards" in CLAUDE.md and Card Foundation: Adding a new card.
Class hierarchy
LitElement
└── LCARdSNativeCard Home Assistant card contract: setConfig, hass, getCardSize, preview detection, host size keys
└── LCARdSCard core singletons, config processing, entities, templates, actions, rules, cleanup
└── LCARdSMyCardExtend LCARdSCard. The layout card and the alert overlay extend LitElement directly because they use none of the foundation; a normal card needs all of it. The full tree of today's cards is in Card Foundation: Overview.
1. The card class
src/cards/lcards-mycard.js. The sample shows the card entity's state on a coloured panel, follows a second entity named in its config, runs the card's actions on tap and takes rule patches.
import { html, css } from 'lit';
import { LCARdSCard } from '../base/LCARdSCard.js';
import { lcardsLog } from '../utils/lcards-logging.js';
import { getMyCardSchema } from './schemas/mycard-schema.js';
export class LCARdSMyCard extends LCARdSCard {
/** CoreConfigManager's key for this card's schema and presets (no "lcards-" prefix) */
static CARD_TYPE = 'mycard';
/** The smallest config that renders, shown in the Home Assistant card picker */
static getStubConfig() {
return {
type: 'custom:lcards-mycard',
entity: 'light.example'
};
}
/** Editors are lazy chunks: import on demand, keep them out of the main bundle */
static async getConfigElement() {
await import('../editor/cards/lcards-mycard-editor.js');
return document.createElement('lcards-mycard-editor');
}
/** Called from src/lcards-main.js once the core has initialised */
static registerSchema() {
const configManager = window.lcards?.core?.configManager;
if (!configManager) {
lcardsLog.error('[LCARdSMyCard] CoreConfigManager not available for schema registration');
return;
}
configManager.registerCardSchema('mycard', getMyCardSchema(), { version: __LCARDS_VERSION__ });
}
static get styles() {
return [
super.styles,
css`
.my-card {
width: 100%;
height: 100%;
border-radius: var(--ha-border-radius-lg);
padding: var(--ha-space-2);
}
`
];
}
constructor() {
super();
this._cardStyle = {};
this._secondaryState = null;
/** @type {Array<() => void>} */
this._extraUnsubs = [];
/** @type {(() => void) | null} */
this._actionCleanup = null;
this._fullSetup = false;
}
// ── Lifecycle ───────────────────────────────────────────────────────
/** Once per element, after the first render */
_handleFirstUpdate(changedProps) {
super._handleFirstUpdate(changedProps);
// The card picker's gallery card never leaves the picker: skip the setup there.
// Never skip on this._isPreviewMode: it is also true in dashboard edit mode.
if (this._checkForAncestor(['hui-card-picker'])) return;
this._fullSetup = true;
this._registerOverlayForRules(
this.config.id || `mycard-${this._cardGuid}`, // overlay id rules target
'mycard', // type (type:mycard selectors)
this.config.tags || [] // tags (tag:<name> selectors)
);
this._resolveStyle();
this._subscribeExtraEntities();
// The first render was the "Loading LCARdS..." placeholder: wait for the card's DOM
this.updateComplete.then(() => this._bindActions());
}
/** Every connect, including a reconnect (edit mode, view changes, moves) */
async _onConnected() {
super._onConnected(); // first; singletons are ready on the next line
// A disconnect dropped every subscribeToEntity() subscription
if (this._fullSetup) this._subscribeExtraEntities();
}
/** CoreConfigManager has replaced this.config with the merged config */
_onConfigUpdated() {
super._onConfigUpdated();
this._resolveStyle();
if (this._fullSetup) {
this._subscribeExtraEntities();
this.updateComplete.then(() => this._bindActions());
}
}
/** A rule started, changed or stopped patching this card (no arguments) */
_onRulePatchesChanged() {
this._resolveStyle();
}
_onDisconnected() {
// Card-specific teardown only; the base drops subscriptions, rules, observers
super._onDisconnected(); // last
}
// ── Entities ────────────────────────────────────────────────────────
_subscribeExtraEntities() {
this._extraUnsubs.forEach(unsub => unsub());
this._extraUnsubs = [];
const id = this.config.secondary_entity;
if (!id) return;
// The callback is not called on subscribe: read the current state first
this._secondaryState = this.getEntityState(id);
this._extraUnsubs.push(this.subscribeToEntity(id, (_id, newState) => {
this._secondaryState = newState;
this.requestUpdate();
}));
}
// ── Actions and style ───────────────────────────────────────────────
_bindActions() {
const el = /** @type {HTMLElement|null} */ (this.shadowRoot?.querySelector('.my-card'));
this._actionCleanup?.();
this._actionCleanup = el ? this.setupActions(el, {
tap_action: this.config.tap_action,
hold_action: this.config.hold_action,
double_tap_action: this.config.double_tap_action
}) : null;
}
_resolveStyle() {
const style = { ...(this.config.style || {}) };
this._cardStyle = this._getMergedStyleWithRules(style); // rule patches last
this.requestUpdate(); // always
}
// ── Render ──────────────────────────────────────────────────────────
_renderCard() {
// A colour, a theme: token or a state map; CSS stays live for alert modes
const background = this._resolveEntityStateColor(this._cardStyle.background, 'theme:colors.ui.primary');
return html`
<div class="my-card" style="background: ${background}">
<span>${this._getStateDisplay()}</span>
${this._secondaryState ? html`<span>${this._getStateDisplay(this._secondaryState)}</span>` : ''}
</div>
`;
}
}What each part relies on:
CARD_TYPEis what_processConfigAsync()passes toconfigManager.processConfig()(LCARdSCard.js). Without it the card falls back toconfig.typeand gets no schema validation and no preset merge. It is also the key ofregisterCardSchema()and the editor'scardType, so use the same string, without thelcards-prefix (the button registers'button', the select menu'select-menu').getStubConfig()includestypeand, when the card needs one,entity.getConfigElement()isasyncand imports the editor, as every card does (lcards-select-menu.js,lcards-data-grid.js); thesize budgettest intests/browser/bundle.spec.jsfails if editors land in the main chunk.- Hooks.
_handleFirstUpdate()runs once per element;_onConnected()on every connect (callsuperfirst);_onDisconnected()callssuperlast. The full tables are in Card Foundation: Lifecycle. - The first render is a placeholder.
LCARdSNativeCard.render()shows "Loading LCARdS..." until_mountResolved, whichfirstUpdated()sets just before it calls the hooks (LCARdSNativeCard.js). Query your own elements afterthis.updateComplete(the button does:_setupButtonActions()inlcards-button.js). - Preview mode.
this._isPreviewModeis alsotrueon every card while the dashboard is in edit mode, and_handleFirstUpdate()never runs again, so a card that skipped its setup on it stays half set up after edit mode ends. Skip only in the card picker (_checkForAncestor(['hui-card-picker']), as the select menu does). See Card Foundation: Preview mode.
Entities
- Your own entity.
this._entityis the state object ofconfig.entity, refreshed on every HASS. A change to it, or to an entity that the card's templates, rules,on_entity_changeanimations,map_rangebindings ortriggers_updatename, re-renders the card: the base keeps those inthis._trackedEntities(_updateTrackedEntities()). Render fromthis._entity; never readthis.hass.statesin_renderCard(). - Other entities:
subscribeToEntity(entityId, callback). The callback gets(entityId, newState, oldState)when the core's systems manager sees the entity change. It is not called on subscribe, so read the current value withgetEntityState(entityId)first. Subscribing also makes a change to that entity reach the core, so there is no need to add it to_trackedEntities. Subscriptions are dropped on disconnect, so subscribe from a method that_onConnected()calls again (the elbow's_subscribeSegmentEntities()). - Reacting in
_handleHassUpdate(newHass, oldHass). It runs on every new HASS object, even when nothing the card uses changed.this._entityis already the new state there, so compareoldHass?.states[id]withnewHass.states[id](the select menu does), not withthis._entity. - Display helpers.
_getStateDisplay(entity),_getEntityName(entity),_getAttributeDisplay(key, entity)format values the way Home Assistant does; never callhass.format*()directly.
Details: Card Foundation: Entity access and HASS flow.
Actions and services
setupActions(element, actions, options)wires tap, hold, double-tap and hover on an element of your shadow DOM through the card's ownLCARdSActionHandler, with the card's animations and sound overrides. Never bind raw click or pointer listeners for Home Assistant actions.- It returns a cleanup function. The base does not keep it: call the previous one before binding again (the button's
_actionCleanup, the data grid's_cellActionCleanups). Bind again when Lit replaces the element. callService(domain, service, data)wrapshass.callService()with logging and error handling; use it instead ofthis.hass.callService():
await this.callService('light', 'turn_on', { entity_id: this.config.entity, brightness: 255 });See Action Handler for the supported actions.
Rules, colours and logging
- Rules.
_registerOverlayForRules(overlayId, type, tags)takes positional arguments and registers the card as a rules target once; the base registers it again after a reconnect. When a patch changes,this.configalready carries it and_onRulePatchesChanged()runs; re-resolve your style there and end inrequestUpdate()._getMergedStyleWithRules(style)applies the patch'sstylelast, so a rule wins over defaults the card adds. See Card Foundation: Rules integration. - Colours. Use
_resolveEntityStateColor(value, fallback)for CSS (avar()stays live, so alert modes recolour without a re-render) and_resolveColorValue(value, fallback)for SVG attributes, Canvas2D and anime.js. Defaults aretheme:tokens ('theme:colors.ui.primary'), never literal colours. See Colour Resolution. - CSS. New
--lcards-*variables go insrc/lcards-vars.jsand--lcars-*ones inscripts/ha-lcars-theme-vars.js, ornpm run validate:css-varsfails the build. Use Home Assistant's spacing, radius and border variables (see Home Assistant CSS Variables). - Logging.
lcardsLog.debug('[LCARdSMyCard] ...', { data }), neverconsole.log. Turn on debug output in the browser console withwindow.lcards.setGlobalLogLevel('debug'). - Animations use the anime.js v4 API through the Animation API.
- Zones. Cards that place text fields in named zones override
_calculateZones(width, height)and call_rebuildZones(width, height)at the top of each render; the zone text helpers are onLCARdSButton, notLCARdSCard. See.claude/rules/cards.md"Zone System".
2. The schema
src/cards/schemas/mycard-schema.js exports the JSON Schema of the card config. It is used four ways: runtime validation (CoreValidationService, through CoreConfigManager), the schema-driven editor fields (FormField), the strict doc example check and the published schema files.
import { entitySchema, actionSchema, cardIdSchema, tagsSchema, stateColorSchema, cardLevelProperties } from './common-schemas.js';
export function getMyCardSchema() {
return {
type: 'object',
required: ['type'],
properties: {
type: { type: 'string', const: 'custom:lcards-mycard', description: 'Card type.' },
id: cardIdSchema,
tags: tagsSchema,
entity: entitySchema,
secondary_entity: {
type: 'string',
description: 'A second entity whose state is shown under the main one.',
'x-ui-hints': { label: 'Second entity', selector: { entity: {} } }
},
style: {
type: 'object',
description: 'Panel style.',
properties: {
background: {
...stateColorSchema,
description: 'Panel colour: a colour, a theme: token or a state map. Default: theme:colors.ui.primary.'
}
}
},
tap_action: actionSchema,
hold_action: actionSchema,
double_tap_action: actionSchema,
...cardLevelProperties // rules, sounds, variables, zones, debug_zones
}
};
}- Reuse the shared nodes in
src/cards/schemas/common-schemas.js(entity, actions, sizes,cardLevelProperties) instead of copying them. Keys every card accepts for Home Assistant and UIX (grid_options,view_layout,visibility,card_mod,uix) are added bywithCardPlumbing()(card-plumbing.js); don't declare them. - Every property needs a
description:npm run validate:schema-descriptions(insidenpm run build) fails otherwise. Before naming a key, check the other cards' schemas for the same idea (see Adding a Card Option). - Option lists the runtime passes in (presets, components, text positions) are factory parameters, as in
getSelectMenuSchema({ availablePresets }).
3. The editor
src/editor/cards/lcards-mycard-editor.js extends LCARdSBaseEditor, sets this.cardType = 'mycard' (the editor reads the schema with configManager.getCardSchema(this.cardType)), renders fields with FormField.renderField(this, 'path.to.key') and commits through _updateConfig() / _setConfigValue(). It registers its own element in its own file:
if (!customElements.get('lcards-mycard-editor')) {
customElements.define('lcards-mycard-editor', LCARdSMyCardEditor);
}The editor guide is Building a Studio Editor; the field rules are in .claude/rules/editor.md.
4. Register the card
Everything goes in src/lcards-main.js, never in the card file.
// At the top, with the other card imports
import { LCARdSMyCard } from './cards/lcards-mycard.js';
// Inside initializeCustomCard().then(() => { ... }), with the other cards
if (!customElements.get('lcards-mycard')) customElements.define('lcards-mycard', LCARdSMyCard);
// In the same block, inside if (window.lcards?.core?.configManager) { ... }
if (LCARdSMyCard.registerSchema) LCARdSMyCard.registerSchema();And one entry in the module-level LCARdSCardClasses array, which is pushed into window.customCards once (guarded against a second bundle evaluation):
{
type: 'lcards-mycard',
name: 'LCARdS My Card',
preview: true,
description: 'One line for the card picker',
documentationURL: docsUrl(DOCS.myCard),
},- The elements are defined after
initializeCustomCard()resolves, so the core singletons exist when the first card connects; the schemas register in the same block, after the core is up. - The
customElements.get()guard stops a second evaluation of the bundle (an old Lovelace resource left next to the integration's script) from throwing. DOCS.myCardis a new entry insrc/utils/docs-url.jspointing at the card's docs page ('/cards/mycard/');tests/docs/docs-links.test.jschecks everyDOCSlink against the built site.getEntitySuggestion(optional) lets the picker suggest the card for an entity, as the button, slider and select menu do.
5. Publish the schema
Add the card to getSchemaEntries() in scripts/lib/card-schemas.js:
import { getMyCardSchema } from '../../src/cards/schemas/mycard-schema.js';
// ...
{ kind: 'card', tag: 'lcards-mycard', schema: getMyCardSchema() },That one list feeds the strict doc example validator, the schema description check and the published schema (npm run generate:llm-schema, doc/public/schema/). tests/docs/schema-registry.test.js fails when an element defined in lcards-main.js has no entry here. If the card config names something from a registry (a preset, component, sound, background), the config checker's registryIssues() needs to know it: see Adding a Card Option: Names that must exist.
6. Element types
npm run types:elementsIt regenerates src/types/elements.generated.d.ts (one HTMLElementTagNameMap entry per customElements.define() in src/), so document.createElement('lcards-mycard') is typed as your class. npm run typecheck, which npm run build runs, starts with generate-element-types.js --check and fails while the file is stale. Run it again after renaming an element. See Type Checking.
7. Card size
LCARdSNativeCard.getCardSize() returns _getCardSize(), which is 1 by default. Override it when the card is taller than one masonry row (about 50 px):
_getCardSize() {
return Math.ceil((this._configPx(this.config?.height) || 200) / 50);
}_configPx() turns 400, "400" or "400px" into a number and returns null for relative units (50vh, %), so those fall back to the default. For the sections view, override getGridOptions() (the select menu does) or _getGridOptions(). Sizing inside layout cards and views follows .claude/rules/cards.md "Sizing in Nested Grids".
8. Build, test and document
npm run buildnpm run build is the only build, for development and releases alike: it runs every validator (strict doc examples, schema descriptions, CSS variables, Home Assistant compatibility lint, typecheck) and writes the bundle straight into custom_components/lcards/: lcards.js (a small loader stub), the hashed chunks/ and their .br / .gz copies. There is no dist/ folder and no copy step.
- See it in Home Assistant. In the devcontainer
custom_components/lcards/is bind-mounted into Home Assistant, so hard-refresh the browser (Ctrl+Shift+R) after a build. The hard refresh is required: each build deletes the previous chunks, and a cached loader stub would ask for a main chunk that no longer exists. Python changes need a Home Assistant restart. - Tests. Every behaviour gets a test: pure logic in a node test under
tests/, rendering in a browser fixturetests/browser/fixtures/mycard-*.yamlwith an assertion intests/browser/*.spec.js. See Testing. - Docs. A page under
doc/cards/mycard/in the format of the other card pages (an overview plus topic pages, each section with a live example and a Key/Type/Default/Description table;.claude/skills/lcards-docs-writer/SKILL.mddescribes it). Its YAML examples must pass the strict check, which proves the schema accepts them. - Demo. A view in
demo/views/for thelcards-demodashboard (npm run demo:push).
The commands and the order to run them in are in COOKBOOK.md ("Daily loop" and "Tests") in the repository root.
Related
- Card Foundation: every hook, member and cleanup step of the base classes
- Adding a Card Option
- Building a Studio Editor
- Building a Pack
- Config Manager: how
processConfig()merges presets, themes and your config