ClassCastException in rule

Hi,

I’m trying to write a rule to reset an item in a group after a period of time, when the group is updated. The group in question is defined as a Switch group using the OR operator. Here’s my rule:

rule "Reset camera status after ten seconds"
when
    Item g_motion_external received update ON
then
    try {
        lock.lock
        Thread::sleep(100)
        logInfo("ResetCamera", "Camera reset starting")
        val trigger = g_motion_external.members.sortBy[lastUpdate].last as SwitchItem
        if(camera_timers.get(trigger) == null) {
            camera_timers.put(createTimer(now.plusSeconds(10)[ |
                postUpdate(trigger, OFF)
                logInfo("ResetCamera", "Resetting camera item")
            ]))
        }
        else camera_timers.get(trigger).reschedule(now.plusSeconds(10))
    } catch(Throwable t) {
        logError("ResetCamera", "Error processing camera reset: {}", t)
    } finally {
        lock.unlock
    }
end

In my log I’m getting the following message:

2015-11-25 21:46:57.565 [INFO ] [enhab.model.script.ResetCamera] - Camera reset starting
2015-11-25 21:46:57.753 [ERROR] [enhab.model.script.ResetCamera] - Error processing camera reset: java.lang.ClassCastException: Cannot cast org.openhab.core.library.items.SwitchItem to void

I’ve confirmed that this message relates to the obvious candidate line of:

val trigger = g_motion_external.members.sortBy[lastUpdate].last as SwitchItem

Anyone have any idea how to solve this?

Hi

Just an idea: At first I would remove the “as SwitchItem” and see what type you have for “trigger”. There seems to be problems with the members function (at least there was for me) and I used the allMembers function instead.

You migth try this as well.

Regards
Dieter

Your g_motion_external group is a switch but is every member of the group a SwitchItem? Are there any subgroups (which would be a GroupItem, not a SwitchItem)?

If you have subgroups that would explain the error. You have a Switch that is part of a subgroup which gets updated and then the subGroup gets updated so you end up with the subgroup instead of your switch. You can probably get around this by using allMembers as @DieterL suggests which will give you a flat list of all the Items that are members of the group and all subgroups in one big list. If you have any items that are not SwitchItems that are members of the group or subgroup you can add a filter[s|instanceof SwitchItem] (I’m pretty sure that will work) to filter out all the non-SwitchItems.

I’ve double checked this and yes, all items are Switches and there are no subgroups.

Upon trying that, I get a different error message, so perhaps it resolves the issue:

2015-11-26 07:42:18.054 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Reset camera status after ten seconds’: Index: 1, Size: 1

I haven’t had chance to look into this in detail, but I guess it relates to my use of the HashMap camera_timers. I’ll continue to debug tonight!

Thanks for the help so far.

Wait, camera_timers is a hashMap? Upon looking closer…

When you add a timer to the hashMap you don’t provide the key. It should be

if(camera_timers.get(trigger) == null) {
    camera_timers.put(timer, createTimer(now.plusSeconds(10), [|

Yep, I just saw that too. Though I won’t be able to try it until tonight.

Thanks.