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?
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.
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 %.
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?
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