Moisture sensor Sample automation

Hi, I’m learning automations. This is a first one for me. The moisture sensor is esp8266 that goes in to sleep every 120 sec for 30min.
The goal is to detect that moisture level is above 0.5V ( which translates in to about 50%), turn on watering relay and turn it off after 60 seconds.

I can not figure out why I’m getting this error. Also looks to me that the automation may will run just ones upon HA restart.
Thank You.

The error: Stopped because only a single execution is allowed at April 9, 2025 at 8:45:57 PM (runtime: 0.00 seconds)
Here the actual YAML.

id: '1744246478390'
alias: Garden Watering ON
description: ''
triggers:
  - trigger: state
    entity_id:
      - sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - trigger: numeric_state
    entity_id:
      - sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
    above: 0.5
conditions: []
actions:
  - type: turn_on
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
mode: single

Delete this superfluous trigger:

  - trigger: state
    entity_id:
      - sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5

You only need the numeric_state trigger.

Hi, Tom,
Still have an issue: the automation will start if sensor would go from above 50 to below 50 while board is wake.
But automation never starts when sensor is below 50 when board wakes up.
I made some changes to increase resolution scale from .4V to .7V
Now sensor reports from 0 to 100 %
P.S. The " superfluous trigger" was there to indicate the sensor status change. Removed now per your advice.

Automation YAML

alias: Garden Watering ON
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
    below: 50
conditions: []
actions:
  - type: turn_on
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
mode: single

Sensor YAML

web_server:
  port: 80    

captive_portal:

deep_sleep:
  run_duration: 2min
  sleep_duration: 30min

sensor:
  - platform: adc
    unit_of_measurement: "%"
    icon: "mdi:flower-outline"
    accuracy_decimals: 0
    pin: A0
    filters:
    - calibrate_linear:
        - 0.4 -> 100
        - 0.7 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    name: "Soil Moisture"
    update_interval: 10s    

# Turns LED on GPIO02 On when Wake
switch:
  - platform: gpio
    pin: GPIO2
    name: "Wake LED"

Ah ok. unavailable to below 50 not triggering. Got it.

Try this:

alias: Garden Watering ON
description: ""
triggers:
  - trigger: state
    entity_id: sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
    to: ~
conditions:
  - condition: numeric_state
    entity_id: sensor.garden_moisture2_soil_moisture
    below: 50
actions:
  - type: turn_on
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
mode: single
max_exceeded: silent

I did copy-paste yaml from this post to the Automation yaml, Unfortunately it did not work. I suspect logic is waiting for the trigger to change to start the automation. As the description says: When garden-moisture2 Soil Moisture changes to any state for 5 seconds
I do not want to stop this testing. But may be as a short term solution change GPIO2 depending on moisture level in the Moisture sensor configuration and monitor the GPIO status as trigger?

Does your sensor go unavailable in Home Assistant when the ESP board is asleep?

If so, then this trigger:

Will trigger 5 seconds after the board wakes up. Assuming the moisture level stays constant for that long.

When sending the moisture level is there a lot of noise?

That could prevent the trigger from working.

Soil moisture does not change that rapidly. Perhaps you need some filtering.

Share your sensor config.

Automation. The config for the sensor in the next post.

alias: Garden Watering Sensor 2
description: ""
triggers:
  - trigger: state
    entity_id: sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
    to: null
conditions:
  - condition: numeric_state
    entity_id: sensor.garden_moisture2_soil_moisture
    below: 50
actions:
  - type: turn_on
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: b645d336a4df1711d8250
    entity_id: 7ee81ee2dea33225d5132
    domain: switch
mode: single
max_exceeded: silent

Sensor config. I’ll grab logs from sensor to see how if data lost because of the noise. On the subject of the sensor The LED that is connected to GPIO2 is ON. But HA Dashboard show it is OFF. I’m new to HA and suspect it is because it is defined as a “switch”?

web_server:
  port: 80    

captive_portal:

deep_sleep:
  run_duration: 2min
  sleep_duration: 5min

sensor:
  - platform: adc
    unit_of_measurement: "%"
    icon: "mdi:flower-outline"
    accuracy_decimals: 0
    pin: A0
    filters:
    - calibrate_linear:
        - 0.4 -> 100
        - 0.7 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    name: "Soil Moisture"
    update_interval: 10s    

# Turns LED on GPIO02 On when Wake
switch:
  - platform: gpio
    pin: GPIO2
    name: "Wake LED"

No it is because of how you have wired it. 3.3V or 5V to LED. LED to GPIO. The LED will only be on when the GPIO is low (off). You can invert the logic in the pin definition or rewire The LED. GND to LED, LED to GPIO, with a current limiting resistor of course.

Your sensor updates every 10 seconds. So it should trigger fine. When the device wakes up and seconds the first value it will be the same value for 10 seconds. So the 5 second hold time in your trigger does nothing. It will always hold the same value for 10 seconds minimum.

The LED is between GPIO2 and GND with limiting R. So GPIO2 is HI which I expect will show the LED ON on the dashboard. In this case Garden moisture3 sensor is always ON, physical LED is ON but the Dashboard show associated LED OFF. I’m confused. :frowning:

The hold time in the automation was changed to 30 seconds, thought I was not expecting the actual moisture to change even in an hour. Still looks like the automation would trigger only after HA full reload. :frowning:

You have no automation in ESPHome to turn this on.

Simply defining the LED is not enough.

Use an on_boot trigger to define an action to turn it on when the deivce is awake.

Yes the garden-moisture2 is a sleep now

Then when it wakes up and changes from unavailable to some value this will always trigger:

  - trigger: state
    entity_id: sensor.garden_moisture2_soil_moisture
    for:
      hours: 0
      minutes: 0
      seconds: 5
    to: null

The to: null monitors for all state changes (but ignores attribute changes).

Well, in my case it will trigger just ones after HA restart. Reading on forums look like some users dealing with situations when a sensor status reported in ESPHome Builder is not the same as reported on Dashboard. I’m experience similar: 3 identical sensors, same config yaml, but one would never reported unavailable in Dashboard but checking ESPHome it is reported offline for extended period of time. Just FYI, my work around was to configure a GPIO High on_boot and trigger automation on it. At the end of the automation GPIO set Low. To clear it status in HA before sensor go in to sleep.

Hi, Tom, I’m still dealing with inconstant behavior of the Entities. I’m turning my GPIO high on_boot and it may stay this way in HA even when sensor is in sleep. I can not find in documentation that describes how to switch the GPIO Low after let’s say 120sec. Hope the correct status will be reported by HA before sensor go in sleep for the next boot.
Hope to make this work before I’m away and figure out inconsistencies with Dashboard when return back from travel.
Thank you

esphome:
  name: garden-moisture2
  friendly_name: garden-moisture2
  on_boot:
    then:
      - switch.turn_on: led

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxx"

ota:
  - platform: esphome
    password: "xxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxxx"
    password: "xxxx"

#web_server:
#`Preformatted text`  port: 80    

captive_portal:

deep_sleep:
  run_duration: 3min
  sleep_duration: 240min

sensor:
  - platform: adc
    unit_of_measurement: "%"
    icon: "mdi:flower-outline"
    accuracy_decimals: 0
    pin: A0
    filters:
    - calibrate_linear:
        - 0.4 -> 100
        - 0.7 -> 0
    - lambda: if (x < 1) return 0; else return (x);    
    name: "Soil Moisture"
    update_interval: 10s    

# Turns LED on GPIO02 On when Wake

switch:
  - platform: gpio
    pin: GPIO4
    name: "Wake LED"
    id: led

Meanwhile I created Ping sensors for some moisture sensors. And for example,Ping sensor 3 status is disconnected but dashboard show Moisture sensor3 available. I’ll try to trigger the automation on these Ping sensors.

just an update, for community benefit, using independent sensor to trigger automation works well. In my case Ping sensor. Other issues still present, my HA has latest updates.