Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typo and examples for cache #2439

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configuration/jsr223.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

:::
Expand Down
17 changes: 10 additions & 7 deletions tutorials/getting_started/rules_advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
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.

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
Expand Down
Loading