From a47f6894f0bd4fed803e4dbbf778513e00bd3dd0 Mon Sep 17 00:00:00 2001 From: ehendrix23 Date: Tue, 28 Mar 2023 09:05:03 -0600 Subject: [PATCH] New function/test/filter has_value for templating (#24360) Co-authored-by: Thomas Dietrich --- source/_docs/configuration/templating.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index da59ee7e351..b3705412d51 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -119,6 +119,7 @@ Not supported in [limited templates](#limited-templates). - `is_state` compares an entity's state with a specified state or list of states and returns `True` or `False`. `is_state('device_tracker.paulus', 'home')` will test if the given entity is the specified state. `is_state('device_tracker.paulus', ['home', 'work'])` will test if the given entity is any of the states in the list. - `state_attr('device_tracker.paulus', 'battery')` will return the value of the attribute or None if it doesn't exist. - `is_state_attr('device_tracker.paulus', 'battery', 40)` will test if the given entity attribute is the specified state (in this case, a numeric value). Note that the attribute can be `None` and you want to check if it is `None`, you need to use `state_attr('sensor.my_sensor', 'attr') is none` or `state_attr('sensor.my_sensor', 'attr') == None` (note the difference in the capitalization of none in both versions). +- `has_value('sensor.my_sensor')` will test if the given entity is not unknown or unavailable. Can be used as a filter or a test.
@@ -175,6 +176,10 @@ Other state examples: {% if states('sensor.train_departure_time') in ("unavailable", "unknown") %} {{ ... }} +{% if has_value('sensor.train_departure_time') %} + {{ ... }} + + {% set state = states('sensor.temperature') %}{{ state | float + 1 if is_number(state) else "invalid temperature" }} {% set state = states('sensor.temperature') %}{{ (state | float * 10) | round(2) if is_number(state)}}