I finally got to toying with this a bit. Copying from the Blockly rule (with which I have no experience, nor much desire to get any :)), I created this rule:
const { rules, triggers, items } = require('openhab');
var OHRT = (() => {
const ohrt = require('openhab_rules_tools');
ohrt.helpers.validateLibraries('4.1.0', '2.0.3');
return ohrt;
})();
rules.JSRule({
name: "countdowntimer-javascript",
description: 'Coutdowntimertest',
triggers: [],
execute: (event) => {
var afteltijdinseconden = 70
if (!cache.shared.exists('MyTimer') || cache.shared.get('MyTimer').hasTerminated()) {
cache.shared.put('MyTimer',
OHRT.CountdownTimer(
'PT' + afteltijdinseconden + 'S',
() => {
cache.shared.put('MyTimer', null);
},
'countdowntestitem',
'MyTimer'));
}
}
});
This works. And (for future reference) I also found a way to interrupt and reset countdowntestitem (although the latter is maybe redundant…):
const { rules, triggers, items } = require('openhab');
var OHRT = (() => {
const ohrt = require('openhab_rules_tools');
ohrt.helpers.validateLibraries('4.1.0', '2.0.3');
return ohrt;
})();
rules.JSRule({
name: "countdowntimer-stoppen-javascript",
description: 'Coutdowntimertest-stoppen',
triggers: [],
execute: (event) => {
var afteltijdinseconden = 70
var countdowntestiteminregel = items.getItem("countdowntestitem")
function timerResetten() {
countdowntestiteminregel.sendCommand(afteltijdinseconden)
}
if (cache.shared.exists('MyTimer')) {
console.log("Timer loopt...")
cache.shared.get('MyTimer').cancel()
setTimeout(timerResetten, 1000)
}
}
});
I assume I can put both in the same rule, and then work with a private cache…
One thing I don’t get, is why countdowntestitem displays the time in 0.0 (seconds):
I would expect the HH:MM:SS format, based on this: openHAB Rules Tools [4.1.0.0;5.9.9.9]?
