File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -48,10 +48,23 @@ Be aware that Home Assistant shows friendly alternatives for some sensors, so wh
4848
4949
5050Example 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
5360Example 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
5669Example of binary low sensor, returning either 100% or 9%
5770``` {{ 9 if states('binary_sensor.my_sensor_low') == true else 100 }} ```
Original file line number Diff line number Diff line change @@ -50,10 +50,23 @@ Specifying a template for devices without a typical battery percentage will crea
5050You can specify a template that must return a percentage (0-100).
5151
5252Example 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
5562Example 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
5871Example of binary low sensor, returning either 100% or 9%
5972``` {{ 9 if states('binary_sensor.my_sensor_low') == true else 100 }} ```
You can’t perform that action at this time.
0 commit comments