Skip to content

Commit 405e150

Browse files
dale3hfabaff
authored andcommitted
Add important template notes (home-assistant#1397)
1 parent a3bda5d commit 405e150

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

source/getting-started/automation-templating.markdown

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ automation 2:
3737
message: {% raw %}{{ trigger.payload }}{% endraw %}
3838
```
3939
40+
## {% linkable_title Important Template Rules %}
41+
42+
There are a few very important rules to remember when writing automation templates:
43+
44+
1. You ***must*** use `data_template` in place of `data` when using templates in the `data` section of a service call.
45+
1. You ***must*** use `service_template` in place of `service` when using templates in the `service` section of a service call.
46+
1. You ***must*** surround single-line templates with double quotes (`"`) or single quotes (`'`).
47+
1. It is advised that you prepare for undefined variables by using `if ... is not none` or the [`default` filter](http://jinja.pocoo.org/docs/dev/templates/#default), or both.
48+
1. It is advised that when comparing numbers, you convert the number(s) to a [`float`](http://jinja.pocoo.org/docs/dev/templates/#float) or an [`int`](http://jinja.pocoo.org/docs/dev/templates/#int) by using the respective [filter](http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters).
49+
1. While the [`float`](http://jinja.pocoo.org/docs/dev/templates/#float) and [`int`](http://jinja.pocoo.org/docs/dev/templates/#int) filters do allow a default fallback value if the conversion is unsuccessful, they do not provide the ability to catch undefined variables.
50+
51+
Remembering these simple rules will help save you from many headaches and endless hours of frustration when using automation templates.
52+
4053
## {% linkable_title Available Trigger Data %}
4154

4255
The following tables show the available trigger data per platform.

0 commit comments

Comments
 (0)