Variables allow you to reuse logic and values across multiple fields. They can be any type (string, number, object, function).
Note: Variables are only evaluated if they are used. If a variable is defined but never referenced, it will not be evaluated.
Simple Usage
You can define a variable once and reference it in multiple style or name fields.
type: custom:button-card
entity: switch.skylight
variables:
color_on: green
color_off: red
is_on: '[[[ return entity.state === "on"; ]]]'
color: |
[[[
if (variables.is_on) return variables.color_on;
else return variables.color_off;
]]]
styles:
icon:
- color: '[[[ return variables.color; ]]]'
Using Variables as Functions
You can store functions in variables to perform complex logic.
type: custom:button-card
variables:
myFunc: |
[[[
return (str) => { return `${str} from myFunc()`; }
]]]
name: '[[[ return variables.myFunc("Nice Name"); ]]]'
Variable Dependencies
Variables can depend on other variables, but circular dependencies (loops) are prohibited and will cause failure.
Advanced: Forcing Variable Evaluation
By default, variables are only evaluated if used. To force a variable to evaluate every time the card updates (useful for advanced JS logic), use the force_eval: true property:
variables:
varName:
force_eval: true
value: '[[[ ... ]]]'