How to convert km/h to mph?

I’m trying to convert the “velocity” attribute from owntracks which seems to be in km/h to mph.
I have searched a bunch, but I am not a programmer so I’m a bit lost on the correct way to do this.
I’m running 0.113.2 supervised install if it matters.

I have this now:

- platform: template
    sensors:
      mom_speed:
        friendly_name: "Mom Speed"
        unit_of_measurement: 'mph'
        value_template: "{{ state_attr('device_tracker.mom', 'velocity') | float / 1.609 | round(0) }}"
        icon_template: mdi-speedometer``

and what this does is divide by 2.
So if we are going 100 km/h it says 50 mph when it should be 62 mph. 101 then 50.5 which I thought the round(0) would round to a whole number.
Should it be and int rather than float? I went with float because examples all seem to use float.

I looked at the templating example and it showed:

{{ (states('sensor.temperature') | float * 10) | round(2) }}

…however if I leave that “)” behind the 10 then the configuration check fails. Passes without it. So either the docs need to be updated or I messed up somewhere else.

So anybody with a grip on how this all works have any suggestions for me?

Thanks

Dan,

You just need some more parentheses. Try this, it worked for me when playing in the Template tab of Developer Tools:
{{ (state_attr('device_tracker.mom', 'velocity') | float / 1.609) | round(0) }}

-Kevin

1 Like

Just add some brackets around the state and the float division.

"{{ (state_attr('device_tracker.mom', 'velocity') | float / 1.609) | round(0) }}"

i.e. What Kevin said…

1 Like

Thanks for responding guys…problem is that adding this “)” is when it fails the config check on 0.113.2.
I wonder what is going on there. I’ll have to go reread the release notes.
Maybe I’ll just have to try it and see if it goes into safe mode.

I’m running same version, this works for me, I just used the values from a tp-link power monitoring switch as a data source.

- platform: template
  sensors:
    test:
      friendly_name: 'Tester'
      value_template: >
         {{ (state_attr('switch.dishwasher', 'voltage') | float / 1.609) | round(0) }}

You added the opening ‘(‘ … as well … before state_attr ?

Hehehe…no. I missed it.
It works now.
So to try and understand with my non programmer mind here… is this saying that it will take the attribute of the entity as a float value then divide by 1.609 and then after that calculation is done take the result of that and round it? Was this all an order of operations thing? I just don’t get why it was dividing by 2 before.

Either way thanks to you guys for helping me out!

1 Like

Yes that was the problem. You were only rounding 1.609 before you added the parentheses.

Parentheses, then
Filters, then
Exponents, then
Multiplication and division, left to right, then
Addition and subtraction, left to right

2 Likes

You need to add a ( and a )

Thanks for the explanation…I’ll never be a programmer in my day job, but I do try to learn. Stuff like this helps.