Using event in a JS UI-defined rule that is triggered periodically and by an item

I have a rule that is triggered by an item receiving a command and also periodically. At the beginning I have the following code

var command = event != undefined ? event.itemCommand : undefined;

and that works fine if the rule is triggered by the item. However, if the rule is triggered by cron, the execution fails and

12:45:10.104 | ERROR | OH-rule-7405d2ace6-1 | b.automation.script.javascript.stack | 259 | Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "event" is not defined
	at <js>.:program(<eval>:2) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1180) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:989) ~[?:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]

is logged. Is there any way (except splitting it to two rules) to use event in a periodically triggered rule?

Add event information in rules for time, manual and RunRuleAction trigger by J-N-K · Pull Request #2965 · openhab/openhab-core · GitHub will probably solve that, but is unfortunately not yet merged.

afaik I use something like

if (typeof maybeObject != "undefined") {
   alert("GOT THERE");
}

to avoid that error with event not defined, cannot confirm since I’m not at home to check, hope it helps … I mainly use this when testing (manual run) where event is not defined, similar (i guess) to when code is run via cron job …

1 Like

You can use this:

var command = this.event !== undefined ? event.itemCommand : undefined;

Using this to access event, you can avoid the ReferenceError.

You can make this shorter by using the Optional chaining (?.) - JavaScript | MDN operator:

var command = this.event?.itemCommand; // command is undefined if event is undefined
4 Likes

I didn’t know that this is possible in JavaScript. It’s such a nice simplification and one of the things I really miss in Java.

4 Likes

I am very much looking forward to that PR.

Do you need the ? on this? I thought that this would always be defined.

this.event?.itemCommand

Or is the ? working in a way I don’t understand?

You are right, ? should only be required for the event.

1 Like