Two templates (with similar expressions) are giving different precisions

I've got two different templates for monitoring my EV mileage (one is "per kWh" and one is "per USD" because my electrical rate varies throughout the day and based upon whether I charge at home). The templates have almost the same code for calculating them, but one gives me 2 decimal places and the other gives me zero decimal places:

First, the YAML code for these two template values:

        - name: "Tesla White Dollar Mileage Lifetime"
          default_entity_id: sensor.tesla_white_dollar_mileage_lifetime
          unique_id: d67022d3-2bc0-4154-8a3c-511dfb39e73f
          unit_of_measurement: "mi/USD"
          device_class: energy_distance
          state: >           
            {% set cost = states('input_number.tesla_white_electricity_cost_lifetime') | float(2) %}
            {% set odo = states('sensor.tesla_white_ev_odometer') | float(0) %}
            {{ (0 if cost < 8 else odo / cost) | float | round(2) }}
                             
        - name: "Tesla White Power Mileage Lifetime"
          default_entity_id: sensor.tesla_white_power_mileage_lifetime
          unique_id: df5ea070-eed5-4367-a012-8ca8ba9278ed
          unit_of_measurement: "mi/kWh"
          device_class: energy_distance
          state: >           
            {% set energy = states('input_number.tesla_white_energy_delivered_lifetime') | float(2) %}
            {% set odo = states('sensor.tesla_white_ev_odometer') | float(0) %}
            {{ (0 if energy < 40 else odo / energy) | float | round(2) }}

Now, I realize that I've probably got a superfluous 'float' in the final expression, but it's in both of those templates. However, if I look at the values reported by these two, I get two decimal places for dollar-mileage and zero decimal places for power-mileage (which I realize should be "energy-mileage"). This certainly looks like a display issue, since you can see that the history graph shows higher resolution than just round integers. However, what's baffling is that, in the "Display precision" for each of these, they both say "Default", but then they give different example precisions.

The only real thing I can see that's different between these two templates is the units ("mi/kWh" and "mi/USD"). Both of these templates are of type "energy_distance", so I wouldn't be surprised if it didn't really understand the "mi/USD" one... but then why is that the one where it gets the precision correct?




The second sensor dose not have a valid unit for the device class. The valid units for each device class are listed here: Sensor - Home Assistant

Now, I'm even more confused. The linked page lists the valid units for energy_distance as:

energy_distance : Energy per distance in kWh/100km, Wh/km, mi/kWh, or km/kWh

"mi/kWh" is listed, yet that's the one that is misbehaving. However, just in the interest in conformance, I changed the units on my dollar-mileage to just device_class:distance, unit_of_measurement:mi, and I still get two decimal places for dollar-mileage and energy-mileage is still displaying zero decimal places.

(Side note: Is there a reason Wh/km and kWh/100km are supported units, but Wh/mi and kWh/100mi are not?)

Your second sensor is:

which is your first configuration:

which does not have correct unit of measurement mi/USD

And as a side note, the argument provided to the float filter is the default value to use when there is an error. It is not the number of decimal places; to get that you need to use the round filter. Round will do both: convert to float and round.

For example:
{{'unknown' | float(2)}} will output 2
{{'1.234' | float(2)}} will output 1.234
{{'unknown' | float(2)}} will output 2
{{'unknown' | round(2)}} will output an error