Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja templates inconsistencies with None/null value #65913

Closed
energywave opened this issue Feb 6, 2022 · 1 comment
Closed

Jinja templates inconsistencies with None/null value #65913

energywave opened this issue Feb 6, 2022 · 1 comment
Labels

Comments

@energywave
Copy link

The problem

I've found several inconsistencies handling None/null value. I list the issues here and I demonstrate them later.

  1. None value, if alone in developers tools models, is shown as null
  2. None value, if alone in developers tools models, is shown as type dict
  3. default filter is not working with None/null value (the result is None/null)
  4. int and float filters are converting the value to 0 (is that right?) but if you provide a default value it will be used

I hope to get better consistency as when I develop complex scripts these things are a huge loss of time, often more than the script itself. I think that a complete check of special values and how they're handled by jinja templates would be great.

Here the text to use in Developers tools -> Models:

{# I set value with a not existant attribute at the moment. Same to assign it with None #}
{% set value = state_attr("media_center.google_mansarda", "volume_level") %}
Value: {{ value }}
Value is None: {{ value == None }}
Default value: {{ value | default(0.6) }}
As float: {{ value | float }}
As float with default: {{ value | float(0.6) }}
As int: {{ value | int }}
As int with default: {{ value | int(0.6) }}

Here is the result:
image

To demonstrate point 1 and 2 use this:

{# I set value with a not existant attribute at the moment. Same to assign it with None #}
{% set value = state_attr("media_center.google_mansarda", "volume_level") %}
{{ value }}

And here is the result:
image

What version of Home Assistant Core has the issue?

core-2021.12.10

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

I suspect there could be other inconsistencies that I've never came across other than that. I suggest a review of how that special values are handled, especially with default values so that we have a predictable result when scripting :)
That said, thank you for your work, I really LOVE it!
PS: I'm a programmer but Python is not in my skill set, otherwise I would have been happy to do it by myself.

@energywave
Copy link
Author

Don't close.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant