Issue with templating. KPH to MPH

Hi all. I am trying to convert an odometer sensor from KPH to MPH however I am getting a wierd output.

This is my template:

{{ states('sensor.smart_odometer')| float / 1.609344 | round(0)  }}

sensor.smart currently shows 54263 as it’s value. Running it through the above sensor without the round on the end gives the correct value of 33717.46500437445 as it’s result. However, when I add the round(0) to the end I get 27131.5. This is neither rounded to 0 decimal places, nor is it the correct value and I cannot quite work out why.

Any ideas?

It happens because the 1.069344 is being rounded into 2 which gives you 27131.5.

Try adding a bracket into the formula like this-
{{ (states('sensor.smart_odometer') | float / 1.609344) | round(0) }}

2 Likes

Also, please keep in ind you have a nice “Template” area in the “Developer tools” where you can test your templates.

Open your Home Assistant instance and show your template developer tools.

Correct it’s the old math challenge called “order of precedent”

In templating the | always gets evaluated first. Then it follows the standard math rules.

1 Like

Damn. I was treating it like a pipe in bash and assumed the output of the first sum would be passed to the round(). Thanks all for your help.