OH 4 – Replacing MQTT JS transformation to SCRIPT transformation

Close to a successful migration from OH 3.4 (stable) to 4.0 (snapshot) for testing purpose, and being aware of Javascript transformation is removed from OH 4 (Java 17) I have tried to replace JS transformation to SCRIPT transformation, with mixed results. For both Modbus and MQTT, I have replaced (within main UI) from

JS:filename.js

to

SCRIPT(graaljs:filename.script)

, obviously also changing file endings from .js to .script in the transform folder. For Modbus, the change is successful for input under readTransform/writeTransform, and linked items get updated as before. However, for MQTT, changes made under transformationPattern does not seem to work, i.e. no errors/log information, but the linked items does not get updated as with JS transformation under OH 3.4.

Anyone being able to use MQTT transformationPattern within main UI using SCRIPT?

Did you install the transformation pattern

JavaScript Scripting, Javascript Transformation and Nashorn addons are installed/migrated from 3.4 to 4.0. OH4: openhab-js installed using npm, unchecked “Use included library” for JavaScript Scripting settings.

Haven’t identified a different/dedicated addon for SCRIPT transformation pattern…? And as mentioned, SCRIPT transform works for Modbus using OH 4.

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!