Hello all , I want your help in adding two consecutive msg.payload array[] and then finding running average for latest value + stored value

can you send some data examples in array form to do some testing?

Something like this maybe?

[{"id":"8f3d969699e2a1fe","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[10,20,30,40,50]","payloadType":"json","x":160,"y":2720,"wires":[["244402f5969de187"]]},{"id":"e693c12579532f86","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[60,70,80,90,100]","payloadType":"json","x":160,"y":2760,"wires":[["244402f5969de187"]]},{"id":"77fc9874c0e3b2d2","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[20,40,60,80,100]","payloadType":"json","x":160,"y":2800,"wires":[["244402f5969de187"]]},{"id":"244402f5969de187","type":"function","z":"bdd7be38.d3b55","name":"Running average","func":"// Get current running average, initialise to current values\nlet running = context.get(\"running\") || msg.payload\nfor (let i=0; i<running.length; i++) {\n    if (typeof msg.payload[i] === \"undefined\") {\n        running[i] = undefined\n    } else if (typeof running[i] === \"undefined\") {\n        running[i] = msg.payload[i]\n    } else {\n        running[i] = (running[i] + msg.payload[i]) / 2\n    }\n}\ncontext.set(\"running\", running)\nmsg.payload = running\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":2760,"wires":[["eaf44ccd924ab573"]]},{"id":"eaf44ccd924ab573","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":2760,"wires":[]},{"id":"5bd16e617b7fbb64","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[10,20,30,40,50]","payloadType":"json","x":120,"y":2860,"wires":[["5adcee3fa90cfdf6"]]},{"id":"5adcee3fa90cfdf6","type":"function","z":"bdd7be38.d3b55","name":"Set first two undefined","func":"msg.payload[0] = undefined\nmsg.payload[1] = undefined\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":2860,"wires":[["244402f5969de187"]]}]
1 Like

[undefined , undefine 74 ,137.5, -876 ,400]

Or use this for the function if you want to be able to adjust the smoothing factor

// set this to 0.5 to get running average of new an previous average
// set it to less than 0.5 (min 0) to get greater smoothing (slower response)
// set it to greater than 0.5 (max 1) to get quicker response
const factor = 0.5
// Get current running average, initialise to current values
let running = context.get("running") || msg.payload
for (let i=0; i<running.length; i++) {
    if (typeof msg.payload[i] === "undefined") {
        running[i] = undefined
    } else if (typeof running[i] === "undefined") {
        running[i] = msg.payload[i]
    } else {
        running[i] = (running[i]*(1-factor) + msg.payload[i]*factor)
    }
}
context.set("running", running)
msg.payload = running
return msg;

That's a very handy snippet @Colin ....I'll bookmark that for later :slightly_smiling_face:

It gives a close approximation to an RC filter, so a step input gives and exponential curve up to the new value. The Time Constant depends on the factor and on the time interval between the samples.

Hello all thank you for your valuable time

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