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.
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.
-toolset:memory# template type be of type "toolset"# Use pre-defined template for this part.# Template has variables.template:name:tools_segarc_icon_statevariables:# Pass the list of variables-entity:0# - entity id-cx:72.5# - center x position-cy:74# - center y position
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
template:type:toolset# toolset template it is...defaults:# Default values for vars-entity:0-cx:25-cy:50-show_scale:falsetoolset:# From here the toolset is defined!position:cx:'[[cx]]'# Templated var!!cy:'[[cy]]'# Templated var!!scale:1tools:-type:segarcid:0# Unique id within tools configentity_index:'[[entity]]'# Templated var!!position:cx:50cy:50start_angle:0end_angle:360width:5radius:25scale:min:0max:100show:scale:'[[show_scale]]'# Templated var!!style:'colorlist'<...># rest of config removed for brevity
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 9101112131415161718192021
-type:'segarc'position:cx:50cy:50start_angle:-130end_angle:130width:7radius:45entity_index:0scale:min:15# Inside temperaturemax:25offset:-4.5show:style:colorstops# Use a colorstopsegments:colorstops:# The colorstops dict is replacedtemplate:# Use template colorstops (re-use!)name:colorstops_temperature_insidevariables:# Pass variables to template-thegap:1
And the full colorstop template definition:
template
1 2 3 4 5 6 7 8 91011
colorstops_temperature_inside:template:type:colorstops# MUST reference colorstopsdefaults:# Give vars a default value-thegap:1colorstops:# The contents of the template!gap:'[[thegap]]'# Use '[[ ]]' for template varscolors:17:'PowderBlue'19:'var(--theme-gradient-color-01)'21:'var(--theme-gradient-color-04)'
The result would be a replaced colorstops part:
result
1 2 3 4 5 6 7 8 910111213141516171819202122
-type:'segarc'position:cx:50cy:50start_angle:-130end_angle:130width:7radius:45entity_index:0scale:min:15# Inside temperaturemax:25offset:-4.5show:style:colorstops# Use a colorstopsegments:colorstops:# The colorstops dict is replacedgap:1colors:17:'PowderBlue'19:'var(--theme-gradient-color-01)'21:'var(--theme-gradient-color-04)'
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.
-type:segarcid:0entity_index:1position:cx:80cy:25start_angle:0end_angle:360width:2radius:15scale:min:0max:100show:scale:falsestyle:'colorlist'derived_entity:template:name:derived_entity_brightness# BUG:# Do need a variable section here and defaults in# template itself. That should not be the case...## A template without variables SHOULD be possible...variables:-dummyvar:'bug'segments:colorlist:gap:1colors:-'var(--primary-text-color)'animation:duration:5styles:foreground:fill:var(--primary-text-color)background:fill:var(--theme-background-color-darken-20)
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.
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 910
-toolset:memory# Use pre-defined template for this part.# Template has variables.template:name:tools_segarc_icon_statevariables:# Pass the list of variables-entity:0# - entity idposition:# 'Overwrite' the position from templatecx:72.5# Yes, we could have passed these as...cy:74# ...variables!
-toolset:memory# Use pre-defined template for this part.# Template has variables.template:name:tools_segarc_icon_statevariables:-entity:0position:cx:72.5cy:74# Scale to 80% of toolset template.scale:.8# Overwrite template with the following configuration not covered by the template# variables. id is required to make this work. So much possibilities...tools:-type:segarcid:0show:scale:truestyle:'colorstops'segments:colorstops:gap:1colors:00:'DarkSeaGreen'70:'yellow'80:'orange'90:'red'
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