Not really. You could easily combine this into one rule. You’d need one trigger per Thing though but the rule itself can be generic. The event passed into the rule will tell you which Thing triggered the rule.
However, for now, Things don’t generate events every time there is communication between it and the end device. For that you’d need a Channel linked to an Item and you can put all those into a Group and even get away with only the one trigger. You could even one of the rule templates to detect when events stop happening for a long time and call a rule you write to handle it, even further reducing the amount of code you need to write.
You could use Open Reminder [3.3.0;3.4.9) - #5 by tdlr (OH 3) or Threshold Alert and Open Reminder [4.0.0.0;4.9.9.9] (OH 4) detect when an Item linked to a Channel on the Thing to call a rule you write to send your alert or handle the case where it hasn’t updated in a given amount of time. All you’d have to write is a Script or rule to do the alerting/setting the Thing to OFFLINE, or what ever makes sense.
I do this with most of my home automation sensors.
For example, I’m on OH 4 so I use the second link. I put one Item that updates frequently from each sensor Equipment into a Group called Sensors
. I’ve configured the rule created from the template as follows:
configuration:
dndEnd: 08:00
rateLimit: ""
invert: true
initAlertRule: new-sensor-status-proc
dndStart: 20:00
alertRule: new-sensor-status-proc
endAlertRule: ""
thresholdState: UnDefType
defaultRemPeriod: ""
operator: ==
hysteresis: ""
reschedule: true
namespace: sensorStatus
gkDelay: 250
defaultAlertDelay: PT15M
group: Sensors
That config will call a rule new-sensor-status-proc
when a member of Sensors
remains in a state for 15 minutes or more or when the Item changes after new-sensor-status-proc
was previously called. You could change the trigger to the rule to received update if you want to count updates as well as changes.
There’s more stuff there (it will wait until 08:00 if an alert occurs after 20:00 to send the alert, the time can be overwritten on the Item level using sensorStatus
Item metadata, etc.).
The rule gets called with the name of the Item that timed out or came back. along with some additional information that can be used by the called rule.
My new-sensor-status-proc
rule looks like this:
configuration: {}
triggers: []
conditions:
- inputs: {}
id: "2"
configuration:
type: application/javascript
script: >
var equipment = actions.Semantics.getEquipment(items[alertItem]);
var statusItem = items[equipment.name+'_Status'];
if(equipment === null || statusItem === null) {
console.warn(alertItem + ' does not belong to an equipment or equipment doesn\'t have a Status Item!');
false;
}
else {
var statusItem = items[equipment.name+'_Status'];
console.debug('Sensor status reporting called for ' + alertItem + ', equipment ' + equipment.label + ', is alerting ' + isAlerting + ', and is initial alert ' + isInitialAlert);
// Sensor is back online Sensor went offline
(isInitialAlert && statusItem.state == 'OFF') || (!isInitialAlert && statusItem.state != 'OFF');
}
type: script.ScriptCondition
actions:
- inputs: {}
id: "1"
configuration:
type: application/javascript
script: >
var {alerting} = require('rlk_personal');
var logger = log('Sensor Alert');
var equipment = actions.Semantics.getEquipment(items[alertItem]);
var statusItem = items[equipment.name+'_Status'];
if(!isInitialAlert && statusItem.state != 'OFF') {
statusItem.postUpdate('OFF');
alerting.sendAlert('Offline: ' + equipment.label + ' has stopped reporting', logger);
}
else if(isInitialAlert && statusItem.state != 'ON') {
statusItem.postUpdate('ON');
alerting.sendAlert('Online: ' + equipment.label + ' is back', logger);
}
type: script.ScriptAction
It’s pretty simple. I have a condition to make sure that the Item is a member of an equipment. If it is an alert gets sent saying it’s stopped responding or has returned and a status Switch Item that is also a member of the Equipment gets set to ON/OFF as appropriate.