How to read the water level of horizontal tank?

Hi everyone, I’m trying to get the water level of an horizontal tank but don’t know how to do it.
I’ve a 4-20mA pressure sensor and I’m able to read a voltage value with an ADS1115 converter but how can I add a complex equation with cosine and square root?
the equation is like this:

import math
WaterLevel = (4.5*(1.56252math.acos(-2.6)+(X-1.25)math.sqrt(X(2*1.25-X))))*1000

X-> is the voltage value of ADS1115 Analog input

I’ve tried to create a new custom input but I don’t know enough python in order to get this to work

Has anyone tried something similar?

Thanks very much

1 Like

The easiest way might be to use a Python 3 Code Input. You can get the voltage measurement, perform the mathematical conversion, then save the new value with the following code in your Python 3 Code Input. Make sure to set the appropriate Measurement/Unit in the Python 3 Code Input options, as well as copying the correct IDs from the ADS1115 Input. You can find/copy the Input ID with the button next to the Input name, and the measurement ID under Configure Measurements next to the channel/measurement name. Enable Log Level: Debug to see the debug messages appear in the Daemon Log.

import math
from mycodo.utils.influx import get_last_measurement

input_id = "REPLACE_WITH_ADS1115_INPUT_ID"
measurement_id = "REPLACE_WITH_ADS1115_MEASUREMENT_ID"
max_age = 1800  # Max age to query the measurement in the database (seconds)

voltage = get_last_measurement(input_id, measurement_id, max_age=max_age)
self.logger.debug("voltage: {}".format(voltage))

if voltage is not None:
    try:
        water_level = (4.5*(1.56252*math.acos(-2.6)+(voltage-1.25)*math.sqrt(voltage*(2*1.25-voltage))))*1000
        self.logger.debug("water_level: {}".format(water_level))
        self.store_measurement(channel=0, measurement=water_level)
    except:
        self.logger.exception("Error evaluating equation")

Thanks for your quick help :slight_smile: , it seems to be easy to do but I don’t know why I’m getting this error in debug log:

2021-07-07 08:30:58,306 - ERROR - mycodo.inputs.python_code_4592032a - 1
Traceback (most recent call last):
  File "/home/pi/Mycodo/mycodo/inputs/python_code.py", line 230, in get_measurement
    run.python_code_run()
  File "/home/pi/Mycodo/mycodo/user_python_code/input_python_code_4592032a-1964-48fc-9939-3ee4e6cd803d.py", line 34, in python_code_run
    self.debug("voltage: {}".format(voltage))
AttributeError: 'PythonInputRun' object has no attribute 'debug'

Any idea? I’ve copied the input_id and measurement_id with the button :frowning:

It should have been self.logger.debug, not self.debug. I fixed the original code.