How to buffer data and output them once the array reach a defined size

I am trying to downsample streamed data I am receiving via MQTT. I wish to build a buffer array and push values/objects to it, then pass the bufferin a payload before a reset.

For instance I want to use a defined number of element.
Later I want to implement a stop condition (based on a timestamp contained in one of the element)
Has anyone tried this before?

got the answer to my own question, at least for the first part.

[{"id":"300dacd9.0ac534","type":"function","z":"c0e2e862.88aff","name":"Build Array","func":"var buffer_size=10; \nif (!context.buffer || context.buffer.length==10) {\n   context.buffer = [];\n}\n\nif(context.buffer.length<buffer_size){\n    context.buffer.push(msg.payload);\n    var t = context.buffer.length;\n    node.status({fill:\"blue\",shape:\"ring\",text:t});\n}\n\nif (context.buffer.length==buffer_size){\n       msg.payload = context.buffer;\n    return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1130,"y":1040,"wires":[["bf118f20.71866"]]},{"id":"166eb1e7.cc22be","type":"inject","z":"c0e2e862.88aff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":980,"wires":[["3aa4a6a7.603702"]]},{"id":"bf118f20.71866","type":"debug","z":"c0e2e862.88aff","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":1080,"wires":[]},{"id":"3aa4a6a7.603702","type":"function","z":"c0e2e862.88aff","name":"Value injected in the array","func":"if(!context.value){\n   context.value = 0; \n}\n\n\nif(msg.payload){\n    \n     msg.payload = context.value;\n     node.status({fill:\"blue\",shape:\"ring\",text:context.value});\n     context.value++;\n     return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":1020,"wires":[["300dacd9.0ac534"]]}]

I think the join node would work.

(I haven't looked at the flow.)

But reading what you say, I'd guess the join node.

You can set it to wait for n number of messages then send the new message as a variety of things.
Strings, arrays, etc.

thank you my join node configure as "Manual | each msg.payload | Array | n=10 works exactly as expected.

(I'm not too worried, but scroll up to that post and tick the solution button.)

That helps others if they have a similar problem find the answer quickly.

BTW, glad to help.

1 Like

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