Serial Input node => different output depend on ID

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?

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.

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:

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

and

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

[{"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":[]}]

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

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


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

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

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.

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

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 :slight_smile: - 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

[{"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":[]}]