I’m trying to run first Javascript rules with my OpenHab. I am running version 2.5.
Below are my two rules used for controlling lighting by presence detectors. I already know how to turn light on/off with presence detector but I am struggling with script which will turn the light OFF only when presence in the room will be OFF for “X” minutes. I have created setTimeout function and it turns light OFF with delay but I do not know how to exit this setTimeout function when the presence will again change to ON while counting already started.
var LGT = getItem("Lgt_TV")
var PSC = getItem("PscJadalnia")
logInfo( "Oświetlenie wyłączy się za 5 sekund");
var t = setTimeout(function(){
sendCommand(LGT, "OFF");
if (PSC == "ON") function (){
clearTimeout(t);
}
}, 5000);
I would use core.actions.js to import ScriptExecution, and then use ScriptExecution.createTimer. You may want to look at the JS Timer example. As a basic rule, whenever possible, use Java or OH classes, interfaces and methods rather than what is available natively in your preferred scripting language.