Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 103188

Jinja behavior change? Accessing undefined attributes causes error

$
0
0

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!

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 103188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>