First time here and been searching for an answer to this for a while, hoping someone may have already done it?
I have an ESP32 setup with a resistance water level board, in order to preserve the copper traces, I want to be able to provide power to the sensor, read the value and then turn off the power.
I’d previously done this with my own firmware, but wanted to try and integrate it with HA. I don’t seem to be able to find a way to turn a GPIO pin on (the one providing power to the sensor) prior to the sensor being read.
Does anyone have any ideas on how this could be achieved?
Define the power GPIO as a GPIO output and the sensor as a resistance sensor with internal: true in ESPHome. Also define a template sensor which is what will be exported to HA. This is to ensure that you never accidentally send “unknown” or wrong values to HA from when the real sensor is not powered up.
I tried to compile it, pasted into the body of one of my projects. With a couple of pin-related alterations for my board, I got:
src/main.cpp: In lambda function:
src/main.cpp:242:21: error: 'class esphome::gpio::GPIOBinaryOutput' has no member named 'state'
if (ws_power->state) {
^
src/main.cpp:243:27: error: 'class esphome::adc::ADCSensor' has no member named 'value'
return wl_sensor->value;
So there doesn’t seem to be a way to check the status of an output, unless you connect it to another input and read that; and the adc should be read with .state not .value.
Also, I’d have thought the template sensor would want to return water_level's state rather than wl_sensor?
Hi, did you get anyware ? I’m looking for a solution to the same problem.
Arduino solves it nicely but i can’t find out how to do the same with esphome.
EDIT: Forget the above. this won’t work, as HA polls the state from the rainsensor even if it is not powered. I thought i could circumvent this by calling component.update: rainsensor. This works, but after the 60s poll period of HA is due it fetches the value from rainsensor again but then it’s zero. So the value is continously jumping from “rain value” to “dry value”.
This is also what should be used with Soil Sensors! You can power them using a GPIO pin to avoid corrosion with pretty much this exact code! Thanks for posting a working version. I was going nowhere and this is great.
calibrated by taking a beaker, measuring the voltage at 5ml, 10ml, 15ml, 20ml, 25ml, 30ml respectively and then using https://mycurvefit.com/ to figure out it needs a cubic regression. used calibrate_polynomial with a degree of 3 and the measured values, and voila!