Convert km to Miles template

I’ve tested this in the Templates Tab of the developer section

sensor:
  - platform: template
    sensors:
      mazda_miles_range:
      friendly_name: "Mazda Miles Range"
      value_template: "{{ ((float(states('sensor.mazda_cx_60_remaining_range')) / 1.6) | round(0)) }}"
      unit_of_measure: "Mi"

I get the following string result:

sensor:
  - platform: template
    sensors:
      mazda_miles_range:
      friendly_name: "Mazda Miles Range"
      value_template: "221"
      unit_of_measure: "Mi"

I added this to the end of my configuration.yaml:

sensor:
  - platform: template
    sensors:
      mazda_miles_range:
      friendly_name: "Mazda Miles Range"
      value_template: "{{ ((float(states('sensor.mazda_cx_60_remaining_range')) / 1.6) | round(0)) }}"
      unit_of_measure: "Mi"

#  debug: False #(optional)

However, when I check my Configuration in the Developer tools the I just get the spinning circle.

Any suggestion as to what I’m doing wrong.

I intend to use the sensor.mazda_miles_range in a gauge tile.

1 Like

Did you reload you templates in dev tools ? are there any errors in the log ?

When I look at the options to reload in Dev Tools I don’t see the option for templates at all.

Do I have “install” templates

Maybe its because this is your first one ?

Anything in the log ?

Have you only got one sensor: entry in your config.yaml ?

Nothing seems to get added to the logs (assuming Settings->System->Logs->View Full Logs - Just go an ongoing spinning circle when checking configuration.

I have only 1 sensor entry in my configuration.yaml

whole file:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  external_url: https://xx.xx.xx

http:
  use_x_forwarded_for: true
  trusted_proxies:
  - 127.0.0.1
  - 172.0.0.0/8
  ip_ban_enabled: false

input_boolean:
  house_empty:
    name: "House Empty"
  frost_protection_active:
    name: "Frost Protection Active"
  vacation_mode:
    name: "Vacation Mode"
  is_evening:
    name: "Is Evening"

sensor: 
  - platform: template 
    sensors: 
      mazda_miles_range: 
      friendly_name: "Mazda Miles Range" 
      value_template: "{{ ((float(states('sensor.mazda_cx_60_remaining_range')) / 1.60934) | round(0)) }}" 
      unit_of_measure: "Mi" 

#  debug: False #(optional)

Does it pass the test without the sensor in ?

If I comment out all the lines, yes it passes without problem.

I’ve got something in the log which could possibly relevant…

2022-11-25 12:24:31.142 ERROR (MainThread) [aiohttp.server] Error handling request                                       
Traceback (most recent call last):                                                                                       
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request                   
    resp = await request_handler(request)                                                                                
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle                                
    resp = await handler(request)                                                                                        
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl                           
    return await handler(request)                                                                                        
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware 
    return await handler(request)                                                                                        
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware            
    return await handler(request)                                                                                        
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware 
    return await handler(request)                                                                                        
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware                      
    return await handler(request)                                                                                        
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 136, in handle                               
    result = await result                                                                                                
  File "/usr/src/homeassistant/homeassistant/components/config/core.py", line 32, in post                                
    errors = await async_check_ha_config_file(request.app["hass"])                                                       
  File "/usr/src/homeassistant/homeassistant/config.py", line 916, in async_check_ha_config_file                         
    res = await check_config.async_check_ha_config_file(hass)                                                            
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 233, in async_check_ha_config_file           
    p_validated = platform_schema(p_validated)                                                                           
  File "/usr/local/lib/python3.10/site-packages/voluptuous/validators.py", line 232, in __call__                         
    return self._exec((Schema(val) for val in self.validators), v)                                                       
  File "/usr/local/lib/python3.10/site-packages/voluptuous/validators.py", line 351, in _exec                            
    v = func(v)                                                                                                          
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__                     
    return self._compiled([], data)                                                                                      
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable            
    return schema(data)                                                                                                  
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__                     
    return self._compiled([], data)                                                                                      
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict                
    return base_validate(path, iteritems(data), out)                                                                     
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 387, in validate_mapping             
    cval = cvalue(key_path, value)                                                                                       
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable            
    return schema(data)                                                                                                  
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 541, in verify                          
    return cast(dict, schema(value))                                                                                     
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__                     
    return self._compiled([], data)                                                                                      
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict                
    return base_validate(path, iteritems(data), out)                                                                     
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 387, in validate_mapping             
    cval = cvalue(key_path, value)                                                                                       
  File "/usr/local/lib/python3.10/site-packages/voluptuous/validators.py", line 229, in _run                             
    return self._exec(self._compiled, value, path)                                                                       
  File "/usr/local/lib/python3.10/site-packages/voluptuous/validators.py", line 353, in _exec                            
    v = func(path, v)                                                                                                    
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable            
    return schema(data)                                                                                                  
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 808, in validator                       
    if key in config:                                                                                                    
TypeError: argument of type 'NoneType' is not iterable                

should be unit_of_measurement

Also, thats the legacy template format, this is the newer one:

template:
  - sensor:
    - name: "Mazda Miles Range"
      state: "{{ states('sensor.mazda_cx_60_remaining_range') | float(0) / 1.60934 | round(0) }}"
      unit_of_measurement: "Mi"

Thanks - That seems to have solved it.

I must have been following an old recipe.

Final code in configuration.yaml

I think I need the unique ID for using this in other areas.

template:
  - sensor:           
    - name: "Mazda Miles Range"     
      unique_id: 'mazda_miles_range'                                                             
      state: "{{ ((float(states('sensor.mazda_cx_60_remaining_range')) / 1.60934) | round(0)) }}"         
      unit_of_measure: "Mi"      
1 Like

Needs to be unit_of_measurement

Just looking at this as I needed the same thing.

I see an odd result

input value 242 from sensor

if I round (0) I get 121
if I round (1) I get 151.25

I came across this page and saw the last comment, be careful not to round the 1.6!

{{ (states('sensor.my_vehicle_range_kms') | float / 1.60934) | round(0) }}