Good {zoned time of day} Developers,
I was reading through the Rules-Advanced page in the Documentation site and came across an example showing how to use the “cache” feature and the createTimer function of “ScriptExecution” Here is the example I am referencing:
console.info('Motion was detected');
items.getItem('FrontPorchLight').sendCommand('ON');
timerId = ruleUID+'_timer';
var lightsOut = function() {
console.info('No more motion, turning off the light');
items.getItem('FrontPorchLight').sendCommand('OFF');
cache.put(timerId, null);
};
var timer = cache.get(timerId);
if(!timer) {
cache.put(timerId, ScriptExecution.createTimer(time.ZonedDateTime.now().plusMinutes(30), lightsOut));
}
else {
timer.reschedule(time.ZonedDateTime.now());
}
When I used this code for my Stairway Lights, I got 2 errors. The first error complained about the cache.put(timerId, null); line. I solved it with editing the line to "cache.private.put(timerId, null)
Then the second error appeared. It complained about the ScriptExecution section of this line:
cache.private.put(timerId, ScriptExecution.createTimer(time.ZonedDateTime.now().plusMinutes(30), lightsOut));
(I already corrected the cache statement to include .private). I solved this by adding the prefix “actions.” to it, i.e. “actions.ScriptExecution.createTimer…” After that the rule ran fine.
I saw the notice at the bottom of the page asking if I caught a mistake and a link to the GitHub page. However I am so far away from being an expert on javascript that I didn’t want to risk saying something wrong, messing up the documentation, etc. So I thought I would quietly bring it up here. Of course, if I am wrong with any of the above then please ignore everything I said. I will still continue to read, learn, and trust the Advanced Rules documentation, its a great resource.