Java error iterating over group of items (previously worked)

Since upgrading my version of Java to the official JDK, I’m hitting an error with one of my scripts and it’s filling my debug log. The pertinent bit seems to be this:

2016-08-07 10:25:00.530 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Humidity Monitor
java.lang.RuntimeException: The name '<XMemberFeatureCallImplCustom>.forEach(<XClosureImplCustom>)' cannot be resolved to an item or type.

The rule that’s triggering it is this - it’s supposed to look all the sensor readings from the Humidity group, and set a string value as the name of which reading is highest.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.String

val int highHumidity = 60

rule "Humidity Monitor"
when Time cron "0 * * * * ?"
	var prevHigh = 0
	var highHum = ""

		if(hum.state as DecimalType > prevHigh){
			prevHigh = hum.state
			highHum = + ": " + hum.state + "%"
    logDebug("humidity.rules", highHum);

It was previously working fine, so I’m at a loss. Any ideas what’s wrong - or maybe a better approach entirely? Thanks!

I’m pretty sure you will have to define prevHigh and highHum outside the rule, as vars aren’t provided to lambdas. Furthermore I’m not sure if the semicolon after logDebug() is correct, but afaik it isn’t needed.

I didn’t test, but maybe this would do the same:

    var humHigh = Humidity?.members.sort().last.state as DecimalType
    var String humHighName = Humidity?.members.sort()

Thanks Udo - looks like the same error though ;(

2016-08-07 14:57:01.487 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Humidity Monitor
java.lang.RuntimeException: The name '<XMemberFeatureCallImplCustom>.sort()' cannot be resolved to an item or type.

If I scroll up a bit, I also see this:

2016-08-07 14:57:01.416 [ERROR] [x.x.scoping.XbaseScopeProvider] - error during scoping
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.eclipse.emf.ecore.resource.Resource$Internal

Different error after a restart (or one I couldnt see before, since the log was flooded):

2016-08-07 15:06:01.078 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Humidity Monitor
java.lang.ClassCastException: org.openhab.core.library.items.NumberItem cannot be cast to java.lang.Comparable

NumberItem can’t be sorted??

So another approach:
Set group to

Group:Number:MAX Humidity

and the rule:

rule "Humidity Monitor"
    Time cron "0 * * * * ?" //or: Item Humidity changed
    highHum = Humidity?.members.filter [f | (f.state as DecimalType) == (Humidity.state as DecimalType)] + ": " + Humidity.state.toString + "%"
    logDebug ("humidity.rules", highHum)
    Dehumidifier_Needed.postUpdate (highHum)

Perfect, thanks Udo! I’m not familiar with that kind of group usage yet, but it seems to have done the trick. I’m not getting any debug messages now for some reason, but I can live with that.

Thank you!