State "unknown", but is_state checks out False

Hey guys! I’m testing out the following bit of code in Developer Tools > Template and I’m a bit amazed by the results

{{ states('sensor.home_realfeel_temperature') }}
{{ is_state('sensor.home_realfeel_temperature', 'unknown') }}

yields

unknown
False

How can that be? Shouldn’t is_state return True in that case?

think of a state as ON/OFF which is true/false

as ‘unknown’ is nothing so it false (off)

I don’t get it.

States can be more than just binary values, AFAIK, but even so… If the state is literally 'unknown' (as demonstrated by dumping it with the states() above), shouldn’t testing for it with is_state() return True?

LE: this is getting even weirder

{{ states('sensor.home_realfeel_temperature') == 'unknown' }}
{{ is_state('sensor.home_realfeel_temperature', 'unknown') }}

results in

True
False
1 Like

A couple things, what’s the expected data format for sensor.home_realfeal_temperature? If it’s anything but a string, then your is_state will return False because you can’t compare a value and a string.

Other thing I can think of and I have encountered this when restarting HA is the frontend and using states() will return unknown and visually it will be displayed as unknown but in reality HA has no clue what the sensor’s value is so it can’t even compare against unknown. I work in Industrial Automation and frequently see issues around compare statements of numeric values and when a sensor or register overflows and therefore is unknown, I get “QNaN is not a number!” errors. I can’t compare against QNaN.

Thank you! I understand and I have considered this, I’ve tried unquoting unknown, but my Python/Jinja skills suck, so I didn’t really understand what was going on behind the scenes.

Let me rephrase my question then…

How can I check to see whether a certain entity exists?

I want to {% set %} a certain value to another variable depending on whether a certain integration exists and, in turn, whether the sensor.home_realfeel_temperature entity is available and has a state value or not.

3 Likes

2 Likes

Thank you, sir! The answer seems to have been as easy as your nickname :smiley:

1 Like

you can also use the following to check if the entity exists:

{{ states.sensor.something.state is defined }}