I’m working on generalizing and consolidating a set of rules and am taking a first stab at using lambdas and HashMaps to do so. I have the following in my rules file:
import static java.lang.Math.*
import java.util.HashMap
val HashMap<String, Timer> WiB_expire_timers = new HashMap<String, Timer>()
val WiB_timer_set_or_reset = [String room, long seconds_delay |
if (WiB_expire_timers.get(room) !== null) {
WiB_expire_timers.get(room).reschedule(now.plusSeconds(seconds_delay))
}
else {
WiB_expire_timers.put(room, createTimer(now.plusSeconds(seconds_delay), [|
sendCommand("occupied_"+room, "OFF")
])
)
}
]
val WiB_timer_cancel = [String room |
if (WiB_expire_timers.get(room) !== null) {
WiB_expire_timers.get(room).cancel()
}
]
rule "Update bathroom Wasp-in-Box Box State"
when
Item bathroomDoor changed to OPEN or
Item bathroomDoor changed to CLOSED
then
WiBbox_bathroom.postUpdate(newState)
if (newState == OPEN) {
WiB_timer_set_or_reset.apply("bathroom", 3*60 as long)
}
end
rule "Update bathroom occupancy"
when
Item bathroomPIR received update ON or
Item bathroomDoorPIR received update ON
then
if (apartmentOccupancy.state == ON) {
sendCommand(occupied_bathroom, ON)
if (WiBbox_bathroom.state == OPEN) {
WiB_timer_set_or_reset.apply("bathroom", 3*60 as long)
}
else if (WiBbox_bathroom.state == CLOSED) {
WiB_timer_cancel.apply("bathroom")
}
}
end
However, when the lambda WiB_timer_set_or_reset
gets called, I get the following error in the log:
2022-02-07 12:41:32.920 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'presence-11' failed: cannot invoke method public java.lang.Object java.util.HashMap.get(java.lang.Object) on null in presence
I followed a few examples from the forum when writing the code, and have looked through the HashMap documentation, but I can’t seem to figure out the problem. Any help would be greatly appreciated!
- Platform information:
- Hardware: x86_64 server
- OS: Linux Mint 20
- Java Runtime Environment: openjdk 11.0.11
- openHAB version: 3.2