Regarding checking a state of sensor.
I have seen in many examples (including here - https://www.home-assistant.io/cookbook/#example-configurationyaml, https://github.com/Petro31/home-assistant-config/blob/62fddff4247b7c3c81d33b00a3b5048174a70c6e/sensor.yaml) something like:
value_template: "{% if states('sensor.source_sensor') -%}
{{ states('sensor.source_sensor') }}
{%- else -%}
{{ states('sensor.destination_sensor') }}
{%- endif %}"
The point was to use a source sensor’s value only if this sensor is meaningful (ready, actually updated etc).
As I understood the 1st condition is true if a value of “states(‘sensor.source_sensor’)” is NOT zero - at least it is in C/C++ and I proposed that same logic is in Perl, Jinjia2 etc. Later I found that I was wrong.
Asked myself - what if the source sensor’s value MAY BE zero (i.e. it is a normal behaviour).
Then I started looking for more precise methods.
Also, in many other places here in forum I’ve seen smth like:
value_template: "{% if not states('sensor.source_sensor') in ['unavailable','unknown','none'] -%}
{{ states('sensor.source_sensor') }}
{%- else -%}
{{ states('sensor.destination_sensor') }}
{%- endif %}"
Note: initially by mistake in this post it was specified unavialable
instead of unavailable
, my thanks to this - Template sensor calculations when values don't exist.
To check if it works used a “Template” feature in HA.
Specified this:
{% if states('sensor.test_zero_value') in ['unavailable','unknown','none'] -%}
first = {{ states('sensor.test_zero_value') }}
{%- else -%}
second = {{ states('sensor.test_zero_value') }}
{%- endif %}
%%%%%%%%%%%%%
{% if not states('sensor.test_zero_value') -%}
first = {{ states('sensor.test_zero_value') }}
{%- else -%}
second = {{ states('sensor.test_zero_value') }}
{%- endif %}
Then changed a value of sensor.test_zero_sensor to 0, unavailable, unknown, none.
Here are the output results:
second = 0
%%%%%%%%%%%%%
second = 0
first = unavailable
%%%%%%%%%%%%%
second = unavailable
first = unknown
%%%%%%%%%%%%%
second = unknown
first = none
%%%%%%%%%%%%%
second = none
So, the check in ['unavailable','unknown','none']
works fine.
But the check for “zero” failed.
Found here (https://jinja.palletsprojects.com/en/master/templates/#if) a possible answer:
you can use it to test if a variable is defined, not empty and not false.
So it is not about “= 0”.
Does that mean that these "is defined, not empty and not false"
are covered by "if states(..)"
?
According to my test the answer is NO - look at the output results, they are always “second = …” for the 2nd check.
So why people used this "if states(..)"
method?
Moreover, why " unavailable','unknown','none
" ?
Can anyone give me a link to a document/code explaining possible states and their meaning?
What I think is (from my experience):
- Unavailable - the sensor is created but its state is not determined.
Example: the state of thermometer device which cannot be polled (the device is switched off, connection lost, …).
- Unknown - the sensor does not exist.
Example: the sensor with RAM usage of remote PC retrieved by Netdata integration - since that PC was not available during HA startup, so the sensor was not created at HA startup.
- None - do not know any possible reasons.