naamah75
(naamah75)
April 24, 2020, 3:37pm
1
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?
petro
(Petro)
April 24, 2020, 3:38pm
2
youre only rounding the 0.001
{{ (states('sensor.template_pzem_004t') | float * 0.001) | round(4) }}
2 Likes
Mutt
(Muttley)
April 24, 2020, 3:45pm
3
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)) }}”
petro
(Petro)
April 24, 2020, 3:47pm
4
That’s not a problem with the round function, the round function just makes it apparent. All numerical values have epsilon error.
Mutt
(Muttley)
April 24, 2020, 3:52pm
5
True, but I thought I thought I’d go with the easy answer and fix.
My bad
naamah75
(naamah75)
April 24, 2020, 5:55pm
6
Yes! just add a bit of math and all works fine!