How to get data for a variable time

Hello Everyone,

I am generating some random data through random node continuously. and i am storing that data in a buffer.
I want to access data from buffer for a variable time period, like last 5 min data or last 1 hour data. Please give me any solution for this. or any other possible way to do this.

Thank you

Here is my flow...

[{"id":"8e9492af.b9b2f","type":"subflow","name":"FIFO","info":"# FIFO\n## First in First Out subflow\nThis flow receives a series of msg.payloads and builds a queue using FIFO (First in First out) method. The LIFO size is configurabe through subflow enviroment variables.\n\n### Input\n`msg.topic == put`\nWill add the `msg.payload` to the end of the array. Everytime a new item is received the second output sends the complete queue.\n\n`msg.topic == get`\nWill remove the first item of the stack and will send it in the first output\n\n`msg.topic == list`\nSends the complete list on the second output.\n\n### Output 1\nThe first item on the top of the stack. It is sent only when `msg.topic == get`.\n\n### Output 2\nThe complete queue in Array format. It is sent everytime a new item is added, removed or when `msg.topic == list`\n\n### Status\nAlways show the queue size.\n\n### Enviroment Variables\n`Buffer_Size` defines the size of the queue. If a new item arrives and the queue is full, the oldest item will be removed.","category":"","in":[{"x":140,"y":120,"wires":[{"id":"cb121310.2edaa"}]}],"out":[{"x":720,"y":60,"wires":[{"id":"e9e46f98.25b71","port":0}]},{"x":900,"y":100,"wires":[{"id":"90e47b30.5284b8","port":0}]}],"env":[{"name":"Buffer_Size","type":"num","value":"6"}],"color":"#DDAA99","icon":"font-awesome/fa-ellipsis-v","status":{"x":1040,"y":140,"wires":[{"id":"1756dadd.968eb5","port":0}]}},{"id":"cb121310.2edaa","type":"switch","z":"8e9492af.b9b2f","name":"Topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"get","vt":"str"},{"t":"eq","v":"put","vt":"str"},{"t":"eq","v":"list","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":270,"y":120,"wires":[["e9e46f98.25b71"],["eef1a399.8dc"],["90e47b30.5284b8"]]},{"id":"eef1a399.8dc","type":"function","z":"8e9492af.b9b2f","name":"put","func":"var queue = flow.get(\"queue\")\n\n\nif (!Array.isArray(queue)){\n    queue = [];\n}else if ( queue.length >= env.get(\"Buffer_Size\") ){\n    queue.pop(1);\n}\n\nqueue.unshift(msg.payload);\n\nflow.set(\"queue\", queue);\nmsg.payload = queue;\n\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":100,"wires":[[]]},{"id":"e9e46f98.25b71","type":"function","z":"8e9492af.b9b2f","name":"get","func":"var queue = flow.get(\"queue\")\nvar item;\n\nif (!Array.isArray(queue)){\n    queue = [];\n}else{\n    item = queue.pop(1);\n}\n\n\nflow.set(\"queue\", queue)\nmsg.payload = item;\n\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":60,"wires":[[]]},{"id":"90e47b30.5284b8","type":"change","z":"8e9492af.b9b2f","name":"list","rules":[{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":140,"wires":[["1756dadd.968eb5"]]},{"id":"1756dadd.968eb5","type":"change","z":"8e9492af.b9b2f","name":"Count","rules":[{"t":"set","p":"payload","pt":"msg","to":"$count(msg.payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":140,"wires":[[]]},{"id":"1701644b.039a1c","type":"inject","z":"40696538.c8266c","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":100,"wires":[["1f0ffc91.b09ca3"]]},{"id":"1f0ffc91.b09ca3","type":"random","z":"40696538.c8266c","name":"","low":"1","high":"10","inte":"true","property":"payload","x":260,"y":100,"wires":[["f208ff83.d1a04"]]},{"id":"2ce6ce4a.a3be52","type":"subflow:8e9492af.b9b2f","z":"40696538.c8266c","name":"","env":[{"name":"Buffer_Size","value":"100","type":"num"}],"x":570,"y":100,"wires":[["4abf6729.57b3e8"],["5df798d7.465e38","88693d37.c9889","4e3654f8.e7785c"]]},{"id":"f208ff83.d1a04","type":"function","z":"40696538.c8266c","name":"Log_msg","func":"var res=msg.payload;\nmsg={\n    \"topic\":\"put\",\n    \"payload\":res\n}\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":100,"wires":[["2ce6ce4a.a3be52"]]},{"id":"4abf6729.57b3e8","type":"debug","z":"40696538.c8266c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":80,"wires":[]},{"id":"7893fd80.cfdec4","type":"inject","z":"40696538.c8266c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":320,"wires":[["4e3654f8.e7785c"]]},{"id":"4e3654f8.e7785c","type":"function","z":"40696538.c8266c","name":"Reset_Buffer","func":"var arr=msg.payload;\nfor(let i=0;i<arr.length;i++)\n{\n    msg={\n            \"topic\":\"get\"\n          }\n    node.send(msg);\n}\nreturn;\n ","outputs":1,"noerr":0,"x":570,"y":280,"wires":[["2ce6ce4a.a3be52"]]},{"id":"5df798d7.465e38","type":"debug","z":"40696538.c8266c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":140,"wires":[]},{"id":"88693d37.c9889","type":"function","z":"40696538.c8266c","name":"Calculation","func":"var arr=msg.payload;\n//var arr = [3, 6, 2, 56, 32, 5, 89, 32];\nvar largest1 = arr[0];\n\nfor (let i = 0; i < arr.length; i++) {\n    if (largest1 < arr[i] ) {\n        largest1 = arr[i];\n    }\n}\n\nmsg.payload=\n{\n    \"Topic\": \"Max_Temp\",\n    \"Temp\": largest1\n}\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":220,"wires":[["114b798a.9a2186"]]},{"id":"114b798a.9a2186","type":"debug","z":"40696538.c8266c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":340,"wires":[]},{"id":"545a639b.9c7cfc","type":"inject","z":"40696538.c8266c","name":"","topic":"list","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":200,"wires":[["2ce6ce4a.a3be52"]]}]```

There are many ways you could approach this. Have you looked at some of the timer nodes available to see which might suit? https://flows.nodered.org/search?term=timer

Hey

Does your data keep coming in every 5 seconds?

If this is not the case, you will also have to provide the time of the data somewhere, I think. Otherwise you can never look back over a desired period. Another option is to have your calculation performed on x number of data, for example for the last 10 measurements.

I think the following is easy to get working:

Code:
var largest1 = arr[0];
var number = msg.number;

if (number === 0){
    number = arr.length
}

for (let i = 0; i < number; i++) {
    if (largest1 < arr[i] ) {
        largest1 = arr[i];
    }
}

msg.payload=
{
    "Topic": "Max_Temp",
    "Temp": largest1,
    "Number": number
}
return msg;
  • inject a number in your calculation.
  • The number represents your last x number of measurements.
    If you inject 0, take all values in the buffer.
    Don't forget that your array must also enter: join node.

I am not a programmer, there will certainly be easier / better ways but with some try and error it will be fine :sweat_smile:

Hi,

In your subflow in the 'put' function you have this :
var queue = flow.get("queue")
you try to get flow queue and it seems that it is not even defined on initialization of the main flow.

and also because you are using a subflow .. from the documents about flow context

Note : for nodes in a subflow, the flow context is shared by those nodes and not the flow the subflow is on. From Node-RED 0.20, the nodes inside a subflow can access the context of the parent flow by prepending $parent. to the context key. For example:

var colour = flow.get("$parent.colour");

Regarding storing data in context "continuously" and then retrieving it by going back even 1 hour .. i don't know .. maybe others can advice on how much data you can store in context.
What is the max time you want to store ?

Maximum time is 12 hour.

( 12 hours x 60 minutes x 60 seconds ) / ( every 5 seconds ) = 8640 records in context ?

as @ArneR mentioned, i guess you will need to store the datetime as well 2020-08-26 07:30:00
so you can later use array.filter to filter out based on the range .. or maybe an advanced jsonata
Array Filter video