Template round(2) not working?

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?

You’re only rounding the 100. Wrap the equation in parenthesis and round that.

1 Like

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) }}"

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 :+1:

2 Likes

thanks all three. its working now!

FYI

Brackets → [ ]
Parentheses → ( )
Quotes → " " or ' '

1 Like

Is that a Python thing? …where I am from “round brackets” is the common description for ( ) and “square brackets” for [ ].