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"] }
(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?
(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