HACS / Tesla / How to change distance units?

I have successfully set up the HACS / Tesla integration, but the distance units are not as I would like, ie kilometres instead of miles.

My HA Settings / General / Unit System is set to “Celcius, kilograms, metric” as this is what I use for temperatures etc, but how can I force the Tesla integration to show range in miles etc? My car is set to miles, not km.

TIA

You can create a new sensor which do the conversion for you.

template:
  - sensor:
    - name: Tesla mileage
      state: "{{ float(states('sensor.xyz'),0) * 0.621371 }}"
      unique_id: tesla_mileage

But don’t define it as device_class: distance otherwise it will be shown in your HA system of unit I believe.

EDIT You can try to put an additional

      unit_of_measurement: mi

not sure if this will work thou

Thank you so much @Olivier1974 .

This code works, but the result has lots of decimal places (that I don’t want).

### Tesla HACS change from km to miles. 
  - sensor:
    - name: Tesla range
      unit_of_measurement: "mi"
      state: "{{ float(states( 'sensor.baloo_range' ),0) * 0.621371 | }}"
      unique_id: tesla_range

So I tried the following, but it didn’t work. What would be the best way to have no decimal places?

### Tesla HACS change from km to miles. 
  - sensor:
    - name: Tesla range
      unit_of_measurement: "mi"
      state: "{{ float(states( 'sensor.baloo_range' ),0) * 0.621371 | round(0) }}"
      unique_id: tesla_range

Neither did this…

      state: "{{ float(states( 'sensor.baloo_range' ),0) * 0.621371 | int }}"

TIA

Common mistake in Jinja, the order of operations :slight_smile:

for 2 decimals, giving 191.43
{{ int(float(states( 'sensor.baloo_range' ),0) * 62.1371) / 100 }}

for no decimal, giving 191
{{ int(float(states( 'sensor.baloo_range' ),0) * 0.621371) }}

EDIT you can add the icon as well,

### Tesla HACS change from km to miles. 
  - sensor:
    - name: Tesla range
      unit_of_measurement: "mi"
      state: "{{ int(float(states( 'sensor.baloo_range' ),0) * 62.1371) / 100  }}"
      unique_id: tesla_range
      icon: mdi:gauge
1 Like