LowPowerLab Forum

Software support => Pi Gateway => Topic started by: HeneryH on February 18, 2018, 05:31:16 PM

Title: Event alert if NODE not heard from in more than X amount of time [SOLUTION]
Post by: HeneryH on February 18, 2018, 05:31:16 PM
Is there a way to get an alert if a metric has not reported in for a period greater than x?
Title: Re: Event alert when not heard from a Mot in longer than x time?
Post by: Felix on February 18, 2018, 06:12:02 PM
I haven't put anything in the default metrics.
But one could be created pretty easily I think, something like a poll event that runs every N seconds and checks the last time your node-metric has been received, and if not, issue an alert?
Optionally it should repeat the alert every time it runs, or just the first time?

Look at the other poll example events, for instance one that would run every 30s and just emit a LOG to the UI with the myMetric value and how long ago it was recorded (I made this on the fly, but give it a try):

metricAlert: {
  label:'Metric Alert',
  icon:'comment',
  descr:'Metric Alert',
  nextSchedule:function(nodeAtScheduleTime) { return 30000; },
  scheduledExecute:function(nodeAtScheduleTime) {
    db.findOne({ _id : nodeAtScheduleTime._id }, function (err, nodeRightNow) {
      if (nodeRightNow) {
        /*just emit a log the status to client(s)*/
        io.sockets.emit('LOG', 'Node Metric Value: ' +
           nodeRightNow.metrics['myMetric'].value +
           ' last updated:' +
           (Date.now() - new Date(nodeRightNow.metrics['myMetric'].updated).getTime()) +
           ' seconds ago');
      }
   });
}},
Title: Re: Event alert when not heard from a Mot in longer than x time?
Post by: HeneryH on February 19, 2018, 03:15:24 PM
Thanks, your lead worked great.

My custom events for my sump pump alert with alerts when level gets closer than 15 cm to the sensor and an alert if the node doesn't check-in for 10 mins (polling every x mins).  You'll need to tweak x, I had it too low and then got flooded with emails when the node went offline.

exports.events = {

sumpEmail : { label:'Sump : Email (below 15cm)',
              icon:'mail',
              descr:'Send email if water < 15cm below surface',
              serverExecute:function(node) {
                     if (node.metrics['CM'] && node.metrics['CM'].value < 15 &&
                        (Date.now() - new Date(node.metrics['CM'].updated).getTime() < 2000)) {
                        sendEmail('SUMP PUMP ALERT', 'Water is only 15cm below surface and rising - [' +
                                  node._id + '] ' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
                        };
                     }
            },

sumpSMS : { label:'SumpPump : SMS (below 15cm)',
            icon:'comment',
            descr:'Send SMS if water < 15cm below surface',
            serverExecute:function(node) {
                     if (node.metrics['CM'] && node.metrics['CM'].value < 15 &&
                         (Date.now() - new Date(node.metrics['CM'].updated).getTime() < 2000)) {
                        sendSMS('SUMP PUMP ALERT', 'Water is only 15cm below surface and rising - [' +
                                 node._id + '] ' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
                        };
                     }
           },

sumpPollAlert : {
  label:'Sump not checking in.',
  icon:'mail',
  descr:'Check every x minutes to see if node is checking in, Alert if last metric is older than 10 min',
  nextSchedule:function(nodeAtScheduleTime) { return x*60*1000 /* x min */ ; },
  scheduledExecute:function(nodeAtScheduleTime) {
    db.findOne({ _id : nodeAtScheduleTime._id }, function (err, nodeRightNow) {
      if (nodeRightNow) {
        /*just emit a log the status to client(s)*/
        io.sockets.emit('LOG', '** Scheduled Poll Event - Node Metric Value: ' +
           nodeRightNow.metrics['CM'].value +
           ' last updated:' +
           ((Date.now() - new Date(nodeRightNow.metrics['CM'].updated).getTime()))/1000 +
           ' seconds ago');

           if (  (Date.now() - new Date(nodeRightNow.metrics['CM'].updated).getTime()) > 10*60*1000 /* 10 min */ )
           {
               io.sockets.emit('LOG', '******* Check in is NOT OK.  *******');
               sendEmail('SUMP PUMP ALERT', 'Sump Pump Node has not checked in in over 10 mins - [' +
                                  nodeRightNow._id + '] ' + nodeRightNow.label.replace(/\{.+\}/ig, '') + ' Now -  ' + new Date().toLocaleTimeString() + ' Last Update -  ' + new Date(nodeRightNow.metrics['CM'].updated).toLocaleTimeString());
           } else
           {
               io.sockets.emit('LOG', '** Check in is OK.');
           }
      }
   });
}},
};
Title: Re: Event alert if NODE not heard from in more than X amount of time [SOLUTION]
Post by: Felix on February 20, 2018, 09:13:21 AM
That's great :)
Congrats for pulling it together!