Hello,
I’m trying to develop a rule using some timers. In my case I have to stop a timer1 (rainSTARTscheduleDelayTimer) from timer2 (rainSTOPscheduleDelayTimer), if timers 1 exists. This is my code:
'use strict';
var log = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var doLog = true;
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
/*
* Stop mower when it starts to rain
*/
this.rainSTOPscheduleDelayTimer = (this.rainSTOPscheduleDelayTimer===undefined) ? null : this.rainSTOPscheduleDelayTimer;
if (event.itemState == "OPEN" && ir.getItem("GardenaRobart_Status").getState() == "OK") {
//chancel rainSTOPscheduleDelayTimer if this rule is retriggered
if(this.rainSTOPscheduleDelayTimer !== null){
this.rainSTOPscheduleDelayTimer.cancel();
this.rainSTOPscheduleDelayTimer = null;
}
//start rainSTOPscheduleDelayTimer for 5 minutes
this.rainSTOPscheduleDelayTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(5), function(){
if (ir.getItem("KNXSteuerAussRegen").getState() == "OPEN" ) {
//stop rainSTARTscheduleDelayTimer if it exists
if(this.rainSTARTscheduleDelayTimer !== null){
this.rainSTARTscheduleDelayTimer.cancel();
this.rainSTARTscheduleDelayTimer = null;
}
events.sendCommand("GardenaRobart_ZeitplanPausieren", "ON")
}
});
}
/*
* Continue mower after rain
*/
this.rainSTARTscheduleDelayTimer = (this.rainSTARTscheduleDelayTimer===undefined) ? null : this.rainSTARTscheduleDelayTimer;
if (event.itemState == "CLOSED" && ir.getItem("GardenaRobart_Status").getState() == "OK") {
//chancel rainSTARTscheduleDelayTimer if this rule is retriggered
if(this.rainSTARTscheduleDelayTimer !== null){
this.rainSTARTscheduleDelayTimer.cancel();
this.rainSTARTscheduleDelayTimer = null;
}
//start rainStartscheduleTimer if there is no rainSTOPscheduleDelayTimer
if(this.rainSTOPscheduleDelayTimer == null){
this.rainSTARTscheduleDelayTimer= ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(120), function(){
events.sendCommand("GardenaRobart_ZeitplanStarten", "ON")
}
}
else {
if (doLog) log.info("Der rainStopscheduleDelayTimer läuft noch, rainStartscheduleTimer wird nicht gestartet");
}
}
In one case I want to stop the “rainSTARTscheduleDelayTimer” if it exists and when the 5 minutes waittime of “rainSTOPscheduleDelayTimer” are over. In the second case I wand to start the “rainSTARTscheduleDelayTimer” if there is no “rainSTOPscheduleDelayTimer”
If I try to stop the “rainSTARTscheduleDelayTimer” if it exists and when the 5 minutes waittime of “rainSTOPscheduleDelayTimer” are over I get the following exception:
2022-05-27 13:45:03.295 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job failed and stopped
jdk.nashorn.internal.runtime.ECMAException: TypeError: Cannot read property "rainSTARTscheduleTimer" from undefined
Does anyone know, what I am doing wrong?
Cheers,
kuczerek