# \[SOLVED\] Multiple inputs to one msg function (noob)

**URL:** <https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228>\
**Category:** General\
**Created:** [14 June 2019 09:38 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228 "2019-06-14T09:38:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stroked\_Papy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stroked_papy/32/9290_2.png) [@Stroked\_Papy](https://discourse.nodered.org/u/Stroked_Papy)\
**Post date:** [14 June 2019 09:38 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/1 "2019-06-14T09:38:08Z")

</div>

Hello

I have 4 sting and you will want to make a message to make a Command (sox -t mp3 SomeSong.mp3 -t wav -r 22050 -c 1 - | sudo ./pifm - 102.3)

I tried the function but it does not work

```auto

var newMsg = { toutp: msg.payload,toutt:msg.topic };
var le1pay;
var le2pay;
var le3pay;
var le4pay;

switch (msg.topic){
    case "le1":
        le1pay = msg.paydoad;
        break;
    case "Musique":
        le2pay = msg.paydoad;
        break;
    case "Frequence":
        le4pay = msg.paydoad;
        break;
    case "le3":
        le3pay = msg.paydoad;
        break;      
}

newMsg.topic = "go";
newMsg.payload = le1pay + le2pay + le3pay + le4pay;
return newMsg;

```

am I good or another function does the same

```auto
[{"id":"fcdd2e5a.b96d","type":"exec","z":"c894e6fb.9c1a38","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"command","x":840,"y":100,"wires":[[],[],[]]}]

```

Thank you for a lot

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 10:01 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/2 "2019-06-14T10:01:09Z")

</div>

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](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 June 2019 12:46 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/3 "2019-06-14T12:46:16Z")

</div>

or store them in context...

```auto

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;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 13:00 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/4 "2019-06-14T13:00:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stroked\_Papy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stroked_papy/32/9290_2.png) [@Stroked\_Papy](https://discourse.nodered.org/u/Stroked_Papy)\
**Post date:** [14 June 2019 13:05 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/5 "2019-06-14T13:05:44Z")

</div>

it's better

I tried but argument are not in order

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b2ddf7d4fd726a7969bd048f94d65d6e2f7226e1.jpeg)

```auto
sox -t mp3 -t wav -r 22050 -c 1 - | sudo ./pifm - 80 /home/pi/pifm/interferences_01.mp3

```

thanks to you

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [14 June 2019 13:09 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/6 "2019-06-14T13:09:23Z")

</div>

@Steve-Mcl What exactly would be the advantage of your solution? After all, it is inflexible and a lot to write...so whats the big benefit?

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [14 June 2019 13:18 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/7 "2019-06-14T13:18:58Z")

</div>

setup the join node to create a key/value object where the topic is the key

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [14 June 2019 13:21 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/8 "2019-06-14T13:21:17Z")

</div>

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.

```auto
sox -t mp3 -t wav -r {{frequency}} -c 1 - | sudo ./pifm - 80 {{song}}

```

---

<div class="post-metadata">

**Author:** ![Stroked\_Papy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stroked_papy/32/9290_2.png) [@Stroked\_Papy](https://discourse.nodered.org/u/Stroked_Papy)\
**Post date:** [14 June 2019 13:23 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/9 "2019-06-14T13:23:25Z")

</div>

thank you to you, but the node le4 is not present

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0c9237f0fd47b8e1b0f77a5cc6bfc207c0b2488f.jpeg)

thanks to you

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [14 June 2019 13:23 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/10 "2019-06-14T13:23:52Z")

</div>

or he could feed the result of the join node into the template node...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 15:35 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/11 "2019-06-14T15:35:31Z")

</div>

Show us what is coming out of the Join node.  
**[Edit]** And show us how you have configured the Join node.

---

<div class="post-metadata">

**Author:** ![Stroked\_Papy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stroked_papy/32/9290_2.png) [@Stroked\_Papy](https://discourse.nodered.org/u/Stroked_Papy)\
**Post date:** [14 June 2019 16:23 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/12 "2019-06-14T16:23:12Z")

</div>

Hello

Output

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/97fa90fe76691e9cf0b5d546745faab49978992d.png)

Join node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/83399fc85e1417e52e78c4e0c729428fe054abfc.png)

Thanks to you

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 17:27 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/13 "2019-06-14T17:27:51Z")

</div>

Look at the example I posted again. You have not got the join node configured correctly. The 'to create' field is wrong.

---

<div class="post-metadata">

**Author:** ![kwa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kwa/32/8835_2.png) [@kwa](https://discourse.nodered.org/u/kwa)\
**Post date:** [14 June 2019 18:26 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/14 "2019-06-14T18:26:31Z")

</div>

> [@Stroked\_Papy](#):
>
> le1pay = msg.paydoad; break;

Aside from the other comments, I noted that you have spelt `payload` incorrectly as `msg.paydoad`.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 June 2019 20:03 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/15 "2019-06-14T20:03:21Z")

</div>

> [@kwa](#):
>
> spelt `payload` incorrectly as `msg.paydoad`

I already pointed that out to OP here...

> [@Steve-Mcl](#):
>
> le1pay = msg.payload; //payload NOT paydoad 🙂

  

* * *

> [@moebius](#):
>
> What exactly would be the advantage of your solution?

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)

> [@moebius](#):
>
> After all, it is inflexible and a lot to write

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...

```auto
//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?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [14 June 2019 21:07 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/16 "2019-06-14T21:07:17Z")

</div>

> [@moebius](#):
>
> @Steve-Mcl What exactly would be the advantage of your solution? After all, it is inflexible and a lot to write...so whats the big benefit?

I find this comment somehow offensive, not the sort of response we are used to here!

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [14 June 2019 21:09 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/17 "2019-06-14T21:09:07Z")

</div>

> [@Steve-Mcl](#):
>
> but to be fair, all I did was add to the original code to achieve the OPs requirement.

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)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 June 2019 21:16 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/18 "2019-06-14T21:16:19Z")

</div>

> [@moebius](#):
>
> btw the join node is not an external node - also not the template node

Point taken 🙂

Options are good & if someone learns something, bonus.

---

<div class="post-metadata">

**Author:** ![Stroked\_Papy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stroked_papy/32/9290_2.png) [@Stroked\_Papy](https://discourse.nodered.org/u/Stroked_Papy)\
**Post date:** [19 June 2019 10:40 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/19 "2019-06-19T10:40:38Z")

</div>

Hello

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

```auto
temperature: 10
humidity: 0
pressure: 999

```

would like

```auto
10 0 999

```

I am satisfied with a function

```auto

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;

```

After I continue 😛

Thank you

---

<div class="post-metadata">

**Author:** ![HuynhNguyet](https://avatars.discourse-cdn.com/v4/letter/h/9dc877/32.png) [@HuynhNguyet](https://discourse.nodered.org/u/HuynhNguyet)\
**Post date:** [26 September 2022 07:39 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/20 "2022-09-26T07:39:44Z")

</div>

Thank you so much.
