Read ADC and perform action based on adjustable value

This is a setup for exterior watering base on soil moisture.
I have d1-mini v3 connected via MQTT, updating from ADC for 30 sec and then going into deep sleep for 60 minutes.
My goal is to trigger a switch (water valve) when the moisture is below a value that I can set from input_number slider
Capture1

This is the code on the esp:

substitutions:
  esphome_name: moisture-01
  ip_address: 172.16.0.151

esphome:
  name: ${esphome_name}

esp8266:
  board: esp01_1m

logger:
  level: NONE

mqtt:
  broker: 172.16.0.126
  username: !secret mqtt_user
  password: !secret mqtt_password
  birth_message:
  will_message:
  
  on_message:
    - topic: stat/holdsleep
      payload: 'ON'
      then:
        - deep_sleep.prevent: sleepy
    - topic: stat/holdsleep
      payload: 'OFF'
      then:
        - deep_sleep.enter: sleepy

ota:
  password: "***"

wifi:
  fast_connect: true
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: ${ip_address}
    gateway: 172.16.0.1
    subnet: 255.255.255.0
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: !secret fallback_ssid
    password: !secret wifi_password

captive_portal:

sensor:
  - platform: adc
    raw: true
    pin: A0
    name: ${esphome_name}
    unit_of_measurement: "%"
    accuracy_decimals: 0
    force_update : true
    filters:
      - multiply: 3.3
      - calibrate_linear:
        - 2290 -> 0.0
        - 910 -> 100.0
    update_interval: 10s

deep_sleep:
  id: sleepy
  run_duration: 30s
  sleep_duration: 60min

I figured in the sensor I would add something like (haven’t figured it out yet) on_value launch HA automation but looking at HA automation in the “below” field it is expecting a value and not entity value (or I could be wrong)
Capture2

Then I figure it might be best to just compare from within EspHome and I found this post that is similar to what I want to do. But since I am using MQTT I am not able to have
platform: homeassistant.

At this point I will need some guidance…

What you could do is create a helper (number / slider) and then use that helper in your automation. You then use the helper as your slider to adjust the value.

Yes this is what I did as per the first image on my post, I’ve created a few number sliders for the different zones. Where I am stuck is not knowing which is the best solution and then how to apply it.
I want to compare the ADC values from the sensor with set values in the number sliders and trigger a switch if ADC < set value.

They say a picture is worth a thousand words…
This is what I am trying to do, I am stuck with the yellow zone.

OK for anyone with the same requirements, I have now a working solution. I left the EspHome as is and I made one automation per esp device, this is the working code for one of them:

alias: automateMoisture02
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.moisture_02') | float <
      states('input_number.moisture02') | float }}
    for: "00:00:01"
condition: []
action:
  - type: turn_on
    device_id: 4a1732b23c4449cab9b977fdf88e9dc3
    entity_id: switch.water_deck
    domain: switch
mode: single

I could not use the “below” in the trigger but using template is working.
I realize ow that I have a serious problem with my naming convention, as my system is growing I am starting to wonder what is what but that is another future chapter.

1 Like

Me again…
I am left with a small issue.
Lets say I set the input_number to trigger at 50
If the sensor is at 51 and on the next wake up it goes to 40, the automation will be triggered. All good so far.

However, if I don’t add enough water and the next wake up goes to 49, even if it is still under 50 it will not trigger until if first goes above 50.

How can it trigger always when it wakes up below 50?

Great diagram. This helps a lot. Seems doable all in ESPHome. But a question first.

  1. why run the pump for a fixed hard coded time?

I think I would continuously check moisture and run pump until threshold reached.

The adjustable value is easy in ESPhome, a number template.

But my cats ate all my plants so I’ll wait to hear back about the pump duration…

Hi,
As a first phase, when the moisture goes down, I trigger the water relays for 3 minutes.
At some point (when I acquire better knowledge) I will create another set of input_numbers to set the time delay of each water relay because my environment includes plants in hanging pots, plants in large boxes under a roof and plants exposed to the weather.

As for what I have now it is near perfect with the only exception that if the automation is triggered, the water relay will turn on but if not enough water to bring the moisture sensor equal or above the threshold, the automation will not happen again. This is confusing and I wonder why it will run only once. I played with the condition mode but makes no difference. There must be some kind of retain/reset somewhere to allow the automation to run everytime the esp wakes up and publish a value that is below the minimum set value.

EDIT: as a side note, my esp sensors are running off batteries so they sleep most of the time.

This sounds like a problem of your own creation. Why use an arbitrary time value that has little correlation to moisture content. Hence the hint to stop using time, and keep the pump running until the desired moisture value it achieved. No retriggers, no weather concerns, a direct correlation.

Because the moisture level will be tested again only 60 minutes later (between deep sleeps) and 60 minutes is way too long to run the pump.

So don’t sleep so long until the moisture level is reached and the pump is off…

When the ESP is on it consumes 72mA and when it is in deep sleep it consumes 170uA
I am using Lithium AA batteries
Based on this web site
if I run 30 seconds and sleep 60 minutes the batteries should last 130 days (120 days is plenty)
if I change the run time to 60 seconds, the battery life goes down to 74 days which is not enough.

floods house because the water sensor falls out

The gardener in me is thinking:

  • sleep for 12 hours
  • instead of triggering on hitting ~50 just check if it’s less than ~50 when it wakes up
  • water for n seconds with n being a guess based on the Δ between the current moisture and the desired moisture
  • deep sleep for 5-10 minutes to let moisture even out
  • recalculate and water for another n seconds
  • repeat, sleeping for another 12 hours

The problem is, Esp maximum sleep time is 71 minutes.

Increment hour counter by one, resleep.

I pretty much finalized my project and it is working well. As for the small issue I had, it is actually not an issue. As the moisture sensor drops below threshold the pump is activated. It is very unlikely that any amount of water will not bring the moisture level above the threshold so it is no longer an issue.

Thanks to all who participated in this thread, it is awesome to be able to share and exchange opinions and knowledge.

1 Like