Cant use Collections.sort in rules

I am trying to eliminate outliers in my temperature measurements. I am taking 5 measurements and storing each in an ArrayList. I am trying to get the median value which should be a simple matter with Collections.sort but designer marks it as “Couldn’t resolve reference to JvmIdentifiableElement ‘Collections’.” and the code does not work at runtime. Here’s the rule:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.Math
import java.util.List
import java.util.ArrayList
import java.util.Collections.*
import org.openhab.core.library.types.DecimalType.*


var ArrayList<Number> theArray = new ArrayList<Number>()
var int place=0

rule "Convert_Input1_To_Temp"
when
    Item Input1 received update
then
	var Number output
	var Number temp1
    var raw1 = (Input1.state as DecimalType).doubleValue
    raw1 = (raw1 + ((indexInput1.state as DecimalType) * 255)).doubleValue
temp1 = Math::log((10000*(860/raw1 -  1)).doubleValue)
temp1 = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp1 * temp1 ))* temp1 )
temp1 = temp1 - 273.15          // Convert Kelvin to Celcius
    postUpdate(SolarTemp, temp1)
    if(place<5){
    theArray.add(place, temp1)
    postUpdate(Place, place)
    place = place+1}
    else{
    place = 0
	var ArrayList<Number> arraySorted = new ArrayList<Number>()
	arraySorted = Collections.sort(theArray)
    output=arraySorted.get(2)
    postUpdate(Var1, output)
    postUpdate(Place, place)
    }

Do rules not support this method?

Use val arraySorted = theArray.sortBy[doubleValue]

The Rule’s DSL/Xtext has a different implementation for Collections from the Java standards.

Thanks! that works great

This problem was solved but I am now having another problem with the same rule.

The rule runs great for a little while but then gets stuck on a value. It’s like it is unable to overwrite the arrays. This has me puzzled because it usually gets through a number of cycles correctly before it gets stuck.

You need to add a whole lot of logging and post your current code, logs for when the rule works and log for when it doesn’t for us to have any hope to help.

It is probably worth opening a new thread as well.

I will open a new thread if I get a chance. thanks for your reply.