Skip to content

X[DEPRECATED] Mod plugin templates

Thomas Lovén edited this page Apr 18, 2023 · 1 revision

A number of my plugins use what I call templates for dynamically updating content or settings.

Currently, templates are used by:

Usage

Wherever templates can be used, they should be put between double square brackets: [[ <template> ]]. Once processed, this will be replaced with the result of the template expression.

The <template> can get the following replacements:

Entity state

If <template> is an entity id, or <entity_id>.state it will be replaced with the current state of that entity.

Example:
[[ light.bed_light ]] ➡️ on
[[ sensor.time.state ]] ➡️ 23:44

Entity attribute

If <template> is <entity_id>.attributes.<attribute> it will be replaced by the current value of that attribute for that entity.

Example:
[[ light.bed_light.attributes.friendly_name ]] ➡️ Bed Light
[[ device_tracker.iphone.attributes.source_type ]] ➡️ gps
[[ light.kitchen_light.attributes.icon ]] ➡️ mdi:lamp

Last change/update time

If <template> is <entity_id>.last_changedor<entity_id>.last_updated` it will be replaced with a timestamp showing the time of the latest change or update for the entity

Example
[[ light.bed_light.last_changed ]] ➡️ 2019-06-07 20:39:46.301114+00:00

Username

If <template> is {user} it will be replaced with the username of the currently logged in user

Example
[[ {user} ]] ➡️ thomas

Browser ID

If <template> is {browser}` it will be replaced with a unique identifier for the currently used device-browser combination.

Now, this one requires some explanation. The Browser ID is a 16 character random string which is generated the first time it is requested. It is then stored in your browser and will be the same every time you return to your lovelace interface using the same browser. It can be useful to show different things on different devices - like if you only want to show a certain lovelace card on the dashboard in your kitchen. Or show more options on your phone than on the kids'.

Note that:

  • Using e.g. Firefox and Chrome on the same computer/phone/tablett will give two different Browser IDs
  • Using e.g Chrome on two different computers will give two different Browser IDs
  • Using incognito mode will give different Browser IDs every time
  • The date is stored locally in your browser (using LocalStorage)
  • ONLY Home-Assistant can access the Browser ID. No other page can read or change it, or use it to identify you in any way.

URL hash

If <template> is {hash} it will be replaced with the hash part of the current URL.

You may already know that http://<hass>:8123/lovelace/0 and http://<hass>:8123/lovelace/0#whatever will load exactly the same page. In this case the "hash part of the current URL" is whatever. This could be useful in combination with the navigate tap_action.

Eg:

If you add the following button card to one of your lovelace views

type: entity-button
entity: time.time
icon: mdi:numeric-1
name: " "
tap_action:
  acton: navigate
  navigation_path: "#something_else"

clicking it will replace the "hash part of the current URL" with something_else without reloading the page - but templates will be re-evaluated.

Condition

If <template> is of the form if(<condition>, <then>, <else>) it will be replaced with the template in <then> if <condition> is true, and with the template in <else> otherwise.

<condition> should be on the form <left> <comparison> <right> where <comparison> is ==, !=, <, <=, >= or >; and <left> and <right> are templates, numbers or strings.

<then> and <else> can be any <template> - including further if()-expressions

Example
[[ if(light.bed_light == "on", "The lights are on", "It's dark") ]]
[[ if(input_number.x_pos <= 30, "small", if(input_number.x_pos <= 70, "Medium", "LARGE")) ]]
[[ if({hash} == "99980b13-dabc9563", "visible", "hidden") ]] (though there are better ways to do this - see state-switch)