Meters-to-miles value_template quit working

Over six months ago, my meters-to-miles value_template quit working. It was fine than all of the sudden it wasn’t. I’ve confirmed the sensor name, and the syntax of the value_template. What has changed?

- platform: template
    sensors:
      distance:
        friendly_name: Distance
        unit_of_measurement: mi
        value_template: >
          {{(states('sensor.russ_iphone_distance_2').replace ('m','')|float/1609.34) | round(2)}}

The state of the sensor?
What is it?

State is “4317” which is the current count in meters.

sensor.russ_iphone_distance_2
4317
unit_of_measurement: m
friendly_name: Russ’ iPhone Distance icon:
mdi:hiking

Is that a copy-paste error or is the formatting exactly as you have shown it?

According to the example shown in the documentation, platform and sensors should be vertically aligned.

- platform: template
  sensors:
    distance:
      friendly_name: Distance
      unit_of_measurement: mi
      value_template: >
        {{(states('sensor.russ_iphone_distance_2').replace ('m','')|float/1609.34) | round(2)}}

If you suspect the template may be at fault, simply paste it into the Template Editor and see what it reports.

{{(states('sensor.russ_iphone_distance_2').replace ('m','')|float/1609.34) | round(2)}}

"Is that a copy-paste error or is the formatting exactly as you have shown it?"

  • I don’t think so, I don’t get any errors when I save the config. Also the UI displays “4317”, so the template is just not working.

"paste it into the Template Editor and see what it reports."

  • the Template Editor reports correctly…2.6 miles.

Yes, sensors: and platform: are vertically aligned

Are you sure you are displaying the right sensor? :wink:

Yes. ‘sensor.russ_iphone_distance_2’ is my iphone and is changing distance as I walk.

It displays and distance changes fine in the Lovelace UI, but in meters rather than miles. If I paste the template code into the Template Editor, it displays the miles correctly.

You have to have sensor.distance displayed in your UI to have the value in miles.

I also gather step from my iphone and that works fine. Here what I have in the UI…this has always worked until a few months ago.

- type: glance
        title: Russ' Pedometer
        entities:
          - entity: sensor.russ_iphone_distance_2
            name: Distance
          - entity: sensor.russ_iphone_steps_2
            name: Steps

If you don’t believe me, just wait another 6.minths to see if it will fix itself :rofl:

Mea culpa. Using sensor.distance worked.
Thanks!

You don’t need the replace in the template, btw

So it was a copy-paste error; the real version is vertically aligned but the posted version lost some indentation in the copy-paste operation. Anything else different between the posted and real version?

Also, do you have any other Template Sensors and are they working correctly?

Then the template is fine but the Template Sensor’s configuration is suspect. What is the current state value of sensor.distance?