MQTT Binding: JS Transformations not working after upgrade to OH4

Usually I manage to do the openhab upgrades, this time I seem to be stuck somehow.
I am using zigbee2mqtt with the mqtt binding. For the bulbs a javascript transformation is used. After upgrading to OH4, these transformations are not working anymore. Whenever triggering a such a Item / thing, I see this error in the logs:

2023-09-13 21:16:18.372 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service JS for pattern openhabdimmer2zigbeebridge.js not found!

my mqtt.things file looks like this:

Bridge mqtt:broker:mosquitto "Mosquitto" @ "System" [ host="mosquitto", port="1883", secure="false", lwtMessage="I'm offline", lwtTopic="lwt/openhab", lwtRetain="true", username="openhab", password="XXXXXXXXXXXXXXXXXXX" ]
{
    Thing topic bulbHueKueche "Kueche Licht" @ "Kueche" {
	Channels:
        Type dimmer : brightness "Kueche Bulb Dimmer" [ stateTopic="zigbee2mqtt/bulb-hue-kueche", commandTopic="zigbee2mqtt/bulb-hue-kueche/set", min=0, max=100, step=1, transformationPatternOut="JS:openhabdimmer2zigbeebridge.js", transformationPattern="JS:tradfri2openhab.js" ]
		Type dimmer : color_temperature "Kueche Bulb ColorTemperature" [ stateTopic="zigbee2mqtt/bulb-hue-kueche", commandTopic="zigbee2mqtt/bulb-hue-kueche/set", min=50, max=100, step=1, transformationPatternOut="JS:openhabColorTemp2zigbeebridge.js", transformationPattern="JS:tradfriColorTemp2openhab.js" ]
		Type string : availability  [stateTopic="zigbee2mqtt/bulb-hue-kueche/availability", transformationPattern="JSONPATH:$.state" ]
	}
}

Reverting to 3.4.4 it’s working like a charm. Is there anything I missed?

Yes. As mentioned in the list of breaking changes you must install the JS Scripting add-on. The old separately installed JS Transformation no longer exists and JS no longer just comes with OH by default. If you want to use JS in a transform, you must install the add-on.

Thanks. Do you know how the add-on is named to list it in addons.cfg? I suppose it has to be added to the transformation=… line?

I have no idea what it’s called in addons.cfg or what line it goes on. I haven’t used addons.cfg for half a decade now. I assume you can still get the name of the addon that goes there from the URL of the readme.

I know it definitely doesn’t go on the transformation line. It’s not a transformation add-on, it’s an automation add-on.

Maybe automation="jsscripting" ?

Note, if you have “js” anything on the transformation line, you’ll be seeing errors in your logs and need to remove it.

Isn’t blockly installed by default and it relies on js and its helper library?

No, nothing gets installed by default any more, not even rrd4j. However, the first run wizard makes suggestions on some common add-ons to install which includes JS Scripting.

If JS Scripting is not installed, Blockly is not shown as an option. But it is true that it’s still there since it’s 100% implemented as part of MainUI and not really separate.

Sorry to re-open this but my JS transforms have recently stopped working in both items and sitemaps. I am running 4.2.1 with JS Scripting installed

The docs still show that this is valid

Number  Livingroom_Temperature_F "Temperature [JS(convert-C-to-F.js):%s °F]"   {/*Some Binding*/}

I have this in my sitemap

Text item=AGMSoC label="AGM SoC [JS(tenth.js):%.1f %%]" icon="Energy"

to convert a value of 795 into 79.5%
what I get is
image
so it seems to have broken the formatting too.
The tenth.js is

(function(i) {
	i = i / 10;
    return i;
})(input);

If I try to put the JS into the item in the text file I get an error.
I’m not sure what’s changed.

Fogot to say I’ve also tried replacing %.1f with %s and it makes no difference. Also changing the js to return i.toString makes no difference either.

Has anyone any ideas?

try return i.toString();

What’s the error?

I suspect that UoM could handle both of these cases without a transform. Without the actual error generated I’m afraid I can’t really offer more than that. Maybe make sure that the JS Scripting add-on is installed?

That still gives me 795 (and the % is missing from the value)

Thanks Rik

Number AGMSoC "AGM SoC" {channel="mqtt:topic:MQTTv2:VictronVenus:AGM_SoC"[profile="transform:JS",toItemScript="tenth.js"]}

Error in the logs is

2025-01-06 15:05:21.932 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script 'tenth.js': Could not get script for UID 'tenth.js'.

JS scripting is installed and the tenth.js file is in the transform folder

BTW The link to the ‘Available transformations’ on this page Transformations | openHAB
gives a 404 error

Permissions on the tenth.js file are correct?

Everything else seems to look correct.

I put the tenth.js file into automation/jsr232 (and into automation/js just in case) it made no difference. Restarted the Docker container and it started working - but from the version in transform. I’m not sure what’s going on here.

It definitely needs to be in the transform folder.

It might be the case that the file watcher isn’t working as it should or there is some issue with Docker and the file watcher. I’ve use only managed configs for quite some time so :man_shrugging: .

Try:

this uses inline jruby (which means you’d need to install the jrubyscripting addon) but you can replace it with the equivalent JS code if preferred

Text item=AGMSoC label="AGM SoC [RB(|'%.1f %%' % (input.to_f/10)):%s]" icon="Energy"

But a much better way to solve this is by making the AGMSoC item to be Dimmer (or number if you think the value can go outside 0-100) and to contain the correct percentage value. Then your label format can be [%.1f %%] without using any transformations

The main issue with your original label format is that transformation results are always strings, so you need to use :%s

It now works using the JS transform in the item - I don’t understand how it worked before as a number. Dimmer won’t work as the values are always over 100