Drawing live data

This is one way of generating an average...

get average function...

var values = [];
var feeds = msg.payload.feeds;

for (let i = 0; i < feeds.length; i++) {
    const feed = feeds[i];
    const f2value = parseFloat(feed.field2);
    if(isNaN(f2value)) continue;
    values.push(f2value);
}

let average = (array) => array.reduce((a, b) => a + b) / array.length;

msg.payload = average(values);
msg.topic = "Average"; //this will be the title on the gauge
return msg;

demo flow...

[{"id":"e123a375.2bcfa","type":"inject","z":"b872cb4b.5a6448","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1560,"y":100,"wires":[["4909027a.831c7c"]]},{"id":"4909027a.831c7c","type":"function","z":"b872cb4b.5a6448","name":"generate fake feeds","func":"var fakefeeds = [];\nfor (let i = 0; i < 100; i++) {\n  fakefeeds.push({created_at: Date.now(), entry_id: 1000+i, field2: ((Math.random() * (0.9 - 0.0001) + 0.0001).toFixed(4))});\n}\nmsg.payload = {\n  feeds: fakefeeds\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1590,"y":160,"wires":[["296c1cd8.e6bc14","385fe36f.47539c"]]},{"id":"296c1cd8.e6bc14","type":"function","z":"b872cb4b.5a6448","name":"get average","func":"var values = [];\nvar feeds = msg.payload.feeds;\n\nfor (let i = 0; i < feeds.length; i++) {\n    const feed = feeds[i];\n    const f2value = parseFloat(feed.field2);\n    if(isNaN(f2value)) continue;\n    values.push(f2value);\n}\n\nlet average = (array) => array.reduce((a, b) => a + b) / array.length;\n\nmsg.payload = average(values);\nmsg.topic = \"average\"\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1830,"y":160,"wires":[["36451c8a.ff5214"]]},{"id":"36451c8a.ff5214","type":"debug","z":"b872cb4b.5a6448","name":"average","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1820,"y":200,"wires":[]},{"id":"385fe36f.47539c","type":"debug","z":"b872cb4b.5a6448","name":"feeds","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1590,"y":200,"wires":[]}]

@Maf I think it is still unclear exactly what you are trying to do. Are you trying to plot the 100 points on a chart or are you trying to take the average of all 100 values and add one average point to the chart each 30 seconds?

I want to save in a file and plot the data but it its fast change as in above so I deceide to take an avarege each specific time

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