Duration in Scheduler

Hello, I have a problem where I have a pump connected to a relay and a soil moisture sensor. I want the program to work so that when I enter the day and time and the soil moisture is below a certain level, the pump turns on for, say, 10 seconds. However, the 'duration' option in the widget doesn’t work at all – sometimes it lasts for a second, sometimes 10, with different times. Also, the pump does not turn off when the desired moisture level is reached; it just keeps running. Here’s a piece of the code responsible for this. What do you think?

// Setting the soil moisture threshold to activate pump no.1 and checking if automatic mode is selected
if (modeSelection == 1) {
    if (schedule.isActive()) {
        if (sensorMoistureVariable1 < soilMoistureSensor1) {
            digitalWrite(Pump1, HIGH); // pump 1 turned on
            pumpStatus1 = true;
        } else {
            digitalWrite(Pump1, LOW); // pump 1 turned off
            pumpStatus1 = false;
        }
    }
} else {
    Serial.println("Select automatic mode!");
}

Can you show the "duration" option? Can you show your complete sketch?

Probably this is due to not having a "hysteresis" in the code... The small bit of code you showed, the pump turns on if the sensorMoistureVariable1 is less than some other confusing named variable with "sensor" in it. When these two values are very close, the slightest change will cause the pump to turn on or off... for 10 seconds or one second.

You will want the pump to turn on when the condition is met, but off only when the condition is lower than normal. Using numbers...

(consider "50" to be "normal"... where your pumps are now turning on/off/on/off)

if (moisture > 50) pump ON (turn on when needed)
if (moisture < 40) pump OFF (lower than normal

"Duration" is not a variable I introduced in the code; it’s part of the schedule.isActive function because this is a widget from the schedule. However, the "duration" option in the schedule does not work properly. As for the conditions, I want the pump to turn on only when two conditions are met: when the schedule is active and when the moisture level is below a certain threshold. In the current situation, however, the pump does not turn off when the moisture level is reached, precisely due to this schedule.

if (moisture < threshold && schedule == active) pumpON();

Only you know.