Hi,
running OH3.2.0 I try to dynamically create timers for group members with blockly, but it is not working as expected:
Situation:
I have a group with items representing window open state.
Whenever a window item from this group changes from CLOSED to OPEN I want to start a timer, that will remind me to close the window.
Of cause I do not want to have one rule (or one expire item) per window, but I only want to have one rule for the entire group.
Whats working:
I have created a rule, triggered by “state of a member of an item group changes”.
I have created a blockly script that will do the following:
if new state == OPEN, than create a timer
If new state == CLOSED, cancel the timer
Once the timer is running, it will send a notification and reschedule.
The rule is working perfect if I just open one window
Whats not working:
As in blockly I can also give the timer a name, if have setup a dynamic generated name as: triggeringItemName+"_timer"
Expecting that I can have multiple timers running in parallel. However when opening multiple windows at the same time (e.g. first opening one window, than while first window is still open, I will open a second window and expectation is to have two timers & receiving two notifications) the script is not working as expected.
Based on the logs I see the following:
Window 1 changes from CLOSED to OPEN
Timer for window 1 is scheduled
Window 2 changes from CLOSED top OPEN
Timer for windows 2 is scheduled
Both windows will remain open, but only timer for window 2 is executed. Once the 2nd (or 3rd…) timer is scheduled, any previous timers created by the same rule (even with different timer name) are not executed anymore.
Question: Is this expected? Whats the purpose of giving the timer a name, if it will always get overwritten?
Code generated by blockly:
var intialDuration, rescheduleDuration, notificationText;
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var zdt = Java.type('java.time.ZonedDateTime');
if (typeof this.timers === 'undefined') {
this.timers = [];
}
intialDuration = 10;
rescheduleDuration = 5;
if (event.itemState == 'OPEN') {
logger.info((String(event.itemName) + '_Timer scheduled'));
if (typeof this.timers[(String(event.itemName) + '_Timer')] === 'undefined' || this.timers[(String(event.itemName) + '_Timer')].hasTerminated()) {
this.timers[(String(event.itemName) + '_Timer')] = scriptExecution.createTimer(zdt.now().plusMinutes(intialDuration), function () {
logger.info((String(event.itemName) + '_Timer running'));
//send notificaiton - code removed
logger.info((String(event.itemName) + '_Timer reschedule'));
if (typeof this.timers[(String(event.itemName) + '_Timer')] !== 'undefined') { this.timers[(String(event.itemName) + '_Timer')].reschedule(zdt.now().plusMinutes(rescheduleDuration)); }
})
}
} else if (event.itemState == 'CLOSED') {
logger.info((String(event.itemName) + '_Timer cancel'));
if (typeof this.timers[(String(event.itemName) + '_Timer')] !== 'undefined') {
this.timers[(String(event.itemName) + '_Timer')].cancel();
this.timers[(String(event.itemName) + '_Timer')] = undefined;
}
}
Log:
2022-01-03 10:21:49.400 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGBadezimmerFenster_Contact_Timer scheduled
2022-01-03 10:21:57.186 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGSchlafzimmerFenster_Contact_Timer scheduled
2022-01-03 10:31:49.606 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGSchlafzimmerFenster_Contact_Timer running
2022-01-03 10:31:49.780 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGSchlafzimmerFenster_Contact_Timer reschedule
2022-01-03 10:34:15.678 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGSchlafzimmerFenster_Contact_Timer cancel
2022-01-03 10:34:23.533 [INFO ] [org.openhab.rule.b49c1e12c3 ] - OGBadezimmerFenster_Contact_Timer cancel
Thanks in advance for any help