Analog data read convert to percent

Hi everyone,
I got an analog Capacitive Soil Moisture sensor.
This sensor when dry gives me value of 530, when in water I get the value 220.
Does anyone knows how I can get an percent value in Home Assistant. between those two values?

Sensor is connected to an D1-mini.

  - platform: mqtt
    name: "Humidity_plant"
    icon: mdi:flower
    state_topic: "tele/Sensor_Woonkamer/SENSOR"
    value_template: "{{ value_json.ANALOG.A0 }}"

Please do read the pinned messages and the guides in the banner, it helps us to help you if you follow those.

Please format your posted code correctly.

value_template: "{{ ( value_json.ANALOG.A0 | float * (-0.32258) + 170.96774 ) round(1) }}"

Tinkerer, sorry for that.

tom_I, thanks!
But how do I calculate it myself ?
Because I want to place more analog Soil sensors.

You have two points (x1,y1) and (x2,y2).

for the equation y = m.x + b

m = (y2-y1)/(x2-x1)

b = y1 - m.x1

Hi @Marcmk. I am reasonably sure that you can achieve the result you want by using a very good solution I found here which was suggested by @nickrout in a discussion started by @Craggy_h

The solution was to help with a discussion about tank level as a percentage but I believe your needs are the same.

Nick put forward that the maths was like this:
Percentage = constant - sensor value x inverted slope

So…The two values you need to calculate for your sensor would then be:

Slope = (0 - 100) / (dry value - saturated value)
Slope = (0 - 100) / (530 - 220)
Slope = -100 / 310
Slope = -0.3225806

Constant = dry value x inverted slope
Constant = 530 x (-0.3225806 x -1)
Constant = 530 x 0.3225806
Constant = 170.967718

So in your case you then have to put that into a value template:
value_template: “{{ (constant - states(‘sensor.your_sensor’) | float * slope) | round(0) }}”

While your config template sensor is:

  - platform: template
    sensors:
      Humidity_plant_percent:
        unit_of_measurement: "%"
        value_template: "{{ (170.967718 - states('sensor.your_sensor') | float * 0.3225806) | round(0) }}" 

I have been using this for several tanks I am monitoring and find it is very good.
I hope it works for your needs!

1 Like

Wellsy, that was exactly what i was looking for!!
Many Thank!!

1 Like

@Marcmk Happy to be able to help!

Hi,
I tried also this solution but I get no real value calculation by using the formular.
I have an analog voltage value which should generate an -90 up to +200 value
the voltag signal is 0-3V but I use analog values just from 0 up to 1,46V
so:
analog value 0 should be -90
analog value 146 should be +200

I get Slope 1,327 but get no real values for Constant (0x???). Can someone give my an Idea what I’am doing wrong.

Since 2021.5 you can use this:

compensation:
  name_you_want_here:
    source: sensor.your_voltage_sensor_here
    unit_of_measurement: your_sensor_unit_here
    data_points:
      - [0, -90.0]
      - [146, 200]

@tom_l
thanks, this is much more easy and fix my request.