Csv Header only once

You are aware that in CSV a header is not any different from a normal row? It's just used as first row describing the columns, but follows the exact same format as a row would do.

@zenofmud is on the right lines, but you would only need that once... so you would need to send that header line each time it created a new file - ie whenever the day changed - so you need a small bit of logic there to save the old day to context - and compare to the new day value - and if changed then prepend the header line to the existing payload, and then update context, etc

Yes, I think it is the way to do it. Could you help me with the code? I'm sorry but I'm newbie on node-red... :pensive:

I'm PLC programmer and JS is all new to me...

Hopefully something like this for your function

context.day = context.day || "";  // restore or create blank variable to hold "old" day

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;
}
if (day != context.day) {  // If day has changed then insert header line first and update context
    msg.payload = "Time,Prueba_1,Prueba_2\n" + msg.payload;
    context.day = day;
}
var hour = d.getHours();
msg.date = t;
msg.filename = "D:/Clientes/Node Red/Pruebas/Logs/"+year+month+day+"_Registro.csv";
return msg;
1 Like

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