Csv Header only once

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:

[{"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"}]}]

Please read How to share code or flow json and edit your post fixing the flow so it can be imported

Thank you. I did it!

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

Sorry again. Now yes!

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?

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?

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)

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?

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

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

if it is already done how are you doing it?

With a function

So add the column headers in the function

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

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

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;

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?

Because I don't know how I can do it

msg.payload = “header1, header2, header3”