Skip to content

Commit 011c897

Browse files
committed
Add more samples
1 parent 4ea29a7 commit 011c897

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

source/_topics/templating.markdown

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ script:
5959
6060
Home Assistant adds extensions to allow templates to access all of the current states:
6161
62-
- Iterating `states` will yield each state sorted alphabetically by entity ID.
63-
- Iterating `states.domain` will yield each state of that domain sorted alphabetically by entity ID.
64-
- `states.sensor.temperature` returns the state object for `sensor.temperature`.
65-
- `states('device_tracker.paulus')` will return the state string (not the object) of the given entity or `unknown` if it doesn't exist.
66-
- `is_state('device_tracker.paulus', 'home')` will test if the given entity is specified state.
67-
- `is_state_attr('device_tracker.paulus', 'battery', 40)` will test if the given entity is specified state.
68-
- Filter `multiply(x)` will convert the input to a number and multiply it with `x`.
69-
- Filter `round(x)` will convert the input to a number and round it to `x` decimals.
70-
- `now` will be rendered as current time in your time zone.
71-
- `utcnow` will be rendered as UTC time.
72-
- `as_timestamp` will convert datetime object or string to UNIX timestamp
73-
- `distance()` will measure the distance in meters between home, entity, coordinates.
74-
- `closest()` will find the closest entity.
75-
- `relative_time(timestamp)` will format the date time as relative time vs now (ie 7 seconds)
76-
62+
- Iterating `states` will yield each state sorted alphabetically by entity ID.
63+
- Iterating `states.domain` will yield each state of that domain sorted alphabetically by entity ID.
64+
- `states.sensor.temperature` returns the state object for `sensor.temperature`.
65+
- `states('device_tracker.paulus')` will return the state string (not the object) of the given entity or `unknown` if it doesn't exist.
66+
- `is_state('device_tracker.paulus', 'home')` will test if the given entity is specified state.
67+
- `is_state_attr('device_tracker.paulus', 'battery', 40)` will test if the given entity is specified state.
68+
- Filter `multiply(x)` will convert the input to a number and multiply it with `x`.
69+
- Filter `round(x)` will convert the input to a number and round it to `x` decimals.
70+
- `now` will be rendered as current time in your time zone.
71+
- `utcnow` will be rendered as UTC time.
72+
- `as_timestamp` will convert datetime object or string to UNIX timestamp
73+
- `distance()` will measure the distance in meters between home, entity, coordinates.
74+
- `closest()` will find the closest entity.
75+
- `relative_time(timestamp)` will format the date time as relative time vs now (ie 7 seconds)
76+
- `float` will format the output as float.
7777

7878
## {% linkable_title Examples %}
7979

@@ -93,9 +93,9 @@ Print an attribute if state is defined
9393
```jinja2
9494
{% raw %}
9595
{% if states.device_tracker.paulus %}
96-
{{ states.device_tracker.paulus.attributes.battery }}
96+
{{ states.device_tracker.paulus.attributes.battery }}
9797
{% else %}
98-
??
98+
??
9999
{% endif %}{% endraw %}
100100
```
101101

@@ -119,7 +119,12 @@ Print out a list of all the sensor states.
119119
120120
{% if states('sensor.temperature') | float > 20 %}
121121
It is warm!
122-
{%endif %}{% endraw %}
122+
{%endif %}
123+
124+
{{ as_timestamp(states.binary_sensor.garage_door.last_changed) }}
125+
126+
{{ as_timestamp(now) - as_timestamp(states.binary_sensor.garage_door.last_changed) }}
127+
{% endraw %}
123128
```
124129

125130
### {% linkable_title Distance examples %}
@@ -183,4 +188,11 @@ It depends per component or platform but it is common to be able to define a tem
183188
184189
# Extract third prime number
185190
{% raw %}{{ value_json.primes[2] }}{% endraw %}
191+
192+
# Format output
193+
{% raw %}{{ "%+.1f" | value_json }}{% endraw %}
194+
195+
# Calculations
196+
{% raw %}{{ value_json | multiply(1024) }}{% endraw %}
197+
{% raw %}{{ value_json.used | multiply(0.0001) | round(0) }}{% endraw %}
186198
```

0 commit comments

Comments
 (0)