[SOLVED] VS Code Warning in rule

Hello,
Of all my rules I have 1 warning problem that pop up on VS Code.
The rule runs fine but I don’t like having a warning

The warning is:
Bounds mismatch: The type arguments <Item, State> are not a valid substitute for the bounded type parameters <T, C extends Comparable<? super C>> of the method sortBy(Iterable, Function1<? super T, C>)

and the rules code is:

rule "Battery Monitor"
when Time cron "0 30 9 * * ?"
then
    if (! Batteries.allMembers.filter([state < lowBatteryThreshold]).empty) {
        val report = Batteries.allMembers.filter([state instanceof Number]).sortBy([state]).map[name + ": " + state.format("%d%%")].join("\n")
        val message = "Battery levels:\n\n" + report + "\n\nRegards,\n\nOur House"
        sendMail("xxxxxxxx@gmail.com", "Low battery alert", message)
    }
end

This rule was pulled out straight out of example rules from the OH1 wiki.

Any way to get rid of it?
And what does it mean?

Thanks

I think the filter part is wrong, no real filter

I use something similar:

val report = gNumber.allMembers.filter([battery | (battery.state as Number) <= lowBatteryThreshold]).sortBy([state]).map

Actually it’s the sortBy that gets highlighted by VS Code
I get the same warning with your code.

Regards

here my complete code:

    if(!gBatterieLevelNU.allMembers.filter([battery | (battery.state as Number) <= (nuBatteryLowErrorLevel.state as Number)]).empty)
    {
        val report = gBatterieLevelNU.allMembers.filter([(state as Number) <= (nuBatteryLowErrorLevel.state as Number)]).sortBy([state as DecimalType]).map[
            name + " - " + label + ": " + state.format("%d%%") + "\n"
        ].join
        
		swBatteryLowError.postUpdate(ON)
		swBatteryLowWarning.postUpdate(OFF)
        val message = "Battery level reaches Error-Level:\n" + report
		sendMail("èmail-address", "Battery level reaches Error-Level", message)
    }

Perfect!
Thank you
I will now look at it in detail and learn

Cheers

Syntax checking and reporting has become much more strict in OH 2.2 compared to 1.x so there is bound to be a lot of similar warnings when grabbing examples from the 1.x wiki.

I assume that all members of Batteries are Number Items?

Try the following (note, most of your parens are not needed and since you don’t use the parens around the map I’ll remove them everywhere else they are not needed:

val report = Batteries.allMembers.filter[bat | bat.state instanceof Number].sortBy[state as Number].map[name + ": " + state.format("%d%%")].join("\n")

The major change is I define an argument to be passed to the lambda (which probably isn’t strictly required but it isn’t clear to me what is causing the error) and I cast the state in the sortBy to Number so we can be sure it will know how to sort it.

Keep in mind this is a warning, not an error. The code probably works as written. The warning itself, as arcane as it appears, is primarily complaining about the types passed to sortBy so my hope is by casting the state to a Number it will clear up the warning.

Thanks Rich,
It is working.
And now I understand the syntax much better.

Lambdas…

Vincent