tom14
(Tom)
August 23, 2022, 11:52am
1
Hi,
I habe general beginner question.
I need a button/switch on/off, after it is on the water irrigation program starts. I have 6 valves thats then needs to be activates one after another.
valve_1: 50s, valve_2: 35s, valve3_120s, etc.
If the switch will be off, then the programm stops working.
Can it be realized with openhab3?
Regards
Hardware: Raspberry / Docker
OS: Raspberry / Docker
openHAB version: 3.3.0
You could create a timer for each device and activate the next timer once the previous one has finished.
Creating a timer as an item goes like this:
Switch a_Timer {expire="5s,command=OFF"}
You activate it like that:
a_Timer.sendCommand(ON)
You react to the timer having finished (like here 5 seconds) as follows:
rule „Timer finished“
when Item a_Timer received command OFF
then
Valve1.sendCommand(ON)
b_Timer.sendCommand(ON)
end
By doing so you could cascade through all devices that need to be switched on.
You can also work with other values in the expires
attribute. A timer for 1 minute takes 1m
as attribute and so on.
Does it make sense?
Hello Tom, welcome to the community.
Please have a look here:
Please see Design Pattern: What is a Design Pattern and How Do I Use Them .
This Design Pattern is a revisitation of a solution I wrote up here .
Problem Statement
There are many situations where one needs to cause a series of activities to take place in sequence with the option of being able to cancel the sequence of activities and be able to control how long each step takes to complete. One common use case is for controlling an irrigation system that consists of more than one zone.
Concept
…
tom14
(Tom)
August 23, 2022, 1:57pm
4
Hi,
thank you for your concepts / ideas. I’m sure both should work. It is in the first look not to easy to understand… Is there an another way to use the capabilities of the JavaScript to just start a thread as a background job that will start stop the valves in the serie? Can the thread cabability of javascript be used in the context of Openhab?
tom14
(Tom)
August 23, 2022, 6:34pm
5
I have experimented and have implemented a solution with clean JavaScript. It creates timers to start and finish each valve separatelly. Everything is in one script. If I stop the task (switch off) the planned timers will be calceled by a watchdog timer. It works preaty stable It could be more optimised using loops and arrays, but for now it works.
var SE = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZDT = Java.type("java.time.ZonedDateTime");
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");
var mytimers = []
var mynow = ZDT.now();
var timerIndex = 0;
logger.info("Init Valves task...");
function myTimerWatcherCheckFunc() {
logger.info("TimerWatcher check...");
state = itemRegistry.getItem("IrrigationController_StartSequence_1").getState() == "ON";
if (state) {
SE.createTimer(ZDT.now().plusSeconds(1), myTimerWatcherCheckFunc);
}
else {
for (timerItemIndex in mytimers) {
timerItem = mytimers[timerItemIndex];
logger.info(timerItem);
if (timerItem !== undefined && !timerItem.hasTerminated()) {
timerItem.cancel();
}
}
events.sendCommand("IrrigationController_Valve_5", "OFF");
events.sendCommand("IrrigationController_Valve_11", "OFF");
events.sendCommand("IrrigationController_Valve_4", "OFF");
events.sendCommand("IrrigationController_Valve_8", "OFF");
}
}
mynow = mynow.plusSeconds(1);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T1 activated...");
events.sendCommand("IrrigationController_Valve_5", "ON");
});
mynow = mynow.plusSeconds(83);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T1 finished...");
events.sendCommand("IrrigationController_Valve_5", "OFF");
});
mynow = mynow.plusSeconds(3);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T2 activated...");
events.sendCommand("IrrigationController_Valve_11", "ON");
});
mynow = mynow.plusSeconds(126);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T2 finished...");
events.sendCommand("IrrigationController_Valve_11", "OFF");
});
mynow = mynow.plusSeconds(3);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T3 activated...");
events.sendCommand("IrrigationController_Valve_4", "ON");
});
mynow = mynow.plusSeconds(66);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T3 finished...");
events.sendCommand("IrrigationController_Valve_4", "OFF");
});
mynow = mynow.plusSeconds(3);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T4 activated...");
events.sendCommand("IrrigationController_Valve_8", "ON");
});
mynow = mynow.plusSeconds(285);
mytimers[timerIndex++] = SE.createTimer(mynow, function() {
logger.info("T4 finished...");
events.sendCommand("IrrigationController_Valve_8", "OFF");
events.sendCommand("IrrigationController_StartSequence_1", "OFF");
});
var myTimerWatcherCheck = SE.createTimer(ZDT.now().plusSeconds(1), myTimerWatcherCheckFunc);