Design Pattern: Event Limit

Tags: #<Tag:0x00007f745aa92e20>

Problem Statement

Often one will be in a situation where an event will occur many times but an action should only be taken on the event once over a certain time period. This is best explained with an example.

For example, in my own rules I generate an alert when the temperature outside is a few degrees cooler than the inside (in the summer) to open the windows and another alert when it is warmer outside than inside to close the blinds.

Every time the temperature changes both inside and outside there is the potential to generate this alert. Given that some of the thermometers report every two minutes receiving an alert every time is unacceptable.

Concept

Create a flag which gets set to true when the first event occurs. Each subsequent event will cause check this flag and suppress the activity if the flag is set to true. At some point, either based on time, a Timer, or another event the flag gets set back to false so the activity can occur again the next time the event occurs.

Simple Example

In this example we only want one alert for a given event a day.

var boolean alerted = false

rule "Alerting event"
when
    // something happens that would generate an alert
then
    if(!alerted){
        // send alert
        alerted = true
    }
end

rule "Reset alerted"
when
    Time Midnight
then
    alerted = false
end

In the above example there is a flag set as a global. It starts out as false and the first time that an event occurs that generates an alert the flag is set to true. Each subsequent event will avoid sending the alert. At midnight the flag is reset to false.

Therefore, in effect, the alert will only go out once a day.

Complex Example

This design pattern is used in the Generic Is Alive design pattern, look for the code that uses the notification map. In that example, when OH first starts up it sets the notification flag to false for each Item. When an update occurs that sets that Item to ON an alert is generated. But these events can occur many many times so the notification flag keeps additional alerts from going out until the Item is set to OFF at which point an alert indicating the device is offline goes out. When it comes back an alert that it is online again goes out.

6 Likes