mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-09 18:36:51 +00:00
Add zip template documentation (#33934)
* Add zip template documentation * Update source/_docs/configuration/templating.markdown Co-authored-by: Stefan Agner <stefan@agner.ch> --------- Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> Co-authored-by: Stefan Agner <stefan@agner.ch>
This commit is contained in:
parent
f4fdd7f077
commit
a6d0f39cb4
@ -1091,6 +1091,38 @@ While Jinja natively supports the conversion of an iterable to a `list`, it does
|
||||
|
||||
Note that, in Home Assistant, to convert a value to a `list`, a `string`, an `int`, or a `float`, Jinja has built-in functions with names that correspond to each type.
|
||||
|
||||
### Iterating multiple objects
|
||||
|
||||
The `zip()` function can be used to iterate over multiple collections in one operation.
|
||||
|
||||
{% raw %}
|
||||
|
||||
```text
|
||||
{% set names = ['Living Room', 'Dining Room'] %}
|
||||
{% set entities = ['sensor.living_room_temperature', 'sensor.dining_room_temperature'] %}
|
||||
{% for name, entity in zip(names, entities) %}
|
||||
The {{ name }} temperature is {{ states(entity) }}
|
||||
{% endfor %}
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
`zip()` can also unzip lists.
|
||||
|
||||
{% raw %}
|
||||
|
||||
```text
|
||||
{% set information = [
|
||||
('Living Room', 'sensor.living_room_temperature'),
|
||||
('Dining Room', 'sensor.dining_room_temperature')
|
||||
] %}
|
||||
{% set names, entities = zip(*information) %}
|
||||
The names are {{ names | join(', ') }}
|
||||
The entities are {{ entities | join(', ') }}
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
### Functions and filters to process raw data
|
||||
|
||||
These functions are used to process raw value's in a `bytes` format to values in a native Python type or vice-versa.
|
||||
|
Loading…
x
Reference in New Issue
Block a user