Receive real time data

Receive real-time data once every 10 seconds.

I'm a student and beginner.
I want to average the data. (by 10 times)
I think... First, sort the data that is measured once every 10 seconds...
I've tried many times, but I don't know how. :cry:

Hi and welcome to Node-Red.

I'm not sure I understand the question.

Say you are getting: 2 5 7 5 9 3 1 7 9 4

You want the average of those numbers?

1 Like

Here is a basic flow that shows you what to do.

You will have to change things to make it work as you want.

[{"id":"799cc16e.505f18","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2530,"wires":[["85be1c1d.8b3928"]]},{"id":"85be1c1d.8b3928","type":"random","z":"b9924a74.4d98f8","name":"","low":"1","high":"10","inte":"true","property":"payload","x":250,"y":2570,"wires":[["f95b8e2b.24fa98"]]},{"id":"f95b8e2b.24fa98","type":"join","z":"b9924a74.4d98f8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":580,"y":2570,"wires":[["6dcc8c5f.9e1664","a6bdfd63.1d7ab8"]]},{"id":"6dcc8c5f.9e1664","type":"debug","z":"b9924a74.4d98f8","name":"Just to show you what is happening.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":820,"y":2610,"wires":[]},{"id":"a6bdfd63.1d7ab8","type":"function","z":"b9924a74.4d98f8","name":"Average","func":"//  This is a basic way to do it.\n//  You will have to add extra lines\n//  to get all 10 values and change the last\n//  line to `/ 10` and not `/ 3`\n\nx = msg.payload[0];\n//node.warn(x);\nx = x + msg.payload[1];\n//node.warn(x);\nx = x + msg.payload[2];\n//node.warn(x);\nx = x / 3;\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":2570,"wires":[["140608e8.a14c37"]]},{"id":"140608e8.a14c37","type":"debug","z":"b9924a74.4d98f8","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":2570,"wires":[]},{"id":"d20bcf7f.07deb","type":"comment","z":"b9924a74.4d98f8","name":"This is your flow here.","info":"","x":630,"y":2530,"wires":[]},{"id":"a21efb2d.b420c8","type":"comment","z":"b9924a74.4d98f8","name":"Simulation.  Press 3 times to see what happens.","info":"","x":260,"y":2490,"wires":[]}]
1 Like

real time data is blue color text(msg.payload)
I want to avg of payload numbers ;
49.9827, 49.9449, 60.1888, 68.110099999 ••• (average of every 10 times)

Did you get that?

The flow I posted does that for three numbers.

Look at the node called average and edit it to do it for ten numbers, not three.

What is happening with the flow:

The first two nodes are simulation.

Press the inject node three times and you will get an average in the result node.

The join node is used to create an array which can then be handled by the next node.
You will have to change that to be 10 and not 3 as it is now.

Then edit the average node as I said above.

That will do a basic job of giving you an average from ten inputs.

Can you understand how I wrote the code?

This is the join node part you need to change.

1 Like

Thanks for everything.
you're the best teacher ever.

Looking at your screen shot though that is only using 6 values.

You said 10.

Just checking, because sometimes mistakes like that can bite you further down the track and you can't understand what is going on.

Just to be 100% sure, look at the picture below. Those two nodes.

Export them and post the code.

This is done by drag-selecting the nodes then selecting EXPORT from the menu (top right of the screen.)

Screenshot from 2020-06-22 12-25-45

and pasting the code in your reply and inserting it correctly.

This is done by clicking the </> button at the top of the edit screen, and pasting the code then.

Thank you for the reply and Thank you for your concern.
it' just a test. i understand! :grin:


All good. I just wanted to be sure you understand the tricks involved in doing what you want and do understand all the things that needed to be changed to get it working as you described.

All good. Enjoy!

Have a look at the node-red-contrib-calculate node.
Is very easy to average and release every 10 samples.

Thanks for telling me :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.