Inline JS transformation not working

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 :wink:

  • Platform information:
    • openHAB version: 3.2.0

This isn’t a valid parameter for an MQTT Thing Channel.

Try transformationPattern=

I suspect your log would show complaints about the former…

Thanks, there are no messages regarding this in openhab.log.

I tried your suggestions, yet the value still isn’t changing.

Edits to Things files aren’t always picked up - what happens after you restart openHAB?

But it might be that this method of specifying a JS transformation doesn’t work via Things files.

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.

So the example for inline transformation of https://www.openhab.org/addons/transformations/javascript/#inline-script-example will never work?#

If I get back a String, I can’t do transformation with a number item. I was hoping that inline transformation works differently than via a JS file.

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.

This is what you need to do.

Like this?

Type number : bezug [stateTopic="vzlogger/data/chn0/raw", transformation="JS(| parseFloat(input) / 1000)"]

I think this was the solution.

I moved the transformation with a JS File to the items definition, now it works.

.items

Number Strom_Bezug              "Strom Bezug [%.1f kWh]"                { channel="mqtt:topic:zaehler:bezug" [profile="transform:JS", function="divideBy1000.js"]}

divideBy1000.js

(function(i) {
    return parseFloat(i) / 1000;
})(input)
1 Like

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.

Here is some example Thing config code:

UID: mqtt:topic:mosquitto:c39af590ba
label: "MQTT | Vindriktning bedroom"
thingTypeUID: mqtt:topic
configuration:
  availabilityTopic: ESP32_bedroom/status/LWT
bridgeUID: mqtt:broker:mosquitto
channels:
  - id: sys_free_ram
    channelTypeUID: mqtt:number
    label: System Free RAM
    description: null
    configuration:
      stateTopic: ESP32_bedroom/sysinfo/freeheap
      transformationPattern: JS:| parseFloat(input) / 1000
      unit: KiB
3 Likes