Return raw & processed data from 1 sensor, code

Hi all. (Sorry if this is too wordy!)

First ESPHome project and relatively new to HA ( :heart: it though) while I am confident enough with C to muddle my way through (although it is pretty casual project usage), I don’t know of the differences in C++ syntax etc.
I’ve tried applying the examples as much as possible but have always found I pick up things quickest (especially code) when working on my own problems (I know it sounds selfish).
I have embedded a capacitive moisture sensor in some bamboo and would like this sensor to report moisture levels. I would then monitor and eventually automate the Rachio irrigation controller to apply extra water as required.
Firstly, I am a little confused (from the examples) how to implement the piecewise equations provided with the sensor.
Second I would prefer to pass both the raw voltage from the sensor as well as the processed values into HA. This is mostly for my sanity and would allow me to check the voltage is matching what it should.
I haven’t seen of way of doing this?

Thank you in advance for any tips :smiley:

  name: bamboo-moisture
  platform: ESP32
  board: esp32dev

# Enable logging

# Enable Home Assistant API

  password: ""

  ssid: ""
  password: ""

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Bamboo-Moisture Fallback Hotspot"
    password: ""


  - platform: adc
    pin: A0
    name: "court_bamboo_voltage"
		update_interval: 5min
    attenuation: 6db 
		unit_of_measurement: 'V'
		#return V, then return calculation?!?!?
			lambda: |-
				if (id(courtyard_bamboo_soil_voltage).state <= 1.1) {
					lambda: return X * 10 - 1 
				else if {id(court_bamboo_voltage).state > 1.1 && id(court_bamboo_voltage.state) <= 1.3 {
					lambda: return X * 25 - 17.5 
				else if (id(court_bamboo_voltage).state > 1.3 && id(court_bamboo_voltage.state) <= 1.82 {
					lambda: return X * 48.08 - 47.5
				else if (id)court_bamboo_voltage).state > 1.82 && id(court_bamboo_voltage).state <= 2.2 {
					lambda: return X * 26.32 - 7.89
				else if (id(court_bamboo_voltage).state > 2.2 && id(court_bamboo_voltage).state <= 3 
					lambda: return X * 62.5 - 87.5 
				else {
					return ERROR
		#most soils holding capacity tops out at 50%, 2.2v
    unit_of_measurement: '%'

Rather than 5 different linear equations in if/else templates have you thought about using either:


As to returning both the raw and filtered result. Don’t apply the filter to the ADC component, give it a name and it will be reported as the raw voltage to HA.

Then implement a template sensor to return the moisture level: Template Sensor — ESPHome

I think I could probably work it out. I wasn’t really sure how to apply it other than perhaps running the equations through excel to gather some points to use.
I was just using the equations provided here Vegetronix moisture sensor.
Will give the template sensor a crack with polynomials and see how I go tonight.
Thank you for the guidance

I think I have setup the polynomial correctly. However I cannot work out how to pass the sensor reading into the template. Perhaps a gloabal variable? If so I’m not sure how to set the global variable from the sensor reading sorry.

Give the ADC an id. e.g. id: adc_volts

Use this in your lambdas: