Edit: Rewritten for OH 4.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for a description of what Design Patterns are and how to use them.
Problem Statement
Many sensors report status to openHAB periodically (e.g. temperature sensor) or unpredictably (e.g. motion sensors) and a subset of these sensors do not have a built in way for openHAB to query whether or not they are alive. The only way for openHAB to detect that these devices are still alive is to assume they are dead if they have not communicated after a certain period of time.
Concept
Create Timers that get reset every time the sensor Item receives and update. If a certain amount of time passes (I recommend at least 2x the deviceās normal reporting period) perform some action to respond to the no longer reporting sensor.
Rule Template
There is rule template on the marketplace that implements this design pattern.
Install Template and Instantiate and Configure the Rule
This rule template implements this design pattern. Configure it as follows:
-
Create a Group and add all the Items that represent a sensor reading to that Group. Weāll call it āAllSensorsā.
-
Add
expire
metadata to all the Items added to the AllSensors Group so that they get set toUNDEF
if they do not receive an update for too long. -
Create a rule or a script that will be called when the sensor is determined to be offline. This rule can do anything you want including sending an alert, changing the state of a Switch Item (tracks the online status of the sensor), take remedial action like restarting a binding or a service, etc. See below for an example that sends an alert when a sensor stops reporting and starts reporting again.
-
Install the Threshold Alert rule template from MainUI ā Add-on Store ā Automation ā Rule Templates.
-
Create a new rule and choose "Threshold Alert " as the āCreate from Templateā. This will provide a form to enter the rule template parameters which will customize the behavior of this instance of the template.
-
Configure the rule as follows (if a parameter is not mentioned, you can leave it as the default or set it as desired):
Property | Value | Purpose |
---|---|---|
Rule UID | Something meaningful | the unique identifier for the rule that is about to be created |
Name | Something meaningful | Name under which the rule will appear under Settings. ā Rules |
Description | Something meaningful | A sentence or two explaining what the rule does |
Triggering Group | AllSensors |
The group created in 1 above, select from your Items. Changes to this Groupās members trigger the rule. |
Threshold State | UNDEF |
The state to look for and alert on |
Alert Delay | 'PT15M` | An ISO8601 duration indicating how long the Item should be in the UNDEF state before calling the alerting rule. In this case we use 15 minutes. |
Reminder Period | PT24H |
An ISO8601 duration indicating how long to wait before calling the alert rule again if the Item remains UNDEF after the initial alert. In this case we get a reminder every day if the Item remains UNDEF . |
Alert Rule | UID of the Rule created in 3 above | This is the rule that gets called when an Item remains UNDEF for 15 minutes. |
- Click āSaveā and you are done.
If you want an alert when the sensor comes back after it has been alerted as offline, create the rule to alert and set it for the āEnd Alert Ruleā property. It can be the same rule as the āAlert Ruleā.
The rules that get called with have values passed into them including the name (alertItem
) and state (alertState
) of the Item that is UNDEF
and whether the Item is alerting (isAlerting
). See the rule template docs for details.
Processing Rule
The rule you created in step 3 above will be called by the rule instantiated from the rule template.
Blockly
As previously mentioned, the called script can do anything desired. Iāve set mine up to keep track if weāve already alerted on this piece of Equipment (based on the Semantic Model and with the help of a Switch Item) so I only get one alert for the whole equipment, even if more than one sensor went offline for that Equipment.
JS Scripting
var msg = (isAlerting) ? ' is offline!' : ' is reporting again!';
actions.NotificationAction.sendNotification('test@example.org', items[alertItem].label + msg);
Rules DSL
Unfortunately Rules DSL does not support accessing the variables passed in from the calling rule.
Timestamps
If all you need is a timestamp of when the last time the sensor reported instead of generating an alert or taking some action, you should create a DateTime
Item to represent the last report time of a given sensor. Link that Item to the Channel(s) that represent the sensor and apply the timestamp
Profile. Any time any of the linked Channels receives an update, the Item will be updated to now.