@jkandasa I managed to get the result using a slightly different approach. I saved previous node state in a repository variable, then looped over the nodes list and compared actual state with previous one, sending en email only when state changes.
I'm not a java programmer, so any suggestion on the code is more than welcome:
var myImports = new JavaImporter(java.io, java.lang, java.util,java.lang.Object, org.mycontroller.standalone.utils.McUtils);
with(myImports) {
var options = new HashMap();
options.put("orderBy", "Eui");
options.put("order", "asc");
options.put("pageLimit", new Long(-1)); // -1 to list all the nodes
var nodes = mcApi.node().getAll(options);
var nodesVar = mcApi.variable().get("Nodes state");
var nodesOU = JSON.parse(nodesVar.value);
var nodesOD = JSON.parse(nodesVar.value2);
var nodesU = [];
var nodesD = [];
var subject = "[MyController] Nodes status";
var messageU2D = "";
var messageD2U = "";
var message = "";
for (i = 0; i < nodes.data.length; i++) {
var nodeStatus = new Object();
nodeStatus['key'] = nodes.data[i].getGatewayTable().getId() + '-' + nodes.data[i].getEui();
nodeStatus['state'] = nodes.data[i].getState();
if (nodeStatus['state'] == "UP") {
nodesU.push(nodeStatus['key']);
var prevState = "";
for (j = 0; j < nodesOU.length; j++){
if (nodesOU[j] == nodeStatus['key']) prevState = "UP";
}
if (prevState != "UP")
messageD2U += nodes.data[i].getEui() + " : " + nodes.data[i].getName() + " : " + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "<BR>";
}
if (nodeStatus['state'] == "DOWN") {
nodesD.push(nodeStatus['key']);
var prevState = "";
for (j = 0; j < nodesOD.length; j++){
if (nodesOD[j] == nodeStatus['key']) prevState = "DOWN";
}
if (prevState != "DOWN")
messageU2D += nodes.data[i].getEui() + " : " + nodes.data[i].getName() + " : " + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "<BR>";
}
}
if (messageU2D != ""){
message += "Nodes DOWN: <BR>";
message += messageU2D;
}
if (messageD2U != ""){
message += "Nodes UP: <BR>";
message += messageD2U;
}
if (message != "")
// mcApi.operation().sendPushbulletNote(null, "abc@xyz.com", null, subject, message);
mcApi.operation().sendEmail("abc@xyz.com", subject, message);
nodesVar.value = JSON.stringify(nodesU);
nodesVar.value2 = JSON.stringify(nodesD);
nodesVar.save();
}