Send values to an array, write to CSV when full

Heya folks

I have a project underway which will read sensor values and write to a CSV file. So far with 1 sensor I managed to add a timestamp and value to the file in CSV format with each read. I think in order to be most efficient it would be best to red all 6 sensors consecutively (modbus), update an array, write to CSV, repeat. This needs to be done as fast as possible. Essentially 1 timestamp and then the 6 sensor values comma seperated. Have only added 2 sensors at the moment

I suck and am a complete Noob at java script. Any push in the right direction would be of great help. What I've done so far is prob quite easy to understand, so hoping someone could have a quick look.

Thanks!

[{"id":"4d95c064.56bc5","type":"modbus-flex-getter","z":"fb27fd6.2ca3f","name":"test","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"daab589a.cd8e08","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":650,"y":340,"wires":[["511817c3.98bf08"],[]]},{"id":"5fca0e71.449cb","type":"function","z":"fb27fd6.2ca3f","name":"function","func":"msg.payload = { \n    'fc': 4, \n    'unitid': 1, \n    'address': 1010, \n    'quantity': 2 \n} ;\nreturn msg;\n","outputs":1,"noerr":0,"x":500,"y":340,"wires":[["4d95c064.56bc5"]]},{"id":"511817c3.98bf08","type":"modbus-response","z":"fb27fd6.2ca3f","name":"","registerShowMax":20,"x":890,"y":380,"wires":[]},{"id":"46ee83e8.c042ac","type":"debug","z":"fb27fd6.2ca3f","name":"","active":true,"console":"false","complete":"payload","x":870,"y":340,"wires":[]},{"id":"5673c892.5d8d58","type":"function","z":"fb27fd6.2ca3f","name":"payload","func":"msg.payload = { value: 0 , 'fc': 6, 'unitid': 1, 'address': 1010 , 'quantity': 1 }; return msg","outputs":1,"noerr":0,"x":500,"y":100,"wires":[["3ffcdcbb.fa5c44"]]},{"id":"3ffcdcbb.fa5c44","type":"modbus-flex-write","z":"fb27fd6.2ca3f","name":"Auto Range Off","showStatusActivities":false,"showErrors":false,"server":"daab589a.cd8e08","x":680,"y":100,"wires":[["be36ee78.71b1e"],[]]},{"id":"be36ee78.71b1e","type":"modbus-response","z":"fb27fd6.2ca3f","name":"","registerShowMax":20,"x":890,"y":100,"wires":[]},{"id":"7ee1a410.aae57c","type":"comment","z":"fb27fd6.2ca3f","name":"Set Auto Range To Off","info":"Specifies whether automatic range selection is used\n0 – the measurement range written to the RangeRegister is used for UV value\nacquirement\n1 – the sensor selects the measurement range automatically. If set, the RangeRegister\nwill be read only. After starting the sensor the AutoRangeRegister is set to 1.´ ","x":260,"y":60,"wires":[]},{"id":"a864fe69.12a84","type":"function","z":"fb27fd6.2ca3f","name":"payload","func":"msg.payload = { value: 4 , 'fc': 6, 'unitid': 1, 'address': 1011 , 'quantity': 1 }; return msg","outputs":1,"noerr":0,"x":500,"y":220,"wires":[["47d8297e.06e058"]]},{"id":"47d8297e.06e058","type":"modbus-flex-write","z":"fb27fd6.2ca3f","name":"Set Range","showStatusActivities":false,"showErrors":false,"server":"daab589a.cd8e08","x":670,"y":220,"wires":[["2ceec987.77e3e6"],[]]},{"id":"2ceec987.77e3e6","type":"modbus-response","z":"fb27fd6.2ca3f","name":"","registerShowMax":20,"x":890,"y":220,"wires":[]},{"id":"c057ae0f.ebe7a","type":"comment","z":"fb27fd6.2ca3f","name":"Set Range Register To Pre-defined Range","info":"Second Number:\n0 – reserved\n1 – range 1 (highest sensitivity)\n2 – range 2\n3 – range 3\n4 – range 4\n…","x":320,"y":180,"wires":[]},{"id":"a1d6ac1d.b4436","type":"comment","z":"fb27fd6.2ca3f","name":"Read Range Register Settings","info":"First Number:\n0 = Custom setting written in following node\n1 = Auto - Sensor selects\n\nSecond Number:\n0 – reserved\n1 – range 1 (highest sensitivity)\n2 – range 2\n3 – range 3\n4 – range 4\n…","x":280,"y":300,"wires":[]},{"id":"305daf6e.650b5","type":"modbus-flex-getter","z":"fb27fd6.2ca3f","name":"Get UV","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"daab589a.cd8e08","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":640,"y":520,"wires":[["ed72ec53.2ef5b","ed72baf.7f76548","36c0fcd1.3aff54"],["1fd33e83.bba581"]]},{"id":"777665c0.38b99c","type":"inject","z":"fb27fd6.2ca3f","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":320,"y":520,"wires":[["b38adfe9.8ee28","a180f114.be998"]]},{"id":"b38adfe9.8ee28","type":"function","z":"fb27fd6.2ca3f","name":"function","func":"msg.payload = { \n    'fc': 3, \n    'unitid': 1, \n    'address': 1000, \n    'quantity': 2 \n} ;\nreturn msg;\n","outputs":1,"noerr":0,"x":480,"y":520,"wires":[["305daf6e.650b5"]]},{"id":"ed72ec53.2ef5b","type":"modbus-response","z":"fb27fd6.2ca3f","name":"","registerShowMax":20,"x":870,"y":560,"wires":[]},{"id":"36c0fcd1.3aff54","type":"debug","z":"fb27fd6.2ca3f","name":"","active":true,"console":"false","complete":"payload","x":850,"y":520,"wires":[]},{"id":"2fb7ea7f.9467e6","type":"comment","z":"fb27fd6.2ca3f","name":"Read UV Range Registers","info":"","x":270,"y":420,"wires":[]},{"id":"65c0f113.6a59a","type":"inject","z":"fb27fd6.2ca3f","name":"","topic":"Startup","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":130,"y":100,"wires":[["6e81187b.4de8e8"]]},{"id":"3f3d1804.6bf9c8","type":"delay","z":"fb27fd6.2ca3f","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":330,"y":220,"wires":[["a864fe69.12a84","d88af0de.d7ce5"]]},{"id":"d88af0de.d7ce5","type":"delay","z":"fb27fd6.2ca3f","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":330,"y":340,"wires":[["5fca0e71.449cb"]]},{"id":"6e81187b.4de8e8","type":"delay","z":"fb27fd6.2ca3f","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":330,"y":100,"wires":[["5673c892.5d8d58","3f3d1804.6bf9c8"]]},{"id":"ed72baf.7f76548","type":"split","z":"fb27fd6.2ca3f","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":470,"y":640,"wires":[["41c5b592.1698cc"]]},{"id":"41c5b592.1698cc","type":"switch","z":"fb27fd6.2ca3f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":640,"wires":[[],["ebfc77e0.ea0c18"]]},{"id":"7fd901bc.d02f6","type":"csv","z":"fb27fd6.2ca3f","name":"","sep":",","hdrin":true,"hdrout":false,"multi":"mult","ret":"\\n","temp":"time, payload","skip":"0","x":1030,"y":640,"wires":[["c9135c11.57caf"]]},{"id":"ebfc77e0.ea0c18","type":"change","z":"fb27fd6.2ca3f","name":"","rules":[{"t":"set","p":"object","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":640,"wires":[["af39f313.4f3ff"]]},{"id":"c9135c11.57caf","type":"file","z":"fb27fd6.2ca3f","name":"\\log.csv","filename":"log.csv","appendNewline":false,"createDir":true,"overwriteFile":"false","x":1160,"y":640,"wires":[[]]},{"id":"af39f313.4f3ff","type":"function","z":"fb27fd6.2ca3f","name":"javascript","func":"var date;\ndate = new Date();\ndate = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2));\npayload = {\"time\":date, \"payload\":msg.object};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":640,"wires":[["7fd901bc.d02f6","38098473.1125bc"]]},{"id":"38098473.1125bc","type":"debug","z":"fb27fd6.2ca3f","name":"","active":true,"console":"false","complete":"payload","x":1070,"y":580,"wires":[]},{"id":"1e95de21.ba8032","type":"function","z":"fb27fd6.2ca3f","name":"function","func":"msg.payload = { \n    'fc': 3, \n    'unitid': 2, \n    'address': 1000, \n    'quantity': 2 \n} ;\nreturn msg;\n","outputs":1,"noerr":0,"x":480,"y":740,"wires":[["d444efbe.8f1"]]},{"id":"a180f114.be998","type":"delay","z":"fb27fd6.2ca3f","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":330,"y":740,"wires":[["1e95de21.ba8032"]]},{"id":"d444efbe.8f1","type":"modbus-flex-getter","z":"fb27fd6.2ca3f","name":"Get UV","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"daab589a.cd8e08","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":640,"y":740,"wires":[["d224553f.7df358","c7366315.74594","6dd70ef6.9477f"],[]]},{"id":"8f5ba873.372ab8","type":"change","z":"fb27fd6.2ca3f","name":"","rules":[{"t":"set","p":"object","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":840,"wires":[[]]},{"id":"c2d7d067.01bcb","type":"switch","z":"fb27fd6.2ca3f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":840,"wires":[[],["8f5ba873.372ab8"]]},{"id":"d224553f.7df358","type":"split","z":"fb27fd6.2ca3f","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":470,"y":840,"wires":[["c2d7d067.01bcb"]]},{"id":"c7366315.74594","type":"debug","z":"fb27fd6.2ca3f","name":"","active":true,"console":"false","complete":"payload","x":850,"y":740,"wires":[]},{"id":"6dd70ef6.9477f","type":"modbus-response","z":"fb27fd6.2ca3f","name":"","registerShowMax":20,"x":870,"y":780,"wires":[]},{"id":"1fd33e83.bba581","type":"debug","z":"fb27fd6.2ca3f","name":"","active":true,"console":"false","complete":"payload","x":1170,"y":480,"wires":[]},{"id":"710889.f0c52778","type":"comment","z":"fb27fd6.2ca3f","name":"Change Function node to set Unit-ID. Leave Unit-ID blank in server setup","info":"","x":440,"y":460,"wires":[]},{"id":"a90a598a.3eecc8","type":"comment","z":"fb27fd6.2ca3f","name":"Next try to add values of sensor 2 into the CSV file.. See how fast can be written. OR read all 6 sensors, write to array then update CSV with 1 timestamp","info":"testing testing","x":660,"y":940,"wires":[]},{"id":"daab589a.cd8e08","type":"modbus-client","z":"","name":"modbus","clienttype":"serial","bufferCommands":false,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyS2","serialType":"RTU-BUFFERD","serialBaudrate":"19200","serialDatabits":"8","serialStopbits":"1","serialParity":"even","serialConnectionDelay":"1","unit_id":"","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"1000"}]

edit your post and use:

3 backticks: `

your code

end with 3 backticks: `

Think I did it....Doesn't appear to have changed

You didn't.

(edited it for you... the backticks need to be on a separate line)

This sounds like a perfect job for a join node set to create an array and send the message once you've received 100 elements.

edit: like so:

[{"id":"edce42b4.209a2","type":"inject","z":"5936699f.67912","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":220,"wires":[["300a7ece.66fcba"]]},{"id":"300a7ece.66fcba","type":"join","z":"5936699f.67912","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"100","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":290,"y":220,"wires":[["e08afffe.ac5288"]]},{"id":"e08afffe.ac5288","type":"csv","z":"5936699f.67912","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"","skip":"0","x":410,"y":220,"wires":[["4e0ff03d.ca3f4"]]},{"id":"4e0ff03d.ca3f4","type":"file","z":"5936699f.67912","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","x":530,"y":220,"wires":[[]]}]

Indeed, sounds a straightforward approach.

Considering that the output of the modbus node already gives you an array of all 6 sensors then all you will need is to insert the timestamp as the first element of this array. You can modify the function node that generates the timestamp adding one line of code as below:

var date;
date = new Date();
date = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2));

msg.payload.unshift(date);
return msg;

The function node would need to be connected between the modbus node that reads the values and the CSV node.

Each line of the generated CSV file will look like (reading two values (3 and 4) in this example:

2019-05-14 10:57:36,3,4

Sorry for the late reply but boy am I busy. About to try your suggestions. Thanks all who helped out....I'll be sure to remember the 3 back ticks :slight_smile:

Heya folks. So I seem to have managed to get an array of 6 values and update the csv file.

I'm wondering if it's possible to create a loop so the sensors are continuously read after say a switch is switched on the dashboard. I managed to create a button with message pass through and that worked, however a switch that could be turned on and off is what I want. When using a switch it doesn't seem to loop and only reads the sensors once.