Struggling to add headers to CSV file

So i have been struggling with this problem a few days now and actually started to ignore it but i want it to work correctly. Adding headers to CSV file and then add the payload.
Here is the flow: (a plc payload goes into the function node)
image

inside the function node:

and inside the CSV node:

Add a debug node (set to display the 'Complete msg object') to the output of the function node and see if the data coming out is in the format the csv node needs it to be in.

You want to write column names: "Object to CSV options".
Look at the screenshot of the csv node.

image

Ticking that box result in this:
image

So you are sending a row of data at a time but adding a title line in each row.

Why not send the title row, then send all the data rows?
Tiy should also fix the warnings in your function node.

  • send all rows at once and check the box or
  • first set the first line with column names and just append the data as normal strings instead of using the csv node

Note: - There is a Pull Request waiting for next release that will improve this, and try to handle it automatically (ie send header once then as many rows of data as you want until you send a reset command).

I used two csv nodes to solve this problem. One configured with col header and the other not. You can then switch between the two nodes dependant on whether it is the first row or not.

Okay so how can i program it so that every night at 00:00 it will create a new csv with headers and then add values to it

Try this example

[{"id":"27715663.ec571a","type":"inject","z":"53c7fdaa.e46f0c","name":"value input","topic":"","payload":"98","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":108,"y":288,"wires":[["9fddc311.389708"]]},{"id":"9fddc311.389708","type":"function","z":"53c7fdaa.e46f0c","name":"","func":"let value = msg.payload\nlet d = new Date().toISOString().split(\"T\")\nlet date = d[0];\nlet time = d[1].split(\".\")[0]\nlet filename = \"./data_\"+date+\".csv\"\n\nlet csv = date+\",\"+time+\",\"+value+\"\\r\"\n\nreturn {filename:filename,payload:csv}","outputs":1,"noerr":0,"x":266,"y":288,"wires":[["115a5f9e.20a688"]]},{"id":"115a5f9e.20a688","type":"file","z":"53c7fdaa.e46f0c","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":482,"y":240,"wires":[["cba862c2.e9afa"]]},{"id":"9a89661b.1e6e3","type":"inject","z":"53c7fdaa.e46f0c","name":"Create new file at 00:00","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":158,"y":192,"wires":[["de60ef75.0fe2d8"]]},{"id":"de60ef75.0fe2d8","type":"function","z":"53c7fdaa.e46f0c","name":"","func":"\nlet d = new Date().toISOString().split(\"T\")\nlet date = d[0];\nlet filename = \"./data_\"+date+\".csv\"\nlet headers = \"Date,Time,Value\"\n\nreturn {filename:filename,payload:headers}","outputs":1,"noerr":0,"x":338,"y":192,"wires":[["115a5f9e.20a688"]]},{"id":"cba862c2.e9afa","type":"debug","z":"53c7fdaa.e46f0c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":626,"y":240,"wires":[]}]

This does not use the csv node (as csv is comma separated text).
At 00:00 it creates a new file data_<date>.csv and the values are written that file.

File contents looks like:

Date,Time,Value
2020-04-28,06:47:23,98
2020-04-28,06:47:24,98
2020-04-28,06:47:24,98

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