How can I test for the absence of an attribute?

This; {{ state_attr('media_player.XXXX', 'entity_picture') }}, returns None.

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. :sweat:

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?

Thanks in advance.

{{ state_attr('media_player.XXXX', 'entity_picture') == none }} should do it.

6 Likes

Yep, that does the trick. Thanks, tom_l!

Out of curiosity, do you happen to know why this (your solution) works:
{{ state_attr('media_player.XXXX', 'entity_picture') == None }}

…but this doesn’t?
{{ is_state_attr('media_player.XXXX', 'entity_picture', None ) }}

:thinking:

No I don’t. I just tried a few options in the template editor :man_shrugging:

1 Like

:grin:

Me too, but I missed that one. :confused:

Thank again for the help. :slightly_smiling_face:

It would be nice to know why.

@123 or @pnbruckner or @petro might know.

1 Like

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
5 Likes

Wow, that’s a definitive answer – right down to pinpointing the source code! (I’m assuming)

The depth of Petro’s knowledge never ceases to amaze me. :sunglasses:

1 Like

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.

  1. The is_state_attr function calls another function state_attr.
  2. state_attr returns the entity’s value or, if the entity doesn’t exist, it returns None.
  3. is_state_attr always reports a received None value as False.
  4. 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.

.

4 Likes

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. :sweat_smile:

Thanks for the additional details. :sunglasses: