OH 4 – Replacing MQTT JS transformation to SCRIPT transformation

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.

Hi, I have experienced some issue with JS transformation with ModBus and I need some help here. Below is what I have configured based on the description:

Thing data hold4746 [ readStart="4746", readValueType="int16", writeStart="4746", writeValueType="int16", writeType="holding", readTransform="SCRIPT:graaljs:divide10.script", writeTransform="SCRIPT:graaljs:multiply10.script"  ]

In the log I got the error message:

2023-08-11 23:17:23.644 [WARN ] [s.internal.SingleValueTransformation] - couldn't transform response because transformationService of type 'SCRIPT' is unavailable

I have JavaScript scripting installed, any suggestion where to look? Thanks!

Did you read OH 4 – Replacing MQTT JS transformation to SCRIPT transformation - #21 by rlkoshak in this thread a few posts above yours ?

In openHAB 4.0 SCRIPT transformation was removed, and replaced with meta transformation that uses the scripting language’s abbreviation. For JavaScript it’s JS.

More details can be found here:

So your thing should be defined as:

Thing data hold4746 [ readStart="4746", readValueType="int16", writeStart="4746", writeValueType="int16", writeType="holding", readTransform="JS:divide10.js", writeTransform="JS:multiply10.js"  ]

Note you should rename your transform script file from .script to .js

Or you can use the inline syntax if you prefer.

1 Like

Hi thank you for your reply! I did read but was not sure if it’s the syntax in the final release, so I used the one marked as solution. Thanks for pointing it out!

1 Like

Thank you for the help! I changed to this syntax and it works again!
Now looks like the only problem with modbus is after restart all configured things are gone, have to manually update the things file to bring them back, bumpy journey to upgrade I guess :slight_smile: