Problem with round()

I’m facing with a strange behaviour using round() in template… using the following code

{{ states('sensor.template_pzem_004t') | float * 0.001 }}
{{ states('sensor.template_pzem_004t') | float * 0.001 | round(4) }}
{{ states('sensor.template_pzem_004t') | float * 0.001 | round(3) }}
{{ states('sensor.template_pzem_004t') | float * 0.001 | round(2) }}
{{ states('sensor.template_pzem_004t') | float * 0.001 | round(1) }}
{{ states('sensor.template_pzem_004t') | float * 0.001 | round(0) }}

I get the following results:

0.33727
0.33727
0.33727
0.0
0.0
0.0

… where’s the problem?

youre only rounding the 0.001

{{ (states('sensor.template_pzem_004t') | float * 0.001) | round(4) }}
2 Likes

There is also a problem with the python round() function (it doesn’t always return what you expect, I had the same sensor (copied) on two ha instances one displayed correctly the other with an extra two decimals. Try a text format instead if you have the same : -

“{{ ‘%.3f’ | format(distance(states.device_tracker.life360_bob)) }}”

That’s not a problem with the round function, the round function just makes it apparent. All numerical values have epsilon error.

True, but I thought I thought I’d go with the easy answer and fix.
My bad :rofl:

Yes! just add a bit of math :wink: and all works fine!