Search code examples
javascriptmonitoringnode-redalarmtemperature

Node Red: Temperature Alarm if measured value is at multiple times different to set value


I'm using Node Red to monitor a set of aquaria. We have 14x DS18b20 sensors, one for each aquarium. I'm able to send an alarm via email if the measured temperature of one sensor is different to the set value. However, sometimes, during maintenance, we take the sensors out of the aquaria. Therefore, I'd like to write a fucntion that only sets the alarm if the abnormal value was mesured e.g. three consecutive times (values are measured every 15 min). How could I do that?
Currently, I wrote a function that sets msg.payload to "Alarm" if any msg.payload[i].temp (the 14 measured values in an array) is more than 1.5 °C diffrent to the set value. This fucntion is followed by a switch that then triggers an email.
Do you have suggestions for my problem? Thanks for your help!


Solution

  • You could try using global context to save a counter of successive abnormal measures. For example, on a function node named "Evalute global abnormal measure"

    // Set the variable GlobalAbnormalAlert (the counter) to not get undefined error later
    if(!global.get("GlobalAbnormalAlert")){
        global.set("GlobalAbnormalAlert", 0);
    }
    
    // Check if is anormal  measure, and if it so, add 1 to the counter
    if (msg.payload[i].temp > 1.5 ){
        global.set("GlobalAbnormalAlert", global.get("GlobalAbnormalAlert") + 1);
    }
    else {
        // If not anormal, reset the counter
        global.set("GlobalAbnormalAlert", 0);
    }
    return msg;
    

    Then you could configure a switch on global.GlobalAbnormalAlert to trigger a mail (or to not): Switch

    Then, the flow would look like something like this:

    Flow

    Where the initial node Trigger measure is your recollection process (from where you get msg.payload[i].temp)