Generate CSV files

I run into a problem generating a CSV file, maybe someone can help me.

The TTN Uplink Node gets the data, the Change Node generates the array and the Edit CSV Node generates a comma separated CSV file which is saved with a File Node.

[{"id":"cc7221f7.8586c","type":"tab","label":"Lucht","disabled":false,"info":""},{"id":"a8e73786.bcdee8","type":"ttn uplink","z":"cc7221f7.8586c","name":"Vocht en temperatuur","app":"320ca0e6.622ee","dev_id":"humitemp-achtertuin","field":"","x":120,"y":220,"wires":[["7967c7f0.a265f8"]]},{"id":"7967c7f0.a265f8","type":"change","z":"cc7221f7.8586c","name":"Generate Array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t    { \"Temperatuur\":payload.tempachtertuin,\"Luchtvochtigheid\":payload.humiachtertuin,\"Accu\":payload.voltachtertuin }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":220,"wires":[["9a19774e.5c37c8"]]},{"id":"9a19774e.5c37c8","type":"csv","z":"cc7221f7.8586c","name":"Build CSV","sep":",","hdrin":true,"hdrout":true,"multi":"mult","ret":"\\n","temp":"Temperatuur, Luchtvochtigheid,Accu","skip":"0","strings":true,"x":630,"y":220,"wires":[["93dd4592.4cbb68"]]},{"id":"93dd4592.4cbb68","type":"file","z":"cc7221f7.8586c","name":"Save CSV","filename":"CSV/lucht.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":830,"y":220,"wires":[[]]},{"id":"320ca0e6.622ee","type":"ttn app","z":"","appId":"humtitemp-achtertuin","accessKey":"ttn-account-v2.Lfk6s7XgacQe7GjCCCC2DkviRu_gF3_DNPjNx9BUtv0","discovery":""}]

This works but the CSV file is not correct, as you can see empty lines are added and the labels are repeated. That should be like the one on the right in the picture:

I have searched and tried solutions but I still can't get a good CSV output. What do I need to change?

You are sending that, in each message, an array containing one object. It says in the help text for the CSV node
If the input is an array of arrays, or an array of objects, a multiple-line CSV string is created.
So it is generating a multi-line csv file for each element in the array, but there is only one element, so you get the headings and one line of values each time. To do it the way you want you need all the values in one array of objects in one message.
Alternatively you could write the headings to the file using a separate bit of flow and then use the csv node to add the lines of data one at a time, in which case each message should just be an array of values rather than an object, I think.

Thanks for helping. After I now first created the CSV file manually, I put the headers in it with the nano editor. Thus, the first row is correct and subsequent rows are correctly completed from NodeRed. Issue solved :slight_smile:

1 Like

You could easily do that in node-red of course.

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