Templating
The template helper enables one to mathematically manipulate values and use variables to extract values from JSON.
For a complete overview, check the Jinja2 Template documentation.
Accessing variables
The variables are handled the same way as in Python.
Method | description |
---|---|
{{ value.x }} or {{ value.['x'] }} |
Normal value |
{{ value_json.x }} |
JSON value |
The evaluation leads to an empty string if it’s unsuccessful and printed or iterated over.
The states
variable
The template support has a special states
variable:
- Iterating
states
will yield each state sorted alphabetically by entity id - Iterating
states.domain
will yield each state of that domain sorted alphabetically by entity id states.sensor.temperature
returns state object forsensor.temperature
Example templates and what they could result in:
Template | Output |
---|---|
{{ states.device_tracker.paulus.state }} |
home |
{% for state in states.sensor %}{{ state.entity_id }}={{ state.state }}, {% endfor %} |
senor.thermostat=24, sensor.humidity=40, |
Mathematical functions
The mathematical methods convert strings to numbers automatically before they are doing their job. This could be useful if you recieve data in the wrong unit of measurement and want to convert it. They are used like the standard Jinja2 filters.
Method | description |
---|---|
multiply(x) |
Multiplies the value with x |
round(x) |
Maximal number x of decimal places for the value |
A sample sensor entry for your configuration.yaml
file could look like this:
# Example configuration.yaml entry sensor: platform: dweet device: temp-sensor012 value_template: '{{ value_json.temperature | multiply(1.02) | round(2) }}' unit_of_measurement: "°C"