Value template returns wrong results

Why does value template give me wrong results in the code below and how to solve this.
You can verify the results below in the template editor.

{%set value = 20.1%}
{{ value + 0.7 | round(1)}}

returns: 21.9 this is correct

{%set value = 20.1%}
{{ value + 0.8 | round(1)}}

returns: 20.900000000000002 which is wrong why? What am I missing?

It even gets worse:

{% set value = 20.1%}
{{ value + 0.8 | float | round }}

returns: 21.1

Is it a bug? Home Assistant 77.3

It’s not wrong, its called epsilon error. This is a byproduct of how computers work when rounding. All coding languages do this on every computer everywhere. It’s just that you normally don’t see this kind of thing because programmers hide it behind the UI.

If you are really worried about numbers beyond the 10th decimal place.

{%set value = 20.1%}
{% set value2 = value + 0.8 | round(1)%}
{{ "{:f}".format(value2) | float }}


Mainly what @petro said. :slight_smile: But for this particular expression, it’s correct. The float and round filters apply to 0.8. So, 0.8 converted to float is still 0.8, and then round with no parameters means round to nearest and change to int, so it becomes 1. Which, when added to 20.1 results in 21.1.

I think maybe you meant to do this instead:

{% set value = 20.1%}
{{ (value + 0.8) | float | round }}

Which results in 21. (BTW, the float filter is unnecessary here, since value and 0.8 are both already floats.)