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?




