Note: This is a complete rewrite of the previous Threshold Alert and Open Reminder rules. In addition to merging the behaviors, the behaviors are slight different from both of these predecessors. Please read carefully.
See Threshold Alert and Open Reminder [4.0.0.0;4.1.0.0) - #2 by rlkoshak for configuration examples. This is template can do a lot of different things depending on the parameters configured.
Problem Statement
Frequently one wants to do something when one or more Items exceed or fall below a certain threshold, matches a given state for a given amount of time, or fails to change state (or update) for a certain amount of time.
What the Rule Does
This rule is triggered by changes to the members of a Group. The Group should have members that are all of the same type of Item. At this time it does not support a mix of compatible Item types (e.g. Switches, Dimmers and Color Items) except in the case where itās looking for NULL
or UNDEF
as the alerting state.
When the rule triggers, it compares the state of the Item with a threshold state using a user selected comparison.
- If the comparison evaluates to
true
(orfalse
if invert is enabled), an optional user defined rule is called. - After a user defined delay an optional alert rule is called.
- When the Item leaves this alerting state, the rule will optionally call an end alert rule, but only if the threshold alert rule was called.
- Any call to any rule that occurs during a user defined Do Not Disturb (DND) period are suppressed to the end of that period.
- Finally, an optional reminder period can be defined to call the alert rule repeatedly while the Item continues to meet the threshold comparison. These reminders also follow the do not disturb period.
The alert rule and end alert rule will receive these parameters passed to them when called:
Variable | Purpose |
---|---|
alertItem |
name of the Item that generated the alert |
alertState |
current state of the Item |
isAlerting |
boolean, when true the Item meets the threshold comparison, when false the Item exited the threshold state |
isInitialAlert |
boolean, when true the Item just now met the threshold comparison, when `false, the Item has been in this state for awhile. |
threshItems |
JavaScript Array of JavaScript Item Objects whose state meet the alerting criteria |
threshItemLabels |
JavaScript Array of the Item labels whose state meet the alerting criteria |
nullItems |
JavaScript Array of JavaScript Item Objects whose state is NULL or UNDEF
|
nullItemLabels |
JavaScript Array of the Item labels whose state is NULL or UNDEF
|
To access these attributes in your called rule varies from language to language. Blockly has a context
block. JS Scripting ECMAScript 2021 injects them as variables (e.g. alertItem
). Nashorn JS Scripting (and other JSR223 rules engines) makes them available via context.getAttribute('alertItem');
.
Rule Properties
Property | Format | Required | Default | Purpose |
---|---|---|---|---|
Triggering Group | Group Item | X | Changes to itās members cause the rule to run. | |
Threshold State | String | X | The state to compare the members of Triggering Group to using the Comparison Operator. If itās parsable as a number, a number comparison will be made. If itās a number with units, a Quantity comparison will be made. UnDefType , NULL and UNDEF are all normalized to UnDefType and treated as the same state. In all other cases a string comparison will be made. |
|
Comparison Operator | X | The comparison operator to use (e.g. == ) |
||
Invert Comparison | Boolean | false |
Invert the result of the comparison. | |
Reschedule | Boolean | false |
When true , if an alert event occurs when an alerting timer already exists, the timer will be rescheduled instead of ignoring the new event. The different is whether the alert occurs based on the first alerting event or the most recent alerting event. |
|
Hysteresis | String | '' |
Optional parameter which, when populated, will wait until the Itemās state changes at least that amount from the threshold before disabling the alert. Only applied for states which are Numbers or QuantityTypes . If the comparison is > , the Item will need to drop at least this amount below the threshold before alerting ends, and vice versa for < . If == the value needs to move above or below the threshold by this amount. It doesnāt make sense to use with != . |
|
Alert Delay | ISO8601 duration string (e.g. PT15M) | '' |
How long to wait after the Item meets the threshold comparison before calling the Alert Rule. Leave blank to call the rule immediately. | |
Reminder Period | ISO8601 duration string | '' |
How long to wait after the first call to the alert rule before calling it again as a reminder. The rule will continue to be recalled by this period until the Item no longer meets the threshold comparison. | |
Metadata Namesapce | String | thresholdAlert |
An optional metadata namespace where a per Item override for Alert Delan and Reminder Period can be defined (see below). | |
Alert Rule | Rule UID | X | Rule called when the Item meets the threshold comparison and repeatedly thereafter if Reminder Period is defined. | |
End Alert Rule | Rule UID | '' |
Rule called when the Item first exits the threshold comparison. This can be the same as Alert Rule or Initial Alert Rule. | |
Initial Alert Rule | Rule UID | '' |
Rule called when the Item first meets the threshold comparison. This can be the same as Alert Rule or the End Alert Rule. | |
Do Not Disturb Start Time | HH:MM |
'' |
The start time for the optional DND. If this is after the End DND time, the period will span midnight. | |
Do Not Disturb End Time | HH:MM |
'' |
The end time for the optional DND. The most recent call to either Alert Rule or End Alert Rule will be delated until this time. | |
Gatekeeper Delay | Number | 0 |
The minimum amount of time between calls to the alert and end alert rules before the next call will be allowed to occur. If you experience āMulti-threadedā exceptions, increase this time until they stop. The time is in milliseconds. | |
Rate Limit | ISO8601 Duration String | '' |
The minimum amount of time between calls to the alert rule before the next call will be allowed to occur. Unlike Gatekeeper Delay, the rate limit will simply drop any events that occur rather than queueing them up and working them off in sequence. If used with Gatekeeper Delay, Rate Limit should be significantly longer. |
Any or all of the above properties except for Triggering Group and Metadata Namespace can be defined at the Item level using Item metadata with the addition of one extra parameter. Instead of threshold
, a thresholdItem
can be defined which will cause the state of that Item to be used as the threshold instead of the statically defined threshold. In the UI a full set of metadata should look like the following
value: " "
config:
thresholdItem: OfficeHumidity_Setpoint
operator: <
invert: false
reschedule: false
hysteresis: 4 %
alertDelay: PT1M
remPeriod: PT15M
alertRuleID: new_humidity_proc
endRuleID: new_humidity_proc
initAlertRuleID: new_humidity_proc
dndStart: 22:01
dndEnd: 08:02
gatekeeperDelay: 10
rateLimit: PT30M
All of these metadata are optional. If missing, the ruleās setting will be used for that Item. dndStart
and dndEnd
must both be present or both be absent and represent a time using either 24 hour (e.g. 22:00) or 12 hour (e.g. 10:00 PM) format. gatekeeperDelay
must be a positive integer.
Running the rule manually will cause the rule to verify the configuration of the rule parameters and the Item metadata configurations. Watch the logs for warnings and errors.
Use Cases
- Generate an alert and reminders when a door is left open for too long.
- Turn on a humidifier when the humidity gets too low, turn it back off when rises back to the threshold.
- Take remedial action (e.g. restart it) when a service goes offline.
- Alert when a sensorās state remains
UNDEF
orNULL
for too long. - Control a light based on a motion sensor.
Requirements
Language: JS Scripting ECMAScript 2021
Dependencies:
- A script to call when an Item meets the threshold
- A Group containing all the Items to trigger this rule. All members of the Group must be the same type.
- openhab-js 4.1+
- openhab_rules_tools 2.0.2+
TODO:
- DateTime comparisons (i.e. a DateTime Itemās state is too long ago)
- Restart alerting on OH startup
- Make initAlertRule follow DND
- Support using separate DateTime Items for DND periods (so we can use Astro based times)
- Option to operate at the semantic model equipment level (alert is called on Equipment, not point Items)
- Ignore events during DND instead of moving them to the end of DND
- Figure out why Iām still getting Multi-threaded exceptions even with a half second gatekeeper (mostly during startup)
- Figure out
Failed to execute rule 'sensor-status-detection': Fail to execute action: 1
even though the rule appears to run Support standard motion sensor timer use cases (e.g. turning on/off lights).Normalize UnDefType so it matches both NULL and UNDEFOption to call a rule when alert timer is created, when it expires, reminder, and when Item is not alerting any morebring back the summary list of Items that meet the thresholdbring back the rate limithysteresis-
Support for Thing status eventsSee Thing Status Reporting [4.0.0.0;4.1.0.0) , Itās going to be too much to add support for that here. Generate an alert if an Item doesnāt change for too longAllow most of the properties to be overridden through Item metadataProvide override to threshold in another Item (identified via metadata) for use cases like controlling a device that is controlled by a setpoint Item-
Address multithreaded exception that occurs during startup if the rule gets pounded. Itās less than it used to be but still occurs occasionally for me.See Delay Start [4.0.0.0;4.1.0.0), note that at least on OH 4.0 SNAPSHOT #3368 the error does not occur for me. Catch exception in cases where called alert or end alert rule is disabled.
Changelog
Version 0.9
- give each Item itās own logger to make tracing logs easier
- schedule initAlertTimer to the end of DND like the alertTimer and endAlertTimer
Version 0.8
- normalize
UNDEF
andNULL
toUnDefType
so both are treated as the same - added an initial alert rule called when the Item first enters the alerting state to make motion sensor light type use cases easier to implement
Version 0.7
- refresh the Itemās settings inside the looping timer so reminders always run with the latest Item metadata settings
- cancel any running timers in
int()
Version 0.6
- added a
reschedule
parameter which causes the rule to call the alert rule alert duration after the most recent alerting event instead of after the first alerting event. This implements alerting when an Item doesnāt change for too long a time, mostion sensor timers, etcā¦
Version 0.5
- added ability to define another Item whose state will be used as the threshold (
thresholdItem
) - any and all rule properties can be overridden through Item metadata
- todo: update the docs above to cover the metadata and throughly test the config checking
Version 0.4
- added rate limiting option
Version 0.3
- catch and log exception if the alerting rule no longer exists or is disabled
- pass a list of all the Items and their labels that are null/undef and those that are alerting to have parity with the old Threshold Alert rule to ease migration.
Version 0.2
- added support for hysteresis
Version 0.1
- initial release