Ah, thanks! I wanted to use a map, but couldn’t find the proper search incantation. The solution below seems to be working (note newHashMap
not createHashMap
). To be clear, if I actually wanted the global scope of
sharedCache
to make a value available between rules, I would need to run a nightly, move to another scripting engine, or wait for a release that includes it for Xtend? No other workaround for global scope?
import java.util.Map
val Map<String, Timer> timers = newHashMap
rule "Motion to Presence"
when
Member of gIndoor_Motion_Sensors changed
then
val triggeringItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItemName)
val actionItemName = triggeringItem.name.toString.replace("_Motion","_Presence")
if (ScriptServiceUtil.getItemRegistry.getItems(actionItemName).length < 1) return
var actionItem = ScriptServiceUtil.getItemRegistry.getItem( actionItemName )
var newValue = if (triggeringItem.state == OPEN) ON else OFF
logWarn("security", "transforming {}={} to {}={}", triggeringItem.name, triggeringItem.state, actionItem.name, newValue)
var presenceTimer = timers.get(actionItem.name.toString)
if (newValue == ON) {
actionItem.sendCommand(newValue)
if (presenceTimer === null) timers.put(actionItem.name.toString,
createTimer(now.plusSeconds(30)) [|
logWarn("presence", "{} timeout", actionItem.name.toString)
actionItem.sendCommand(OFF)
timers.remove(actionItem.name.toString)
]
) else presenceTimer.reschedule(now.plusSeconds(30))
}
end
2023-01-19 14:23:37.847 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BedroomsUp_Motion' changed from OPEN to CLOSED
2023-01-19 14:23:37.853 [WARN ] [g.openhab.core.model.script.security] - transforming BedroomsUp_Motion=CLOSED to BedroomsUp_Presence=OFF
2023-01-19 14:23:41.638 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BedroomsUp_Motion' changed from CLOSED to OPEN
2023-01-19 14:23:41.643 [WARN ] [g.openhab.core.model.script.security] - transforming BedroomsUp_Motion=OPEN to BedroomsUp_Presence=ON
2023-01-19 14:23:45.871 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BedroomsUp_Motion' changed from OPEN to CLOSED
2023-01-19 14:23:45.877 [WARN ] [g.openhab.core.model.script.security] - transforming BedroomsUp_Motion=CLOSED to BedroomsUp_Presence=OFF
2023-01-19 14:24:11.646 [WARN ] [g.openhab.core.model.script.presence] - BedroomsUp_Presence timeout
2023-01-19 14:24:11.647 [INFO ] [openhab.event.ItemCommandEvent ] - Item 'BedroomsUp_Presence' received command OFF
2023-01-19 14:24:11.648 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BedroomsUp_Presence' changed from ON to OFF
2023-01-19 14:24:11.648 [INFO ] [hab.event.GroupItemStateChangedEvent] - Item 'gEntry_Presence' changed from ON to OFF through BedroomsUp_Presence