OH 4 – Replacing MQTT JS transformation to SCRIPT transformation

You can add logging to the script. Add some to see whether or not the transform in question is even being invoked.

The JavaScript Transformation no longer exists in OH 4. In fact, if you have it in use somewhere you’ll see a warning in the log about that. Did you recode the transformation to be compatible with ECMAScript 11? The JS Transformation used Nashorn ECMAScript 5.1 and there might be incompatabilities.

There isn’t. It’s part of core and will work with an Rules language you have installed.

I’m also working on this to prepare for OH4.
I still have a few MQTT-Things as text files in OH 3.4. And that seems to work with new Script Transformation:

graaljs:filename.script → Is graaljs correct? Or just js? Also in docu only DSL, JS, JRuby are mentioned. But this are maybe only examples . . . not sure.

As described in docs, graaljs seems to be the correct approach:
https://github.com/openhab/openhab-js#script-transformation

Was a bit unclear here, all the listed addons were migrated from 3.4 to 4.0, although JavaScript Transformation was removed within OH4. Have installed openhab-js (npm).

For MQTT channel, following the documentation and defining

transformationPattern: SCRIPT(graaljs:compressorPercentage.script)

I now get the following log warnings:

[WARN ] [t.generic.ChannelStateTransformation] - Transformation service SCRIPT(GRAALJS for pattern compressorPercentage.script) not found!

Did you install the JS Scripting add-on? Note that openhab-js comes with the add-on and doesn’t need to be installed separately unless there is some new feature you want to use before the next release of OH.

1 Like

Yes, JavaScript Scripting add-on is already installed as part of OH3.4, and is migrated to OH4.0 (as mentioned above). Also, SCRIPT(graaljs:filename.script) works for the Modbus part, just not for MQTT transformationPattern.

I haven’t used SCRIPT myself yet (I don’t need many transformations in my system) so there must be something wrong with the syntax. But I don’t know what.

Hi,
I have the same problem! SCRIPT(graaljs:filename.script) with MQTT not working.

Opened a GitHub issue:

1 Like

Correct syntax is

transformationPattern: SCRIPT:graaljs:filename.script
1 Like

Here is is small example on how to tackle the whole transition to 4.0 in terms of js transformation
while using file based configuration (and current development state).


edit /services/addons.cfg and add the needed services to automation like this

automation = jsscriptingnashorn,jsscripting

this will install the needed services.


Rename all the previous used .js files to .script in your /transform directory

Here is an example of the correct syntax for the actual transformationPattern in my mqtt.things file
(“=” instead of “:” after transformationPattern followed by quotes and lowercase on script type.) see:

Type number : Solar_WR_CARPORT_DC_YieldDay_kWh "Tagesertrag in Kilowattstunden" [ stateTopic="inverter/Carport/ch0/YieldDay", transformationPattern="SCRIPT:js:Wh2kWh.script"]

restart oh after editing your files.


E’t Voila
@michaeljoos this might help. In case you haven’t figured it out yet.

How do you change it for UI defined items/channels?

Depending on the thing type, but from main UI, you should have a code tab for your thing. Just change code from JS:filename.js to SCRIPT:graaljs:filename.script, meaning you also need to copy/change your .js file to .script file within your openhab-conf/transform folder.

Example for MQTT thing/channel below:

I use JS transforms in channel links mainly. This does not have a code tab.
JS is gobne in the channel profile settings. I assume there is no alternative way today to replace all those JS transforms?

As described under Announcement, Inline JS scripts in transformations and JS profiles are still work in progress.

OK, i have read that but thought there was a kind of workaround. So means waiting :slight_smile:

Hello everyone! I tried to update my OH from 3.4 to 4.0 milestone build recently and I have some issues with transformations of MQTT things.

This is the definition of thing in file that I made


Thing mqtt:topic:dae79259:bulb_e27_02 "Żarówka E27 02" (mqtt:broker:dae79259) @ "Light" {
Channels:
    Type switch : switch "Switch"                   [ stateTopic="zigbee2mqtt/bulb_e27_02/state", commandTopic="zigbee2mqtt/bulb_e27_02/set/state", on="ON", off="OFF",retained = false]
    Type dimmer : dimmer "Dimmer"                   [ stateTopic="zigbee2mqtt/bulb_e27_02/brightness", commandTopic="zigbee2mqtt/bulb_e27_02/set/brightness", formatBeforePublish="%d", min=0, max=254 ]
    Type number : colortemp     "Color Temperature" [ stateTopic="zigbee2mqtt/bulb_e27_02/color_temp", commandTopic="zigbee2mqtt/bulb_e27_02/set/color_temp", transformationPattern="SCRIPT:graaljs:Innr_ColorTemp.script",transformationPatternOut="SCRIPT:graaljs:Innr_ColorTempOut.script"]
    Type dimmer : saturation "Saturation"           [ stateTopic="zigbee2mqtt/bulb_e27_02/color-xy", commandTopic="zigbee2mqtt/bulb_e27_02/set/color-y", min=0, max=100 ]
    Type colorHSB : color         "Hue/Saturation"    [ stateTopic="zigbee2mqtt/bulb_e27_02", commandTopic="zigbee2mqtt/bulb_e27_02/set", transformationPattern="SCRIPT:js:Hue_HSB.script",transformationPatternOut="SCRIPT:js:Hue_HSB_Out.script"]
    Type string : colorString "Color (String) "     [ stateTopic="zigbee2mqtt/bulb_e27_02/color",commandTopic="zigbee2mqtt/bulb_e27_02/set"]
    Type string : command       "Effect"            [ stateTopic="zigbee2mqtt/bulb_e27_02/effect",commandTopic="zigbee2mqtt/bulb_e27_02/set"]
    Type number : linkquality "Link Quality"        [ stateTopic="zigbee2mqtt/bulb_e27_02/linkquality" ]
}

yields following errors:

2023-05-22 18:13:30.030 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service SCRIPT for pattern js:Hue_HSB.script not found!

2023-05-22 18:13:30.031 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service SCRIPT for pattern graaljs:Innr_ColorTemp.script not found!

Above, I tried both graaljs and js just to verify if its doing any difference. I have also installed

jsscriptingnashorn,jsscripting

Thanks in advance!

Which OH 4 milestone? All this changed between M1 and M2.

Particularly when using the testing version of OH, when you seen an error like this it’s vital that you review the Latest version of the docs to see if something changed (reviewing the announcements and list of changes is important too).

Looking at the latest docs Transformations | openHAB for GraalVM JS the pattern should be JS:Hue_HSB_Out.js (note the file name needs to end in .js). For Nashorn JS the pattern should be NASHORNJS:Hue_HSB_Out.js.

Because any Nashorn JS transform should be compatible with GraalVM JS, I recommend against using NASHORNJS unless you have a reason (e.g. don’t want to install the GraalVM add-on).

Ah, after I’ve migrated to M2 and noticed errors regarding JS transforms in the logs I incorrectly assumed that latest docs are not up to date. I rolled back to previous config (meaning JS:Hue_HSB_Out.js) and my config is working again.
Thanks for pointing me in right direction!

Just make sure to click on “Latest”. They are not guaranteed to be immediately updated but so far it’s been less than a day between a PR going live and updates to the docs. Just make sure to select Latest in the upper left or else you’re looking at the OH 3.4 docs.