Add details about math, comparision, and logic (#749)

This commit is contained in:
Fabian Affolter 2016-08-08 17:52:35 +02:00 committed by Paulus Schoutsen
parent 79850a1e24
commit 24d3f6adaa

View File

@ -21,19 +21,17 @@ This is an advanced feature of Home Assistant. You'll need a basic understanding
Templating is a powerful feature in Home Assistant that allows the user control over information that is going into and out of the system. It is used for:
- Formatting outgoing messages in, for example, the [notify] and [alexa] components.
- Process incoming data from sources that provide raw data, like [MQTT], [Rest sensor] or the [command line sensor].
- Process incoming data from sources that provide raw data, like [MQTT], [REST sensor], or the [command line sensor].
[notify]: /components/notify/
[alexa]: /components/alexa/
[MQTT]: /components/mqtt/
[Rest sensor]: /components/sensor.rest/
[REST sensor]: /components/sensor.rest/
[command line sensor]: /components/sensor.command_line/
## {% linkable_title Building templates %}
Templating in Home Assistant is powered by the Jinja2 templating engine. This means that we are using their syntax and make some custom Home Assistant variables available to templates during rendering. We will not go over the basics of the syntax, as Jinja2 does a lot better job at this in their [Jinja2 documentation].
[Jinja2 documentation]: http://jinja.pocoo.org/docs/dev/templates/
Templating in Home Assistant is powered by the [Jinja2](http://jinja.pocoo.org/) templating engine. This means that we are using their syntax and make some custom Home Assistant variables available to templates during rendering. We will not go over the basics of the syntax, as Jinja2 does a lot better job at this in their [Jinja2 documentation](http://jinja.pocoo.org/docs/dev/templates/).
<p class='note'>
The frontend has a template editor developer tool to help develop and debug templates.
@ -55,6 +53,13 @@ script:
{% endif %}{% endraw %}
```
[Jinja2](http://jinja.pocoo.org/) supports a width variety of operations:
- [Mathematical operation](http://jinja.pocoo.org/docs/dev/templates/#math)
- [Comparisons](http://jinja.pocoo.org/docs/dev/templates/#comparisons)
- [Logic](http://jinja.pocoo.org/docs/dev/templates/#logic)
## {% linkable_title Home Assistant template extensions %}
Home Assistant adds extensions to allow templates to access all of the current states:
@ -72,7 +77,6 @@ Home Assistant adds extensions to allow templates to access all of the current s
- `closest()` will find the closest entity.
- `relative_time(timestamp)` will format the date time as relative time vs now (ie 7 seconds)
- `float` will format the output as float.
- Filter `multiply(x)` will convert the input to a number and multiply it with `x`.
- Filter `round(x)` will convert the input to a number and round it to `x` decimals.
- Filter `timestamp_local` will convert an UNIX timestamp to local time/data.
- Filter `timestamp_utc` will convert an UNIX timestamp to UTC time/data.
@ -117,7 +121,9 @@ Print out a list of all the sensor states.
Paulus is at {{ states('device_tracker.paulus')) }}.
{% endif %}
{{ states.sensor.temperature | multiply(10) | round(2) }}
{{ states.sensor.temperature | float + 1 }}
{{ states.sensor.temperature | float * 10 | round(2) }}
{% if states('sensor.temperature') | float > 20 %}
It is warm!
@ -180,9 +186,9 @@ The other part of templating is processing incoming data. It will allow you to m
It depends per component or platform but it is common to be able to define a template using the `value_template` configuration key. When a new value arrives, your template will be rendered while having access to the following values on top of the usual Home Assistant extensions:
| Variable | Description |
| ------------ | ----------- |
| `value` | The incoming value.
| `value_json` | The incoming value parsed as JSON.
| ------------ | -------------------------------------- |
| `value` | The incoming value. |
| `value_json` | The incoming value parsed as JSON. |
```jinja2
# Incoming value:
@ -194,9 +200,9 @@ It depends per component or platform but it is common to be able to define a tem
# Format output
{% raw %}{{ "%+.1f" | value_json }}{% endraw %}
# Calculations
{% raw %}{{ value_json | multiply(1024) }}{% endraw %}
{% raw %}{{ value_json.used | multiply(0.0001) | round(0) }}{% endraw %}
# Math
{% raw %}{{ value_json | float * 1024 }}{% endraw %}
{% raw %}{{ float(value_json) * (2**10) }}{% endraw %}
# Timestamps
{% raw %}{{ value_json.tst | timestamp_local }}{% endraw %}