Swiss Army Knife templates¶
Introduction¶
If you are familiar with the decluttering template card
, you already know how to re-use cards and specialize them using variables.
SAK templates operate on a different level: they enable the re-use of tools
, toolsets
, JavaScript templates/snippets
, colorstops/lists
and more.
The upcoming rc.3 will have support for layout templates: you can re-us a complete card in a view!
No need to use the decluttering templates anymore.
SAK templates therefore make re-use within a SAK card, and between SAK cards possible!
And as you can scale a toolset
, you can use different sizes of a toolset
template in various cards. Example 7 shows an example of re-using a toolset in various sizes.
Basics¶
Usage
SAK Templates can cover almost any part of the SAK YAML configuration.
Most used templates are:
- toolsets
- derived entities
- colorstops and colorlists
A template can use another template
Yup. Templates can use templates. A toolset template for instance can use a colorstop template.
But take care: don't create a loop. There is no safeguard for this (ab)use.
Location
SAK templates are stored in the lovelace\sak_templates\templates
folder and automatically included by the sak_templates.yaml
file in the lovelace/sak_templates
folder.
Examples¶
Toolset example¶
This example shows the use of a Toolset template.
- it shows the name of the template to use
- it shows the variables to pass to the template
view-xyz.yaml | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Below the partial config of the tools_segarc_icon_state
template:
- the first 7 lines define the template part with passed variables of the config
- from line 8, the toolset itself is defined. Identical to any inline toolset config. Except for the variable substitution of course
tools-segarc-icon-state template.yaml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Colorstop example¶
Say you want some consistency for the cards that show the inside temperature colors.
You define a colorstops template, and use that in several cards/tools.
view | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
And the full colorstop
template definition:
template | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
The result would be a replaced colorstops part:
result | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Derived Entity example¶
Many examples calculate the brightness
attribute from a light using a derived_entity
: Home Assistant passes the brightness
attribute as a value between 0 and 255. This range is converted using a JavaScript template to a 0..100 (%) range.
Example 10 is using a template for one card to show that you could re-use this JavaScript template.
view | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
And the template definition:
template | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Advanced usage, Overrides and Extensions¶
The standard way of changing the template is to pass pre-defined variables to the template.
In some cases however, one wants to change more parts of the template in which case variables may not suit those needs.
In those cases, SAK templates provide the same sort of functionality as the built-in YAML Overrides and Extensions.
Position override example¶
This example shows the simple use of overwriting parts of the Toolset template without using template variables.
In this case the position
record is simply overridden instead of passing cx
and cy
as variables.
From: view-sake7.yaml | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Tool override within toolset example¶
We can even go a bit further and overwrite a tool within the list of tools.
A tool needs a unique ID to be able to overwrite the tool config
In this case the segarc
has id: 0
and parts of the config are overwritten!
Overriding and extending parts of a template is powerfull but can be error-prone. So take care!
The yellow lines show the parts that are overwritten.
From: view-sake7 | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
The full config for the segarc tool is below. The yellow lines show the dictionaries that are overridden:
- the
show
config dictionary - the
segments
config dictionary
The tool to replace parts of is selected by the id:
field
From: template | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
The resulting YAML config will be as follows with the overridden and/or extended position:
, show:
and segments:
dicts.
From: result | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|