Incoming JS transformation on mqtt channel

Hi,
I am struggling to get a JS transformation on an mqtt channel to work although my setup seems similar to others who have managed to solve this.

I am using Openhab 2.5.
transformation-javascript - 2.5.0
binding-mqtt - 2.5.0

The mqtt channels are working correctly but incoming value is not transformed. There are no errors in the openhab logs.

I have a sensor which produces the distance to the surface of oil in a tank. I want to transform the incoming value to subtract this value from the height of the tank (900) to give me the depth of oil.

.things

Type number : oil [ stateTopic=“rtl_433/145279404/depth_cm”, transformationPattern=“JS:depth.js”]

.items

Number Oil  "Oil Depth [%f cm]"  { channel="mqtt:topic:Oban-MQTT:RTL_433:oil" }

depth.js

(function(i){
return 900 - i;
})(input)

Regards

Make another temporary channel and Item to inspect the raw payload as a String, to confirm topic and payload are received as you expect.

When writing JS transforms, bear in mind parameter and result are both passed as strings. You may need to parse an input string to number before doing maths.
Parsing the string result to something suitable for the channel is the binding’s job.

got it.
sorted with parseInt()

Thanks

1 Like