[SOLVED] Rule for delayed actions in OpenHAB3

Hi,

I’m trying to set up a system in my company’s office where a Flic button affixed to the wall on the inside will allow people to lock the door easily as they leave. In order to give them a fighting chance at leaving the office, I want to add a five-second delay to the locking action during rule execution, but no matter what I try, I don’t seem to be able to get said delay into place.

Following the Design Pattern: Motion Sensor Timer example, I now have something like this as my script:

// ECMAScript type
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Motion Timer");
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime"); 
var now = ZonedDateTime.now();

var runme = function(){ 
    // code that executes later goes here
    events.sendCommand('main_door_DoorLock', 'ON')
    this.timer = undefined; // reset the timer back to undefined
}

if(this.timer === undefined) {
    this.timer = ScriptExecution.createTimer(now.plusSeconds(5), runme);
}
else {
    this.timer.reschedule(now.plusSeconds(5));
}

and the Rule Designer has two actions:

  1. Play Sound: a warning klaxon sound from an MP3 on the file system, 4s long
  2. Run Script: the above script verbatim.

However, any time I hit the Flic Button, the sound plays (as it should) and the lock activates immediately, while the sound is playing (as it should not).

I also tried using the Rules DSL as my script type, having this as the script:

createTimer(now.plusSeconds(5), [ |
    events.sendCommand('main_door_DoorLock', 'ON')
])

but either this didn’t run at all or it ran in parallel with the sound again.

Does anyone have any ideas as to what I’m doing wrong?

you are using wrong quotes
change that into

main_door_DoorLock.sendCommand("ON")

main_door_DoorLock.sendCommand("ON")

Hmmm, does that mean the item is imported implicitly, and I don’t need to declare/import/etc. this as a variable?

you can do that with any item in this script. there is an implicit variable „triggeringitem“ which refers to the name of item which triggered this rule. (mainly useful when member of group has triggered)

Reminder this is a javascript rule, DSL syntax won’t work.

So basically all I need to do is go with the ECMA type, and switch the single quotes to double quotes?

I’m fine with using ECMA or DSL, as long as it works haha

Well, a functional DSL example is in post #1 of the Design Pattern thread you pointed too.

If you want to get javascript working - you’ve gone to the trouble of adding a logger, but not used it. Add some logging to your code so that you can see the paths it follows.

As it turns out, @Oliver2 was right. Changing the single quotes to doubles instantly made it work.

Thank you!