Openhab 4 transform script->SCRIPT

Hi all,
sorry if the qestion is stupid but i read several messages on the forum and i don’t understand what i’m missing.
I upgraded to 4.0, i updated the shelly bindings and near everything works.
Only the transform js script was not working (as expected)
So i changed these items:

Number STAMPA3D_TIME 		"Tempo di stampa  [JS(cputime.js):%s]"     	<time>  	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTime" }
Number STAMPA3D_REMAINING 	"Tempo rimanente  [JS(cputime.js):%s]"   	<time>   	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTimeLeft" }


Number STAMPA3D_TIME 		"Tempo di stampa  [SCRIPT(graaljs:cputime.script):%s]"     	<time>  	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTime" }
Number STAMPA3D_REMAINING 	"Tempo rimanente  [SCRIPT(graaljs:cputime.script):%s]"   	<time>   	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTimeLeft" }

i restarted Openhab and now i get this error:

2023-03-21 12:39:30.596 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '0' on item 'STAMPA3D_REMAINING' with pattern 'SCRIPT(graaljs:cputime.script):%s': Script type 'graaljs' is not supported by any available script engine.
2023-03-21 12:39:30.615 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - transformation throws exception [transformation=org.openhab.core.automation.module.script.ScriptTransformationService@40921d58, value=0]
org.openhab.core.transform.TransformationException: Script type 'graaljs' is not supported by any available script engine.
        at org.openhab.core.automation.module.script.ScriptTransformationService.transform( ~[?:?]
        at org.openhab.core.ui.internal.items.ItemUIRegistryImpl.transform( ~[?:?]
        at org.openhab.core.ui.internal.items.ItemUIRegistryImpl.getLabel( ~[?:?]
        at ~[?:?]
        at ~[?:?]
        at ~[?:?]
        at ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke( ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:?]
        at java.lang.reflect.Method.invoke( ~[?:?]
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation( ~[bundleFile:3.4.5]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$ ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage( ~[bundleFile:3.4.5]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest( ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet( ~[bundleFile:3.4.5]
        at javax.servlet.http.HttpServlet.service( ~[bundleFile:4.0.4]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service( ~[bundleFile:3.4.5]
        at org.ops4j.pax.web.service.spi.servlet.OsgiInitializedServlet.service( ~[bundleFile:?]
        at org.eclipse.jetty.servlet.ServletHolder.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter( ~[bundleFile:9.4.50.v20221201]
        at org.ops4j.pax.web.service.spi.servlet.OsgiFilterChain.doFilter( ~[bundleFile:?]
        at org.ops4j.pax.web.service.jetty.internal.PaxWebServletHandler.doHandle( ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle( ~[bundleFile:9.4.50.v20221201]
        at ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.servlet.ServletHandler.doScope( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.session.SessionHandler.doScope( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle( ~[bundleFile:9.4.50.v20221201]
        at org.ops4j.pax.web.service.jetty.internal.PrioritizedHandlerCollection.handle( ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.Server.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.HttpChannel.lambda$handle$1( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.HttpChannel.dispatch( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.HttpChannel.handle( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.server.HttpConnection.onFillable( ~[bundleFile:9.4.50.v20221201]
        at$ReadCallback.succeeded( ~[bundleFile:9.4.50.v20221201]
        at ~[bundleFile:9.4.50.v20221201]
        at$ ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce( ~[bundleFile:9.4.50.v20221201]
        at ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob( ~[bundleFile:9.4.50.v20221201]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$ ~[bundleFile:9.4.50.v20221201]
        at ~[?:?]

What i’m missing?
I have to install something for GraalVM?
Regards. and Thanks in advance.

There is no JavaScript of any kind there by default in OH 4. If you want to use the old Nashorn JS you need to install it. If you want to use the newer GraalVM JS you have to install it. Obviously, I recommend the newer one. If you choose Nashorn, use SCRIPT(js:cputime.script):%s.

Be aware that SCRIPT also supports Rules DSL, jRuby, and any other supported rules language now.

Also note that the actual names of the file must end with .script, not .js.

In addition, there is a bug right now where OH will not see new files in the transform folder unless it’s restarted. I think it’s been fixed and is available in the snapshots. If not it will be available soon.

Finally, it’s not completely clear what this transform does, but I would not be surprised that you could achieve what you want without it by defining the Item as type Number:Time and using DateTime label formatting to split it to HH:MM:SS for you.

1 Like

Note that both js and graaljs are owned by GraalJS.
If you want Nashorn, you’d need to use application/javascript;version=ECMAScript-5.1

I’m not sure what you are saying here.




in which case, if I’ve both Nashorn and JS Scripting installed how does it know which one I want to use for this transform?

is using NashornJS to run the JS transformation script.

as well as


is using GraalJS.

OK, then we should be consistent in the docs. In the main docs: the example talks about and shows using js but in the JS Scripting docs the example talks about using graaljs.

Given there is no other mention of the fact that there are two JS options, the natural assumption was that the main docs referred to Nashorn and the add-ons docs referred to JS Scripting. We should either change the add-on docs or the main docs to match and we should mention in the main docs that the mime type needs to be used for Nashorn (and it’s odd that it’s the only one that uses the mime type where the rest get a shortcut).

When the main docs were written, NashornJS was the default JS engine and had the js identifier. graaljs was introduced for GraalJS back then, because using the GraalJS MIME type was quite ugly.

Now with 4.0 GraalJS became the default JS engine and took over js, graaljs is kept.
You can provide a PR to openhab-js to update the JS Scripting docs.

Looking at the code (openhab-addons/ at 96038c664ab2fe5e766461568e62a7e0a5ddc33a · openhab/openhab-addons · GitHub), using nashornjs as alias should work. We just need to explain how to use NashornJS for SCRIPT transformation in the Nashorn addons docs. The main docs should be kept neutral.

Excellent! that’s much better.

Except excluding Nashorn from the main docs isn’t being neutral. It’s singling Nashorn out for exclusion. The main docs have examples for DSL, JS (without mention that there are two JS’s to choose from leaving the example ambiguous) and jRuby.

Given the confusion people are already demonstrating with the elimination of the JS Transformation, having a Nashorn example in the main docs will go a long way towards clearing that up.

I take this to mean we want to standardize on js, which makes sense.

IMO it’s neutral not to mention the different JS engines. The example in fact works for both GraalJS and NashornJS the same, the user doesn’t have to care about Nashorn or Graal here. Nashorn is only there to allow old scripts to still work, but no one knows when Nashorn stand-alone will leave us as well.

I disagree here because of the points above. People should just use SCRIPT(js:…) instead of JS(…), and don’t care about Nashorn.
Again, it doesn’t make sense to have separate Nashorn and Graal examples, since most JS transformations will use basic JS which is the same in both. FYI, I was able to switch all my JS transformations to Graal without having any compatibility problems.

Yes, we should standardise to js (which is GraalJS) and don’t care about Graal and Nashorn.

Except it expressly states

The script-prefix is js .

If someone installs only Nashorn JS and tries that it won’t work. And people are already hitting the forum having done just that.

You may not, but I’ve spent a significant amount of time over the past two weeks helping people who are having problems. That’s going to explode when 4.0 release happens. I’d much rather spend my time on other things than helping with confusion that could be fixed by adding one sentence to the main docs.

Agreed. I wouldn’t add a separate example. I’d just make it read:

The script-prefix is js for JS Scripting and nashornjs for JS Scripting (Nashorn).


Got it, thanks for your help for all the community members.

Looks good :+1:

I’ve just approved your JS Scripting docs PR, note that we need the same PR for openhab-js.

Hi sorry for writing again with 4.0M1 was working.

Number STAMPA3D_TIME 		"Tempo di stampa  [SCRIPT(graaljs:cputime.script):%s]"     	<time>  	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTime" }

i updated to M3 and again doesn’t work.

In the 4.0 docs seems that Java script scripting use prefix js, so i tested also with:

Number STAMPA3D_TIME 		"Tempo di stampa  [SCRIPT(js:cputime.script):%s]"     	<time>  	{channel="mqtt:topic:MQTT_CASA:3D_PRINTER:printTime" }

but sill get the error message:

Failed transforming the state '3461' on item 'STAMPA3D_TIME' with pattern 'SCRIPT(js:cputime.script):%s': Couldn't transform value because transformation service of type 'SCRIPT' is not available.

Java script scripting is installed:

288 │ Active │  80 │ 4.0.0.M3               │ openHAB Add-ons :: Bundles :: Automation :: JavaScript Scripting
289 │ Active │  80 │ 0.3.0.v20220506-1020   │ EdDSA-Java

and i also try with JavaScript Scripting (Nashorn)

289 │ Active │  80 │ 0.3.0.v20220506-1020   │ EdDSA-Java
294 │ Active │  80 │ 4.0.0.M3               │ openHAB Add-ons :: Bundles :: Automation :: JavaScript Scripting (Nashorn)

What i forgot to install?

Nothing but you missed a bunch of important things in the docs.

The script needs to be placed in the $OPENHAB_CONF/transform folder with the native extension for the chosen language, for example stringlength.js for a transformation using JS Scripting. . .

For the modern JS Scripting, the script file name is stringlength.js and the transformation is JS(stringlength.js)

Your file is not named correctly, which I think will still probably work. But the “JS” isn’t a mere prefix, it’s the whole transform. You don’t use “SCRIPT” anywhere now.


Thank you for the solution and for the link to docs!