Fix typo and examples for cache (#2439)
* Fix typo for RuleDSL sharedCache Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au> * Minor adjustments in cache example Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au> * Explain cached timer's automatic cancellation Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au> --------- Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>pull/2488/head^2
parent
048bc7ed6b
commit
54bafbb3b1
|
@ -485,7 +485,7 @@ sharedCache.remove("x")
|
||||||
```java
|
```java
|
||||||
sharedCache.put('foo', 'bar')
|
sharedCache.put('foo', 'bar')
|
||||||
sharedCache.get('foo') // returns null if doesn't exist
|
sharedCache.get('foo') // returns null if doesn't exist
|
||||||
shareCache.put('foo', null) // deletes the entry
|
sharedCache.remove('foo') // deletes the entry
|
||||||
```
|
```
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
|
@ -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).
|
(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.
|
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.
|
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.
|
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.
|
We will use it to save that Timer so we can reschedule it later when needed.
|
||||||
|
@ -150,27 +150,30 @@ We will use it to save that Timer so we can reschedule it later when needed.
|
||||||
console.info('Motion was detected');
|
console.info('Motion was detected');
|
||||||
items.getItem('FrontPorchLight').sendCommand('ON');
|
items.getItem('FrontPorchLight').sendCommand('ON');
|
||||||
|
|
||||||
timerId = ruleUID+'_timer';
|
timerId = 'FrontPorchLight_timer';
|
||||||
var lightsOut = function() {
|
var lightsOut = function() {
|
||||||
console.info('No more motion, turning off the light');
|
console.info('No more motion, turning off the light');
|
||||||
items.getItem('FrontPorchLight').sendCommand('OFF');
|
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) {
|
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 {
|
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.
|
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.
|
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.
|
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.
|
Save and test that the rule sends the on and off commands as described.
|
||||||
|
|
||||||
### But only if: Conditions
|
### But only if: Conditions
|
||||||
|
|
Loading…
Reference in New Issue