WTH is there no good way to test for an attribute that is `None`

Currently it is complicated to distinguish between whether an attribute doesn’t exist or it does exist but is equal to None.

Docs suggeset that testing for None should be done by either of these two methods:

state_attr('sensor.my_sensor', 'attr') is none
state_attr('sensor.my_sensor', 'attr') == None

But those will both also yield True if the attribute is not defined.

is_state_attr('sensor.my_sensor', 'attr', None) will return False in every scenario, so is not helpful.

The only viable method is to obtain the state object, and then these two options are possible:

states.sensor.my_sensor.attributes.attr == None
states.sensor.my_sensor.attributes.attr is defined and state_attr(my_sensor, 'attr') == None

But both require obtaining the state object first.

WTH isn’t there a test, filter, or function we can use on an entity_id?

Not an expert but I think you should use is_state_attr

1 Like

is_state_attr has some odd logic that makes it impossible to check an attribute for None, I’ll be putting in a PR later today to address this WTH using is_state_attr

2 Likes
5 Likes

Bery cool!

The PR just got approved, I’m assuming it will be merged sometime soon.

4 Likes