[SOLVED] Javascript transformation using openhab 2.4 and new MQTT binding

I’m using the lastest openhabian (2.4.0.1) on a raspi0 and have an attached ultrasonic depth sensor giving me depth measurements of my hydroponic nutrient reservoir. The depth sensor’s output is in cm depth from the reservoir’s lid. I would like to transform the depth measurement into a tank fill percentage (i.e. is the tank 10% full, 50% full…). To do this I’d like to use a simple javascript transformation. I’d also like to log the transformed value given I track my setup using graffana.

my mqtt.items file is as follows:

Number ReservoirDepth "Depth: [%.1f cm]"     <contactable>    { 
channel="mqtt:topic:mosquitto:greenhousePi3:reservoirDepthSensor" }
Number ReservoirDepthPercent "Reservoir Depth %" { 
channel="mqtt:topic:mosquitto:greenhousePi3:reservoirDepthSensor" [profile="transform:JS", 
function="DepthToPercent.js"] }

and my mqtt.things file is as follow:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhabian", clientID="openHAB2" ]
{

    
Thing topic greenhousePi3 "Greenhouse PI3" @ "Greenhouse" {
Channels:
    Type number : reservoirDepthSensor      "Reservoir Depth" [ stateTopic="greenhouse/rp3/RES-DEPTH" ]
		
}
}

My javascript file is a follows:

(function(dis) {
	var EmptyDis = 28
	var FullDis = 8
	var TotalDis= EmptyDis-FullDis
	
	percentFull=(EmptyDis-dis))/(TotalDis)*100
	
	return percentFull
})(input)

This is the log error I get:
2019-05-31 01:54:01.966 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state ‘25.8264541626’ with function ‘DepthToPercent.js’ and format ‘%s’

I’ve tried a few other configs but nothing seems to work. Any ideas?

Try this:

(function(i) {
    var distance = parseFloat(i) - 8;
    var percentmulti = 100 / 20;
    var percent = percentmulti * distance;
    return percent;
})(input)

you dont need two Items either. You can use the js transformation directly on the channel of the generic mqtt item. Not sure about the syntax but if you create a percentage type channel in paper ui you can just use this as a incoming transformation.
Than you would only need the Second Item.
Best Regards Johannes

@JGKK, thanks for your help. So it turns out I had a few issues. I was able to fix my javascript issue by adding the parseFloat function, but I still had issues with my .things file and .items file.

I wanted the raw depth number and the transformed number (i.e. percent) for logging. To accomplish this I added a 2nd channel to do the transformation (as you suggested). See .things file below

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhabian", clientID="openHAB2" ]

{

Thing topic greenhousePi3 "Greenhouse PI3" @ "Greenhouse" {
Channels:
    
	Type number : reservoirDepthPercent      "Reservoir Depth %" [ stateTopic="greenhouse/rp3/RES-DEPTH", transformationPattern="JS:DepthToPercent.js" ]
	Type number : reservoirDepthSensor      "Reservoir Depth" [ stateTopic="greenhouse/rp3/RES-DEPTH" ]
	
}

}

Specifying the transformation in the .items file never seemed to work so I removed it. See final .items file below:

Number ReservoirDepth "Reservoir Depth: [%.1f cm]"     <contactable>    { channel="mqtt:topic:mosquitto:greenhousePi3:reservoirDepthSensor" }
Number ReservoirDepthPercent "Reservoir Depth Percent [%.1f ] " { channel="mqtt:topic:mosquitto:greenhousePi3:reservoirDepthPercent"  }

The working javascript file is below:

(function(dis) {
	var EmptyDis = 28
	var FullDis = 8
	var TotalDis= EmptyDis-FullDis
	
	percentFull=(EmptyDis-parseFloat(dis))/(TotalDis)*100
	
	return percentFull
	//return 1
})(input)