MQTT Javascript transformation not working

Spent a fair amount of time trying to do something simple. I get a MQTT moisture reading and I want to have items for the raw value and a percentage calculation.

Number  Mqtt_PyPortal_Garage_moisture "Garage Portal" (gMoisture)  {channel="mqtt:topic:mosquitto:PyPortal_Garage:moisture"}
Number Mqtt_PyPortal_Garage_moisture_percent "Garage Portal" (gMoisture) {channel="mqtt:topic:mosquitto:PyPortal_Garage:moisture"[profile="transform:JS", function="serialHumidity.js",sourceFormat="%.3f"]}

I’ve tried different source formats…

the javascript looks like this:

(function(i) {
    i = i - 300
    i = i / 700 * 100
    return i;

I’ve simplified the javascript for testing - don’t worry about the values or script name. The incoming channel is a number.

I get errors like these:

2020-12-05 11:31:56.185 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state '522' with function 'serialHumidity.js' and format '%.0f'

2020-12-05 11:35:09.249 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state ‘524’ with function ‘serialHumidity.js’ and format ‘%.3f’
2020-12-05 11:40:04.570 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state ‘524’ with function ‘serialHumidity.js’ and format ‘%.3f’

Can you show us the full MQTT message that you’re receiving? I suspect this has something to do with Javascript transforms only working with strings, so your i in your Javascript is a string, and needs to first be transformed into a Javascript number, I think…or is it that the Javascript Transformation can only return a string…can’t remember!

1 Like

Thanks - I’m just using MQTT.FX to send the integer to the MQTT path. I was not aware of the string issue so I’ll tinker with that. Sending the value in MQTT as an integer…

I’m trying to back all the way to the beginning with string values and build up from there, step by step…

Yeah, it works just like I’d want it to if I leave the item as a string type.
If I want to have the item be a Number type, it seems like I need to use a rule.
Was just trying to keep this neat.
Am I missing something?

What are you ultimately trying to do with the final Item? Just display it? If so, then it doesn’t really matter what type it is…

It will be used to trigger some routines based on measurements. I realize I can convert it to a number as needed but that will probably cause other problems. I guess if I’m going to write some rules around it I’ll do a transformation in a rule.

1 Like

and influxdb/grafana charting

Here’s the core issue:

And here’s the fix, but only for openHAB3, not 2.5.