But this; {{ is_state_attr('media_player.XXXX', 'entity_picture', None ) }}, returns False.
I want to create a Template Binary Sensor that I can use as a condition for displaying the entity_picture and I can’t feasibly test for the presence of the attribute because it’s an arbitrary string (URL). And since None is returned I also can’t test for defined (eg: {% if state_attr('media_player.XXXX', 'entity_picture') is defined ... %}). I feel like I’m missing something simple here but have been racking my brain with no luck.
How can I get a binary result for the existence of an arbitrarily defined attribute?
Or is there a better method for creating this condition that I’m overlooking?
It’s because of the way that the is_state_attr function works. It checks to see if the attribute != None and if the attribute == value_passed_in. So ultimately, the second half passes, but the first half doesn’t. Resulting in a False response.
def is_state_attr(hass, entity_id, name, value):
"""Test if a state's attribute is a specific value."""
attr = state_attr(hass, entity_id, name)
return attr is not None and attr == value
Your question gave me a sense of déjà vu. Someone else asked the same question a month ago and I replied with the same explanation.
If you’re interested, that thread contains a deeper discussion of the issue. The issue is that None has a special meaning for these two functions.
The is_state_attr function calls another function state_attr.
state_attr returns the entity’s value or, if the entity doesn’t exist, it returns None.
is_state_attr always reports a received None value as False.
However, in this particular case, the entity does exist but its value is None so is_state_attr correctly returns None but state_attr, which is hard-coded to understand None means the entity doesn’t exist, incorrectly reports False.
Ah, yeah great info. He got further than I did in initially dissecting the issue.
The fact that None was returned with a capital “N” was a dead give-away that the result was being ‘pre-interpreted’, which was consistent with is defined always returning true, but I couldn’t figure a way around that (I could’ve sworn I’d already tried == None unsuccessfully).
I don’t think I’ve ever tried is_state_attr (or is_state) before, and after running into this and rethinking my approach several times I now can’t recall what led me to use it in the first place.