I’ve been working on implementing the Timer Management. So far, I had both successes and failures. What I’m working currently on is switching on 2 lights when, at night time, the frontdoor or backdoor is opened. However, when either or both these two lights are already on, the timer should not be started for that specific light that is already on (winter case where one uses more lights). Else we’ll end up in darkness.
I first tried
var logger =
Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Lights");
scriptExtension.importPreset("default");
this.ZonedDateTime = (this.ZonedDateTime === undefined) ? Java.type("java.time.ZonedDateTime") : this.ZonedDateTime;
// Load TimerMgr
this.OPENHAB_CONF = (this.OPENHAB_CONF === undefined) ? java.lang.System.getenv("OPENHAB_CONF") : this.OPENHAB_CONF;
load(OPENHAB_CONF+'/automation/lib/javascript/community/timerMgr.js');
load(OPENHAB_CONF+'/automation/lib/javascript/community/timeUtils.js');
// Create timer only when there isn't one, create function to xcan timer
this.tm = (this.tm === undefined) ? new TimerMgr() : this.tm;
// Get light states
var wandState = items["koofSpots_gFloor_switch"]; var pilaarState = items["pilaarverlichting_gFloor_switch"];
if(wandState == "OFF") {
events.sendCommand("koofSpots_gFloor_switch", "ON");
this.tm.check("wand", "30s", function() { events.sendCommand("koofSpots_gFloor_switch", "OFF");
}
);
}
The 30s is just a test, or I’ll be waiting too long during the test phase. What I only accomplished in the rule above is: when the light is off (one light for simplicity), it goes on when the rule is triggered and switches off as the timer expires. So this does work.
Now for resetting or rescheduling the timer I tried
...
events.sendCommand("koofSpots_gFloor_switch", "ON");
this.tm.check("wand", "30s", function() { events.sendCommand("koofSpots_gFloor_switch", "OFF");
}
);
...
And this does work as well. Each time an ON command is sent and the timer is rescheduled/reset for another, in this case, 30 seconds.
So I thought I’ll make it into this:
...
if(wandState == "OFF" || tm.hasTimer("wand")) {
events.sendCommand("koofSpots_gFloor_switch", "ON");
this.tm.check("wand", "30s", function() { events.sendCommand("koofSpots_gFloor_switch", "OFF");
}
);
}
...
The idea is that the timer is rescheduled/reset to start again if the door is opened because that same code runs when 1) the light has already gone out or 2) when the timer is active. It also prevents from switching off lights that were on in the first place as the timer would never start. However, somehow this doesn’t work. It looks like the timer is cancelled when the door is opened for a second time, so when the timer has started counting down. The lights will then stay on indefinitely. If I do not open the door during the running timer, it works as advertised and the light goes off in 30s. This tells me that the timer is created on the first run.
Anyone who has a solution for this issue? Or maybe even better. Cleaner code/more elegant or generic way of coding?