I have tried to rescript for openhab 4.2.0 a javascript for simple timercreation/retrigger as long the device draws current und switch off if not.
var now = time.ZonedDateTime.now();
// Function to run when the timer goes off.
function switch_off () {
console.info('WaschOFF timer run out Washingmachine switched off now');
items.getItem('Keller_WaschmaschineOnOff').sendCommandIfDifferent('OFF');
}
//create timer if not yet done
if (cache.shared.exists('WMTimer') === false || cache.shared.get('WMTimer').hasTerminated()) {
cache.shared.put('WMTimer', actions.ScriptExecution.createTimer('WMTimer', now.plusMinutes(12), switch_off, ));
console.info('WaschOFF timer created');
}
//reschedule if timer active and washingmachine still running
else if (cache.shared.get('WMTimer').isActive()&& items.getItem("Strom_Waschmaschine").numericState<0.300){
cache.shared.get('WMTimer').reschedule(now.plusMinutes(6));
console.info('WaschOFF timer rescheduled');
}
else {
console.info('WaschOFF timer NOT rescheduled');
}
Im seeing the logfile:
2024-08-01 18:13:40.434 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer created
2024-08-01 18:13:44.200 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:15:26.879 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer created
2024-08-01 18:15:35.480 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:15:43.607 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:18:01.537 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer created
2024-08-01 18:18:13.829 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:18:21.633 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:18:29.968 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer rescheduled
2024-08-01 18:19:40.948 [INFO ] [nhab.automation.script.ui.c283d3976d] - WaschOFF timer created
My question is why it is creating multiple timer and does not remember that there is one existing which it could reschedule or not reschedule if no current? I dont understand why it is creating new ones. Could someone give me a hint?