You are reading the development docs (2026.12.1-dev.1): features here may not be released yet.
Skip to content

Template System ​

Unified template evaluation for all LCARdS cards and text fields. There is no singleton: evaluators are created per use.


Overview ​

All dynamic text in LCARdS goes through UnifiedTemplateEvaluator (src/core/templates/UnifiedTemplateEvaluator.js). It runs up to four phases in a fixed order over one string: JavaScript, tokens, data sources, Jinja2. Cards do not keep an evaluator: LCARdSCard.processTemplate() builds a fresh one with the current context on every call, and the rules engine keeps one of its own for Jinja2 conditions and templates in rule patches.


Key classes ​

Class / moduleFileRole
UnifiedTemplateEvaluatorsrc/core/templates/UnifiedTemplateEvaluator.jsOrchestrator: runs the four phases in order (evaluateAsync()), or the first three (evaluateSync())
LCARdSCardTemplateEvaluatorsrc/core/templates/LCARdSCardTemplateEvaluator.jsJavaScript [[[...]]] and token {...} templates; owns an HATemplateEvaluator (created in its constructor) for the Jinja2 phase
HATemplateEvaluatorsrc/core/templates/HATemplateEvaluator.jsSends Jinja2 to Home Assistant (render_template subscription, 5 s timeout)
renderLive(), createTemplateOwner()src/core/templates/live-templates.jsA card's Jinja2 templates kept subscribed, shared per connection
DataSourceTemplateEvaluatorsrc/core/templates/DataSourceTemplateEvaluator.js{datasource:...} / {ds:...} and the legacy {name.path} form
TemplateEvaluatorsrc/core/templates/TemplateEvaluator.jsBase class of the three evaluators above: context, updateContext(), stats, _safeEvaluate(), _validateContent()
TemplateDetectorsrc/core/templates/TemplateDetector.jsFast checks (detectTemplateTypes(), hasTemplates()) so phases that have nothing to do are skipped
TemplateParsersrc/core/templates/TemplateParser.jsToken parsing, JavaScript extraction, extractJinja2Entities(), parseMSDReference()
templateRefs()src/core/templates/template-refs.jsWhat a template refers to (entities, data sources, theme tokens, variables); used for template provenance in processTemplate() and by the editor's Templates tab and sandbox

Template types and syntax ​

1: JavaScript [[[...]]] ​

yaml
text: "[[[return entity.state.toUpperCase()]]]"
text: "[[[return Math.round(entity.attributes.brightness / 2.55) + '%']]]"

The code runs in new Function(...) with these names (LCARdSCardTemplateEvaluator._safeEvalCode()):

NameValue
names in context.extraCard-supplied extras (for example the astrometrics card's sky and point); listed first, so the standard names below win
entitycontext.entity (the card's entity object)
configcontext.config
hasscontext.hass
statescontext.hass.states (or {})
usercontext.hass.user (or {})
variablescontext.variables (or {})
themecontext.theme (or {})
Math, String, Number, Boolean, parseFloat, parseInt, Array, Object, DateThe globals

The code needs its own return. JavaScript templates always see raw values; displayFormat does not apply to them.

2: LCARdS tokens {...} ​

yaml
text: "{entity.state}"
text: "{entity.attributes.friendly_name}"
text: "{states.sensor.outside_temp.state}"
text: "{variables.label}"
text: "{theme:colors.ui.primary}"
text: "{config.name}"

_evaluateTokens() matches {...} but not {{, {%, {#, {datasource: or {ds:, and keeps a match its deferToken test accepts (a legacy data source reference, below). _resolveToken():

  • theme:<path>: window.lcards.core.themeManager.getToken(path), else a dot walk of context.theme, else '';
  • states.<domain>.<object_id>.state and states.<domain>.<object_id>.attributes.<key>: rebuilds the entity id from the two segments after states (because hass.states is a flat map keyed by domain.object_id) and formats the value per displayFormat;
  • anything else: a dot walk of the context; entity.state and entity.attributes.<key> are then formatted per displayFormat.

context.displayFormat (default 'friendly') controls those entity values:

displayFormatOutput
friendlyHome Assistant's display string (haFormatState / haFormatAttrValue)
rawThe raw state or attribute
partsThe formatted parts joined (joinParts)
unitOnly the unit (extractUnit)

3: Data sources {ds:...} ​

yaml
text: "{ds:sensor_name}"                    # base value: locale-formatted number + unit
text: "{ds:sensor_name.celsius}"            # processor output: raw value, no unit
text: "{ds:sensor_name.celsius:.1f}"        # format spec: number only
text: "{ds:sensor_name.celsius:.1f} °C"     # manual unit suffix
text: "{datasource:temp.rolling_avg}"       # explicit long prefix

DataSourceTemplateEvaluator._resolveDatasourceReference() parses name[.path][:format] (TemplateParser.parseMSDReference()); name.processor is read as name.processing.processor. With a format spec the output is the number only. Without one, the base value is formatted with haFormatNumber() and gets the entity's unit; a processor output is returned as is. transformations.* paths still work with a deprecation warning.

Legacy form. The evaluator also resolves {name.path} without a prefix (_evaluateLegacyDatasources()), the old MSD syntax. Phase 2 leaves it alone: UnifiedTemplateEvaluator gives the card evaluator a deferToken test (_isLegacyDataSourceRef()), and _evaluateTokens() keeps a {...} the test accepts as written. A token is deferred when its first segment (before . or :) is not a context key (entity, config, variables, hass, ... win on a clash), it is not theme:, and DataSourceTemplateEvaluator.canResolve() resolves it now. So "{config.name} {temp:.1f}" gives "N 21.5", and "{temp}" alone resolves too (phase 2 used to empty both). An unknown {name} is still a token and still resolves to ''. Prefer {ds:...}.

4: Jinja2 {{...}} / {% %} / {# #} ​

yaml
text: "{{ states('sensor.temp') }} °C"
text: "{% if is_state('light.kitchen', 'on') %}On{% else %}Off{% endif %}"

HATemplateEvaluator._renderTemplate() sends the template with these variables, each only when the template names it (a word match on config, user, variables or an extra's name):

javascript
variables: {
  ...context.extra,             // card-supplied extras as top-level names
  config: context.config,
  user: context.hass.user.name,
  variables: context.variables
}

A card's templates stay subscribed (src/core/templates/live-templates.js, since 2026-11-18). LCARdSCard.processTemplate() puts the card's template owner in the context (_liveTemplates), and renderLive() keeps one render_template subscription per connection, template and variables, shared by every card that uses it. Home Assistant renders again when the template's entities change (or now() moves) and sends only a changed result; a held template answers the next evaluation at once. A changed result calls each owner's onChange, which is _onLiveTemplateChanged() on the card: it schedules a template pass, so a pass that read the old result just before the new one arrived catches up. Before, each evaluation subscribed, took one result and unsubscribed: three messages per template per state change (about 6,800 messages a minute on the Bridge dashboard while helpers changed, 380 after; Fire HD 8 kiosk feedback).

  • Holding and letting go: the owner keeps what it evaluated until release() (in setConfig() and _onDisconnected()) or until a template pass no longer uses it (_processTemplates() wraps the pass in beginPass() / endPass(); an overlapped pass lets nothing go). It holds at most 64 templates, least recently used dropped first. A template nobody holds closes 10 seconds later, so a new config or a moved card picks it up again without a new subscription. A reconnected card that had templates runs a pass to hold them again.
  • Variables key the subscription: that is why only the named ones are sent. A template reading config subscribes again when the card config changes (a preset merged in, rule patches).
  • Render once instead: callers outside a card's template pass. These are the rules engine, the alert overlay's config, the editor's template tab and sandbox, card-supplied names (options.extra, the astrometrics points) and options.live: false (astrometrics bindings, refreshed per changed entity). They use the one-shot path: subscribe, take the first result, unsubscribe. Home Assistant can send the subscribe result and the first event in one websocket frame, so the callback may run before subscribeMessage() resolves with the unsubscribe function; a done flag makes the .then() unsubscribe in that case, so no subscription stays open.
  • Errors: a template Home Assistant refuses, or no first result in 5 seconds, rejects; the evaluation returns the string unchanged and the subscription is dropped, so the next pass tries again. home-assistant-js-websocket subscribes again after a reconnect, and the re-sent result counts as a change when it differs.

If hass.connection is not there yet it waits 100 ms once, then fails. No result in 5 seconds rejects with a timeout.


Evaluation order ​

Detection runs again on the current result before each phase, so a JavaScript template may emit a token or Jinja2 that a later phase then evaluates. evaluateSync() runs the first three phases only (no Home Assistant round trip), for values needed without an await, such as the slider's marker value / min / max on every hass update (lcards-slider.js).

Failure behaviour ​

PhaseOn failure or no value
JavaScriptAn exception keeps the original [[[...]]] text; null / undefined give ''
TokensUnresolved, null, undefined or an exception give ''
Data sourcesAn unknown source or missing value keeps the original text; with no dataSourceManager the content is returned unchanged (with a warning)
Jinja2A server error or timeout returns the string as it was before the phase
Whole evaluateAsync()Any exception returns the original input

How cards evaluate templates ​

LCARdSCard.processTemplate(template, options) (src/base/LCARdSCard.js) builds the context and a new evaluator:

javascript
const context = {
  ...(extra ?? {}),                  // options.extra names at the top level too
  ...(extra ? { extra } : {}),
  entity: options.entity !== undefined ? options.entity : this._entity,
  config: this.config,
  hass: this.hass,
  states: this.hass?.states,
  variables: this.config?.variables || {},
  theme: this._singletons?.themeManager?.getActiveTheme?.(),
  displayFormat: options.displayFormat ?? 'friendly'
};

const evaluator = new UnifiedTemplateEvaluator({
  hass: this.hass,
  context,
  dataSourceManager: window.lcards?.core?.dataSourceManager
});
const result = await evaluator.evaluateAsync(template);

After evaluation it records template provenance: templateRefs(template, { entityId }) gives the entities, data sources, variables and theme tokens, which go to this._provenanceTracker.trackTemplate(field, template, result, deps, processor). If a {ds: / {datasource: template arrives before the DataSource manager exists, the card schedules a retry.

Other evaluators:

  • RulesEngine._initializeTemplateEvaluator() creates one UnifiedTemplateEvaluator with context: { config: {}, variables: {} }, and sets hass on it before each patch evaluation (see Rules Engine).
  • lcards-button.js and BackgroundAnimationRenderer.js use LCARdSCardTemplateEvaluator directly for synchronous JavaScript and tokens.
  • The editor's template evaluation tab and sandbox (src/editor/components/templates/) create UnifiedTemplateEvaluators for previews.

A standalone use, for code outside a card (inside a card, call this.processTemplate()):

javascript
import { UnifiedTemplateEvaluator } from '../core/templates/UnifiedTemplateEvaluator.js';
import { lcardsLog } from '../utils/lcards-logging.js';

const evaluator = new UnifiedTemplateEvaluator({
  hass,
  context: {
    entity: hass.states['sensor.outside_temp'],
    config,
    hass,
    states: hass.states,
    variables: config.variables || {},
    theme: window.lcards?.core?.themeManager?.getActiveTheme?.(),
    displayFormat: 'friendly'
  },
  dataSourceManager: window.lcards?.core?.dataSourceManager
});

const label = await evaluator.evaluateAsync('{entity.state} · {{ now().hour }}');
lcardsLog.debug('[MyFeature] label', { label });

UnifiedTemplateEvaluator API ​

MethodDescription
constructor({ hass?, context?, dataSourceManager? })Copies hass into context.hass if missing; creates the card evaluator (with context) and the DataSource evaluator
evaluateAsync(content)All four phases; non-strings are returned as is
evaluateSync(content)JavaScript, tokens, data sources
evaluateDatasources(content)Phase 3 only
updateContext(newContext)Merges into the card evaluator's context (TemplateEvaluator.updateContext()), which this, the card evaluator and the Jinja2 evaluator then share
updateDataSourceManager(dsm)Replaces the DataSource manager
updateHass(newHass)Sets hass and context.hass (tokens and Jinja2 alike)
extractDependencies(content)Entity ids read by Jinja2 templates (TemplateParser.extractJinja2Entities())

The card evaluator's HATemplateEvaluator shares its context object, so a hass set either way reaches Jinja2. The rules engine still sets evaluator.hass and evaluator.context.hass directly. For every reference in a config use templateRefs().


See also ​

Released under the MIT License. | About