Convert voltage to litres

I have a sensor in my water tank reading a voltage
I want to output the litres of water in the tank
The voltage reads 0 - 1.5 V
The tank holds 22 000 litres

How does the voltage rating of a sensor convert to volume? Is it linear so that 0V = 0l; 0.75V = 11,000l, and 1.5V=22,000l. What device varies voltage based on a volume? I am curious because I have not heard of that before.

Hi Stephenn
Thanks for your reply
Yes the relationship is linear
I use a sensor like this https://www.aliexpress.com/item/1005004940352922.html?spm=a2g0o.productlist.main.7.59771d707vreZS&algo_pvid=aad6aa1d-a103-49d9-be25-d403cade3ebf&aem_p4p_detail=2023030507353514081670765274860020519812&algo_exp_id=aad6aa1d-a103-49d9-be25-d403cade3ebf-3&pdp_ext_f={"sku_id"%3A"12000031093042639"}&pdp_npi=3%40dis!NZD!86.56!60.59!59.73!!!!%40211bea6216780305358988677d06fe!12000031093042639!sea!NZ!138429292&curPageLogUid=vDK1xHePE6fL&ad_pvid=2023030507353514081670765274860020519812_4&ad_pvid=2023030507353514081670765274860020519812_4
I connect it to a Shelly uni and this sends the voltage back to my Home Assitant
This is used to report on the water level in my rain water tank
Simon

1 Like

This will do it:

Enter your [voltage, volume] data points (minimum of 2 for linear) and give it the required unit.

2 Likes

This is really useful as Iā€™m doing the very same project with monitoring water levels in a tank using a pressure sensor.

Iā€™ve updated the above for me which works well but with one minor issue.

The water sensor goes to 0.00v when itā€™s down to 75 litres in the tank, and max is 525L. Can I get it to go down on a linear scale to 0.01v / 75 litres then report zero litres once it hits 0.00v? At the moment it reports below 75 litres when itā€™s emptying, for example 22l.

compensation:
  water_tank_volume:
    source: sensor.shelly_sensor_board_adc
    unique_id: water_tank_volume_litres
    unit_of_measurement: litres
    precision: 0
    lower_limit: true
    data_points:
    - [0, 0]
    - [0.01, 75]
    - [0.1, 134.271099744246]
[cont]
    - [3.90, 523.657289002557]
    - [3.91, 525]

Thanks for any help you can give on this as Iā€™m still learning these functionsā€¦

Use the lower_limit option.

Thanks @tom_l but I tried that option and it prevents it being reported as a minus value but doesnt seem to help with preventing it showing less that 75 litres. Ideally it should go from 0l to 75l, then 76, 77 etc. Do you know if thatā€™s posslbe to do?

You are going to need to filter your compensation sensor with a template sensor for that. This should do the trick:

configuration.yaml

template:
  - sensor:
      - name: "Tank Volume"
        unique_id: water_tank_volume_75_and_up
        unit_of_measurement: L # do not change this to "litres"
        device_class: volume
        state_class: measurement # only include this if you want long term statistics
        state: >
          {% if states('sensor.water_tank_volume')|float < 75 %}
            0
          {% else %}
            {{ states('sensor.water_tank_volume') }}
          {% endif %}
        availability: "{{ has_value('sensor.water_tank_volume') }}"
1 Like

Many thanks @tom_l

Iā€™ve updated it to match the entity ids and pointed my dashboards to the new and itā€™s working perfectly. :smiley:

1 Like

Hi Guys, First time here. Trying to setup a water tank level as above. Sensor is .5v to 5v range through a shelly uni. Have connected and voltage reading on dashboard. Trying to covert it to liters as above and a little stuck. Here is my YAML code below. First time using YAML as well. I donā€™t understand the data point layout and what number is related to what. Just copied from the above. Tank is 5000L. Ill try and post a screen shot of the error. I have the gauge on the dashboard reading liters but its just maxed out at 400 liters when the voltage is at minimum.
Cheers in advance.

For your data points, put a space between each - and [

Thanks for that a step closer. I just need to work out how to calibrate. I dont know what the data point order is. Is the first number the ADC voltage from the sensor and the 2nd number the reading your want? IE liters?
So if .5v is my min voltage and 5v max and a 5000 liter tank can it just be

  • [ .5, 0 ]
  • [ 5, 5000 ]
    Not sure what the [ Cont ] does in the middle. I changed the numbers saved and did a quick reload in the system page. Added a new card using the compensation sensor and number is exactly the same. Made no difference. In the YAML file I have a green tick and saved happy. But when I went to do a quick reload I got this error. Latest
    Cheers Mark.

Here is my YAML file. Only let me upload one pic as new user.

Please donā€™t post pictures of text. Post the text correctly formatted for the forum.

You have some very odd indentation there but nothing that will actually prevent the integration from loading. The problem is the [ cont ] entry. This is not a pair of numbers.

https://www.home-assistant.io/integrations/compensation/#configuration-variables

Uncompensated == your voltage, Compensated == Litres

- [ voltage, litres ]

Hi, Sorry about the pics. See updated text below.
So data point as I thought. So the below should have the range of my sensor and tank. Sensor .5v to 5v and tank 0 to 5000 liters. That is all I should need for the data points yeah?
I think I copied a lot of code across that is not relevant to me. The template I copied above only seems to be to stop the reading going below 75. Not relevant to me so deleted it. No change in my calibration. I updated the compensation based on the original post with my source, Unique ID and data points. Is the first line ā€œwater_tank_volumeā€ just a name and not linked to anything ?

compensation:
water_tank_volume:
source: sensor.header_tank_adc
unique_id: e54278148eed4fbff45394cc75a54241
unit_of_measurement: litres
precision: 0
data_points:

  • [ .5, 0 ]
  • [ 5, 5000 ]

I get a green tick in the yaml file and can save. When I try and reload the ymal file in the developer tools or reboot HA I get the below error. Seems im missing a dictionary or something. Not sure how to fix that.
Everything I change has no effect on the output number I am getting. Still around 400 litres.

Failed to call service homeassistant/reload_all. Cannot quick reload all YAML configurations because the configuration is not valid: Invalid config for [compensation]: expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜data_pointsā€™]. Got [[0.5, 0], [5, 5000]] expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜precisionā€™]. Got 0 expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜sourceā€™]. Got ā€˜sensor.header_tank_adcā€™ expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜unique_idā€™]. Got ā€˜e54278148eed4fbff45394cc75a54241ā€™ expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜unit_of_measurementā€™]. Got ā€˜litresā€™ expected a dictionary for dictionary value @ data[ā€˜compensationā€™][ā€˜water_tank_volumeā€™]. Got None. (See /config/configuration.yaml, line 13).

Cheers
Mark

Being new to this I am very lost and just following along. I have installed the pressure sensor in to my 2m tall tank holding 3000 liters, we have just had rain so now my tank is full at showing 8.41 vdc. I have the gauge working showing the voltage but when I copy the code above in to my config file I cannot make it work or even find the new output to add to a dashboard. floundering around like a fish out of water.
My original config file is pretty basic as per below so do I need to add the platforms first? Maybe I need a yaml for dummies as a starting point??
Thanks Bruce
(I canā€™t even work out how to post code in here :upside_down_face:

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

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

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