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?
[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.
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().
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.
@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
I know the topic is solved, just wanted to add this helped me to fix the NaN when adding units and using a JS transform, as stated it is a string that is received in variabele i where the first is the value and the second argument the unit.
When returning you also need to return the unit.
I solved it like this, hope it helps somebody.
Tom
Of course, it would be even better to stay working with Quantity but I have no idea what units is being used here so I have no idea what unit to set on 36785360. It’s unclear how doing these operations actually result in a value in the same unit in the end anyway unless the binding is reporting the wrong unit to begin with. So returning the same unit as was passed in is almost certainly wrong.
If this transform is doing a conversion of units, all you probably need to do is set the unit metadata on the Item to the unit you want and you can use the rounding Basic Profile to round the value before the Item gets updated.
The problem with reopening a two year old thread, is it’s missing two years worth of development in the original.