Notifications in group design pattern

I forgot to address this issue in my previous response. I’ve written up how I do this in the Event Limit Design Pattern with a more complete example in the Generic Is Alive Design Pattern. And since I’ve written those, I’ve started applying the Assocaited Items Design Pattern to store the boolean indicating whether the alert has been sent instead of in global variables.

Something like (note I’m currently testing this code, there may be logical errors):

import java.util.concurrent.locks.ReentrantLock

val ReentrantLock statusLock = new ReentrantLock

rule "A sensor changed its online state"
when
	Item gSensorStatus received update
then
    try {
    	statusLock.lock
    	Thread::sleep(100)
    	val recentUpdates = gSensorStatus.members.filter[sensor|sensor.lastUpdate("mapdb").isBefore(now.minusSeconds(1).millis)]
    	recentUpdates.forEach[sensor|
    		val alerted = gOfflineAlerted.members.filter[a|a.name==sensor.name+"_Alerted"].head
    		
    		if((alerted.state == ON && sensor.state == ON) ||
    		   (alerted.state == OFF && sensor.state == OFF)){
    			aInfo.sendCommand(transform("MAP", "admin.map", sensor.name) + " is now " + transform("MAP", "admin.map", sensor.state.toString) + "!")
    			alerted.postUpdate(if(sensor.state == ON) OFF else ON)
    		}
    	]
    }
    catch(Exception e){
    	logError("admin", "Error processing an online status change: " + e.toString)
    }
    finally {
    	statusLock.unlock
    }
end

In the above code, if the Associated Alerted Item is ON and the sensor has changed state to ON I send an alert and change the alerted flag to OFF. If the sensor has changed to OFF and alerted is OFF I send an alert and change the state to ON. If the sensor’s state is ON and alerted is OFF I know I’ve already sent an alert so no new alert needs to be sent. Similarly, if the alerted is ON and the sensor’s state is OFF I know I’ve already sent an alert so no new alert needs to be sent.

Because I’m using Items and restoreOnStartup this information gets persisted across OH reboots and rules edits.