From 3ab7138157dc94dd5093976474250323edd92e18 Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Wed, 1 Jan 2025 13:21:41 +1000 Subject: [PATCH 1/3] Fix typo for RuleDSL sharedCache Signed-off-by: Jimmy Tanagra --- configuration/jsr223.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration/jsr223.md b/configuration/jsr223.md index 92444cf854..ebde311197 100644 --- a/configuration/jsr223.md +++ b/configuration/jsr223.md @@ -485,7 +485,7 @@ sharedCache.remove("x") ```java sharedCache.put('foo', 'bar') sharedCache.get('foo') // returns null if doesn't exist -shareCache.put('foo', null) // deletes the entry +sharedCache.remove('foo') // deletes the entry ``` ::: From 30d80fb4f54666242b28a4fb2d54d25c0cfe597f Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Wed, 1 Jan 2025 13:22:35 +1000 Subject: [PATCH 2/3] Minor adjustments in cache example Signed-off-by: Jimmy Tanagra --- tutorials/getting_started/rules_advanced.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tutorials/getting_started/rules_advanced.md b/tutorials/getting_started/rules_advanced.md index aea5038094..3bf1d2cd48 100644 --- a/tutorials/getting_started/rules_advanced.md +++ b/tutorials/getting_started/rules_advanced.md @@ -141,7 +141,7 @@ Save and test that you see the log statement and the Item receive the `ON` comma (hint, change the time passed to the timer to something smaller to make testing easier then change it back once things are working). Now all we are lacking is the ability to reschedule that timer if motion is seen again in the 30 minute period. -Looking back at the docs we find the [`cache`](/addons/automation/jsscripting/#cache). +Looking back at the docs we find the [cache](/addons/automation/jsscripting/#cache). This is a map of key/value pairs that exists outside of the rule. Given that position it is able to share data between different rules or between runs of the same rule. We will use it to save that Timer so we can reschedule it later when needed. @@ -150,23 +150,22 @@ We will use it to save that Timer so we can reschedule it later when needed. console.info('Motion was detected'); items.getItem('FrontPorchLight').sendCommand('ON'); -timerId = ruleUID+'_timer'; +timerId = 'FrontPorchLight_timer'; var lightsOut = function() { console.info('No more motion, turning off the light'); items.getItem('FrontPorchLight').sendCommand('OFF'); - cache.put(timerId, null); + cache.private.put(timerId, null); }; -var timer = cache.get(timerId); +var timer = cache.private.get(timerId); if(!timer) { - cache.put(timerId, ScriptExecution.createTimer(time.ZonedDateTime.now().plusMinutes(30), lightsOut)); + cache.private.put(timerId, ScriptExecution.createTimer(time.ZonedDateTime.now().plusMinutes(30), lightsOut)); } else { - timer.reschedule(time.ZonedDateTime.now()); + timer.reschedule(time.ZonedDateTime.now().plusMinutes(30)); } ``` -Notice that we use the `ruleUID` which is a variable made available by the Helper Library to ensure that we don't overwrite on something added to the `cache` from another rule. Also notice a line was added to `lightsOut` to delete the entry in the `cache` when the timer ran. That will cause the rule to create a new timer the next time the rule runs. It could be coded to reuse the Timer instead which is an exercise for the reader. From 2842fc6d158c53f21fcc96e3f14d5a987753ed79 Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Wed, 1 Jan 2025 13:51:07 +1000 Subject: [PATCH 3/3] Explain cached timer's automatic cancellation Signed-off-by: Jimmy Tanagra --- tutorials/getting_started/rules_advanced.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tutorials/getting_started/rules_advanced.md b/tutorials/getting_started/rules_advanced.md index 3bf1d2cd48..cd373ea949 100644 --- a/tutorials/getting_started/rules_advanced.md +++ b/tutorials/getting_started/rules_advanced.md @@ -170,6 +170,10 @@ Also notice a line was added to `lightsOut` to delete the entry in the `cache` w That will cause the rule to create a new timer the next time the rule runs. It could be coded to reuse the Timer instead which is an exercise for the reader. +Another benefit of saving the timer in the `cache` is that it will be cancelled when the last script that references the cache is unloaded or reloaded. +This will prevent the timer from executing after the original script had been removed or reloaded. +Care must still be taken within the timer function not to reschedule itself if it has been cancelled. + Save and test that the rule sends the on and off commands as described. ### But only if: Conditions