Skip to content

Commit 8849da7

Browse files
Template docs (#4411)
1 parent 62bce12 commit 8849da7

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

docs/faq.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,23 @@ Be aware that Home Assistant shows friendly alternatives for some sensors, so wh
4848

4949

5050
Example of voltage sensor with a maximum capacity of 3 volts, with a linear percentage (3 volts = 100%, 0 volts = 0%)
51-
```{{ (states('sensor.my_sensor_voltage')|float(0) / 3 * 100) | round(0) }}```
51+
```yaml
52+
{% set v = states('sensor.my_sensor_voltage') %}
53+
{{
54+
(v | float / 3 * 100) | round(0)
55+
if v not in ['unknown','unavailable'] else 'unknown'
56+
}}
57+
```
58+
5259

5360
Example of voltage sensor with a maximum capacity of 3 volts, where 2 volts should be equivalent to 10%
54-
```{{ [0, (((states('sensor.voltage')|float(0) - 2) / (3 - 2)) * 90 + 10) | round(0)] | max }}```
61+
```yaml
62+
{% set v = states('sensor.my_sensor_voltage') %}
63+
{{
64+
[0, (((v | float - 2) / (3 - 2)) * 90 + 10) | round(0)] | max
65+
if v not in ['unknown','unavailable'] else 'unknown'
66+
}}
67+
```
5568

5669
Example of binary low sensor, returning either 100% or 9%
5770
```{{ 9 if states('binary_sensor.my_sensor_low') == true else 100 }}```

docs/index.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ Specifying a template for devices without a typical battery percentage will crea
5050
You can specify a template that must return a percentage (0-100).
5151

5252
Example of voltage sensor with a maximum capacity of 3 volts, with a linear percentage (3 volts = 100%, 0 volts = 0%)
53-
```{{ (states('sensor.my_sensor_voltage')|float(0) / 3 * 100) | round(0) }}```
53+
```yaml
54+
{% set v = states('sensor.my_sensor_voltage') %}
55+
{{
56+
(v | float / 3 * 100) | round(0)
57+
if v not in ['unknown','unavailable'] else 'unknown'
58+
}}
59+
```
60+
5461

5562
Example of voltage sensor with a maximum capacity of 3 volts, where 2 volts should be equivalent to 10%
56-
```{{ [0, (((states('sensor.voltage')|float(0) - 2) / (3 - 2)) * 90 + 10) | round(0)] | max }}```
63+
```yaml
64+
{% set v = states('sensor.my_sensor_voltage') %}
65+
{{
66+
[0, (((v | float - 2) / (3 - 2)) * 90 + 10) | round(0)] | max
67+
if v not in ['unknown','unavailable'] else 'unknown'
68+
}}
69+
```
5770

5871
Example of binary low sensor, returning either 100% or 9%
5972
```{{ 9 if states('binary_sensor.my_sensor_low') == true else 100 }}```

0 commit comments

Comments
 (0)