Sample and old the highest value in a Node Red flow

I cant believe something like this is not built into Node red.   I want to see the highest value a sensor reads, specifically I want to see how hot it actually gets in my attic.   Mostly because I need to do some calculations for wire loading and temperatures for an EV charger.  I need to run the wires through the attic and you have limits to the wires insulation temperatures.  a LOT of people never think of this stuff, heck many electricians dont.  But running a wire at 80% capacity for 3-8 hours can generate significant heat.  Estimate about a 10-20C rise in temperature just from the power run through it.   in the wall in an airconditioned home, that rise is nothing.  in a 140F attic, that is a problem. at that temp the wire under load will hit 185F   Still under the 194F insulation rating for THHN, but too close for my comfort.  so I need to do data gathering.

// Initialize flow variable for the highest value if it doesn't exist yet

var highestValue = flow.get('highestValue') || 0;
var incomingValue = parseFloat(msg.payload);

// Check if a reset command was sent, or if this is the first run
if (msg.payload && msg.payload.reset === true) {
    highestValue = 0;
    flow.set('highestValue', highestValue);
    msg.payload = "Peak value reset to 0";
    return msg;
}

// Compare incoming value to highest stored value
if (!isNaN(incomingValue) && incomingValue > highestValue) {
    highestValue = incomingValue;
    flow.set('highestValue', highestValue);
}

// Append the peak value to the message payload to pass to the next node
msg.payload = highestValue;
return msg;

Comments

Popular Posts