Strange problem with templating in an automation

I have an automation that shows something on a display if it’s music. There are a few words hidden in the attributes to catch and stop the automation, but they do something weird…

This results in that in the template viewer:

{{ is_state_attr('media_player.woonkamer_atv','media_artist', 'None') }}
{{ state_attr('media_player.woonkamer_atv','media_title') }}
{{ state_attr('media_player.woonkamer_atv','media_artist') }}
False
NOS Journaal
None

So the state is ‘None’, but it does result in ‘False’. If I change it to not is_state it becomes obviously ‘True’. But then the automation continues.

What am I missing?

The first template checks if the value of media_artist is equal to the string 'None'. It’s not so the result is boolean False.

The third template reports the value of media_artist. It has no value so a null value is reported as None which is not the same as string 'None'

Try this version:

{{ is_state_attr('media_player.woonkamer_atv','media_artist', none) }}

Read the documentation, it explicitly mentions that is_state and is_state_attr does not work for comparisons against none.

  • is_state_attr('device_tracker.paulus', 'battery', 40) will test if the given entity attribute is the specified state (in this case, a numeric value). Note that the attribute can be None and you want to check if it is None, you need to use state_attr('sensor.my_sensor', 'attr') is none or state_attr('sensor.my_sensor', 'attr') == None (note the difference in the capitalization of none in both versions).

Thanks. I read over that part I guess. I have to say that it’s weird that null is also none (None and none apparently; and printed!) but is also ~.

you need to use state_attr('sensor.my_sensor', 'attr') is none or state_attr('sensor.my_sensor', 'attr') == None (note the difference in the capitalization of none in both versions).
I note the difference in capitalisation, but what does it mean? Is there really a difference?

You need different capitalization depending on whether your comparison operator is is or ==. Why? Can’t say I’m technical enough to understand or explain that, but I think it has something to do with which “layer” does the comparison. Capitalized None is Python while lowercase none happens in Jinja? Or something like that…