# Horizontal cylinder tank - volume calculation

hello,

I have created an ultrasonic sensor to calculate the volume of an oil tank.
Although my sensor readings were accurate I couldn’t calculate the volume with the same accuracy.

So I have asked from the tank manufacturer to email me more details about the tank volume, calculation etc.

They sent me the following photo which I share with you.

Apart the photo they also sent me a table with the tank’s volume and height.

For example, if the level of oil (starting from bottom) is 53,8cm then the tank contains 600 liters of oil.

I was wondering if I could create a template sensor which will give me the tank’s volume based on my sensor’s reading and comparing it with this table. Is it possible?

You can use a template sensor for this purpose. I have created a template by making an equation of the curve produced by the given statistics. It is not 100% accurate but the tolerance is low at +/- 12 litres. But the advantage of this method is that it will give the appox volume for all the sensor readings.

``````  - platform: template
sensors:
tank_volume:
friendly_name: "Tank Volume"
unit_of_measurement: "Litres"
value_template: "{% set x = states('sensor.senor_name') | float %} {{(-5.876302 + 6.803452*x + 0.1275233*x**2 - 0.0009407021*x**3 + 0.000004060763*x**4 - 3.565947e-8*x**5) | round(0)}}"
``````

Please change the sensor.sensor_name to your sensor entity_id. Always happy to help.

3 Likes

If you want to compare the values to the graph, you can use this. This will first compare and then if the value of the sensor is not in the standard table, it will use the formula to calculate. You can eliminate the use of formula by deleting the last line but in that case you the length sensor value has to be precise for the volume to the updated.

``````  - platform: template
sensors:
tank_volume:
friendly_name: "Tank Volume"
unit_of_measurement: "Litres"
value_template: >
{% set x = states('sensor.sensor_name') | float %}
{% if x == 13.0 %} 100
{% elif x == 17.5 %} 150
{% elif x == 22.3 %} 200
{% elif x == 26.3 %} 250
{% elif x == 30.8 %} 300
{% elif x == 35.8 %} 350
{% elif x == 38.7 %} 400
{% elif x == 42.3 %} 450
{% elif x == 46.1 %} 500
{% elif x == 49.8 %} 550
{% elif x == 53.8 %} 600
{% elif x == 57.3 %} 650
{% elif x == 61.3 %} 700
{% elif x == 65.3 %} 750
{% elif x == 69.0 %} 800
{% elif x == 73.7 %} 850
{% elif x == 78.0 %} 900
{% elif x == 83.0 %} 950
{% elif x == 88.0 %} 1000
{% elif x == 94.7 %} 1050
{% else %} {{(-5.876302 + 6.803452*x + 0.1275233*x**2 - 0.0009407021*x**3 + 0.000004060763*x**4 - 3.565947e-8*x**5) | round(0)}}
{% endif %}
``````
1 Like

Nice but it will show this value only if at this height.
For example, if 17.6cm, it will not show 150 litres.

Instead it will use your formula., which is fine but it has this tolerance.
I can’t confirm that is only 12 Litres as you mentioned, but if it is then its fine.

I was wondering, if using the above table, is possible to create a mathematical equation, which will calculate the tank’s volume with greater accuracy.

I dont know how to construct a formula for volume from the data as the shape of the tank is not standard. But mathematical equation I have used has been made only with the provided data. You can check the tolerance by giving the value of x in the equation as the distance in the table and compare the result with the volume from the table. A more accurate equation could be made which will have the power of x greater than 5 which is what I have used here. But i dont have tools for that.