# Serial Input node =\> different output depend on ID

**URL:** <https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720>\
**Category:** General\
**Created:** [22 January 2020 11:32 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720 "2020-01-22T11:32:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dani\_89](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@dani\_89](https://discourse.nodered.org/u/dani_89)\
**Post date:** [22 January 2020 11:32 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/1 "2020-01-22T11:32:23Z")

</div>

Hello,

I'm relatively new to node red, but im still using it for some communication from / to my home automation system.

Now, i want to attach a serial device, which is similar to an modbus device. If it is connected to my node-red system, i get messages like "200 13264", "202 16441", "206 541" and so on.  
So the first number is a register in the device, and the second numbers is the value.

What i want to do in node red is, to have the serial device as input, and then have serveral outputs for each register. As the example from above: Output 1 (Register 200) get the Message "13264", Output 2 (Register 202) get the message "16441" and so on.

How would you solve this in detail? Useage of split / switch / function?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 January 2020 11:39 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/2 "2020-01-22T11:39:00Z")

</div>

I think it will depend on _how_ the data arrives.  
Is this all in one _packet_? Is there a terminator?

Just you have shown it as different messages sent.  
The first is:  
200 13264  
The second is:  
202 16441  
And the third is:  
206 541

How the data arrives needs to be clarified first.

One option would be the `split` node.

 ![Screenshot from 2020-01-22 22-36-21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f0a6d4b877fe912ed20c90ecf156749c3755c3c.png)

Oh, sorry, at the bottom you may need to tick the "copy key to" and set it to `msg.payload`

Also note I put a space in the separate field.  
(Not sure if it should be really `" "` (quotes around it)

Then when the message arrives with `200 13264` it gets split into two messages.

Anyway, that's maybe a start for you.

Here's a basic working flow to split the message.

Ok, I inject the 200 13264 as `text`. But. . .. .

You get two messages like this:

```auto
{"topic":"","payload":"200","parts":{"id":"f37d54ed.c04608","type":"string","ch":" ","index":0,"count":2},"_msgid":"4aae3d91.eee4f4"}

```

and

```auto
{"topic":"","payload":"13264","parts":{"id":"f37d54ed.c04608","type":"string","ch":" ","index":1,"count":2},"_msgid":"dcb4a901.3aad58"}

```

You can see the messages have an `index` and `count` which indicates which is the first and which is the second part.  
`index` tells you how many parts the message has and `count` tells you which one in the sequence it is. Alas it is **zero counting**. So be careful that the count will only ever get to `index`-1.

```auto
[{"id":"29f9b619.982fe2","type":"split","z":"184dc884.7aba5f","name":"","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":330,"y":2060,"wires":[["795d2938.0d9618"]]},{"id":"e397d3c9.193308","type":"inject","z":"184dc884.7aba5f","name":"","topic":"","payload":"200 13264","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":2060,"wires":[["29f9b619.982fe2"]]},{"id":"795d2938.0d9618","type":"debug","z":"184dc884.7aba5f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":480,"y":2060,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dani\_89](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@dani\_89](https://discourse.nodered.org/u/dani_89)\
**Post date:** [22 January 2020 11:58 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/3 "2020-01-22T11:58:30Z")

</div>

Thank you for your very fast answer. Yes, this would be perfect for splitting the value. But i think, this is the second part of my "problem".  
The first problem is, to seperate the message to different output.

Yes, you are right.  
The first message of the serial device is:  
200 13264  
The second message could be:  
202 16441  
But the second message could also be:  
200 13268

To be safe, that you know what i mean: 200 means for example "outside temperature", 202 "inside temperature". So i have to split it to different output. The Output of the whole process should be this:  
If i add a debug node to output 1: Here should be only the value of the 200 register  
If i add a debug node to output 2: Here should be only the value of the 202 register and so on  
I think there are about 30 different registers.

Every message from the serial device transmit only 1 register with its value (200 1546). So for example the first message is register 200, the second 202 but the second could be also 200 or 206 or whatever. It depends on, how the data is changed at the serial device =\> but the order of the register should not play a role for node-red process.

To your questions:  
1.) One message of the serial device transmit only 1 register with its value  
2.) Terminator : No, the serial device transmit's the messages automatically, depending on last change on its device. I only want to read this values

---

<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:** [22 January 2020 11:58 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/4 "2020-01-22T11:58:48Z")

</div>

Assuming they are coming in as separate messages, one way would be to use a Switch node configured as shown to switch the messages based on the string the message starts with (the ^ means start of string).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc2a3cbd33a17334029966921a37c187918aa44a.png)  
Then in each output you need to set the topic and remove the channel id from the front. You could do all that in a Change node using JsonATA but as a beginner it would probably be more instructive to use a function node. So in each path put a Function node containing something like

```auto
msg.payload = msg.payload.split(" ")[1]
msg.topic = "Output 1"
return msg

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 January 2020 12:01 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/5 "2020-01-22T12:01:35Z")

</div>

That is a good idea Colin.

You beat me to it.

Yes that would be a good way to get the messages going to the right place first. Then he could use the `split` node as I showed to split the message.

---

<div class="post-metadata">

**Author:** ![dani\_89](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@dani\_89](https://discourse.nodered.org/u/dani_89)\
**Post date:** [22 January 2020 14:35 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/6 "2020-01-22T14:35:34Z")

</div>

Thank you guys for your fast answers.  
I will try this soon. It looks exactly like what I want to do with node-red

---

<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:** [22 January 2020 14:50 UTC](https://discourse.nodered.org/t/serial-input-node-different-output-depend-on-id/20720/7 "2020-01-22T14:50:49Z")

</div>

I'm not sure the split node would help in this instance as that will just turn it into two messages. You may be better using a simple function node to split the string into two parts (say topic and payload 🙂 - then use the switch based on topic. If you import the flow below and look inside the function I think you will see what is going on.

Here is a quick flow to show what I mean

```auto
[{"id":"e0e8a0c7.ba5e2","type":"inject","z":"c4f68fc3.2dd88","name":"","topic":"","payload":"200 13264","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":780,"wires":[["f0838f38.18818"]]},{"id":"1f5f1ed8.8427b1","type":"inject","z":"c4f68fc3.2dd88","name":"","topic":"","payload":"202 16441","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":820,"wires":[["f0838f38.18818"]]},{"id":"91e55004.35a31","type":"inject","z":"c4f68fc3.2dd88","name":"","topic":"","payload":"200 13268","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":860,"wires":[["f0838f38.18818"]]},{"id":"f0838f38.18818","type":"function","z":"c4f68fc3.2dd88","name":"","func":"var parts = msg.payload.split(\" \");\nmsg.topic = parts[0];\nmsg.payload = Number(parts[1]);\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":820,"wires":[["b45e3838.284418"]]},{"id":"b45e3838.284418","type":"switch","z":"c4f68fc3.2dd88","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"},{"t":"eq","v":"202","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":530,"y":820,"wires":[["cbc6e6f8.dd8c28"],["6822e7f9.3973c8"],[]]},{"id":"cbc6e6f8.dd8c28","type":"debug","z":"c4f68fc3.2dd88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":710,"y":780,"wires":[]},{"id":"6822e7f9.3973c8","type":"debug","z":"c4f68fc3.2dd88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":710,"y":820,"wires":[]}]

```
