Rule to manage multiple alarm sensors

This rule is used to check sensor alarm status and send «alarm» following a classification.

Requirements:

Concept:

Instead of having one group for each sensor’s type and a specific rule defined for each groups, use one group and one generic rule to manage all.

First thing to do, classify sensor alarm:

-No delay: Alarm message is send immediately

-Not present: Alarm message is send only when I am not @home

-No one: Alarm message is send only when nobody is @home

and then add tag on alarm item.

The script is triggered by a status change of the member of the sensor’s group.

Functioning:

-List all item’ status than need to trigger a alert message (statusList).
-Retreive data from item (Name. label, status and tag).
-Check that item’ status is on statusList
-Set mail (server, destination, subject, body)
-Search for tag
-For tag1 → Action
-For tag2 and Not@Home → Action
-For tag3 and NoOne@Home → Action

Action:

  • send mail
  • call script to send push message (with 2 parameters: itemLabel and itemStatus)

Blockly script:

The Code:

var statusList, itemName, itemLabel, itemStatus, itemTag, msg, foundPos, smtpServer, mailDest, mailSubject, mailBody, foundTag1, foundTag2, foundTag3;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

function addFrameworkService (serviceClass) {
  var bundleContext = Java.type('org.osgi.framework.FrameworkUtil').getBundle(scriptExtension.class).getBundleContext();
  var serviceReference = bundleContext.getServiceReference(serviceClass);
  return bundleContext.getService(serviceReference);
}

var ruleManager = addFrameworkService('org.openhab.core.automation.RuleManager');

function convertDictionaryToHashMap (dict) {
  if (!dict || dict.length === 0) return null;
  var map = new java.util.HashMap();
  Object.keys(dict).forEach(function (key) {
    map.put(key, dict[key]);
  });
  return map;
}

var Things = Java.type('org.openhab.core.model.script.actions.Things');


logger.debug('Sensor rule init');
statusList = 'ON, OPEN';
itemName = itemRegistry.getItem(event.itemName).getName();
itemLabel = itemRegistry.getItem(event.itemName).getLabel();
itemStatus = itemRegistry.getItem(event.itemName).getState();
itemTag = Java.from(itemRegistry.getItem(event.itemName).getTags());
msg = ['DEBUG: ',itemName,' / ',itemLabel,' / ',itemStatus,' / ',itemTag].join('');
logger.debug(msg);
foundPos = statusList.indexOf(itemStatus) + 1;
logger.debug(foundPos);
if (foundPos > 0) {
  smtpServer = 'yourSmtpServer';
  mailDest = 'yourMailDest';
  mailSubject = 'OpenHAB Alarm: ';
  mailSubject += String(itemLabel);
  mailBody = mailSubject;
  mailBody += ' with status: ';
  mailBody += String(itemStatus);
  foundTag1 = itemTag.indexOf('NoDelay') + 1;
  foundTag2 = itemTag.indexOf('NotPresent') + 1;
  foundTag3 = itemTag.indexOf('NoOne') + 1;
  if (foundTag1 > 0) {
    ruleManager.runNow('SendPushMsg', true, convertDictionaryToHashMap({'key0': itemLabel, 'key1': itemStatus}));
    Things.getActions('mail', smtpServer).sendMail(mailDest, mailSubject, mailBody);
  } else if (foundTag2 > 0 && itemRegistry.getItem('PresenceCyril').getState() == 'OFF') {
    ruleManager.runNow('SendPushMsg', true, convertDictionaryToHashMap({'key0': itemLabel, 'key1': itemStatus}));
    Things.getActions('mail', smtpServer).sendMail(mailDest, mailSubject, mailBody);
  } else if (foundTag3 > 0 && itemRegistry.getItem('gPresence').getState() == 'OFF') {
    ruleManager.runNow('SendPushMsg', true, convertDictionaryToHashMap({'key0': itemLabel, 'key1': itemStatus}));
    Things.getActions('mail', smtpServer).sendMail(mailDest, mailSubject, mailBody);
  }
  foundTag1 = '';
  foundTag2 = '';
  foundTag3 = '';
}
foundPos = '';
3 Likes