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


    Hi Jeeva,

    Thank You for the forum article. It's really helpful.

    I need you help with respect to the JavaScript related to Node Last Seen Status. I have followed your forum article with the latest snapshot of MyController. But I get "400: Bad Request" Error when I try to run the script from Script Webpage. The mycontroller log is as follows (I have removed the actual value in fromAddress and smtpUsername below, they are updated with the required correct details)

    2017-07-10 00:20:13,293 INFO [Acme.Utils.ThreadPool(1)-PooledThread: Acme.Serve.Serve$ServeConnection@1b14d08] [] EmailSettings(, smtpPort=587,, enableSsl=true, useStartTLS=true,
    2017-07-10 00:20:13,295 ERROR [Acme.Utils.ThreadPool(1)-PooledThread: Acme.Serve.Serve$ServeConnection@1b14d08] [org.mycontroller.standalone.utils.McScriptFileUtils:236] Exception:McScript(engineName=null, mimeType=null, extension=js, name=NodesLastSeen, canonicalPath=/home/pi/mycontroller/conf/resources/scripts/operations/NodesLastSeen.js, type=OPERATION, size=1849, lastModified=1499626206000, bindings={}),
    java.lang.NullPointerException: null
            at org.mycontroller.standalone.scripts.api.UtilsApi.sendEmail(
            at jdk.nashorn.internal.scripts.Script$6$\^eval\_.:program(<eval>:44)
            at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(
            at jdk.nashorn.internal.runtime.ScriptFunction.invoke(
            at jdk.nashorn.internal.runtime.ScriptRuntime.apply(
            at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
            at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
            at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
            at jdk.nashorn.api.scripting.NashornScriptEngine.eval(
            at javax.script.AbstractScriptEngine.eval(
            at org.mycontroller.standalone.scripts.McScriptEngine.executeScript(
            at org.mycontroller.standalone.utils.McScriptFileUtils.executeScript(
            at org.mycontroller.standalone.api.jaxrs.ScriptsHandler.runNow(
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(
            at java.lang.reflect.Method.invoke(
            at org.jboss.resteasy.core.MethodInjectorImpl.invoke(
            at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(
            at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(
            at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(
            at org.jboss.resteasy.core.SynchronousDispatcher.invoke(
            at org.jboss.resteasy.core.SynchronousDispatcher.invoke(
            at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(
            at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(
            at org.jboss.resteasy.plugins.server.tjws.TJWSServletDispatcher.service(
            at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(
            at javax.servlet.http.HttpServlet.service(
            at Acme.Serve.Serve$ServeConnection.runServlet(
            at Acme.Serve.Serve$ServeConnection.parseRequest(
            at Acme.Serve.Serve$
            at Acme.Utils$ThreadPool$

    Can you please help me in figuring out where the error could be?
    Thank You.



    @naguraob Are you using single to address and more than one email address on to?

    Try with the following scenario, It will help me to fix this issue.

    • multiple address: var toAddresses = ",";
    • Single address: var toAddresses = "";

Log in to reply