Templates
Templates put live values into a card's settings: an entity's state in a label, a word or colour worked out in JavaScript, a Home Assistant Jinja2 template, or a processed data source value. There are four kinds, and one string can mix them.
- Tokens
{entity.state}are the quickest: they fill in a value without asking Home Assistant. - JavaScript
[[[ ]]]is for logic and calculations, Jinja2{{ }}is Home Assistant's own template language, and data sources{ds:name}show values that LCARdS has smoothed, converted or otherwise processed. - A card works out its templates again when an entity it follows changes; see Entity Tracking for which ones it follows.
Try it
A card editor's Main Engineering > Templates tab lists every template on the card with its live result, and opens any of them in a sandbox that completes entities, data sources and theme tokens. See Main Engineering.
Token Templates
A token in single braces fills in one value: the card's entity state, an attribute, another entity, a config value, a variable or a theme token. It is worked out at once, in the browser, with no round trip to Home Assistant.
{entity.state}is the state as Home Assistant shows it: translated, and with its unit (21.5 °C,Open). A text field'sdisplay_formatpicks the raw state or the unit only.- The card does not follow an entity named in a
{states.…}token by itself: list it intriggers_update, as below. - Anything in single braces is read as a token, so one that names nothing (a typo, an attribute the entity doesn't have) shows as nothing. On a text field,
template: falseshows braces as written. - Tokens are an LCARdS shorthand, not Jinja2. If you know Home Assistant's templates, Jinja2 gives you all of it; use tokens where the value must be ready at once.
type: custom:lcards-button
entity: sensor.demo_temperature
preset: lozenge
variables:
deck: DECK 7
triggers_update:
- light.demo
text:
default:
color: var(--lcars-black)
reading:
content: "{entity.state}"
position: top-left
deck:
content: "{variables.deck}"
position: top-right
light:
content: "LIGHT {states.light.demo.state}"
position: bottom-left
grid_options:
rows: 2| Token | Value |
|---|---|
{entity.state} | The card entity's state, as Home Assistant shows it |
{entity.attributes.NAME} | One of its attributes, as Home Assistant shows it |
{entity.name} | Its name (the friendly name, else the entity ID) |
{states.domain.object_id.state} | The state of another entity, named by its full entity ID |
{states.domain.object_id.attributes.NAME} | An attribute of another entity |
{config.NAME} | A value from this card's own config |
{variables.NAME} | One of the card's variables |
{hass.user.name} | The name of the Home Assistant user looking at the dashboard |
{theme:token.path} | A theme token's value: see Themes |
JavaScript Templates
[[[ ]]] runs JavaScript in the browser and puts in what it returns: use it for any logic or calculation. Write return with the value; a template that returns nothing shows as nothing.
- JavaScript gets the raw entity:
entity.stateison, notOn, and a number in a state is a string, so useNumber(entity.state)to calculate with it. - A template with an error is shown as written, and the browser console logs a warning.
- The card does not follow entities a template reads from
statesorhass.states: list them intriggers_update.
type: custom:lcards-button
entity: light.demo
preset: lozenge
text:
default:
color: var(--lcars-black)
status:
content: "[[[ return entity.state === 'on' ? 'ACTIVE' : 'STANDBY' ]]]"
position: top-left
level:
content: >-
[[[
const b = entity.attributes.brightness;
return entity.state === 'on' && b ? Math.round(b / 255 * 100) + '%' : '-';
]]]
position: top-right
grid_options:
rows: 2| Variable | Type | Contents |
|---|---|---|
entity | object | The card's entity: entity.state, entity.attributes, entity.entity_id, entity.last_changed |
states | object | Every entity's state object by ID, as states['sensor.outdoor_temp'].state |
hass | object | The whole Home Assistant object: hass.states, hass.user, hass.formatEntityState(entity) ... |
user | object | The current Home Assistant user (hass.user) |
config | object | This card's whole config |
variables | object | The card's variables |
theme | object | The active theme, with its tokens: theme.colors.ui.primary |
Jinja2 Templates
{{ }} and {% %} are Home Assistant's own templates, rendered by Home Assistant: every function and filter you know from automations works. Use them when you already know Home Assistant's templating, or need something only the server knows.
- The value arrives a moment after the card draws, since Home Assistant renders it. On an error, or with no answer in 5 seconds, the template is shown as written.
- Home Assistant follows every entity the template reads and sends a new value when one changes, so a Jinja2 template keeps itself up to date (see automatic tracking for the card's other templates).
- The card's
configandvariables, anduser(the user's name), are passed in:{{ variables.room }}works.
type: custom:lcards-button
entity: light.demo
preset: lozenge
text:
default:
color: var(--lcars-black)
temperature:
content: "{{ states('sensor.demo_temperature') | float(0) | round(1) }} °C"
position: top-left
status:
content: "{% if is_state('light.demo', 'on') %}LIGHT ON{% else %}LIGHT OFF{% endif %}"
position: bottom-left
grid_options:
rows: 2More about Jinja2 templates
- When it renders: Home Assistant renders it again whenever an entity it reads changes, and at the start of every minute when it uses
now(). The card keeps the template open while it shows it and draws again when the value changes. - The whole string goes to Home Assistant: text around the template and any tokens already filled in are sent with it and come back unchanged.
Data Source Templates
{ds:name} puts in the current value of a data source, a live value LCARdS keeps and processes in the browser; {datasource:name} is the same. Declare the data source in the card's data_sources, and the card updates whenever it does.
- Without a format, the value is shown as Home Assistant shows numbers, with the entity's unit:
21.4 °C. - With a format, you own the output: the number is formatted to it and no unit is added, so write your own after it.
- A dot picks a processor's value:
{ds:name.processor}, and one statistic of astatisticsprocessor with{ds:name.processor.max}. A processor's value has no unit, and without a format shows as a plain number.
type: custom:lcards-button
preset: lozenge
data_sources:
outdoor_temp:
entity: sensor.demo_outdoor_temperature
processing:
fahrenheit:
type: convert_unit
from: c
to: f
text:
default:
color: var(--lcars-black)
native:
content: "{ds:outdoor_temp}" # Home Assistant's number format, with the unit
position: top-left
precise:
content: "{ds:outdoor_temp:.2f} °C" # your precision, your unit
position: top-right
converted:
content: "{ds:outdoor_temp.fahrenheit:.0f} °F" # a processor's value
position: bottom-left
grid_options:
rows: 2| Format | Example | Result |
|---|---|---|
| (none) | {ds:temp} | 21.4 °C: Home Assistant's number format, with the entity's unit |
:.Nf | {ds:temp:.1f} | 21.4: N decimal places, no unit |
:int | {ds:temp:int} | 21: rounded to a whole number, no unit |
:.N% | {ds:level:.0%} | 75%: a percentage. A value whose unit is already % is not multiplied by 100 |
:str | {ds:mode:str} | The value as plain text |
:bool | {ds:flag:bool} | true or false |
The number format follows your Home Assistant language settings, so {ds:temp} reads 21,4 °C where a comma is the decimal mark. An unknown format shows the plain number.
Where Templates Work
Text takes all four kinds of template; colour and other style settings take JavaScript and Jinja2. For a theme colour in a style setting, use theme: instead of a token.
| Setting | Template kinds |
|---|---|
text.<field>.content on the button, elbow and slider (Text Fields) | All four |
| Data grid cells (Data Grid) | All four |
Astrometrics label content (Live Data) | All four |
Slider style.ranges[].label.text (Ranges) | All four |
Slider style.ranges[].value, .min and .max | Tokens, JavaScript and data sources: no Jinja2, since positions are worked out again on every state change |
Style values: style, icon_style (icon_style.icon included) and a text field's color on the button, elbow and slider; the elbow's elbow settings; style on the chart; MSD overlay styles | JavaScript and Jinja2 |
Rule condition (Rule Conditions) | JavaScript and Jinja2 |
Template Evaluation Order
When one string mixes kinds, they are worked out in a fixed order: JavaScript, then tokens, then data sources, then Jinja2. Each step works on the text the one before left.
[[[ ]]]: JavaScript{token}: tokens{ds:…}/{datasource:…}: data sources{{ }}: Jinja2, last because it waits for Home Assistant
Settings that must have a value at once, such as a slider marker's position, stop after step 3.
Entity Tracking & Reactivity
A card works out its templates again when an entity it follows changes: its own entity, the entities its Jinja2 templates name, and the entities in triggers_update. A data source in the card's own data_sources updates it too, whenever the data source changes.
Automatic tracking
Entities named in a Jinja2 template are found and followed with no extra config. The card reads the entity IDs written in states(), state_attr(), is_state(), is_state_attr() and has_value().
- Only IDs written out in quotes are found. The Jinja2 template still updates for an ID built from variables, or the
states.sensor.x.stateform, since Home Assistant follows those itself; add them totriggers_updateonly when the card's other templates (JavaScript, tokens) should update with them.
type: custom:lcards-button
entity: light.demo
preset: lozenge
text:
default:
color: var(--lcars-black)
toggle:
content: "{% if is_state('input_boolean.lcards_test_toggle', 'on') %}TOGGLE ON{% else %}TOGGLE OFF{% endif %}" # followed: is_state()
position: top-left
grid_options:
rows: 2Manual tracking with triggers_update
triggers_update lists more entities whose changes make the card work out its templates again. Use it for the entities LCARdS can't find for itself: those a JavaScript template reads and those in {states.…} tokens.
- It adds to what the card follows; its own
entityis always followed.
type: custom:lcards-button
entity: light.demo
preset: lozenge
triggers_update:
- input_boolean.lcards_test_toggle
- sensor.demo_temperature
text:
default:
color: var(--lcars-black)
status:
content: |
[[[
const toggle = states['input_boolean.lcards_test_toggle']?.state;
const temp = states['sensor.demo_temperature']?.state;
return toggle === 'on' ? `ON (${Number(temp).toFixed(1)}°)` : 'OFF';
]]]
position: top-left
grid_options:
rows: 2| Key | Type | Default | Description |
|---|---|---|---|
triggers_update | list | - | Entity IDs to follow as well as the card's own: when one changes, the card works out all its templates again |