NOTE: Please upgrade if you installed this prior to 2022-10-19. There is a pretty bad bug in earlier versions.
This rule template addresses the problem where one wants to do something periodically after an Item changes from a given state for long enough. For example, send an alert when a door has been open for 15 minutes and repeat the alert every five minutes until the door is closed. Or wait until a motion sensor stops triggering for five minutes and turn off the light.
This rule template is closely related to Threshold Alert. The primary difference is Threashold Alert looks for numerical values above or below a given value where this one looks for Item states that are exactly the same or exactly different from a given state. In other words, Threshold Alert is for number Items and this is for Switch, Contact, or looking for Items going to NULL
or UNDEF
. Some day I hope to merge them but the configuration is going to be challenging.
In addition to supporting an initial timeout (how long the Item should be in the alert state before alerting) and a potentially different repeat timeout (how often to generate the alert again), the template also supports an optional time period where the alerts are not allowed so, for example, you can prevent an alert on your phone every five minutes during the day when you donāt care as much if the window is open. Any alert that occurs during this time will be moved to the end of the period, as long as the Item remains in the alerting state.
The rule has a number of properties.
Property | Required | Default | Description |
---|---|---|---|
Group of Triggering Items | X | The Group containing the Items this rule will monitor. | |
Alert State | X | The state the Item has to be different from for alerts. For example, an open door reminder would use CLOSED as the Alert State. |
|
Invert | false |
If checked, it inverts the meaning of Alert State so the alerts are sent when the Item is the same as Alert State instead of different. | |
Default Initial Timeout | PT15m | By default how long the Item needs to be different from the Good State before the first time the alert script is called. Set to āPT0sā to run immediately. | |
Default Item Initial Timeout Metadata | rem_time | The Default Initial Timeout can be overridden on a per Item basis by adding metadata to the Items. The value of the metadata is used and should be in duration format (see below). | |
Repeat Period | How often to call the alert script again after the first initial timeout. If left blank no repeats will be performed. | ||
Reschedule | false | If the rule is triggered again with the alerting state, whether to reschedule the looping timer or ignore the state change. Default is to ignore the state change. | |
Alert Rule | Rule UID to be called after the initial timeout and again every repeat period. | ||
Alert Starting Time | 00:00 | The time to start allowing the rule to call the alert rule. | |
Alerting Ending Time | 00:00 | The time to stop allowing the rule to call the alert rule. |
If you need to have a different trigger type than changed
, you can edit the rule after you create it. Unfortunately it appears that the trigger type cannot be a rule template parameter.
The initial timeout, value of the initial timeout metadata, and repeat period should all be in ISO-8601 Duration format. In general that will be a String starting with PT
followed by numbers and a designator for time units. For example, one hour and two seconds would be PT1h2s
.
You provide the Item metadata namespace for the default Item initial timeout metadata. The value of this metadata will be the initial timeout duration used for that Item. In a text Item, assuming the namespace is ārem_timeā that will look like rem_time=PT10m
. In the UI add metadata named rem_time
and supply the timeout String as the value.
If the alerting starting time and ending time are the same, the rule will call the alert script with no time based restrictions. If the starting time is later than the ending time, the time period is assumed to span midnight (e.g. if starting time is 09:00 and ending time is 21:00, the do not disturb period will be between 9:00 pm and 9:00 AM the next morning). If an alert is looping/ongoing or an alert needs to start during the do not disturb period, the alerts will be moved to the end of the period, as long as the Item remains in the alerting state.
The call to your alerting rule will follow any conditions defined there so you can provide additional restrictions as desired (for example, only alert on an open window when the AC is ON).
If this rule is triggered and you manually edit it, be sure to run the rule manually to clear out the old looping timers or you will see ācontext has been closedā errors in the log.
Language: ECMAScript 2011
Dependencies:
- JS Scripting add-on
- openhab-js version after Aug. 17, 2022 (until the fix makes it into the release to install the fix from the
$OH_CONF/autmation/js
folder runnpm install openhab
). Remember to remove it or manually keep it up to date. - openhab-rules-tools 1.1.2 or later (to install navigate to the
$OH_CONF/automation/js
directory and runnpm install openhab_rules_tools
).
To Do
- see if there is any way to merge this and Threashold Alert into one rule template
Changelog
Version 0.7
- fixed a pretty significant two part bug. The first part was the test to see if the timer was schedule to go off between DND times failed if the DND spanned midnight. This was actually a bug in the upstream openhab-js and Iāve submitted a fix there and added a work around here. The second was if the DND time spanned midnight, the rule would erroneously schedule the looping timer to repeat at the DND end time today which resulted in a time in the past and the loop would execute immediately creating a near infinite loop.
Version 0.6
- added an option to reschedule the timer in cases where the rule is triggered with the alerting state and an Item already has a Timer scheduled. This is useful for stuff like motion sensor timers where you want something to occur only after the Item has been in the alerting state for the timeout as opposed to wanting an alert from the first event.
Version 0.5
-
Changed how I avoid timers going off at the same time to be based on the actual scheduled timerās times instead of when the rule is triggered. This avoids collisions when the timeout times are different for each Item as well as avoids the looping reschedules from colliding.
-
If an alert is looping, or a new alert occurs during the do not disturb period, move the next alert to the end of the do not disturb period instead of canceling it entirely.
-
Added the ability to choose the event that triggers the rule to support more use cases.Apparently this is not supported at this time.
Version 0.4
- if a lot of Items match all at once, the timers will be triggered on top of each other causing āmultithreadedā exceptions. This version staggers the timers half a second apart when they occur too close together. This staggering will eventually be moved to all the timer libraries in openhab-rules-tools.
Note this change corresponds with a requirement to upgrade openhab-rules-tools to v1.1.2.
Version 0.3
- added the ability to invert the match so any Item that does match the āgood stateā triggers the alert.
Version 0.2
- added ability to not repeat the alert by setting the
repeatPeriod
to the empty string.
Version 0.1
- initial release