JS Scripting: How to use a Timer?

This rule should only fire once with a 10 sec block.
Therefore i use a Timer inside the DSL rule, which is working without problems. After 10 sec the rule can fire again.
Is something like that also possible with JSScripting?
my current DSL Rule looks like this:

rule "Klingel Rundruf"

when

Item Sonoff16_Sensor changed from OFF to ON

then

    if(klingel_sperre ===  null){

        // Klingelsperre für 10s; now.plusMinutes für Minuten

        klingel_sperre = createTimer(now.plusSeconds(10), [ |

            klingel_sperre = null

        ])

        val telegramAction = getActions("telegram","telegram:telegramBot:raspi_telegram")

        telegramAction.sendTelegramPhoto((Long.decode(Telegram_familie.state.toString)), "http://XXX.XXX.X.XX:XX/snapshot.jpg", "Es klingelt!", "XXXX", "XXXX")

        // Fritzbox Rundruf mit XXXXXX

        executeCommandLine("/etc/openhab/scripts/klingel.sh")

    }
end

Yes, of course. See JavaScript Scripting - Automation | openHAB for where to store your Timer variable so you can get it next time the rule runs (note that all your JS Scripting rules will use the same cache so choose names wisely). See JavaScript Scripting - Automation | openHAB for examples of creating timers. Use the following to get to your telegram Thing’s actions.

var telegramAction = actions.thingActions("telegram","telegram:telegramBot:raspi_telegram");

It’s easier to use a timestamp. Just save a timestamp and check to see if it’s been enough time. You’d still use the cache but save a JavaScript joda Date there instead of a Timer. Then instead of testing to see if the timer exists, just test to see if now is enough time past when the timestamp was taken.

It would be best to put that test inside a Condition. If in the UI create a new Condition under “but only if”, choose “Run script” and just add the test there (no if). See JavaScript Scripting - Automation | openHAB for details.

If using text based rules you can put the variable at the top of your file outside of the rules or continue to use the cache. See JavaScript Scripting - Automation | openHAB for details on adding conditions in text based rules.

See JavaScript Scripting - Automation | openHAB for how to call executeCommandLine.

1 Like

Thank you very much for your great help.
I am going to switch with JSS over from file based to UI.
Can we already use “let” counter… in the UI without any errors?
How can we use that cache? Sorry for that question but i am completely new to JSS?
A counter is any choosable name of a “cache” variable or does it belong to the cache? What is “times”? Is that the name of the variable? I do not really know what belongs to my saved variable. Can you make it more clear to me?
I need a variable “timestamp”, which should get saved when the rule is fired for the first time. My only if condition would be: now() > timestamp+10sec.

These are the examples of the JSS Page:
Get a previously set value with a default value (times = 0)

let counter = cache.get("counter", () => ({ "times": 0 }));
console.log("Count",counter.times++);

Get a previously set object

let counter = cache.get("counter");
if(counter == null){
     counter = {times: 0};
     cache.put("counter", counter);
}
console.log("Count",counter.times++);

No

That’s just an example. The cache is a Map. Key/value pairs.

That line is using an anonymous function (denoted by =>) to provide a default value when trying to get the counter value from the cache.

That saves the value of counter to the cache using the key/name "counter".

That retrieves the value saved under the key/name "counter". If no entry exists with the key “counter”, null is returned.

In this example, counter is a dict containing a times variable.

let counter = cache.get("counter", () => ({ "times": 0 }));

will return an Object with times == 0 instead of null when there is no entry with the key/name “counters”. That one line replaces

let counter = cache.get("counter");
if(counter == null){
     counter = {times: 0};
     cache.put("counter", counter);
}

Again, in the UI you can’t use let.