From f7385a2d31e1a576fb6370ffbaae8ff1073ee626 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 23 May 2023 15:08:24 +0200 Subject: [PATCH] Improve template sensor documentation (#27492) Co-authored-by: Franck Nijhof --- source/_integrations/template.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/_integrations/template.markdown b/source/_integrations/template.markdown index 9228e8ed5fc..5c929d280ea 100644 --- a/source/_integrations/template.markdown +++ b/source/_integrations/template.markdown @@ -113,7 +113,7 @@ sensor: type: map keys: 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 type: template unit_of_measurement: @@ -460,6 +460,8 @@ template: ### 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. +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 %} @@ -469,10 +471,12 @@ template: - name: "Transmission Down Speed" unit_of_measurement: "kB/s" state: "{{ states('sensor.transmission_down_speed')|float * 1024 }}" + availability: "{{ is_number(states('sensor.transmission_down_speed')) }}" - name: "Transmission Up Speed" unit_of_measurement: "kB/s" state: "{{ states('sensor.transmission_up_speed')|float * 1024 }}" + availability: "{{ is_number(states('sensor.transmission_up_speed')) }}" ``` {% endraw %}