I already pointed that out to OP here...
Readable, portable ~(i.e. no external nodes), full control of how & when things occur, self contained (no external switches / changes etc. Each to thier own. I often find a single function node doing several relatively small task is far cleaner than the multiple nodes to achieve the same thing (though not saying that would be the case here)
Inflexible - yes, but to be fair, all I did was add to the original code to achieve the OPs requirement.
He didnt ask for flexible & didnt provide a specification
On the flexible point...
However, if lets say the OP had a requirement for any number of unknown topic
s to sum up & wanted it to be truly flexible he could do this dynamically...
//Premise: Store any value received against its topic & return the sum of them all;
//if topic is empty or == "go", dont store anything, just return the sum
//NOTE: Probably need error handling and type checking but should work if only known types are sent to the node.
//NOTE2: Untested - but probably works :)
let lookup = context.get("lookup") || {}; //get/create the lookup
if(msg.topic && msg.topic != "go") {
lookup[msg.topic] = msg.payload;//store this value in lookup against topic
context.set("lookup", lookup) || {}; //store updated lookup for next time
}
//scan the lookup for every topic value sent & add them up
let sumval = 0;
for (var prop in lookup) {
let v = lookup[prop];
if(v) //if v is "something...
sumval += v; //add it
}
msg.topic = "go";
msg.payload = sumval;
100% flexible - though I suspect this is not really what is wanted either. might prove useful to someone?