JavaScript - Transformation Services missing in openhab 4

Hi there,
I’m missing the JavaScript - Transformation Services in openhab 4.

How can I now make sure that my items which are linked to a float channel only hold integer values so they only trigger a change when a massive change happend and not somthing minimal.
So 45.67 to 45.66 shouldn’t trigger thats why I saved it to 45 in my item.

I used the following javascript transform script before:

(function (i) {
    return parseInt(i) // The value sent by OH is a string so we parse into an integer
})(input)

I hope the function return till openhab 4 gets released. Or is there a other way to do this now?

Since Milestone 2 you just need to install the JavaScript Scripting add-on.

Hi @J-N-K,
so happy to here that. Thanks so much!

@J-N-K: How is the new syntax to use the transformation in an item channel? The following is not working:

{channel="deconz:humiditysensor:bridge:00158d0004210ddb010405:humidity"[profile="SCRIPT", function="float2int.script"]}
1 Like

The profile name is JS. If that doesn’t help, I can’t support you, except my general advice not to use textual configuration.

The profile name ´JS´ does not work. It gives the following error in the log:
blubb.items :

{channel="deconz:humiditysensor:bridge:00158d00042fdca5010405:humidity"[profile="JS", function="float2int.script"]}

log :

[WARN ] [.thing.internal.CommunicationManager] - No ProfileFactory found which supports profile 'system:JS' for link 'MS_Werkstatt_Luftfeuchte -> deconz:humiditysensor:bridge:00158d00042fdca5010405:humidity'

Side-Topic:
With the update to openhab 3 I changed my complete textual configuration because of many advices from very popular community members. One year later now I’m changing everything back except the Things to textual configuration.
It just works so much quicker and easier for me.
I can rename via search-and-replace quickly and having a new sensor is most of the time copy and paste of one existing sensor plus two search-and-replace calls for the itemname and the channelid.
I’m so happy textual configuration is still possible.

3 Likes

And - as you can see - struggle to find out how to configure things that you can configure with one click in the UI.

Do you use M2? Before M2 it’s SCRIPT and you have to prefix the filename with js. With M2 it’s JS and you only need to use the filename.

Please check out the documentation

It is not fully compatible.

Instead of function, it’s now called toItemScript and toHandlerScript

Okay I recognized know that my problem is not to call the transform function in the profile.
The problem is that the function is not working if I use the simple function Math.round().

{channel="deconz:humiditysensor:bridge:00158d00042fdca5010405:humidity"[profile="transform:JS", toItemScript="float2int.js"] }

This gives absolut no value and no error in the log:

(function (data) {
    return Math.round(data)
})(input)

This example gives me always 11:

(function (data) {
    return 11
})(input)

So why can’t I use the Math.round function? And why don’t I get an error?
Is there maybe a bug im 4.0.0.M2?

I’m happy for any suggestions!

You can’t round a String. input/data is a String.

Thats why among many options also tried

(function (data) {
    return Math.round(Number(data))
})(input)

But this also didn’t work.

EDIT: Oh I see you’ve fixed the profile=“JS”.

try

(function (data) {
    return data
})(input)

Like in the documentation you posted profile=“transform:JS” is working. But the Math.round actually not.
The documentation here is not working:

My problem is still to make an interger from the float value which is not working yet.

I don’t know much about javascript. If you want to try jrubyscripting:

  • install jrubyscripting addon

profile="transform:RB", toItemScript="|input.to_f.round"

Add some logging (you can do that easily now in OH 4 because the JS Scripting helper library is available) and log out the intermediate steps. You can use console.log() in a transform.

If your Item has units (e.g. 12.34 W as opposed to 12.34) Number(data) won’t work. That will return NaN. But parseFloat is a little more flexible in that it will parse up to the first non-numeric character so will ignore the units.

console.log(Math.round(Number("12.45 W")));

console.log(Math.round(parseFloat("12.45 W")));

produces

NaN
12
1 Like

@rlkoshak as very often you are my hero! Thank you so much not only for the solution but especially for the explanation. I learned already so much from your posts here in the forum.
Without people like you we wouldn’t be able to have a smarthome Thanks a lot.

data: 69.54 %
parseFloat(data): 69.54
Number(data): NaN