core icon indicating copy to clipboard operation
core copied to clipboard

"Default" Jinja filter not working in template

Open joshtbernstein opened this issue 2 years ago • 1 comments

The problem

The Jinja default filter isn't working when used in templates.

What version of Home Assistant Core has the issue?

2022.7.7

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

template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

{{ states('sensor.invalid_entityid') | default(0) }} #Returns "unknown"
{{ states('sensor.valid_entityid') | default(0) }} #Returns value of sensor
{{ states('sensor.invalid_entityid') | default(0) | int }} #Returns an error for int

Anything in the logs that might be useful for us?

When chaining filters {{ states('sensor.invalid_entityid') | default(0) | int }}:

ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.invalid_entityid') | default('0') | int }}' but no default was specified

Additional information

No response

joshtbernstein avatar Aug 09 '22 16:08 joshtbernstein

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

can confirm this

Mrten avatar Sep 08 '22 21:09 Mrten

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Dec 07 '22 22:12 github-actions[bot]

confirming for 2021.11.4, will upgrade to 2012.12.0 later

ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.invalid_entityid') | default(0) | int }}

Mrten avatar Dec 08 '22 08:12 Mrten

{{ states('sensor.invalid_entityid') | int(default=1) }} gives the expected result, though

Mrten avatar Dec 08 '22 08:12 Mrten

If sensor.invalid_entityid doesn't exist then the result of this:

states('sensor.invalid_entityid')

is unknown. The states() function fails to get the state value of the non-existent sensor so it reports unknown.

Piping unknown to default(0) won't produce 0 because default is meant for when the value is undefined. unknown is a defined value.

The last filter in your template is int but it lacks a default value (like int(0)). It can't convert unknown to an integer and, without a specified default value, it produces an error (namely the error you received).

The most concise way to report 0 if the entity doesn't exist is like this:

{{ states('sensor.invalid_entityid') | int(0) }}

Use the default filter only if there's a danger that the given variable is truly undefined. Examples:

{{ states.sensor.whatever.state | default(0) }}

{{ foo | default(0) }}

tdejneka avatar Jan 12 '23 18:01 tdejneka

Should be in the documentation somewhere, thanks for writing it out.

Mrten avatar Jan 12 '23 21:01 Mrten

It is documented somewhere.

default is a built-in filter in the Jinja2 templating language and is documented here:

https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.default

Home Assistant's documentation on templating explains that it employs Jinja2 and provides links to Jinja2's documentation. https://www.home-assistant.io/docs/configuration/templating/#building-templates

tdejneka avatar Jan 12 '23 23:01 tdejneka

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.