Split MQTT Value using JS

I split Values returned by MQTT Topic in things using JS:

thing:

Type number : temperature_outside [ stateTopic="ebusd/24849/temperature.outside", transformationPattern="JS:|(input).split(';')[6]" ]

value (as visible in MQTT Explorer):

0;0;0d;°C;50.0;-50.0;22.0

this returns in item the correct value 22.0

a string type thing

Type string : status_heating  [ stateTopic="ebusd/24849/status.heating", transformationPattern="JS:|(input).split(';')[6]" ]

with value

51;1;00;0;25.5;0.0;Abgeschaltet

returns nothing.

The value returned ist a semicolon separated array, JS split method should return the element at index 6 (array starts with index 0), but only if I define thing as number.

What is wrong?

Thanks
Werner

I would guess because it is a string it is not going to be separated by the ;

I just did a quick JS script to test and you have to put each part of the string that is separated by the ; into an array.

Below should give you something to work with:

var str = "51;1;00;0;25.5;0.0;Abgeschaltet";
var temp = new Array();
// This will return an array with strings "1", "2", etc.
temp = str.split(";")[6];
console.log(temp)

Output is: Abgeschaltet

So how can I use a script inside a things definition replacing the transformationPattern= code?

There is a profile section under the thing then the item that is linked to the thing:

Then under settings>transformations you create the transformation code:
Screenshot from 2024-06-23 21-22-13

And the code is something like this:

//this checks the code of fronius errors
(function(i) {
var val = parseFloat(i);
if (val == 567 || val === 0 )
{ return val; }
})(input);

I am using OH4.1.3 and only use the UI. If you are using files then I don’t know how to do it that way.

Basically you add the transform using the script IDin this case config:js:6912317660
If you click the field it tells you the the code name.

I use only files, my OH is 4.1.3 but I started with version 2 so most of my config relays on files.
I don’t know how to write a working js file for this case to put into the scripts directory, the way to define the thing I found in the meantime:

transformationPattern="SCRIPT:graaljs:somejavascript.script"

OK that should work I guess.
I started with 2.5 and went to version 3 and now 4.1.
When I went from 2.5 to 3 I dropped all DSL scripts and dropped all files and just used the UI and it made it so much easier in some ways.
The only thing I missed was being able to grep for code in the files but in the UI you can do that in the developer tools.
Good luck. Hope it works for you.