[SOLVED] MQTT: How to filter implausible values on channel?

I am trying to solve a problem with sensors that send implausible values from time to time. The values are transferred to OH by MQTT. Some outside temperature sensors report correct temperatures every minute but once or twice a day temperatures > 1000 degrees Celsius (which obviously is false).
Is there a way to filter out these values > 1000? I found some information on Regex filters but this seems not to be suitable for my intended use case. Can I also filter values based on logical operations like numer intervals (e.g. temperatures only valid if >= -100 and <= 100 ) ?
As I am using the sensor values in charts a single sensor reading with e.g. 2600 degrees will mess up the complete graphics as you cannot scale the graphics in a suitable way anymore.
Any help or hints are highly appreciated!

1 Like

Are these reported as just numbers or encoded in JOSN or XML or some other way?

If they are just numbers you can use a JS transformation and return “UNDEF” if it’s out of range. UNDEF shouldn’t get saved to persistence so your charts shouldn’t be messed up.

5 Likes

Thank you very much, that did the trick!
So my new JS transformation function “filter_ltminus100_gt100.js” which filters all values above 100 degrees and below -100 degrees now looks like this:

(function(i) {
	var val = parseFloat(i);
	if (val >= 100 || val <= -100 )
		{ return 'UNDEF'; }
	else
		{ return val; }
})(input)

and the channel definition in the .things file like this:

Type number :   TE_MJHTV1_5C8E  "TE_MJHTV1_5C8E "        [ stateTopic="OHAB/TE_MJHTV1_5C8E", transformationPattern="JS:filter_ltminus100_gt100.js" ]

Again thank you very much @rlkoshak , that solves my problem!

5 Likes