JS Templates
What is a JS Template¶
custom:button-card supports javascript code in specific fields identified by a on the JS column in the configuration options.
The code will be executed and the result will be used as a value for the configuration field.
How to use a JS Template¶
The template rendering uses a special format. All the fields where JS template is supported () also support plain text. To activate the templating feature for such a field, you'll need to enclose the javascript code inside 3 square brackets:
[[[ javascript code here ]]]
If you are using a template in a nested custom:button-card, either in a custom field or config for an action (e.g. custom:button-card as content in browser_mod.popup) then you can include your template in extra enclosing [] pair. The template will then be rendered in the nested custom:button-card with the extra enclosing [] pair removed. Template nesting can be at multiple levels, just include another enclosing [] pair. See here for an example.
Don't forget to quote if it's on one line:
name: '[[[ if (entity.state > 42) return "Above 42"; else return "Below 42" ]]]'
name: >
[[[
if (entity.state > 42)
return "Above 42";
else
return "Below 42";
]]]
Those are the configuration fields which support templating:
name(Supports also HTML rendering): This needs to return a string or anhtml`<elt></elt>`objectstate_display(Supports also HTML rendering): This needs to return a string or anhtml`<elt></elt>`objectlabel(Supports also HTML rendering): This needs to return a string or anhtml`<elt></elt>`object- All the
tooltipparameters. (Tooltipcontentsupports also HTML rendering): This needs to return a string or anhtml`<elt></elt>` entity_picture: This needs to return a path to a file or a url as a string.icon: This needs to return a string in the formatmdi:icon- All the styles in the style object: This needs to return a string
- All the value of the state object, apart from when the operator is
regex operator: template: The function forvalueneeds to return a boolean- Else: The function for
valueneeds to return a string or a number - All the
custom_fields(Support also HTML rendering): This needs to return a string or anhtml`<elt></elt>`object - All the
styles: Each entry needs to return a string (See here for some examples) - The
extra_stylesfield - Everything field in
*_action - The confirmation text (
confirmation.text) - The lock being enabled or not (
lock.enabled) - all the
cardparameters in acustom_field - all the
variables hidden: should return a boolean
Special fields which do support templating but are only evaluated once, when the configuration is loaded. Error in those templates will only be visible in the javascript console and the card will not display in that case:
entity: You can use JS templates for theentityof the card configuration. It is mainly useful when you define your entity in as an entry invariables. This is evaluated once only when the configuration is loaded.
type: custom:button-card
variables:
my_entity: switch.skylight
entity: '[[[ return variables.my_entity; ]]]'
triggers_update: Useful when you define multiple entities invariablesto use throughout the card. Eg:
type: custom:button-card
variables:
my_entity: switch.skylight
my_other_entity: light.bedroom
entity: '[[[ return variables.my_entity; ]]]'
label: '[[[ return localize(variables.my_other_entity) ]]]'
show_label: true
triggers_update:
- '[[[ return variables.my_entity; ]]]'
- '[[[ return variables.my_other_entity; ]]]'
JS Templates helpers¶
Inside the javascript code, you'll have access to those variables:
this: The button-card element itself (advanced stuff, don't mess with it)entity: The current entity object, if the entity is defined in the cardstates: An object with all the states of all the entities (equivalent tohass.states)user: The user object (equivalent tohass.user)hass: The completehassobjectvariables: an object containing all your variables defined in the configuration. See Variables- Helper functions availble through the object
helpers: helpers.localize(entity, state?, numeric_precision?, show_units?, units?): a function which localizes a state to your language (eg.helpers.localize(entity)) and returns a string. Takes an entity object as argument (not the state of the entity as we need context) and takes optional arguments. Works with numerical states also.- If
stateis not provided, it localizes the state of theentity(Eg.helpers.localize(entity)orhelpers.localize(states['weather.your_city'])). - If
stateis provided, it localizesstatein the context of theentity(eg. :helpers.localize(entity, entity.attributes.forecast[0].condition)orhelpers.localize(states['weather.your_city'], states['weather.your_city'].attributes.forecast[0].condition)) numeric_precision(number or'card'. Default isundefined): For state which are numbers, force the precision instead of letting HA decide for you. If the value is set to'card', it will use thenumeric_precisionfrom the main config. Ifundefined, it will use the default value for the entity you're willing to display. The latter is the default.show_units(boolean. Default istrue): Will display units or not. Default is to display them.units(string): Will force the units to be the value of that parameter.- To skip one or multiple parameter while calling the function, use
undefined. Eg.helpers.localize(states['sensor.temperature'], undefined, 1, undefined, 'Celcius')
- If
- Date, Time and Date Time format helpers, all localized (takes a string or a
Dateobject as input):helpers.formatTime(time): 21:15 / 9:15helpers.formatTimeWithSeconds(time): 9:15:24 PM || 21:15:24helpers.formatTimeWeekday(time): Tuesday 7:00 PM || Tuesday 19:00helpers.formatTime24h(time): 21:15helpers.formatDateWeekdayDay(date): Tuesday, August 10helpers.formatDate(date): August 10, 2021helpers.formatDateNumeric(date): 10/08/2021helpers.formatDateShort(date): Aug 10helpers.formatDateMonthYear(date): August 2021helpers.formatDateMonth(date): Augusthelpers.formatDateYear(date): 2021helpers.formatDateWeekday(date): Mondayhelpers.formatDateWeekdayShort(date): Monhelpers.formatDateTime(datetime): August 9, 2021, 8:23 AMhelpers.formatDateTimeNumeric(datetime): Aug 9, 2021, 8:23 AMhelpers.formatDateTimeWithSeconds(datetime): Aug 9, 8:23 AMhelpers.formatShortDateTime(datetime): August 9, 2021, 8:23:15 AMhelpers.formatShortDateTimeWithYear(datetime): 9/8/2021, 8:23 AM- Example:
return helpers.formatDateTime(entity.attribute.last_changed)
helpers.relativeTime(date, capitalize? = false): Returns an lit-html template which will render a relative time and update automatically.dateshould be a string.capitalizeis an optional boolean, if set totrue, the first letter will be uppercase. Usage for eg.:return helpers.relativeTime(entity.last_changed)helpers.parseDuration(duration,format?='ms',locale? = <Home Assistant locale>): Parses a string duration to number.helpers.parseDuration('1 day', 's')returns86400.helpers.parseDuration('1 jour', 'd', 'fr')returns1. If a locale is specifiedenis also used as a fallback.helpers.toastMessage(message)andhelpers.toast(toastParams): See toast helpers
See here for some examples or here for some advanced configuration using templates.