Value_Template - Help for a beginner to display attribute values that are converted into UK format

Hi, I am trying to display some data from a sensor I have (BMW) in my panels, I want to display some attribute values in the panel, as well as convert some of them into UK miles.

I have tried to follow the various bits of information to no luck, I someone could point me in the right direction I would really appreciate it.

The sensor name is:
binary_sensor.420d_condition_based_services

It has a state of:
on or off

The attributes are:
brake fluid date: 2020-07-01
brake fluid status: OK
car: 420d
oil date: 2019-07-01
oil distance: 6000 km
oil status: OK
vehicle check date: 2021-07-01
vehicle check distance: 37000 km
vehicle check status: OK
vehicle tuv date: 2020-07-01
vehicle tuv status: OK
friendly_name: Service Status Data
device_class: problem
icon: mdi:door

I want to display the attribute oil distance for example, but in miles (e.g value in KM divided by 1.609, then round to just whole number such as 4566)

If someone could point me in the right direction I am struggling to work out how to add this into my configuration.yaml file and what dependencies it has etc.

1 Like

Maybe:

sensor:
  - platform: template
    sensors:
      oil_distance:
        friendly_name: Oil distance
        unit_of_measurement: mi
        value_template: >
          {{ state_attr('binary_sensor.420d_condition_based_services', 'oil_distance')|float/1.609|round }}

Hi thanks for that, it certainly moves me along a bit more. It shows as 0.0 in the sensor, but I think thats because the value of the attribute is 6000 km.

The km bit is going to stop it from being divided (I proved this by removing the maths part and it displays 6000km).

Any ideas how I can just retrieve the numerical value of that attribute?

{{ (state_attr('binary_sensor.420d_condition_based_services', 'oil_distance').replace('km','')|float/1.609)|round }}

I also forget a set of parentheses.

That is perfect! Works a treat! Thanks very much. That will help me out a lot.

You’re welcome! Now please check one of the “this solves my problem” check boxes so this topic is marked solved. That way when others come along they know you don’t need help with this anymore.

Done, sorry meant to do that just now.

Would this forum be the right place for Smartthings to Mosquitto to Home Assistant discussion? I think I have a config issue with it

No problem. I just mention it because a lot of people that ask for help don’t seem to do it or are not aware that they should. Helps to streamline things. :slight_smile:

Certainly. I’ve seen several posts along those lines. Just start a new topic and good luck!

Guys - Hate to resurrect an old thread but I’m struggling to convert km to miles, I had the following config:

# Addded to convert km to Mi
  - platform: template
    sensors:
      fuel_range:
        friendly_name: Fuel Range
        unit_of_measurement: mi
        value_template: >
          {{ (state_attr('sensor.m140i_remaining_range_fuel').replace('km','')|float/1.609) }}

I get ‘Unknown mi’ in the sensor panel ?

The state_attr function takes two parameters:

  1. entity_id
  2. attribute name

In your example, the state_attr function is assigned only one parameter, namely an entity_id called sensor.m140i_remaining_range_fuel. You need to also supply the name of one of this entity’s attributes.

states('sensor.m140i_remaining_range_fuel', 'attribute_goes_here').replace('km','')|float/1.609)

If your goal was to use the entity’s state, and not one of its attributes, then you should use the states function (it uses one parameter: entity_id).

states('sensor.m140i_remaining_range_fuel').replace('km','')|float/1.609)

Thanks for the response - Thats not working for me unfortunately. I’m trying to convert the value from km to mi and display the new value in a sensor panel, out of the box it displays km and working, but with

states('sensor.m140i_remaining_range_fuel').replace('km','')|float/1.609)

the ‘unknown’ value on the sensor panel has been replaced with

states(‘sensor.m140i_remaining_range_fuel’).replace(‘km’,’’)|float/1.609)

If I add the {{ }} I get another error?

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['fuel_range']['value_template']. Got "{{ states('sensor.m140i_remaining_range_fuel').replace('km','')|float/1.609) }}\n". (See ?, line ?).

its exactly what is says: a superflous ). A very small but significant typo.

you should have probably found out yourself but this would do it:

{{states('sensor.m140i_remaining_range_fuel').replace('km','')|float/1.609}}

it even works for me, and I don’t have the sensor.m140i_remaining_range_fuel :wink:

Yep that’s fixed it … I must have read that line of code a hundred times and never spotted it - Thanks man !

cool. gotta thank @123 though, he’s the one solving the template for you, I just had my ocd eyes wake up this fine morning :wink:

1 Like