Advanced automation documentation using templating.
2016-04-24 08:30 +0100
true
false
true
true
/getting-started/automation-templating/
In Home Assistant 0.19 we introduced a new powerful feature: variables in scripts and automations. This makes it possible to adjust your condition and action based on the information of the trigger.
The trigger data made is available during template rendering as the trigger variable.
# Example configuration.yaml entriesautomation:trigger:platform:stateentity_id:device_tracker.paulusaction:service:notify.notifydata_template:message:>{% raw %}Paulus just changed from {{ trigger.from_state.state }}to {{ trigger.to_state.state }}{% endraw %}automation 2:trigger:platform:mqtttopic:/notify/+action:service_template:>{% raw %}notify.{{ trigger.topic.split('/')[-1] }}{% endraw %}data_template:message:{% raw %}'{{ trigger.payload }}'{% endraw %}
{% linkable_title Important Template Rules %}
There are a few very important rules to remember when writing automation templates:
You must use data_template in place of data when using templates in the data section of a service call.
You must use service_template in place of service when using templates in the service section of a service call.
You must surround single-line templates with double quotes (") or single quotes (').
It is advised that you prepare for undefined variables by using if ... is not none or the default filter, or both.
It is advised that when comparing numbers, you convert the number(s) to a float or an int by using the respective filter.
While the float and int filters do allow a default fallback value if the conversion is unsuccessful, they do not provide the ability to catch undefined variables.
Remembering these simple rules will help save you from many headaches and endless hours of frustration when using automation templates.
{% linkable_title Available Trigger Data %}
The following tables show the available trigger data per platform.