Rules.js

I am currently investigating triggers in JS and have been using “Raw API” but the documentation has the following example using extensions:

'use strict';

var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF");
load(OPENHAB_CONF+'/automation/lib/javascript/core/rules.js');
var me = "HelloWorld.js";

JSRule({
    name: "Javascript Hello World (GenericCronTrigger raw API with JS helper libraries)",
    description: "This is an example Jython cron rule using the raw API",
    triggers: [
        TimerTrigger("0/10 * * * * ?")
    ],
    execute: function( module, inputs){
        logInfo("Hello World!");
    }
});

so I thought I would give it a go. Unfortunately I fell at the first hurdle because I’ve been unable to find “rules.js” to download :unamused: I have tried “openhab-rules-tools-main” and “openhab-helper-libraries-master” both without sucess. It obviously exists as there a questions in the forum which show code loading it. So would some kind sole point me to where I can find it to download.

Many thanks

It’s part of the Helper Libraries.

@rlkoshak Many thanks I now realise my silly basic mistake, I followed the jsr223 tree rather than the lib tree​:face_with_hand_over_mouth::face_with_hand_over_mouth:

Having installed everything in the helper library on my machine I now find that it fails to load rules.js with the following error:

2021-02-02 23:51:43.795 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: script

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.eclipse.smarthome.model.script.actions.ScriptExecution cannot be found by org.openhab.core.automation.module.script_3.0.0

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:531) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1438) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.load(Context.java:962) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.Global.load(Global.java:1720) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$2513$rules$cu1$restOf.:program(/etc/openhab/automation/lib/javascript/core/rules.js:16) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1438) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.load(Context.java:962) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.Global.load(Global.java:1720) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$2478$\^eval\_$cu1$restOf.:program(<eval>:6) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:456) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:413) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:409) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:162) ~[jdk.scripting.nashorn:?]

	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]

	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) ~[?:?]

	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]

	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) ~[?:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1179) [bundleFile:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1031) [bundleFile:?]

	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1047) [bundleFile:?]

	at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:314) [bundleFile:?]

	at jdk.internal.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) ~[?:?]

	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]

	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]

	at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) [bundleFile:1.0.9]

	at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) [bundleFile:1.0.9]

	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) [bundleFile:1.0.9]

	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) [bundleFile:1.0.9]

	at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) [bundleFile:1.0.9]

	at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) [bundleFile:1.0.9]

	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) [bundleFile:1.0.9]

	at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [bundleFile:1.0.9]

	at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:216) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:301) [bundleFile:1.0.9]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:220) [bundleFile:1.0.9]

	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [bundleFile:3.1.0]

	at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:276) [bundleFile:1.0.9]

	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:852) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:544) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [bundleFile:?]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1581) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1307) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:293) [bundleFile:?]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:482) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1549) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1204) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [bundleFile:9.4.20.v20190813]

	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [bundleFile:?]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.Server.handle(Server.java:494) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:374) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:268) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:367) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:782) [bundleFile:9.4.20.v20190813]

	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:918) [bundleFile:9.4.20.v20190813]

	at java.lang.Thread.run(Thread.java:834) [?:?]

Caused by: java.lang.ClassNotFoundException: org.eclipse.smarthome.model.script.actions.ScriptExecution cannot be found by org.openhab.core.automation.module.script_3.0.0

	at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:484) ~[org.eclipse.osgi-3.12.100.jar:?]

	at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:395) ~[org.eclipse.osgi-3.12.100.jar:?]

	at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:387) ~[org.eclipse.osgi-3.12.100.jar:?]

	at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:150) ~[org.eclipse.osgi-3.12.100.jar:?]

	at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]

	at java.lang.Class.forName0(Native Method) ~[?:?]

	at java.lang.Class.forName(Class.java:398) ~[?:?]

	at jdk.nashorn.internal.runtime.Context.findClass(Context.java:1180) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.NativeJava.simpleType(NativeJava.java:546) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:326) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:318) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:314) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$2520$utils$cu1$restOf.:program(/etc/openhab/automation/lib/javascript/core/utils.js:29) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

	... 79 more

Basically it appears that it is trying to load utils.js which is having problems finding:

org.eclipse.smarthome…

If I replace all the refs to" org.eclipse.smarthome" with “org.openhab.core” in my local copy then it fails with:

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.joda.time.DateTime cannot be found by org.openhab.core.automation.module.script_3.0.0

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:531) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1438) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.load(Context.java:962) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.Global.load(Global.java:1720) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$2761$rules.:program(/etc/openhab/automation/lib/javascript/core/rules.js:16) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1438) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.Context.load(Context.java:962) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.objects.Global.load(Global.java:1720) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$2726$\^eval\_.:program(<eval>:6) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]

The Helper Libraries for JavaSciot have not been updaed for OH 3 still it works seem. All references to org.evlipse.smarthome have moved. There is no more Joda DateTime which has been replaced with ZobedDateTime. There are some other changes that need to be made in sure.

@rlkoshak OK no probllem, I can continue using the RAW API, but am having a few problems brought about by my ignorance/lack-of-knowledge regarding which trigger types are available for use and the parameters that they require.

Could you point me to where I can get a list of trigger types that can be used by TriggerBuilder and the inputs taken by .withConfiguration( new Configuration()). The only JS documentation I can find is the Raw API example which only shows the generic cron trigger. So I looked at the core.triggers Documentation which is more comprehensive and enabled me to create triggers, within JS, of type “core.ItemStateChangeTrigger” but other types such as “ItemUpdatedTrigger” and " ItemRegistryTrigger" fail with the rule displaying the error:

which I assume means they have not been implemented yet. Whereas another trigger type I thought I might use “core.GenericEventTrigger” failed with rule showing the error:

which I am assuming/hoping means that I got the configuration parameters wrong, the trigger builder I used in that case was:

   ( TriggerBuilder.create()
        .withId("Destroy")
        .withTypeUID("core.GenericEventTrigger")
        .withConfiguration(
            new Configuration({
                "eventSource": "InLineSwitch01_Switch",
                "eventType"  : "ItemUpdatedEvent"

            })).build()

which I derived from this example. Hence the request for information on where I can find information about the Raw API trigger types and parameters available to JS.

Once again may thanks for your help and patience.

I would look at the Python Helper Library’s code for the when annotation. The triggers are the same no matter what language you are using.

@rlkoshak Thanks again I have now got a rule triggering when an Item is updated. It would appear the main issue was that although the documentation says “eventTopic” is optional I found that in practice it still needs to be present in the key configuration, even if only as: eventTopic : “”. Thus it would appear the value is optional not the key.