Help with Transformation for MQTT Sensor Values (JavaScript Transformation)

Hi everyone,
I’m trying to transform a value from an MQTT topic before it is processed in OpenHAB. My goal is to divide the value by 1,000,000 to adjust it to the correct scale. Here’s my current configuration:

Type number : conductivitySensor1  "Soil Fertility Sensor1"    [ stateTopic="florasensordevice/sensor/miflora1_conductivity/state", transformationPattern="JS:dividingByMillion.js"]```

Unfortunately, the transformation is not working as expected, and I either get no value or an error in the logs.

What I’ve Tried So Far:

  1. Specified the transformation pattern as JS:divideByMillion.js.
  2. Created a divideByMillion.js file in the transform directory with the following content:
cat /etc/openhab/transform/dividingByMillion.js
// dividingByMillion.js
var value = parseFloat(input);
if (!isNaN(value)) {
    return (value / 1000000).toString();  // Convert µS/cm to S/cm by dividing by 1,000,000
} else {
    return "0";  // Return 0 if the input is not a valid number
}

  1. Verified that the JavaScript Transformation Add-on is installed.

My Questions:

  • Is my configuration correct, or am I missing something important?
  • How can I ensure the transformation is actually being executed? Are there debugging tools or log levels I should enable?
  • Can anyone confirm if parseFloat works in the context of JavaScript transformations?

Any help or pointers on how to resolve this issue would be greatly appreciated! :blush:

Thank you in advance!

You should define a function in your script. i.E.:

(function(input){
   console.info('my function', input)
   let ret = '0'
   const myDict = JSON.parse(input)
   ... 
   return ret
})(input)

mostly mqtt returns values as stringified dictionary, You may see these values using i.e. mqtt-explorer

That’s not required. However, @milo, if you do not define a self calling anonymous function you cannot use return statements. The last line executed will be the return value.

Remove the "return"s or put it all in an self executing function.

Add logging statements. With the exception of event everything available in a rule is available to you in the transform.

Yes.

It should look like:

var value = parseFloat(input);
if (!isNaN(value)) {
    (value / 1000000).toString();  // Convert µS/cm to S/cm by dividing by 1,000,000
} else {
    "0";  // Return 0 if the input is not a valid number
}

or like @Manni51 's example.

I changed it to

Type number : conductivitySensor1  "Soil Fertility Sensor1"    [ stateTopic="florasensordevice/sensor/miflora1_conductivity/state", transformationPattern="JS:dividingByMillion.js"]

and

var value = parseFloat(input);
if (!isNaN(value)) {
    (value / 1000000);  // Convert µS/cm to S/cm by dividing by 1,000,000
} else {
    "0";  // Return 0 if the input is not a valid number
}

but still no value on the item

Add logging.

If it has an error in the code you should see an error in openhab.log when it gets invoked.

Note that the transformation will not be invoked until, as configured in the OP, a new MQTT value is published to that state topic. It doesn’t apply the transformation after the fact.

As an aside, this might be solvable using UoM if the value is coming in using one of the supported conductance units

there is no error in the log and the value is a simple number and yes i piblished a new value

the value is in µS/cm and the UoM is S/m therefore i nned to devide the value by 10000

here is my item description

Number:Dimensionless                  Conductivity_Garden_MiFlora1  "Soil Fertility Sensor1 [%d µS/cm]"   <lawnmower>     { channel="mqtt:topic:miflora:conductivitySensor1",stateDescription=" "[ pattern="%d %unit%"], unit="S/m" }

I m not sure if this is solved

UoM supports modifiers like that. Have you tried seeing is it works without the transformation? Even though it’s not in the docs I think µ is supported. If not, “nano” might work. Not every combination of every unit is fully documented.

As configured, it looks like you are setting the Item’s unit to S/m and trying to display it as %d µS/cm which implies that maybe covering between these units us already happening.

But you still have neither added logging to the transformation nor posted any logs so :man_shrugging: . But the first thought I would do is make all the units applied to the Item be the same and make the unit of the Channel match the unit of the value after the transformation. Or eliminate the units entirely.

One other thing to note, the proper Item type for conductance is Number:ElectricConductivity. You have Number:Dimensionless so I’m surprised you aren’t seeing errors. S/m et al are not supported by Dimensionless, db, % and ratios are.

You’ll need to set the correct unit also in the channel:
MQTT Channel:

Type number : conductivitySensor1  "Soil Fertility Sensor1"    [ stateTopic="test/miflora1_conductivity/state", unit="µS/cm"]

Item:

Number:ElectricConductivity ConductivitySensor1 "Soil fertility sensor1" {channel="mqtt:topic:mosquitto:test:conductivitySensor1",unit="S/m",stateDescription=""[ pattern="%.4f %unit%"]}

Sending value 1 (µS/cm) via mqtt results in 0.0001 S/m:
grafik
grafik
The second screenshot is from Items list.

Thanks for the help

I cleared the cach and everything is running also with the transformations script