It can if you use the cache. But you don’t need two rules and in fact this would be simpler to implement if you didn’t have two rules. Just have the one rule that triggers when the light changes to ON or OFF (two triggers) and determine whether to create the timer or cancel the timer using an if statement in the action. Then the Timer can live in the one rule and the logic is still super simple.
var timer = cache.shared.get('timer');
if(event.itemState == 'ON' && timer !== null) {
timer.cancel();
}
else if(event.itemState == 'OFF' && timer !== null){
timer.reschedule(time.toZDT('PT5S'));
}
else {
cache.put('timer', actions.ScriptExecution.createTimer(time.toZDT('PT5S'), () => {
// do occupancy stuff here
cache.put('timer', null);
}
}
And the above is probably more than you would need. The case where you received OFF but there is already a timer should never happen. I just included it for thoroughness. In Blockly this is super easy because I think putting the timer in the cache and all that book keeping stuff is handled in the timer block automatically. You just need to drop the timer into the script and set the properties.
Ultimately that DP is deprecated because you can just install and configure an implementation of it now. You don’t need to write the code at all. And I don’t intend to ever really go back to it and update it for OH 3 and OH 4 because it is now an installable rule template. That’s what I mean by deprecated. I almost deleted it entirely.
Since all the templates are in the same place, you’ll spend less time investigating those than investigating how to do it if you coded it yourself. It’s never waisted time to review the templates. And even if you choose to implement it yourself, you’ll have a working example with an explanation for how it works to go from in the template.
It’s always a trade off. But there are enough hard problems to solve in OH and home automation. I’d rather make it so OH users can spend their time solving home automation problems, not coding problems. But if you want to solve coding problems (there’s no problem with that) you can of course do that too.
And it’ll still be less time than trying to go at it blindly from scratch.
It’s always good to review the design pattern posts. However, if one is labeled deprecated, that’s because there is an implementation of it in a rule template or in openhab_rules_tools and for the latest explanation for how it works and working code, see that. The rest of the DP post is kept for legacy support.
Feel free to ask questions if you have any. That one in particular is one of the more complicated ones because it has a bunch of extra features (e.g. do not disturb periods, repeated reminders, ec.).
There is a design pattern on state machines (not written by me), an add-on in the marketplace (though that may only handle washing machines) and I’ve a rule template for a time driven state machine.
And just to be thorough, the Debounce rule template could be useful here as well. Instead of needing to manage the Timer yourself in a rule, you can create a proxy Item for the lights and configure Debounce to only update the proxy with OFF when the actual light Item has been OFF for five seconds.
And for examples of using some of my rule templates see How I Use Rule Templates