Hello,
i think i solved it. For the community here my Solution. In the future i will provide a description about the hole setup.
The HA UI that i use is this:

The esp32 wakes up after the set sleep time and is doing this:
- measuring the moisture and sending the value to HA
- If its under the set Threshold it pumps for 10s
- Sending the last time pumped to HA
- If pumped it goes just for a few minutes to deep sleep
- Check if it is allowed to go to deep sleep (I have this to be able to update the code)
- Only if no pumping is needed it goes to the set duration into deep sleep
In HA i had to make two input.number for the moisture threshold and the Sleep time duration, as well as one input.boolean for the prevention of deep sleep.
The code for the esp32 ist here:
Note: i commented the interesting parts that where complicated.
esphome:
name: esp32-gieskanne1
esp32:
board: az-delivery-devkit-v4
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "xxxx"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp32-Gieskanne1"
password: "xxx"
captive_portal:
time:
- platform: homeassistant
id: homeassistant_time
deep_sleep:
run_duration: 30s
sleep_duration: 5min
id: deep_sleep1
sensor:
- platform: adc # airdry: 2,8V; waterwet: 1,58V
pin: 34
name: "soil Moisture"
id: moisturesensor
attenuation: auto
unit_of_measurement: "%"
accuracy_decimals: 0
update_interval: 5s
filters:
- median: #median over the last 7 values
window_size: 7
send_every: 4
send_first_at: 1
- calibrate_linear: #setting the volatge values to equalent percantages
- 2.80 -> 0.00
- 1.58 -> 100.00
# limit the value to 0 and 100%
- lambda: |-
if (x < 0) return 0;
else if (x > 100) return 100;
else return (x);
on_value:
then:
- if:
condition: #check that the threshold value from HA UI is imported
sensor.in_range:
id: moisture_set
below: 100.0
then:
- if:
condition: #compare value from sensor with threshold value, it is more complicated that "on_range" bcs it also triggers now when the value didnt changed
lambda: 'return id(moisturesensor).state < id(moisture_set).state;'
then:
- deep_sleep.prevent: deep_sleep1
- switch.turn_on: pump1
- delay: 10s
- switch.turn_off: pump1
- sensor.template.publish: #sending Timestamp of pumping to HA UI
id: pump1_last_update
state: !lambda 'return id(homeassistant_time).now().timestamp;'
- if:
condition: #prevent esp32 from entering deep_sleep for updating purpose
binary_sensor.is_on: preventdeepsleep_set
then:
- deep_sleep.prevent: deep_sleep1
else:
- deep_sleep.enter:
id: deep_sleep1
sleep_duration: 2min
else:
- switch.turn_on: LED2
- delay: 15s
- switch.turn_off: LED2
- if:
condition: #prevent esp32 from entering deep_sleep for updating purpose
binary_sensor.is_on: preventdeepsleep_set
then:
- deep_sleep.prevent: deep_sleep1
else:
- deep_sleep.enter:
id: deep_sleep1
sleep_duration: !lambda 'return id(sleeptime_set).state*60000;' #for converting in min *60000, for converting in h *3600000
- platform: homeassistant
name: "Moisture Set Point"
entity_id: input_number.desired_moisture1
id: moisture_set
accuracy_decimals: 0
- platform: homeassistant
name: "Sleep Time"
entity_id: input_number.desired_sleeptime1
id: sleeptime_set
- platform: template
name: "Pump1 last watered"
device_class: timestamp
id: pump1_last_update
binary_sensor:
- platform: homeassistant
name: "prevent deep_sleep"
entity_id: input_boolean.prevent_deep_sleep
id: preventdeepsleep_set
switch:
- platform: gpio
pin: 22
name: "pumping"
id: pump1
restore_mode: ALWAYS_OFF
icon: "mdi:water-pump"
- platform: gpio
pin: 23
name: "running"
id: LED1
inverted: true
- platform: gpio
pin: 14
name: "status"
id: LED2
restore_mode: ALWAYS_OFF
Following problems that i struggled with i solved here:
- the “on_value_range” command only triggers when the new value is inside the range and the previous value was outside the range. With this it didnt triggered the pumping if the value was still under the threshold. So i used the “on-value” and if condition.
- Becuse of the excessive use of deep_sleep for battery saving reasons, often on booting the script had the measured moisture value but dont had the threshold value from HA and went to completly strange conditions. For this i build an if condition to just check the threshold value if its available. (I think i abused it a bit.
)
The part of the code that iam talking about is this:
on_value:
then:
- if:
condition: #check that the threshold value from HA UI is imported
sensor.in_range:
id: moisture_set
below: 100.0
then:
- if:
condition: #compare value from sensor with threshold value, it is more complicated that "on_range" bcs it also triggers now when the value didnt changed
lambda: 'return id(moisturesensor).state < id(moisture_set).state;'
So this is my walk of pain to this solution. I hope it helps further anonymus masochists in the future.
As i mentioned i will make a post in the future about the hole project with Part list and how it is set up in my plants.