Jinja math bug?

Hi!

Is this a bug in HA?

Try this in the template editor:

{{6 * 0.1|round(1)}}

It evaluates to 0.6000000000000001. Why not 0.6?

And without the round function:

{{6 * 0.1}}

It evaluates to 0.6000000000000001. Why not 0.6?

BR/Nicklas

Order of operations. The |round filter evaluates first on 0.1

Try:

{{ (6 * 0.1) | round(1) }}

The other oddness you are seeing is probably due to the finite accuracy limitation of floating point math in binary computation.

1 Like

Ahhh, thanks! :smiley:

Priority of operations:

Brackets then Filters

Then the same as for python

Great!

Do you also know why this does not work?

{{(states('sensor.out') * 0.1 + 1)|round(1)}}

Get: Unknown error rendering template

{{(states('sensor.out')  |float * 0.1 + 1)|round(1)}}

You can also use | int if the sensor contains an integer (as a string) and you are a RAM miser :slight_smile:

All states are stored as strings and need to be converted if performing math functions.

Ah, forgot that states(‘sensor.out’) is a string.

Thanks!

1 Like