# Csv Header only once

**URL:** <https://discourse.nodered.org/t/csv-header-only-once/21907>\
**Category:** General\
**Created:** [18 February 2020 09:36 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907 "2020-02-18T09:36:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 09:36 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/1 "2020-02-18T09:36:20Z")

</div>

I'm reading data from a S7 PLC and exporting to a csv file. Every 5 seconds it creates a new row.

I would like the first line were the header (the Tag name could be fine). The problem is that if I click the 'Include column name row', it insert it every time.

How could I fix it?

Thank you!

The code:

```auto
[{"id":"ba9c1de4.e4f78","type":"s7 in","z":"a0ee9845.7178d8","endpoint":"2ff9070d.93aa08","mode":"all","variable":"Prueba_1","diff":true,"name":"","x":150,"y":140,"wires":[["fe06df33.f33b5","5cc7c3cd.21a1dc"]]},{"id":"fe06df33.f33b5","type":"debug","z":"a0ee9845.7178d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":440,"y":160,"wires":[]},{"id":"8f87343d.7b9cb8","type":"function","z":"a0ee9845.7178d8","name":"Guardar Diario","func":"var d = new Date();\nvar t = d.getTime();\nvar year = d.getFullYear();\nvar month = d.getMonth()+1; \nif(month.toString().length == 1) {\n month = '0'+month;\n}\nvar day = d.getDate();\nif(day.toString().length == 1) {\n day = '0'+day;\n}\nvar hour = d.getHours();\nmsg.date = t;\nmsg.filename = \"D:/Clientes/Node Red/Pruebas/Logs/\"+year+month+day+\"_Registro.csv\";\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":340,"wires":[["23c62f4a.bd6bf"]]},{"id":"5cc7c3cd.21a1dc","type":"delay","z":"a0ee9845.7178d8","name":"PeriodoArchivacion","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":210,"y":340,"wires":[["72de3e4f.2b8ee"]]},{"id":"72de3e4f.2b8ee","type":"function","z":"a0ee9845.7178d8","name":"AñadirFecha","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));\nmsg.payload.Time=date;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":340,"wires":[["f5e8cbb7.ec5978"]]},{"id":"f5e8cbb7.ec5978","type":"csv","z":"a0ee9845.7178d8","name":"","sep":";","hdrin":true,"hdrout":true,"multi":"mult","ret":"\\r\\n","temp":"Time,Prueba_1,Prueba_2","skip":"2","strings":false,"x":630,"y":340,"wires":[["8f87343d.7b9cb8"]]},{"id":"23c62f4a.bd6bf","type":"file","z":"a0ee9845.7178d8","name":"store S7 data to csv file","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"false","x":1050,"y":340,"wires":[[]]},{"id":"2ff9070d.93aa08","type":"s7 endpoint","z":"","transport":"iso-on-tcp","address":"192.168.10.11","port":"102","rack":"0","slot":"1","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapterauto":true,"adapterport":"","busaddr":"2","adapteraddr":"0","cycletime":"1000","timeout":"1500","verbose":"default","name":"","vartable":[{"addr":"DB1,REAL0","name":"Prueba_1"},{"addr":"DB1,REAL4","name":"Prueba_2"}]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 10:12 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/2 "2020-02-18T10:12:34Z")

</div>

Please read [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and edit your post fixing the flow so it can be imported

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 10:35 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/3 "2020-02-18T10:35:55Z")

</div>

Thank you. I did it!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 10:58 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/4 "2020-02-18T10:58:23Z")

</div>

All you have provided is one node - the S7 in node. Where is the rest of your flow showing what you are doing?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 11:11 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/5 "2020-02-18T11:11:19Z")

</div>

Sorry again. Now yes!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 11:16 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/6 "2020-02-18T11:16:49Z")

</div>

How do you know if it is the first time? Can you stick a debug on the output of the s7 in node and show what the data looks like?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 11:42 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/7 "2020-02-18T11:42:58Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/598119994c6c4e453ab7a94e723e71582b4129cb.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 11:59 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/8 "2020-02-18T11:59:03Z")

</div>

thank you for showing the debug data however you did not reply to my question.

How do you know when the first data comes in so you can determine if you need to output the column headers?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 12:12 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/9 "2020-02-18T12:12:43Z")

</div>

A new file is created every day using the 'Date'.  
The procedure should be:

- Create the file (already done).
- Insert the header.
- Insert data every 5 seconds (already done)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 12:45 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/10 "2020-02-18T12:45:21Z")

</div>

So why don't you create the header when you create the file each day and skip putting in the column names in the CSV node?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 13:38 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/11 "2020-02-18T13:38:45Z")

</div>

Yes, that could be a solution but I don't know how I can do it... (it's my first project with node-red)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 13:40 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/12 "2020-02-18T13:40:48Z")

</div>

Didn't you just say 'Create the file (already done).'

if it is already done how are you doing it?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 13:52 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/13 "2020-02-18T13:52:46Z")

</div>

With a function

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 14:41 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/14 "2020-02-18T14:41:33Z")

</div>

So add the column headers in the function

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 14:47 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/15 "2020-02-18T14:47:27Z")

</div>

Could you say me how can I do it?  
Thank you in advance

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 14:50 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/16 "2020-02-18T14:50:29Z")

</div>

Show me the function code you use to create the file.

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 15:15 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/17 "2020-02-18T15:15:09Z")

</div>

```auto
var d = new Date();
var t = d.getTime();
var year = d.getFullYear();
var month = d.getMonth()+1; 
if(month.toString().length == 1) {
    month = '0'+month;
}
var day = d.getDate();
if(day.toString().length == 1) {
    day = '0'+day;
}
var hour = d.getHours();
msg.date = t;
msg.filename = "D:/Clientes/Node Red/Pruebas/Logs/"+year+month+day+"_Registro.csv";
return msg;

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 15:27 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/18 "2020-02-18T15:27:51Z")

</div>

So that function just builds the name of the file you will create. Why not add the column headers to msg.payload so it will be written to the file?

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 15:55 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/19 "2020-02-18T15:55:56Z")

</div>

Because I don't know how I can do it

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 15:59 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/20 "2020-02-18T15:59:01Z")

</div>

msg.payload = “header1, header2, header3”

[Next page](https://discourse.nodered.org/t/csv-header-only-once/21907.md?page=2)
