Jinja behavior change? Accessing undefined attributes causes error

I am pretty sure that the following code

{% set m = { 'attribute': 'value' }%}
{{ 'yes' if m.attribute == 'value' else 'nope' }}
{{ 'yes' if m.not_an_attribute == 'value' else 'nope' }}

used to print

yes
nope

But since I updated to 2022.5 it seems to produce an error:

UndefinedError: 'dict object' has no attribute 'not_an_attribute'

Is this expected? Am I mistaken? What’s the cleanest way to achieve the desired result with the least amount of code? Is this the least verbose way to guard against undefined attributes?

{% set m = { 'attribute': 'value' }%}
{{ 'yes' if m.attribute == 'value' else 'nope' }}
{{ 'yes' if m.not_an_attribute is defined and m.not_an_attribute == 'value' else 'nope' }}

Thanks!

1 Like


on 2022.4.7 here

1 Like

You’re not. In the past this worked fine:


{{ states.binary_sensor 
  |selectattr('attributes.device_class', 'in', ['motion', 'presence', 'window', 'door', 'opening', 'vibration'])
  |selectattr('state', 'in', ['on', 'opening', 'closing'])
  |map(attribute='name') |join('\n') }}

Now I get


UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'device_class'

Did you raise an issue?

Ah, cool:

4 Likes