1mfaasj
(Bowa)
January 13, 2024, 1:49pm
1
I dont get it. I can’t get it gone to convert the result of the template into an amount with 2 digits after the comma
the template:
"{{ ( states('sensor.scrape')|float(0) - states('input_number.mynumber')|float(0) ) / states('input_number.mynumber')|float(0) * 100 | round(2) }}"
I thought the round(2)
was meant to short numbers?
sensor.scrape gives this back:
108,32 €
and input_number.mynumber:
96.37 €
the result is:
"12.400124520078851"
But I was hoping for "12.40"
Even if I try to add round(2)
after each sensor it gives the same result.
Can anyone help me?
petro
(Petro)
January 13, 2024, 1:50pm
2
You’re only rounding the 100. Wrap the equation in parenthesis and round that.
1 Like
1mfaasj
(Bowa)
January 13, 2024, 1:54pm
3
Hi Petro what do you mean? where to put he “”?
Even if I put the round after the sensors, it gives the same result. for instance
"{{ ( states('sensor.scrape')|float(0) | round(2) - states('input_number.mynumber')|float(0) | round(2) ) / states('input_number.mynumber')|float(0) * 100 }}"
or
"{{ ( states('sensor.scrape')|float(0) | round(2) - states('input_number.mynumber')|float(0) ) / states('input_number.mynumber')|float(0) * 100 }}"
"{{ ((states('sensor.scrape')|float(0) - states('input_number.mynumber')|float(0) ) / states('input_number.mynumber')|float(0) * 100) | round(2) }}"
jchh
((not John))
January 13, 2024, 2:06pm
5
Parenthesis means brackets (not quotes).
Try this:
"{{ ( (states('sensor.scrape')|float(0) - states('input_number.mynumber')|float(0) ) / states('input_number.mynumber')|float(0) * 100) | round(2) }}"
Edit: @mobile.andrew.jones beat me to it!
2 Likes
At least we both came up with the same answer, which adds confirmation that it’s the right way to achieve it
2 Likes
1mfaasj
(Bowa)
January 13, 2024, 2:31pm
7
thanks all three. its working now!
petro
(Petro)
January 13, 2024, 4:34pm
8
FYI
Brackets → [ ]
Parentheses → ( )
Quotes → " "
or ' '
1 Like
jchh
((not John))
January 13, 2024, 5:32pm
9
Is that a Python thing? …where I am from “round brackets” is the common description for ( )
and “square brackets” for [ ]
.