Convert cm to % HC-SR04

I need help creating a template sensor to convert a cm reading from a HC-SR04 distance sensor to either % or litres.
The sensor is measuring heating oil level in a tank. When the reading is 90cm the tank is empty and when it’s 15cm the tank is full.

How can I convert this to a percentage reading in Hass? i.e 90cm equals 0% and 15cm equals 100% full tank. Alternatively convert it roughly to litres so 90cm equals 0 litres and 15cm equals 900 litres?

Thanks.

1 Like

Is it the maths or the expression of it in a template that is causing you problems? If h is the measurement then the percentage p is p=120-1.3333h. litres is just 9x that or l=1080-12h. This assumes the tank is linear.

Templates is not my strength sorry.

1 Like

It’s the formula I need for the template. I have the template added and it displays as cm.
I read all the docs but can’t find the correct code/syntax to do the math to convert the reading to %.

Here’s one I tried but it’s not right.

value_template: “{{ (states(‘sensor.sonoff_oil_level’)|float-90/100*90)|round(0) }}”

It gives a percent reading but it increases as the tank empties instead of decreasing.

1 Like

See my post for correct maths.

Ok I’ve used the math from your first post and it gives me the correct percentage.

If the placement of the sensor changes and let’s say the empty was at 95cm instead of 90cm and the full was still 15cm what would the math be?

Once I know the formula I can work it out at any distance in future.

Thanks.

in the first example -1.33333 is the slope of the graph line, ie

(0-100)/(90-15) = -100/75 = -1.3333333

From there you can work out the constant 120.

In your second example the slope is

(0-100)/(95-15) = -100/80 = -1.25

It is easier to picture if you draw a quick graph, but I don’t think the forum lets me do that!

1 Like

I had figured out the 1.25 bit in my head but the only bit I couldn’t figure out is where the 120 came from. Presumably this number changes for the second example?

Never mind, I figured out the constant. It’s 95*1.25 = 118.75

For anyone else who needs a calculation similar to this here is the final template code:

  • platform: template
    sensors:
    oil_status:
    friendly_name: “Oil Status”
    unit_of_measurement: “%”
    value_template: “{{ (118.75 - states(‘sensor.sonoff_oil_level’)|float * 1.25) |round(1) }}”

Thanks for your help.

5 Likes

Great i got it all up and running but i have a slight niggle, on reboot the new sensor will default to 118% until the sensor responds after a few minutes which messes with my history graphs, is there away for the new sensor to be “available” only when the source sensor is “available”.
(here is the template code with formatting for any body it might help)

#Oil tank as a percentage
  - platform: template
    sensors:
      oil_status:
        friendly_name: "Oil Status Percent"
        unit_of_measurement: "%"
        value_template: "{{ (118.75 - states('sensor.oil_level_sr04_distance')|float * 1.25) |round(0) }}"
        icon_template: mdi:oil