Hello Promy ..
You can use Node-red's Context in order to save those values.
Flow Context could be an Array in this case and you can push the moisture values to this array until the time to make the average. You can read more about Context in the documentation here
I think trying to work out a solution based on timing could be a little tricky and values could get out of sync.
Would it be possible, if you have access to the code on the ESP8266, to send a msg to Node-red (ie complete
) to notify Node-red that the last value is sent (before it goes to deep sleep) so you can then make the average calculation ?
Example Flow using Context and calc. average on complete :
[{"id":"260f4b90e1219457","type":"function","z":"54efb553244c241f","name":"Average","func":"// if msg from mqtt is \"complete\"\nif (msg.payload === \"complete\") {\n let moisture = flow.get(\"moisture\")\n let total = 0\n let average = 0\n\n if (moisture.length > 0) {\n moisture.forEach(val => total += val)\n average = total / moisture.length\n\n // send message\n node.send({\n \"topic\": \"moisture\",\n \"payload\": average\n })\n\n flow.set(\"moisture\", []) // reset Context to empty array\n }\n}\n\n// if payload is a Number\nelse if (!isNaN(msg.payload)) {\n let moisture = flow.get(\"moisture\") || []\n moisture.push(msg.payload) // add to Array\n flow.set(\"moisture\", moisture) // save to Context\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":2160,"wires":[["2bd3a9df9a604fdc"]]},{"id":"2bd3a9df9a604fdc","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":2160,"wires":[]},{"id":"ccafb3ce4b544815","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":210,"y":2060,"wires":[["260f4b90e1219457"]]},{"id":"90ca977ae11dcbe8","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":210,"y":2120,"wires":[["260f4b90e1219457"]]},{"id":"7e7542a9e2ab98d8","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"70","payloadType":"num","x":210,"y":2180,"wires":[["260f4b90e1219457"]]},{"id":"ea19a230362b35c5","type":"inject","z":"54efb553244c241f","name":"complete","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"complete","payloadType":"str","x":200,"y":2260,"wires":[["260f4b90e1219457"]]}]