Float (default=0) buzz

In recent version of HA there is a lot of buzz because lot of warnings in system about int, float for entities when state is unknown

no default was specified. Currently 'int' will return '0', however, this template will fail to render in Home Assistant core 2022.1

I suppose it applies to the majority of systems. Are there somewhere official docs with rules, best practices expected?

If really there are no plans/possibility for improving that on the system level.

Jinja2 docs:


HA docs:

There are links from Developer Tools.

That is the improvement.

Previously you would not have known your template was rendering incorrectly.

But, regarding that links and rules

So shouldn’t be a problem.

So let the system warn me, not stop working.

But that clarifications in link “how to” are great. Thanks.

That’s the Jinja version of float. Float has been overridden in the HA environment. You need to provide a default. Please don’t link to that float anymore. Thanks.

Is there HA documentation for this? Best I could find is the Templating bit (Templating - Home Assistant) which links to the Jinja page…

The docs will be updated when it turns into an error. Currently this is the docs section for it and the current docs are correct but they do not mention a warning. In 2021.12 the docs will be updated to reflect the requirement for default. Also, seeing that you are decent at templates, you’ll realize that default isn’t 100% needed when using these filters. It’s only needed when the conversion might fail. I.e. this will never fail {{ '1' | float }} where {{ states('xyz.abc') | float }} may fail if the entity is unavailable or unknown. I’ve been just telling template novices to use it everywhere because they may not understand that difference.

1 Like

Isn’t that jinja version of filters linked in the HA docs?

And this stil, for me, remain the question - is it improvement? I believe that ts for some important purpose but… at least confusing.

It’s an improvement so you can now detect a failed conversion if you want. Before you couldn’t. This was put into place because one of the largest issues with energy and statistics is when sensors drop to zero instead of going unavailable. This is so that people can catch unavailable and properly project it to their template sensors, rest sensors, and any other sensors that use templates.

Ok, it is great to warn users. Really useful. I didn’t realize this threat for a long time. But a warning in docs and in the logs looks like a safer option than failing to reender.

Well, the decision is made to make it an error in 2021.12, so be prepared and remove all your warnings.