Splitting Mulitple Object Payloads

39 views
Skip to first unread message

Sunny P

unread,
Jul 22, 2017, 1:59:42 PM7/22/17
to Node-RED
Hi, My first post.

I am stuck trying to separate out payload for each cpu core on my pi:

This is the debug:

22/07/2017, 18:55:232core_1 : msg : Object{ "payload": 4, "topic": "core_1", "model": "ARMv7 Processor rev 5 (v7l)", "speed": 900, "_msgid": "b12594a4.4eda68" }
22/07/2017, 18:55:232core_2 : msg : Object{ "payload": 2, "topic": "core_2", "model": "ARMv7 Processor rev 5 (v7l)", "speed": 900, "_msgid": "b12594a4.4eda68" }
22/07/2017, 18:55:232core_3 : msg : Object{ "payload": 1, "topic": "core_3", "model": "ARMv7 Processor rev 5 (v7l)", "speed": 900, "_msgid": "b12594a4.4eda68" }
22/07/2017, 18:55:232core_4 : msg : Object{ "payload": 21, "topic": "core_4", "model": "ARMv7 Processor rev 5 (v7l)", "speed": 900, "_msgid": "b12594a4.4eda68" }

I am trying to get payload for each core:
1. to create a moving average of each core
2. overall average of all cores

I have tried to split but had no luck or just call msg.topic.core_1.

Obviously, i'm not good at programming.
Any help would be great.

Mark Setrem

unread,
Jul 22, 2017, 3:06:53 PM7/22/17
to Node-RED
It would be useful to spend some time reading one of the online tutorials on Javascript Objects and Arrays.
My personal favourite is https://www.w3schools.com/js/js_objects.asp

This will come in very useful for your work manipulating objects in node-red

Colin Law

unread,
Jul 22, 2017, 3:47:36 PM7/22/17
to node...@googlegroups.com
You can use a Switch node to test the topic and send the message to
one of four outputs. That will give you a stream of messages for
core_1 on o/p 1, core_2 on o/p 2, etc. Does that get you started?

Colin
> --
> 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.
> 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/d6bcbb4d-408b-4049-b1dc-a5081ffd16fc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sunny P

unread,
Jul 22, 2017, 4:29:05 PM7/22/17
to Node-RED
Hi Mark,

I have looked through here but doesnt seem to making sense to me. I have this flow:

[{"id":"5671e78a.726fc8","type":"debug","z":"5cbdd29a.d91a2c","name":"1","active":true,"console":"false","complete":"true","x":550,"y":80,"wires":[]},{"id":"193c38.de4d4bc8","type":"cpu","z":"5cbdd29a.d91a2c","name":"cpu","x":430,"y":240,"wires":[["30968d79.d4bdf2","5513c8ac.772c28","fc88c01d.67eff","e5e8b3eb.6eb85","7ecac864.d0f1a8"]]},{"id":"fc88c01d.67eff","type":"function","z":"5cbdd29a.d91a2c","name":"avgcpu","func":"var msg = msg.payload;\nvar a = msg;\n\nreturn a;","outputs":"1","noerr":0,"x":500,"y":160,"wires":[["5671e78a.726fc8"]]},{"id":"2b1515a8.c04622","type":"inject","z":"5cbdd29a.d91a2c","name":"cpu cmd","topic":"piData","payload":"","payloadType":"date","repeat":"108000","crontab":"","once":true,"x":340,"y":320,"wires":[["193c38.de4d4bc8"]]}]

The function now outputs the payload for each core, but all as separate messages. I can't seem to specifically select a single core payload.

Debug shows:

22/07/2017, 21:25:121msg : number12
22/07/2017, 21:25:121msg : number8
22/07/2017, 21:25:121msg : number8
22/07/2017, 21:25:121msg : number8

I have a strong feeling its something simple but for life of me i cant get it.

Zenofmud

unread,
Jul 22, 2017, 5:13:21 PM7/22/17
to node...@googlegroups.com
What node-red node are you using for the CPU node?

Zenofmud

unread,
Jul 22, 2017, 7:00:58 PM7/22/17
to node...@googlegroups.com
Ok, I see are using node-red-contrib-cpu. Since this node will spit out a separate message for each core, as Colin said, just use a SWITCH node. Here is a sample configuration you can use

Secondly, your function node has an issue. You should read the documentation about functions (https://nodered.org/docs/writing-functions#writing-a-function) to see what you need to do to send a message.

Aslo, the INFO on the CPU node says the "payload is the core usage (as a percentage)” i.e. msg.payload is the usage, so if it is 14, then it is 14%

Paul

Sunny P

unread,
Jul 23, 2017, 3:08:29 AM7/23/17
to Node-RED
Yep, embarrassed. I can now fully understand what the node red site now means.

Cheers Paul and Colin. Switch node worked. I did try this before but I must have not set something else up right.

Thanks for the support.
Reply all
Reply to author
Forward
0 new messages