Outgoing channel transformation with Javascript

I am trying to create a transformation for a channel of an MQTT outgoing channel. Ideally I would like to use Javascript for this, but I could not get this to work yet. I have tried various things, but always end up with error messages in the log file.
My last attempt was to put a file ‘jalTransform.js’ into the ‘transform’ directory of the server and reference this in the outgoing value transformation as


But I get the error message

Transformation service JS(TRANSFORM/JALTRANSFORM.JS) for pattern %s not found!

for this. I have also tried to put it into the scripts directory as documented here, but this doesn’t work either.
Any help how to formulate a Javascript transformation for an outgoing channel would be greatly appreciated.

I’m running OpenHAB 3.4.4 inside a Docker container on a Linux machine.

Indeed, the transform file must go under the transform folder. But when you reference it, omit the “transform/”. OH only looks in the transform folder so including it is redundant and therefore not supported.


Make sure you have the JavaScript transformation installed. Just before upgrading to OH 4, remove the JavaScript transformation and install the JS Scripting add-on and this will continue to work as written.

Note that you have not followed the documentation you linked to. In particular:

The script needs to be placed in the $OPENHAB_CONF/transform folder with an extension .script regardless of the actual script type.

Given the filename stringlength.script , the transformation pattern is SCRIPT(<script-type>:stringlength.script):%s

So if you wanted to use the SCRIPT transform, as documented your filename must be jalTransform.script and you’d reference using SCRIPT(JS:jalTransform.script):%s.

However, the syntax for the SCRIPT transform has changed in OH 4 to go back to using the name of the language as the file extension and as the identifier, which makes the old JavaScript transform work unchanged now (so long as you have one of the two JavaScript automation add-ons installed).

Thanks for your explanation. Unfortunately it still doesn’t work, if I change the file suffix and put


into the field for outgoing value transformation. The error message in the log file is

2023-07-04 17:55:32.505 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service SCRIPT(JS for pattern jalTransform.script):%s not found!

However I could get it to work, when I rename the suffix to “.js” and reference the script as


I haven’t tried, if it’s necessary to change the suffix, but It seems, that the documentation is wrong anyway.

Would you mind sharing your script? I am looking for a starting point to swap on and off for a switch .

I’m happy to share my script, but it’s not yet working as expected. It seems, I am expecting the wrong strings as input, but I haven’t found a way to print a debug message from within Javascript in a transformation. Here is my short script, which intends to convert the values received from OpenHAB to something, that my roller shutter understands via Zigbee2MQTT:

(function (value){
   if (value == "UP" || value == "OPEN")    return('{ "state" : "OPEN" }');
   if (value == "DOWN" || value == "CLOSE") return('{ "state" : "CLOSE" }');
   if (value == "STOP") return('{ "state" : "STOP" }');

   return("{ \"position\" : " + value + " }");

If you find a way to output a debug message, please let me know.

1 Like

You never indicated where you were trying to apply the transform and since you were using the Item label syntax that’s what I provided. For certain buildings, the syntax is different.

The documentation is right, the context is wrong. I missed that you are using this on an MQTT Channel. The MQTT binding docs take about how it uses a different syntax, HTTP binding too. Everywhere else uses the above syntax except for the “:%s” part which is only used for labels.

Take a look at this response to my ticket: