Improve template sensor documentation (#27492)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2023-05-23 15:08:24 +02:00 committed by GitHub
parent 09bc5c2e11
commit f7385a2d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -113,7 +113,7 @@ sensor:
type: map type: map
keys: keys:
state: state:
description: Defines a template to get the state of the sensor. description: "Defines a template to get the state of the sensor. If the sensor is numeric, i.e. it has a `state_class` or a `unit_of_measurement`, the state template must render to a number or to `none`. The state template must not render to a string, including `unknown` or `unavailable`. An `availability` template may be defined to suppress rendering of the state template."
required: true required: true
type: template type: template
unit_of_measurement: unit_of_measurement:
@ -460,6 +460,8 @@ template:
### State based sensor changing the unit of measurement of another sensor ### State based sensor changing the unit of measurement of another sensor
With a Template Sensor, it's easy to convert given values into others if the unit of measurement doesn't fit your needs. With a Template Sensor, it's easy to convert given values into others if the unit of measurement doesn't fit your needs.
Because the sensors do math on the source sensor's state and need to render to a numeric value, an availability template is used
to suppress rendering of the state template if the source sensor does not have a valid numeric state.
{% raw %} {% raw %}
@ -469,10 +471,12 @@ template:
- name: "Transmission Down Speed" - name: "Transmission Down Speed"
unit_of_measurement: "kB/s" unit_of_measurement: "kB/s"
state: "{{ states('sensor.transmission_down_speed')|float * 1024 }}" state: "{{ states('sensor.transmission_down_speed')|float * 1024 }}"
availability: "{{ is_number(states('sensor.transmission_down_speed')) }}"
- name: "Transmission Up Speed" - name: "Transmission Up Speed"
unit_of_measurement: "kB/s" unit_of_measurement: "kB/s"
state: "{{ states('sensor.transmission_up_speed')|float * 1024 }}" state: "{{ states('sensor.transmission_up_speed')|float * 1024 }}"
availability: "{{ is_number(states('sensor.transmission_up_speed')) }}"
``` ```
{% endraw %} {% endraw %}