The above fails with an error message that ‘hellox’ is not found. This is a deviation from prior versions where the default of 0 is expected. We do expect a “not found” error from somejson.hellox but not from somejson['hellox'].
Only when the x[‘y’] construct is nested within an expression (such as with
a filter of | float(0)) does the error stop the template. And this is new behavior as this worked as expected with 2025.2.5
I am seriously not following. The problem is that the same exact construct works when not nested in larger arithmetic expression, but fails when in a larger expression. Its like the rules have changed for attribute lookup with 2025.3 with the latest jinja.
Thank you for your attention, trying to work through this as best I can, and seems to me like a change with latest jinja, just trying to point out and make some sense.
Yes, sorry for the syntax copy & paste error. My point about the difference of x[‘y’] alone vs. in a larger expression still stands. This works x['y'] but x['y'] | float(0) doesn’t. And used to with prior HA.
The first action errors, and the second doesn’t. This is a deviation from prior HA’s. The only difference between these two expressions is that the ['hellox'] expression is nested in a larger expression. Prior HA’s allowed both of these action expressions to work the same – without error.
The error that is issued is that ‘hellox’ is not found. This is incorrect behavior as this should not issue a not found error, but instead proceed to | float(0).
Of course, we expect an error from json.hellox, but not from json['hellox']. And why it is now allowed alone, but not within a larger expression is at issue.
What’s the error? Is it that the dict object has no attribute ‘hellox’?
What you have now is certainly valid Jinja syntax. There is a significant difference between the two cases.
The second expression, where you don’t use the float filter, evalutes to undefined, because your JSON dictionary only has a key named hello, and not hellox. But you don’t get an error, because Jinja can complete the evaluation and figure out that it’s undefined; it tells HA that the expression is undefined, and HA just treats the undefined value as an empty string.
The first expression, however, will give you
'dict object' has no attribute 'hellox'
That’s because Jinja can’t finish the evaluation. The Jinja engine needs to apply the float filter to something, but when it tries to get that something, it hits an undefined error.
You can eliminate that problem with the default filter, like this:
That’s the recommended way to handle the possibility that an expression being piped into a filter is undefined. You can verify this in the Template Editor. Try:
{{ ('{ \"hello\": \"there\" }' | from_json)['hellox'] is defined }}
You should get false.
I believe the default filter is a relatively recent addition. So depending on what version of Jinja you upgraded from, you might well be experiencing a difference in behavior–although I don’t know what Jinja would do in this scenario before addition of the default filter.
Right. Because it never gets to the float; it hits an error trying to get the value to pass to float–so that’s what it complains about, as explained in my post above.