previousState is not working correct. I open a Ticket, but it is not solved at the moment.
What I do is a little bit different.
I start the timer if window is open and cancel the timer if window gets closed.
I wrote a procedure for this named escalationtimer. You can send a maximum time the window should be open before a message will send.
import java.util.HashMap
var HashMap<String, Timer> tEscalationTimer = newHashMap()
// relatedItem = Item welches überwacht wird
// tEscalationTimer = Hashmap Timer of the escalation
// iEscalationTimer = Hashmap min of the timer to run before escalate
// stEscalationTimer = Hashmap Message with put into stEskalationMessage Item
// iEscalationTimerStartInterval = min of the timer to run before escalate
// fuEscalationFunction = Function which should be executed if escalationinterval is reached
val Procedures$Procedure6<GenericItem, HashMap<String, Timer>, HashMap<String, Integer>, HashMap<String, String>, Integer, Functions$Function1<GenericItem, Boolean>> startEscalationTimer=[
relatedItem,
tEscalationTimer,
iEscalationTimer,
stEscalationTimer,
iEscalationTimerStartInterval,
fuEscalationFunction |
// little cosmetic if there is some error messages somewhere
if(stEscalationTimer.size == 0)
stEskalationMessage.postUpdate("")
// EscalationTimer should start
if(iEscalationTimerStartInterval != 0)
{
// set escalationsstartinterval in sec. to Hashmap
iEscalationTimer.put(relatedItem.name, iEscalationTimerStartInterval)
// if still an escalation timer for this item runs, restart it will full value
if(tEscalationTimer.get(relatedItem.name) !== null)
{
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " restarted with value " + iEscalationTimer.get(relatedItem.name) + " sec.")
tEscalationTimer.get(relatedItem.name).reschedule(now.plusSeconds(iEscalationTimer.get(relatedItem.name)))
}
else
{
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " started with value " + iEscalationTimer.get(relatedItem.name) + " sec.")
tEscalationTimer.put(relatedItem.name, createTimer(now.plusSeconds(iEscalationTimer.get(relatedItem.name)))
[ |
stEscalationTimer.put(relatedItem.name,"Problems with "+relatedItem.label)
stEskalationMessage.postUpdate("Problems with "+relatedItem.label)
// if escalationinterval is more then 5min restart timer
iEscalationTimer.put(relatedItem.name,iEscalationTimer.get(relatedItem.name)/2)
if(iEscalationTimer.get(relatedItem.name) >= 300)
{
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " decreased with value " + iEscalationTimer.get(relatedItem.name) + " sec.")
tEscalationTimer.get(relatedItem.name).reschedule(now.plusSeconds(iEscalationTimer.get(relatedItem.name)))
stEscalationTimer.put(relatedItem.name,"Problems with "+relatedItem.label)
stEskalationMessage.postUpdate("Problems with "+relatedItem.label)
// do whatever you need by every escalation interval change
logInfo("Loesslein", "Escalation for Item " + relatedItem.label + " initialized")
fuEscalationFunction.apply(relatedItem)
}
// If interinterval is shorter then 5min give up and end timer
else
{
// If after first escalation the time to no more escalation is reached put message to string
if(stEscalationTimer.get(relatedItem.name) !== null)
{
stEscalationTimer.put(relatedItem.name,"Problems with "+relatedItem.label)
stEskalationMessage.postUpdate("Problems with "+relatedItem.label)
}
tEscalationTimer.get(relatedItem.name).cancel()
tEscalationTimer.put(relatedItem.name, null)
tEscalationTimer.remove(relatedItem.name)
// do whatever you need by escalation ended because interval is to short
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " ended but not solved.")
fuEscalationFunction.apply(relatedItem)
}
])
}
}
// escalationTimer stopped manually because incident is no longer available
else
{
// timer still running, but no excalation needed any more
if(tEscalationTimer.get(relatedItem.name) !== null)
{
tEscalationTimer.get(relatedItem.name).cancel()
tEscalationTimer.put(relatedItem.name, null)
tEscalationTimer.remove(relatedItem.name)
// if an escalation message exist delete it from hashmap
if(stEscalationTimer.get(relatedItem.name) !== null)
stEscalationTimer.remove(relatedItem.name)
// if there are any other escalation messaged available post one of them
if(stEscalationTimer.size == 0)
stEskalationMessage.postUpdate("")
else
stEskalationMessage.postUpdate(stEscalationTimer.values.get(0))
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " no longer needed - canceled ")
}
// timer was ended before, now post that the incident is ended too
else
{
if(stEscalationTimer.get(relatedItem.name) !== null)
{
stEscalationTimer.remove(relatedItem.name)
if(stEscalationTimer.size == 0)
stEskalationMessage.postUpdate("")
else
stEskalationMessage.postUpdate(stEscalationTimer.values.get(0))
logInfo("Loesslein", "EscalationTimer for Item " + relatedItem.label + " solved after escalationinterval ended")
}
}
}
]
rule "Check bathroom window open too long"
when
Item DoorSensor01 changed
then
Thread::sleep(100)
if(DoorSensor01.state == OPEN)
{
startEscalationTimer.apply(DoorSensor01, tEscalationTimer, iEscalationTimer, stEscalationTimer, (nuDoorSensor01_Delay.state as DecimalType).intValue * 60, doEscalationForItem)
}
else if(DoorSensor01.state == CLOSED)
{
startEscalationTimer.apply(DoorSensor01, tEscalationTimer, iEscalationTimer, stEscalationTimer, 0, doEscalationForItem)
}
end
sitemap:
Text item=stEskalationMessage visibility=[stEskalationMessage!=""]