Hi all,
I’m trying to work with a group rule and be able to create a timer. The issue I have is that I cannot figure out how to create the timer’s name dynamically based on the triggering items name.
Example: I have 6 items in this group. They are zwave sensors, which I grab their zwave_lastwake time form the API using the HTTP binding. If that doesn’t change in 3 hours I send alerts that the sensors battery is likely dead (because they always report 100% battery).
So In my normal (not group) rule I successfully do this like this for 1 item:
var Timer frontdoorSensor_Recent_Timer = null
rule "Front Door Sensor Last Wake check"
when
Item frontdoorSensor_LastUpdate changed //triggered when a new ZWAVE_LAST_WAKEUP is detected
then
logInfo("Front Door Sensor","!!!!!!!!!!!!!!!!!!! New Wakeup Time Received")
postUpdate(frontdoorSensor_Lowbat,"OK") //set lowbat to indicate battery is OK
if (frontdoorSensor_Recent_Timer !== null) { //create timer to trigger battery low if not reset before it runs out.
frontdoorSensor_Recent_Timer.reschedule(now.plusMinutes(180))
logInfo("frontdoorSensor_Recent_Timer","!!!!!!!!!!!!!!!!!!! Reset 3 hour battery timer for front door sensor")
} else {
frontdoorSensor_Recent_Timer = createTimer(now.plusMinutes(180)) [|
if (doorsensor_Lowbat_Notify.state == ON) {
pushoverActions.sendMessage("The Front Door Sensor appears to be dead, please check the battery", "openHAB")
ODTNotification.sendCommand("{\"title\": \"openHAB\", \"text\": \"The Front Door Sensor appears to be dead, please check the battery\", \"level\": \"critical\"}") //Send to Desktop Computer (ODT)
}
logInfo("Front Door Sensor","!!!!!!!!!!!!!!!!!!! Wakeup Check timed out")
postUpdate(frontdoorSensor_Lowbat,"Check")
]
logInfo("frontdoorSensor_Recent_Timer","!!!!!!!!!!!!!!!!!!! Created 3 hour battery timer for front door sensor")
}
end
This works great, however right now I repeat this same rule 6x for each of my 6 sensors. I am trying to move this to a group rule. My issue is that I cannot seem to figure out how to create a timer with a dynamic name.
import org.openhab.core.model.script.ScriptServiceUtil
var Timer frontdoorSensor_Recent_Timer = null
var Timer sidedoorSensor_Recent_Timer = null
var Timer backdoorSensor_Recent_Timer = null
var Timer upporchdoorSensor_Recent_Timer = null
var Timer sidedoorDoorbell_Recent_Timer = null
var Timer frontdoorDoorbell_Recent_Timer = null
rule "ZWave Sensor Last Wake check"
when
Member of gzwave_lastwake changed //triggered when a new ZWAVE_LAST_WAKEUP is detected
then
val Itemname = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.toString.replace("_LastUpdate",""))
val timerItem = ScriptServiceUtil.getItemRegistry.getItem(Itemname.name.toString + "_Recent_Timer")
val lowbatItem = ScriptServiceUtil.getItemRegistry.getItem(Itemname.name.toString + "_Lowbat")
logInfo("Zwave Sensor Check","!!!!!!!!!!!!!!!!!!! New Wakeup Time Received for " + triggeringItem.name.toString)
postUpdate(lowbatItem,"OK") //set lowbat to indicate battery is OK
if (timerItem !== null) { //create timer to trigger battery low if not reset before it runs out.
timerItem.reschedule(now.plusMinutes(180))
logInfo("Zwave Sensor Check","!!!!!!!!!!!!!!!!!!! Reset 3 hour battery timer for " + triggeringItem.name.toString)
} else {
timerItem = createTimer(now.plusMinutes(180)) [|
if (doorsensor_Lowbat_Notify.state == ON) {
pushoverActions.sendMessage("The Sensor \'" + Itemname + "\' appears to be dead, please check the battery", "openHAB")
ODTNotification.sendCommand("{\"title\": \"openHAB\", \"text\": \"The sensor \'" + Itemname + "\' appears to be dead, please check the battery\", \"level\": \"critical\"}") //Send to Desktop Computer (ODT)
}
logInfo("Zwave Sensor Check","!!!!!!!!!!!!!!!!!!! Wakeup Check timed out for " + triggeringItem.name.toString)
postUpdate(lowbatItem,"Check")
]
logInfo("Zwave Sensor Check","!!!!!!!!!!!!!!!!!!! Created 3 hour battery timer for " + triggeringItem.name.toString)
}
end
When this rule gets triggered I get the following error:
2022-01-24 10:58:16.160 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'battdet-1' failed: Item 'backdoorSensor_Recent_Timer' could not be found in the item registry in battdet
I (now) know its because my code is trying to find the Timer item in the registry but its not there since it isn’t defined in a .items file (and cannot be).
So, Is there a way to do what I am trying to do here and create a timer with different names based on the triggering item of a group rule?