Automation GPIO Switch

Hello, good day,
I have created an automation in which a GPIO switch is switched on when a threshold value is exceeded. Everything works as hoped.

Is there a way to determine how long this GPIO switch remains switched on, or do I have to switch it off again via automation (the trigger would then be the start of this automation).

Although you can set how long the threshold must remain exceeded for the action to start, you cannot set how long the GPIO switch stays on. At least I haven’t seen such an attitude.

My wish in this regard is that the GPIO switch remains switched on for a maximum of 2 seconds and then is LOW again.

Thank you very much in advance!

Is this GPIO entity from ESPHome?

You can set a delay in the automation actions, although this is not ideal as it can stop counting down if rebooted or some such like. You can use a timer helper and trigger that that to count down the time and then action whatever when it finishes. You could no doubt also do this with a template but I don’t know how or why you would.

For reliable turn-off you should do the automation in the gpio device itself.
In case of esphome add this to your switch config:

    on_turn_on:
      - delay: 2000ms
      - switch.turn_off: my_switch # id of your actual switch

yes, i use a wemos d1mini esp8266

thank you, i will throw an eye on it…

thank you. I will try that and report later.

OK, it’s harder than I thought.
First of all:
I simulate moisture by touching the soil moisture sensor between thumb and index finger → value: ~1,180V. If I let it go I get a value of ~2.108V. For testing purposes, I then set the threshold to 1,300. Therefore, the trigger is initiated when the threshold value is undershot.

Explanation:
The automation is supposed to start a 5V pump, which then pumps water to irrigate a plant.
However, since it would take too long to wait for the water to reach the sensor, I only want to water for 2 seconds, and if the sensor value is still below the threshold value the next time it is polled, then it will water for another 2 seconds, and so on …
I thought my automation (now including delay, thanks, works great) would do this exactly.

Unfortunately it doesn’t. If the threshold value is undercut for the first time during a query, the pump runs for 2 seconds. So far so good. If the sensor value falls below the threshold value in the subsequent query, then nothing happens. The pump does not start even with further queries. I hold the sensor between my thumb and forefinger the whole time.
If I let go of the sensor, a value of 2,196 is displayed, which is also good. If I then touch the sensor again, it falls below the threshold value and the pump runs - only once.

It looks like the threshold needs to be exceeded again before the automation starts again. This is not good.

How do I get the pump to run for 2 seconds for EVERY query that falls below the threshold?

Thank you very much in advance!

Trigger on any change of the sensor and build the parameters in the condition section.
Or in the action section with choose statements.
Avoid delay because if HA is reset in the middle of that, it might leave the pump on forever.
(Might) want a second watchdog automation that triggers when the thing is on say over 4 seconds or whatever is appropriate so that there is a backup. That would be a trigger. If things are going right, that is never used. I would also add a notify in that automation so that if it actually triggers, it lets you know there is a problem.

hi, thank you.
uff… a lot stuff for a little thing. Why the automation only starts once?
which parameters in the condition section ? You mean the threshold parameter?

what instead of delay ?

You mean a notification for the watchdog automation ?

Many thanks in advance !

Here’s a cookbook link to some sections for suggested reading. The Home Assistant Cookbook - Index

I assume this is something specific to your needs. Is the idea to slowly water (over and over) until it’s watered enough based on the sensor?

I would approach it as “trigger when it needs water” i.e. when the sensor is low then run the pump some amount of time to make sure it’s “watered”. That’s what I do manually - stick my finger in the soil and if dry dump some water on it. Repeat in a few days.

Otherwise you have to time poll, and that always feels odd to me in HA vs. triggering on an event.

It’s impossible to help if you don’t post your code. I don’t even know if your automation is on esphome or HA.

In case you use esphome sensor component with on_value_range, the behavior is exactly like you described. It triggers only when it enters the range.
In that case you need to use on_value automation with conditions.

Hello,
specifically tailored to my needs, hmm, I don’t think so.
There are many reasons why I want to do it this way (2 second pour).
Of course I can test how long the pump has to run to pump exactly the amount of water, but why?
I just wanted to check the humidity every 10 minutes. If the threshold is exceeded, please water as often as necessary for 2 seconds.
I don’t want to water the neighbors’ heads :wink:

# Board: Wemos D1 Mini (ESP8266) (Wemos)
# Definition: definitions/boards/d1_mini/manifest.yaml

esphome:
  name: esp-8266d1-ip150
  friendly_name: ESP-8266D1-IP150

esp8266:
  board: d1_mini

logger:

api:
  encryption:
    key: "qNBzL2JaA5grGIhqcyWJuq+WZtr2wlabpWhE6qzw="

ota:
  - platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  domain: ".local"
  power_save_mode: none
  manual_ip:
    static_ip: 192.168.178.150
    gateway: 192.168.178.1
    subnet: 255.255.255.0
    dns1: 192.168.178.1
    dns2: 192.168.178.1
  ap:
    ssid: ESP-8266D1-IP15 Fallback Hotspot
    password: f4B428D5SSSZ1CC
captive_portal:

i2c:
  - id: i2c_1
    scl: 5
    sda: 4

ads1115:
  - id: ads1115_1
    address: "0x49"
    i2c_id: i2c_1
    continuous_mode: true

sensor:
  - platform: ads1115
    name: kapazitiver Bodenfeuchtesensor ADS1115_1-A0
    id: sensor_ads1115_1_A0
    gain: "4.096"
    multiplexer: A0_GND
    update_interval: 10s
    force_update: true
switch:
  - platform: gpio
    name: GPIO Switch IP150-D5
    id: gpio_switch_ip150_d5
    pin: GPIO14
    on_turn_on:
      then:
        - delay:
            milliseconds: '2000'
        - switch.turn_off: gpio_switch_ip150_d5

So your automation is on HA.
But you could bake it directly to esphome (if desired).

It’s my first automation and i followed the docs - more or less…
i build the automation in the automation section of HA and i configure it over esphome builder.
is it the only solution to build this automation directly in esphome ?
Only two seconds if the threshold is underbiten… every check

triggers the option force_update: true a state change ?

Of course not. But it’s the most straightforward and failproof solution.

    on_value:
      then:
        - if:
            condition:
              lambda: 'return x < 1.3;'
            then:
              - switch.turn_on: gpio_switch_ip150_d5

thanks.
just a link where i can read about that ?

if you would put your two answers together so i can mark these as solution. if not, only one would get the mark - i think the first one, because it is closer to the title…