In a function node only the latest message is available. A solution to this is to use the Join node to combine the messages, as in this example from the cookbook. https://cookbook.nodered.org/basic/join-streams
var le1pay = context.get("le1pay") || 0; //get from context or default to 0
var le2pay = context.get("le2pay") || 0; //get from context or default to 0
var le3pay = context.get("le3pay") || 0; //get from context or default to 0
var le4pay = context.get("le4pay") || 0; //get from context or default to 0
switch (msg.topic){
case "le1":
le1pay = msg.payload; //payload NOT paydoad :)
context.set("le1pay", le1pay); //store in context for next time
break;
case "Musique":
le2pay = msg.payload; //payload NOT paydoad :)
context.set("le2pay", le2pay); //store in context for next time
break;
case "Frequence":
le4pay = msg.payload; //payload NOT paydoad :)
context.set("le4pay", le4pay); //store in context for next time
break;
case "le3":
le3pay = msg.payload; //payload NOT paydoad :)
context.set("le3pay", le3pay); //store in context for next time
break;
}
msg.topic = "go";
msg.payload = le1pay + le2pay + le3pay + le4pay;
return msg;
The function node has the possible disadvantage that it will send a message on each input message even if not all the topics have been received. If that mattered then the function would need additional logic.
Did you consider using the template node (not the dashboard template, the core template) for building the string ?
The variable part could be stored in context variables. Once you press a button in the dashboard or an inject node the flow will build the final string.
the line inside the template node would be something like this
I don´t know what are the other variable parts of the command.
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 unknowntopics 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?
i see...point taken.
But it's clear that op wrote "paydoad" and the code didnt work - thats one of the major arguments against the function node...(btw the join node is not an external node - also not the template node)
Thank you very much who help me (Steve-Mcl, Colin, moebius .....)
Because my stroke I confuse the letters
When I use Joins sequences the result
temperature: 10
humidity: 0
pressure: 999
would like
10 0 999
I am satisfied with a function
var le2pay = context.get("le2pay") || 0; //get from context or default to 0
var le4pay = context.get("le4pay") || 0; //get from context or default to 0
switch (msg.topic){
case "Musique":
le2pay = msg.payload;
context.set("le2pay", le2pay); //store in context for next time
break;
case "Frequence":
le4pay = msg.payload;
context.set("le4pay", le4pay); //store in context for next time
break;
}
msg.topic = "go";
msg.payload = "sox -t mp3 " + le2pay + " -t wav -r 22050 -c 1 - | sudo ./pifm - " + le4pay;
return msg;