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:
Specified the transformation pattern as JS:divideByMillion.js.
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
}
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!
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
}
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
}
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
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 . 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.