I’m using the inline JS transformation from the documentation, afaik exactly as described
Thing configuration:
Bridge mqtt:broker:mybroker [ host="192.168.178.120", secure=false ]
Thing mqtt:topic:zaehler "Zweirichtungszaehler" (mqtt:broker:mybroker) {
Channels:
Type number : bezug [stateTopic="vzlogger/data/chn0/raw", transformation="JS(| input / 1000)"]
Type number : einspeisung [stateTopic="vzlogger/data/chn1/raw", transformation="JS(| input / 1000)"]
Type number : leistung [stateTopic="vzlogger/data/chn2/raw"]
}
I’ve linked the channel to an Number item in the UI without any other options.
Yet, the raw input value from MQTT is not being divided by 1000 as expected. I’ve also tried JS transformation in Profile, yet this did not work, as there were exceptions, as the return value of the script is a String instead of a number.
Any idea wha the transformation in the Channel is not working? I hope it is not just some trivial mistake on my side
Yes, inputs and outputs of transformations are always strings.
You can’t do maths with strings.
You’ll need to parse the input string into a number first.
Not sure why you think that; the first operation is .split(), a string function.
EDIT - ah I was looking at wrong example. No, I don’t think example JS(| input / 10) is ever going to work.
You don’t have to worry about what type you return, the transformation service takes care of normalising it to string (just like a print statement really) and the binding/channel sorts that string out in turn to what it is dealing with at the time - number, rollershutter, blah.
For those finding this trough the search: I was looking for something similar and it turns out to be possible with an inline script. To convert raw Bytes from mqtt to something more useful is possible with transformationPattern: JS:| parseFloat(input) / 1000.