Z-ware spikes (misreads?) avoiding

Hi,

I get sometimes values back from a Z-wave sensor what is incorrect. Mostly related to temperature (zero value). I want to avoid this but are struggling to solve this.
Please advice if the approach below is ‘totally’ wrong and if not how to fix the syntax errors. I never used transform before. Sorry for the stupid question.

I created a div.js file in the transform folder with the following
(function(i,y) {
if (i + 1 >= y) return (y);
if (i - 1 <= y) return (y);
return (i);
})(input)

The idea is that I call this function with the current temperature and the previous correct temperate. If the new received temperature is > previous + 1 or less than previous -1 I return the previous correct temperature what means ignore the new received value.

I want to call this from a rule as below.

rule “Temperatuur controle”
when
Item Temperatuur_gBGWoonkamer changed
then {
// get multiple errors on the next line but don’t know how to solve it. Tried multiple ways to solve it without success.
Temperatuur_gBGWoonkamer.state = transform=“JS(div.js)”, “tempValue1”, “Temperatuur_gBGWoonkamer.state”)

tempValue1 = Temperatuur_gBGWoonkamer.state as Number

}
end

I solved this by creating a ‘pre’ item that I check in a rule and post update to the real item I want to monitor. In my case I was throwing out bogus values, but you can adjust as needed. I didn’t try it with the transform. Here’s a sample rule where I only want to get humidity values less than 100% (can’t have anything higher than this, but the sensor sometimes gives nonsensical values):

rule "Filter Humidity for out of range values"
when
        Item OUTDOOR_HUMIDITY_PRE changed
then
    var Number temp
    if (OUTDOOR_HUMIDITY_PRE.state instanceof DecimalType) {
        temp = OUTDOOR_HUMIDITY_PRE.state as DecimalType
        if (temp <= 100.0) {
            OUTDOOR_HUMIDITY.postUpdate(temp)
        } else {
          logWarn("Humidity Filter","ERROR - not updating OUTDOOR_HUMIDITY > 100")
        }
    }
end

You cannot use a transform here, because transformation service only accepts one input. You could do the same simple arithmetic directly in the rule though, as @jswim788 shows.