Split a msg.payload with Substrings

3,048 views
Skip to first unread message

Sebastian Ditz

unread,
Jun 27, 2017, 4:09:07 PM6/27/17
to Node-RED
Hi,

Im new to node-red.
So maybe a stupid question.

I would like to "Split" a msg.payload.
In my payload there are substrings .
I attached a Screenshot



In this case I would like to get the info if of the string 
"num_adopted"

Everything a found to split the payload dosen't work ;-)

Can you give a hint?
Thx

Nick O'Leary

unread,
Jun 27, 2017, 4:11:34 PM6/27/17
to Node-RED Mailing List
Hi,

that screenshot shows your payload is an array of objects.

To reference the element you've pointing at, you would use msg.payload[0].num_adopted

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/938de04e-689a-4395-b9d0-6148daf48e18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

trollmar

unread,
Jun 27, 2017, 6:02:06 PM6/27/17
to Node-RED
Thx.

Ok . Learned this a array.

When is use your hint in the debug node the output is correct.



But how can I use this in the "split node" do get the output? 

I found this thread 

But no information how to configure "split"

when I insert the variable msg.payload[0] in the "node split" nothings  happens

thx

  



Am Dienstag, 27. Juni 2017 22:11:34 UTC+2 schrieb Nick O'Leary:
Hi,

that screenshot shows your payload is an array of objects.

To reference the element you've pointing at, you would use msg.payload[0].num_adopted

Nick
On 27 June 2017 at 21:09, 'Sebastian Ditz' via Node-RED <node...@googlegroups.com> wrote:
Hi,

Im new to node-red.
So maybe a stupid question.

I would like to "Split" a msg.payload.
In my payload there are substrings .
I attached a Screenshot



In this case I would like to get the info if of the string 
"num_adopted"

Everything a found to split the payload dosen't work ;-)

Can you give a hint?
Thx

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Nick O'Leary

unread,
Jun 27, 2017, 6:05:46 PM6/27/17
to Node-RED Mailing List
There is nothing to configure in the Split node if you are passing in an array.

If you pass that message to the Split node, it will send a message for each of the elements in the msg.payload array. The payload of each of those messages will be an element from that array.

What output do you want? 

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

trollmar

unread,
Jun 28, 2017, 5:32:00 PM6/28/17
to Node-RED




Am Mittwoch, 28. Juni 2017 00:05:46 UTC+2 schrieb Nick O'Leary:
There is nothing to configure in the Split node if you are passing in an array.

If you pass that message to the Split node, it will send a message for each of the elements in the msg.payload array. The payload of each of those messages will be an element from that array.

What output do you want? 



Hi ,
many thx for your help!!!!
The first steps ins this awesome tool is difficult.


Ok. i would like to use the unifi node.
My first step is to extract the info if the unifi Access point is adopted (See debug arrow).
Like this:

If >> num_adopted = 1 >>> then ...


So....
The first split works as aspected. It Splits the array into for Objects (thx for your hint)
The extra debug node is configured as the name of the node ..and the output is =1 (debug pane on top)

But the split node dosen't work.
I attached to screenshots.

What I don't understand?





steve rickus

unread,
Jun 29, 2017, 12:03:02 PM6/29/17
to Node-RED
So you have a switch node that checks the condition msg.payload[0].num_adopted == 1 which for array object 0 (at least) evaluates to "true". This is why you see the output from the debug node called "True". You may be expecting to ONLY see the value of msg.payload[0].num_adopted, but that is not what the switch node does. It will always send the entire msg to output port #1 when the condition is true.

It sounds like you want a change node instead -- this allows you to take whatever the value of the expression is and do something with it. For instance, you could Set: msg.payload to: msg.payload[0].num_adopted, which would have the effect of replacing the entire payload object with the number 1. Is this what you are meaning when you say you want to "isolate" that value?
--
Steve

trollmar

unread,
Jun 29, 2017, 2:12:57 PM6/29/17
to Node-RED
Hi.
Thx for your answer.

You right. Thats exactly what i want to do.

I will try that.

To switch node:
So switch node dosent split the payload into diveded payload it more like a Filter?!

Thx

Nick O'Leary

unread,
Jun 29, 2017, 2:15:07 PM6/29/17
to Node-RED Mailing List
Yes, it is like a 'switch' statement in traditional programming languages. It routes the message based on its contents. It does not modify the contents.

Nick

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

trollmar

unread,
Jun 29, 2017, 5:51:25 PM6/29/17
to Node-RED
Hi,
mmh have to correct myself.

I don't want to change an whatever the value of the expression is to something.

I would like to get the info is  msg.payload[0].num_adopted == 1 or  msg.payload[0].num_adopted == 0
and get or  "translate" it to 1 or 0. No array or other info.

just  msg.payload 1 or  msg.payload 0

After this i would like to transfer 1 or 0 via Mqtt 

best regards

Nick O'Leary

unread,
Jun 29, 2017, 5:53:39 PM6/29/17
to Node-RED Mailing List
So have a Switch node that branches the flow based on the value of msg.payload[0].num_adopted, then on each branch have a Change node that sets msg.payload to the value you want on that branch, then rejoin the branches into your MQTT node.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
Jun 30, 2017, 11:10:00 AM6/30/17
to Node-RED
Sounds exactly like the change node I described -- set msg.payload to be the value of msg.payload[0].num_adopted. This replaces the payload object with either a 0 or a 1, which you can then push to mqtt.
Reply all
Reply to author
Forward
0 new messages