Note
This topic was created prior to the release of 2021.10.0 and was meant to inform the community of important upcoming changes to certain aspects of templating.
After the release of 2021.10.0, petro posted many examples of how to adapt templates to comply with the new requirements.
I recommend reviewing the examples before posting questions.
I skimmed the pre-release notes and selected several notable templating improvements and modifications.
Breaking Change
Several existing functions will handle defaults differently. PR 5643
The following template filters and functions will now log a warning instead of silently returning the input if the input is invalid and no default value is specified:
round
,multiply
,log
,sin
,cos
,tan
,asin
,acos
,atan
,atan2
,sqrt
,timestamp_custom
,timestamp_local
,timestamp_utc
,as_timestamp
,strptime
,float
The float filter will now log a warning instead of silently returning 0 if the input is invalid and no default value is specified.
In Home Assistant core 2021.12 the template will fail to render if no default value is specified.
Examples:
{{ "abc" | float }}
- Will render as 0, and a warning will be logged
{{ "abc" | float(default=5) }}
- Will render as -5, no warning will be logged
{{ float("abc") }}
- Will render as “abc”, and a warning will be logged
{{ float("abc", default=5) }}
- Will render as -5, no warning will be logged
New/Enhanced Functions
is_numeric
New function that indicates if the supplied value is numeric. PR 56705
is_numeric
will returnsTrue
if the input can be parsed by Python’s float function and its value is not inf or nan, in all other cases returnsFalse
. Can be used as a filter.Examples:
{{ "" | is_numeric}}
- will render asFalse
{{ "1" | is_numeric}}
- will render asTrue
{{ "1.1" | is_numeric}}
- will render asTrue
{{ "nan" | is_numeric}}
- will render asFalse
{{ "inf" | is_numeric}}
- will render asFalse
{{ "½" | is_numeric}}
- will render asFalse
{{ True | is_numeric }}
- will render asTrue
{{ False | is_numeric }}
- will render asTrue
{{ "True" | is_numeric}}
- will render asFalse
{{ "False" | is_numeric}}
- will render asFalse
regex_findall
New function that returns an array of matches. PR 54584
Filter
string|regex_findall(find='', ignorecase=False)
will find all regex matches of the find expression in string and return the array of matches.
device_id
Existing function that now accepts a device name. PR 55474
device_id(entity_id)
returns the device ID for a given entity ID or device name. Can also be used as a filter