JavaScript templates¶
A JavaScript template lets a supported card setting change while the card is running. It can use the current entity, another card entity, or another Home Assistant state to calculate text, colors, visibility, sizes, positions, and other dynamic values.
This page shows the basic template syntax, the values available inside a template, and the simplest ways to read current card and Home Assistant data.
Change something from the current entity state¶
This color changes with the state of the entity used by the item:
Use another entity from the same card¶
Use the entity's index in the card's entities list:
Read another Home Assistant entity¶
Read another entity when a dynamic value depends on Home Assistant data that is not the item's own selected entity. Include it in entities so its changes update the card:
entities list, even when no item displays it directly. This includes entities read through states or hass.states. An unlisted entity can still be read, but its changes alone do not update the card. Its latest value is read when a listed entity or another supported card context changes. Theme and light/dark changes also update theme-dependent templates. Calculate a value from a state¶
Use a JavaScript calculation when the displayed/configured value must be derived from the current state instead of copied directly.
Available values¶
These values are available inside a JavaScript template so the calculation can use the current card and Home Assistant context.
| Value | What it contains |
|---|---|
state | State/selected attribute of the entity used by the current item |
entity | Complete current entity |
entities | Entities listed by the card |
states | Home Assistant entity states |
hass | Current Home Assistant object |
config | Complete current card configuration |
constants | Values from the card's constants: block |
entity_slots | Named entity-slot indexes for the current card |
item | Current configured item |
user | Current Home Assistant user |
Keep templates readable¶
Keep the calculation short and return one value. If the value never changes after the card is created, use normal YAML, Reuse, or calc() instead.