How to seperate value from incoming msg.payload?

setting in function node -


setting in debug node-

It shows error still .

Lets forget about function node for moment - disconnect it
what does debug show now?

Below is output in debug from OPC- Static Byte Array Object

In your screenshot you have different formats in your debug window, but your flow window only shows 2 nodes and one debug.

However the screenshot does show the icons that appear when you hover over something in the debug window and there’s a page in the docs that show how you can use this to get the path to something in the msg object
https://nodered.org/docs/user-guide/messages

so if you look at the grey icons in your screenshot (to the far right of the line that is highlighted in grey within the section you have circled) and then at the linked page

I can get now single value seperated at a time

But when I use switch for all the 5 values, I am not getting any output...:frowning:

image

You are not getting output because you are trying to use the text msg.parts.index[0] as a numeric value.

You should just be comparing against the index number 0 , 1 and so on.

I changes switch node as below -

&debug node as

but still something is wrong...

What is wrong? Describe the behaviour you are seeing. Posting screenshots without any explanation of what they show means we have to guess.

Now it gives error as msg.payload.Value :undefined

does I need to change in debug tab ?

Well changing them all to “complete message object” won’t hurt.

I’d also suggest putting a debug node after your split node. Look at what it’s giving you and see if it makes sense for what you think you are inputting to the next node.

Great document! Well worth a read. The explanation of the grey icons will same me lots of time. Thanks for the advice

I removed split & switch node...instead of that I am using function node, can you please check if this function is correct?I searched it on google --I receive msg payload as in debug node, & I want to seperate out these index values ...can you please help??


Every time you post in this thread you are doing something different, whilst I understand that you are trying different things to find an answer it doesn’t make it easy to help you.

You appear to have gone back to the suggestion Nick gave you yesterday How to seperate value from incoming msg.payload?. Adding a port label to the function node outputs doesn’t do anything other than put a label on that output.

Sorry for inconvenience,
actually I want to separate out array values (incoming message) & show it on dash board all as separate values.
thats why I am writing in this thread only.

Yes I tried as per Nick suggested…but not worked…I am not sure if the function is correct , also not clear with the function §

please suggest

Also, posting a screenshot of a function is not helpful. Please copy and paste the code into your reply and put ``` before and after it on newlines.

As I said before, you need to describe the behaviour you are seeing. Don’t assume we can figure it out from a screenshot that doesn’t show your flow and is full of random debug messages with no context.

Below is the function code

var msg5 = msg.payload.Value.map(function (p) {
    node.send({payload: p});
});
return msg5;

I am not clear with this function body…how the 5 array values gets separated ? & what will be the output msg property?

Your function is calling node.send({payload:p}) with each loop of the map function. That will cause a message to be sent out of the first output of the node each time. Furthermore, the map function doesn’t return anything so msg5 will be an array of undefined values - so the return msg5 at the end doesn’t do anything.

Here’s a fixed version:

var msg5 = msg.payload.Value.map(function(p) {
   return {payload:p};
});
return msg5;

The map function now returns each msg object you want to send. This will build up msg5 as an array containing your 5 messages.

You can then return that array and each message will be sent out of the corresponding output.

Hi,
I copied your function body & pasted in my flow


Now in debug when I see as 'complete msg object' , i get below output


This output I get for first output from function node while if I connect debug node to any otehr outputs then I dont get see anything out.

&
when I see as 'msg.payload' , it still says undefined

pls guide me

Right, I'm back at my laptop so I can reply more fully.

Lets go right back to basics here. Looking at your various screenshots, I believe the message you are receiving looks like this:

{
   "Payload": {
      "Value": {
         "0": 1,
         "1": 2,
         "2": 3,
         "3": 4,
         "4": 5,
      }
   }
}

So msg.payload.Value appears to be an Object not an Array. I missed that before - so the adventures in using .map is not going to work. One of your previous attempts using the Split/Switch node combination was the right way to go - it just needed fixing.

Lets step through how to achieve what you want.

  1. First lets move msg.payload.Value to msg.payload - that just makes life easier. We can do that with a Change node.

  2. Next lets use a Split node to break up msg.payload into individual messages. To help see what that does, we add a Debug node set to show complete message.

(The Inject node is configured to inject the msg into the flow).

The Debug node receives 5 messages, as expected, and here they are:

{"topic":"","payload":"1","parts":{"id":"1f26cd06.e0d933","type":"object","key":"0","index":0,"count":5},"_msgid":"dec79443.213868"}
{"topic":"","payload":"2","parts":{"id":"1f26cd06.e0d933","type":"object","key":"1","index":1,"count":5},"_msgid":"2c13f322.d3ec0c"}
{"topic":"","payload":"3","parts":{"id":"1f26cd06.e0d933","type":"object","key":"2","index":2,"count":5},"_msgid":"5f742c94.a08bd4"}
{"topic":"","payload":"4","parts":{"id":"1f26cd06.e0d933","type":"object","key":"3","index":3,"count":5},"_msgid":"c7f811db.3807f"}
{"topic":"","payload":"5","parts":{"id":"1f26cd06.e0d933","type":"object","key":"4","index":4,"count":5},"_msgid":"d6d29bf9.292d68"}

If you look through the message you'll see msg.parts.index of each message is the index of the message in the sequence. We can use that in a Switch node configured as:

So, in all we have:

Which works exactly as you want - each Debug node gets its corresponding part of the original message. Here's the full flow:

[{"id":"de1871b9.0697","type":"change","z":"523eedcd.6de684","name":"","rules":[{"t":"move","p":"payload.Value","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":40,"wires":[["5d7065b7.e0309c"]]},{"id":"96052b4b.558db8","type":"inject","z":"523eedcd.6de684","name":"","topic":"","payload":"{\"Value\":{\"0\":\"A\",\"1\":\"B\",\"2\":\"C\",\"3\":\"D\",\"4\":\"E\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":40,"wires":[["de1871b9.0697"]]},{"id":"5d7065b7.e0309c","type":"split","z":"523eedcd.6de684","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":490,"y":40,"wires":[["f70b9882.208b48"]]},{"id":"f70b9882.208b48","type":"switch","z":"523eedcd.6de684","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":390,"y":200,"wires":[["b40b5dc9.f06b3"],["e1b3138f.a0371"],["2232574a.c1b218"],["8e05766f.b1ba58"],["a503e186.622f8"]]},{"id":"b40b5dc9.f06b3","type":"debug","z":"523eedcd.6de684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":140,"wires":[]},{"id":"e1b3138f.a0371","type":"debug","z":"523eedcd.6de684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":180,"wires":[]},{"id":"2232574a.c1b218","type":"debug","z":"523eedcd.6de684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":220,"wires":[]},{"id":"8e05766f.b1ba58","type":"debug","z":"523eedcd.6de684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":260,"wires":[]},{"id":"a503e186.622f8","type":"debug","z":"523eedcd.6de684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":300,"wires":[]}]

All of this is based on assumptions about what your original message looks like. If this doesn't work, then you'll need to provide an exact copy of the message your ProsysOPC node is sending - not a screenshot, an actual copy of the data. To do that, send a message to a Debug node set to show complete msg, then hover over the message in the debug sidebar and click the clipboard button to copy it to your clipboard - you can then paste it in a reply here (surrounded by ``` to format it properly).

My_Node-RED

1 Like

Thats gr8---it is working good & as expected

Thanks a lot for your guidance...& sorry for the trouble.