Extracting data from an array

Hi there,

I want to record sensor data and save them as an array of objects (with value, timestamp and channel).
Due to the low sample time we need (50ms is the lowest we can get) I try to minimise the computational effort of the cpu. Thats why I use one single Node to poll all Input data at once and write them into an array.
This works fine but now I have 1 array containing all data by timestamp.

What I need is to seperate the single values and write them into one array for each channel as a key/value object with timestamp, value and data type.

Also I would like to scale the values after recording them. How do I do this?

[{"id":"515a74a4.765eac","type":"change","z":"82908ba1.959628","name":"topic = stop","rules":[{"t":"set","p":"topic","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":420,"wires":[["35422587.534daa","cc5dfe87.4112f8"]]},{"id":"b9240ba5.343c48","type":"inject","z":"82908ba1.959628","name":"START 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"start","payload":"","payloadType":"str","x":400,"y":380,"wires":[["35422587.534daa"]]},{"id":"35422587.534daa","type":"link out","z":"82908ba1.959628","name":"","links":["3f6b2974.34d33e"],"x":615,"y":500,"wires":[]},{"id":"cc5dfe87.4112f8","type":"debug","z":"82908ba1.959628","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":820,"y":400,"wires":[]},{"id":"5a18c8bb.7e99c","type":"delay","z":"82908ba1.959628","name":"Messfenster","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":630,"y":380,"wires":[["515a74a4.765eac"]]},{"id":"4a50b8ea.a142e8","type":"change","z":"82908ba1.959628","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":340,"wires":[["5a18c8bb.7e99c"]]},{"id":"4742e2df.b85d64","type":"inject","z":"82908ba1.959628","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1000","payloadType":"str","x":410,"y":300,"wires":[["71792d5c.4bdbac"]]},{"id":"71792d5c.4bdbac","type":"function","z":"82908ba1.959628","name":"global.delay set","func":"var delay = msg.payload;\nglobal.set(\"delay\",delay);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":300,"wires":[["9d149d3c.d3cd18"]]},{"id":"9d149d3c.d3cd18","type":"debug","z":"82908ba1.959628","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":810,"y":300,"wires":[]},{"id":"5bda9867.84864","type":"inject","z":"82908ba1.959628","name":"START 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"start_2","payload":"","payloadType":"str","x":400,"y":420,"wires":[["4a50b8ea.a142e8","35422587.534daa"]]},{"id":"5ef0e41f.8cfbc4","type":"uc-iodataIn","z":"82908ba1.959628","mode":"wi_all_variables","variable":"DIG_1_HAND","name":"","pollInterval":"50","pollIntervalBase":"ms","x":400,"y":560,"wires":[[],["cf8b7e50.d15298"]]},{"id":"3f6b2974.34d33e","type":"link in","z":"82908ba1.959628","name":"","links":["35422587.534daa"],"x":495,"y":640,"wires":[["cf8b7e50.d15298"]]},{"id":"4ac20b1a.942a24","type":"join","z":"82908ba1.959628","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":830,"y":560,"wires":[["4e35b8cb.6d5c"]]},{"id":"22df35f3.c2123a","type":"debug","z":"82908ba1.959628","name":" ","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1130,"y":560,"wires":[]},{"id":"cf8b7e50.d15298","type":"function","z":"82908ba1.959628","name":"Recorder","func":"\nlet recording = context.get(\"recording\") || false;//get logging recording\n\n\n\nif (msg.topic == \"start\") {\n    context.set(\"recording\", true);\n    context.set(\"measures\", []);\n    node.status({fill:\"green\",shape:\"ring\",text:\"Recording\"});\n    return [null, null];//no output\n}\n\nif (msg.topic == \"start_2\") {\n   // context.set(\"recording\", false);\n    msg.payload = [...context.get(\"measures\") || []];\n    context.set(\"measures\", null);\n    msg.topic = \"messung_1\"\n    return [msg, null];//pass recorded data out of pin 1\n    \n}\n\nif (msg.topic == \"stop\") {\n    context.set(\"recording\", false);\n    msg.payload = [...context.get(\"measures\") || []];\n    context.set(\"measures\", null);\n    node.status({ fill: \"red\", shape: \"ring\", text: \"Stopped\" });\n    msg.topic = \"messung_2\"\n    return [null, msg];//pass recorded data out of pin 2\n    \n}\n\nif (recording) {\n    const measures = context.get(\"measures\") || [];\n    measures.push(msg.payload);\n    context.set(\"measures\", measures);\n    return [null, null];//pass msg on to next node\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":620,"y":560,"wires":[["4ac20b1a.942a24"],["4ac20b1a.942a24"]]},{"id":"4e35b8cb.6d5c","type":"change","z":"82908ba1.959628","name":"t: Messungen","rules":[{"t":"set","p":"topic","pt":"msg","to":"Messungen","tot":"str"},{"t":"delete","p":"delay","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":560,"wires":[["22df35f3.c2123a"]]}]


I know that there are a bunch of similar topics about this subject but I felt like none of the ones I've seen really fits my problem.

Can anyone help me out?

Regards
Bastian

Please edit your message and use 3 backticks ` instead of single quotes ' for the code.

1 Like

Since node-red is essentially single-threaded, there would seem to be little point in trying to minimise processing on capture only to then have to do more processing to reformat the data. Capture the data into the correct structure to begin with maybe.

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