So, I’m trying to get some data from MQTT, but need to filter it out and return nothing to openhab in some (most) cases. I’m using a JS transform and right now, if I return “null” as in “no data”, I get my logs filled with warnings of the type
[WARN ] [ab.binding.mqtt.generic.ChannelState] - Incoming payload ‘null’ not supported by type ‘NumberValue’
The data comes in the form of
{ sensor_id : number, sensorvalue: value }
(yes, this is domoticz, but data comes on multiple ids and some need to be filtered out as they’re duplicated and/or carrying incorrect data, on some sensor_ids but not on others).
I would like to “multiplex” this data in a thing with different channels. So far, I can get the data, process it in a JS transformation (simple version below) and only output to a specified channel if the index on that sensor maches, and if data is deemed correct (i.e. I do some things under the hood to decide this).
So, to explain further, here’s a thing:
Thing mqtt:topic:domoticz "Domoticz" (mqtt:broker:2af10a50) @ "Domoticz" {
Channels:
Type number : rssi "RSSI" [ stateTopic="domoticz/out", transformationPattern="JS:domo-rssi.js"
Type number : voltage "Voltage" [ stateTopic="domoticz/out", transformationPattern="JS:domo-voltage.js" ]
} // end of thing
And here’s a corresponding JS:
(function(i) {
var d = JSON.parse(i);
if ( d.idx == 23 )
return parseFloat(d.RSSI);
return null;
})(input)
Expanding futher (to clarify myself):
Say I have a “voltage” on sensors 10 and 15. I’d like to filter out the sensor id 10 and get the voltage from sensor 15 only. I tried using JSONPATH but failed. Perhaps that is also a path.
At the moment, all javascript transforms will be called for all channels each time new data comes in. Only the channel with the correct ID must return a valid data. All others should “abort” and not return anything to openhab.
And one more issue: It would be enormously beneficial to be able to pass multiple parameters to the JS, as in transformationPattern=“JS:myfunc.js(parameter,…)” where that would expand in:
(function(i,parameter,...) {
...
})(input,parameter,...)
It would be such a huge improvement!
BTW, I’m using openhabian2 on a raspberry pi 3B+ and I’m running the latest snapshot from GIT.