# 1 large strng from Arduino, 3 msgs from Function, 3 Outputs?

**URL:** <https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006>\
**Category:** General\
**Created:** [7 July 2019 18:06 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006 "2019-07-07T18:06:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [7 July 2019 18:06 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/1 "2019-07-07T18:06:34Z")

</div>

Hi all,

Sending from Arduino the data from a sensor (ACS712) in 1 single string. It contains 3 "topic" and "payload", values separates with ":" and between the 3 topics ";"

```auto
Serial.println ("Analog:"+String(acs712)+";"+"Mv:"+ String(mv)+";"+"Amperage:"+String(amperage)); 

```

On receiving on Node-Red, it goes through a Function that first will give an array containing the variables

> [@Extracting my valuable data from a msg.payload?](https://discourse.nodered.org/t/extracting-my-valuable-data-from-a-msg-payload/508):
>
> Hi I’m sending, with USB trough serial, data from an arduino to my computer where I have node-red running. I can see the entire messages coming in the debug and with all the data to be directed to a file where they are recorded. This works good. But I’d like to extract some of the data to be redirected to a node-red\_dashboard graph in real time. And I have no idea how to extract the valuable part of the incoming message (msg.payload) to do what I want. I can notice, from what I send from th…

```auto
var myVars=msg.payload.split(";");

```

Once I have the 3 variables in an array, I split this again with variable name and the value payload

```auto
var result={};
myVars.forEach(function(v){
    var parts=v.split(":");
    result [parts[0]]=parts[1];
})
msg.payload=result; 

```

Next, I split them again to have an array with "topic" and "payload"  
[https://groups.google.com/forum/#!topic/node-red/GS6RknsLG4k](https://groups.google.com/forum/#!topic/node-red/GS6RknsLG4k)

```auto
var keys=Object.keys(msg.payload);
var msgs=keys.map(function(key){
    return {topic: key , payload: msg.payload[key]};
}); 

```

And then, return msgs array

```auto
return [msgs];

```

What I am trying to get is 3 outputs from those arrays. What I am getting is 1 output with 3 msgs.  
I tried to add the Switch function and use the msg.parts.index and then the "0,1,2" outputs but it is not working.  
Any ideas of how I can get this 3 outputs from my Function?  
Thanks,

---

<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:** [7 July 2019 18:24 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/2 "2019-07-07T18:24:20Z")

</div>

Aside from why you need this, why didn't you just make a JSON string with data and then upon receipt at node red, you'd have access to the properties by simply running it through a JSON node.

```auto
Serial.println ("{'Analog' : "+String(acs712)+",'Mv' :"+ String(mv)+", 'Amperage' : "+String(amperage) + "}")

```

No need to do any splits and fully dynamic/future proof/extensible

Secondly, if you have already got the data in to an array, just set 3 outputs on the function and create/send 3 `msg`s?

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 20:37 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/3 "2019-07-08T20:37:08Z")

</div>

Just trying to gauge some results on the dashboard. Still practicing so I started from the basics and from what I get directly from the arduino, with no JSON or MQTT... so I can practice and get into the grips of the node red basics. That is why I am trying to get a simple string and do the convertions/splits to get to different outputs.  
Also, the researched above functions are dynamic as it creates as many msgs/topics as you sent.

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 20:42 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/4 "2019-07-08T20:42:07Z")

</div>

I tried to add the below function to create the 3 outputs msgs, but it gives a function error  
TypeError: msg.payload.split is not a function

```auto
var output=msg.payload.split(";");
var msg1={payload:(output[0])};
var msg2={payload:(ouput[1])};
var msg3={payload:(output[2])};

return [msg1,msg2,msg3];

```

After not working the above, I read that it might be the case to use an array function, so I tried the second option to produce 3 outputs, but neither worked.  
TypeError: Cannot read property 'split' of undefined

```auto
var output=msg.payload[0].split(";");
var msg1={payload:(output[0])};
var msg2={payload:(ouput[1])};
var msg3={payload:(output[2])};

return [msg1,msg2,msg3];

```

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 20:44 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/5 "2019-07-08T20:44:08Z")

</div>

The total function is the below, but not working:no\_mouth:

```auto
var myVars=msg.payload.split(";");
var result={};
myVars.forEach(function(v){
    var parts=v.split(":");
    result [parts[0]]=parts[1];
})
msg.payload=result;

var keys=Object.keys(msg.payload);
var msgs=keys.map(function(key){
    return {topic: key , payload: msg.payload[key]};
})

var output=msg.payload[0].split(";");
var msg1={payload:(output[0])};
var msg2={payload:(ouput[1])};
var msg3={payload:(output[2])};

return [msg1,msg2,msg3];

```

Any ideas pls?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 July 2019 20:48 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/6 "2019-07-08T20:48:44Z")

</div>

What does the debug output look like (from the first node - the data source).

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 20:52 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/7 "2019-07-08T20:52:50Z")

</div>

This is the first debug from Arduino  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9a1aa5fb36928f9e8d9ffa5babd55eae61496029.png)

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 20:57 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/8 "2019-07-08T20:57:45Z")

</div>

And this is what I get from the second debug after passing the Function and deleting the last section of the code...

```auto
var myVars=msg.payload.split(";");
var result={};
myVars.forEach(function(v){
    var parts=v.split(":");
    result [parts[0]]=parts[1];
})
msg.payload=result;

var keys=Object.keys(msg.payload);
var msgs=keys.map(function(key){
    return {topic: key , payload: msg.payload[key]};
})

//var output=msg.payload[0].split(";");
//var msg1={payload:(output[0])};
//var msg2={payload:(ouput[1])};
//var msg3={payload:(output[2])};

return [msg];

```

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 July 2019 21:12 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/9 "2019-07-08T21:12:06Z")

</div>

Try this:

```auto
let m = msg.payload
let s = m.split(';')
let out =[]
for(x=0;x<s.length;x++){
    parts = s[x].split(':')
    out.push({payload:{[parts[0]]:parseFloat(parts[1])}})
}

return out

```

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 22:04 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/10 "2019-07-08T22:04:44Z")

</div>

Thanks...  
If I only use your function, the payload is an object and cannot gauge them on the graph...

```auto
let m = msg.payload;
let s = m.split(";");
let out = [];
for (x=0;x<s.length;x++){
    parts = s[x].split(":")
    out.push({payload:{[parts[0]]:parseFloat(parts[1])}})
}

return out;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/df8480e02e6b59ba0504f0d0fd16b0039e3cb2c5.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0f58a8c57f751b233d879d77f562dca1bd8910b4.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 July 2019 22:04 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/11 "2019-07-08T22:04:50Z")

</div>

Steve's suggestion above is the sensible way to do this. IE send a json string and then just parse it.

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [8 July 2019 22:07 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/12 "2019-07-08T22:07:00Z")

</div>

If I add your code to the original one, I got the error:

"TypeError: m.split is not a function"

```auto
var myVars=msg.payload.split(";");
var result={};
myVars.forEach(function(v){
    var parts=v.split(":");
    result [parts[0]]=parts[1];
})
msg.payload=result;

var keys=Object.keys(msg.payload);
var msgs=keys.map(function(key){
    return {topic: key , payload: msg.payload[key]};
});

let m = msg.payload;
let s = m.split(";");
let out = [];
for (x=0;x<s.length;x++){
    parts = s[x].split(":")
    out.push({payload:{[parts[0]]:parseFloat(parts[1])}})
}

return out;

```

---

<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:** [8 July 2019 22:12 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/13 "2019-07-08T22:12:35Z")

</div>

> [@Serg](#):
>
> Also, the researched above functions are dynamic as it creates as many msgs/topics as you sent.

I understand why and how but...  
Your code will not handle nested objects and is your own format. JSON is an EMCA/RFC standard & pretty much built in to JavaScript.  
I've done what you're doing in post projects. Once I used JSON I've never went back to manual parsing.  
But it is entirely your decision.  
...  
To send multiple msg, use either 3 outputs and ...

```auto
return [msg1, msg2, msg3]

```

Alternatively set the function to 1 output and do this

```auto
return [[msg1,msg2,msg3]]

```

Or more verbosity...

```auto
node.send(msg1)
node.send(msg2)
node.send(msg3)
return null

```

Note, if you are sending this to a graph you likely need to set topic differently per message (as the graph help says)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 July 2019 10:13 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/14 "2019-07-09T10:13:17Z")

</div>

Don’t add my code, replace it.  
And in the graph use {{msg.payload.Analog}} etc

---

<div class="post-metadata">

**Author:** ![Serg](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serg](https://discourse.nodered.org/u/Serg)\
**Post date:** [9 July 2019 19:52 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/15 "2019-07-09T19:52:57Z")

</div>

Thanks, both methods work...the Original and the bakman2!!! I was using the {{value}} rather than the {{msg.payload}} on the graph.  
I will stick to the original as I get "topic" and "payload". On the bakman2, only could get "payload"

Original:

```auto
var myVars=msg.payload.split(";");
var result={};
myVars.forEach(function(v){
    var parts=v.split(":");
    result [parts[0]]=parts[1];
})
msg.payload=result;

var keys=Object.keys(msg.payload);
var msgs=keys.map(function(key){
    return {topic: key , payload: msg.payload[key]};
});
return msgs;

```

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

And the bakman2 version: I believe you could also get 'topic" and "payload" separately, but couldn't.

```auto
let m = msg.payload;
let s = m.split(";");
let out = [];
for (x=0;x<s.length;x++){
    parts = s[x].split(":")
    out.push({payload:{[parts[0]]:parseFloat(parts[1])}})
}

return out;

```

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 July 2019 20:24 UTC](https://discourse.nodered.org/t/1-large-strng-from-arduino-3-msgs-from-function-3-outputs/13006/16 "2019-07-09T20:24:40Z")

</div>

```auto
let m = msg.payload;
let s = m.split(";");
let out = [];
for (x=0;x<s.length;x++){
    parts = s[x].split(":")
    out.push({topic:[parts[0]], payload:parseFloat(parts[1]})
}

return out;

```

This would work.

Then for the label use `{{msg.topic}}` and for the value `{{msg.payload}}`
