Help to send email when node goes down


    I received an email from @naguraob for example script to send an email with node last seen details. I thought when we post here, will helpful for other too.

    Original request:

    Can you please help me out with some suggestion on the following, I need to send out an email whenever a node goes down, and another email with the last seen details of the nodes. It would be handy if you can provide some direction on this and I will start with it and contact you in case I run into any issues. Thank You.

    @naguraob here is the solution to send an email:

    Use case #1: send an email when a node goes down
    This is straight forward. You have to create an operation to send an email. and setup a rule to trigger this operation when your node goes down.


    Use case #2: Send node last seen status often:
    For this, we have to create a script

    // Import required packages for our coding. JavaImporter is used to import packages. 
    // You can import any number of packages separated with comma.
    var myImports = new JavaImporter(, java.lang, java.util,java.lang.Object);
    // Add our imports with loop
    with(myImports) {
      // get list of nodes
      // Create a HashMap to add our query filter values.
      var options = new HashMap();
      //Sort by lastSeen.
      options.put("orderBy", "lastSeen");
      //Order by descending
      options.put("order", "desc");
      //Page limit
      options.put("pageLimit", new Long(-1)); // -1 to list all the nodes
      //Get nodes
      var nodes = mcApi.node().getAll(options);
      //Email details
      var toAddresses = ",";
      var subject = "[MyController] Nodes last seen status";
      var message = "\
              <!DOCTYPE html> \
                <style>body {font-size: 12px;}</style>\
                  <b>Dear User,</b>\
                  <br>Nodes last seen details\
                    <table border='0'>\
                    <thead><th>Gateway</th><th>Name</th><th>EUI</th><th>State</th><th>Battery level</th><th>Last seen</th></thead>";
      for (index = 0; index <; index++) {
        message += "<tr><td>"[index].getGatewayTable().getName()+"</td><td>"[index].getName()+"</td><td>"[index].getEui()+"</td><td>"[index].getState()+"</td><td>"[index].getBatteryLevel()+"</td><td>"+mcApi.utils().friendlyTime([index].getLastSeen())+"</td><tr>";
      message += "</table>\
                  <br>-- Powered by <a href=''></a>\
      //send email
      mcApi.utils().sendEmail(toAddress, subject, message);

    Note: Kindly use latest snapshot version from here

Log in to reply