Write a string into a csv file

#1

Hello there,

I want to write a string (with a number and the date) into a csv file. My problem with that is, that I just get the whole string in one column (see first Picture). But i want that the number is in the first (A1) and the date in the second (B1) column.(2 picture)

can anyone help me?

1 picture

Unbenannt

2 picture

2

// laden des Objekts

m = Wert1+' '+Time;

return {payload:[m]};

(Wert1 and Time are the variables)
is how I declare it in Node Red

#2

Are passing the output of that Function straight to a File Out node or are you using a CSV node to format your data?

If you are passing the output of that Function straight to a File Out node then you need to put a comma , in between the values you are running - otherwise it isn’t comma-separated values.

var m = Wert1+','+Time;
return {payload:[m]};
#3

no, I m using a CSV node behind the function node to Format the data. But perhaps maybe I have a wrong configuration in the CSV node, or how can i split the string into different columns and not into different rows?

#4

CSV = comma seperated values
i.e. the comma seperates the values passed to the columns so you need to put a comma between Wert1 and Time as Nick showed you.

#5

I tried so, but it still don t work

I wrote exactly the same code than Nick (var m = Wert1+','+Time; )
with the csv configuration as you can see in Picture 1.

Picture 1

Picture 2

2

#6

connect a debug node to the output of your function node. What does it show?

#7

Unbenannt

#8

Can you share your actual flow here? I don’t understand why you are using a CSV node if your Function node is generating the CSV String to write to a file. Without seeing how you’ve wired the nodes together its hard to say what is happening.

It would also help to open the file in a text editor so we can see its actual contents and not what Excel has parsed out of it.

#9
[{"id":"86741c8b.53c03","type":"function","z":"c8832cea.c6494","name":"Gesamtzähler für Backup, I 00000001 [1]","func":"(function() {\n    var days = ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'];\n\n    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];\n\n    Date.prototype.getMonthName = function() {\n        return months[ this.getMonth() ];\n    };\n    Date.prototype.getDayName = function() {\n        return days[ this.getDay() ];\n    };\n})();\n\nvar now = new Date();\nvar year    = now.getFullYear();\nvar monthX   = now.getMonth()+1; \nvar dayX     = now.getDate();\nvar hour    = now.getHours();\nvar minute  = now.getMinutes();\nvar second  = now.getSeconds(); \nif(monthX.toString().length == 1) \n{\nvar monthX = '0'+monthX;\n}\nif(dayX.toString().length == 1) \n{\nvar dayX = '0'+dayX;\n}   \nif(hour.toString().length == 1) \n{\nvar hour = '0'+hour;\n}\nif(minute.toString().length == 1) \n{\nvar minute = '0'+minute;\n}\nif(second.toString().length == 1) \n{\nvar second = '0'+second;\n} \n\n\n\n\nvar day = now.getDayName();\nvar month = now.getMonthName();\n\n// Variablen für Objekt\nvar Wert1 = global.get('Stundenzähler1FBup') ||0;\nvar Time = global.get('StundeFBup') ||0;\n\n\n// Stunde\n\n    // Zählen von Wert1\n    Wert1 = global.get('Stundenzähler1FBup')||0;\n    Wert1++;\n    global.set('Stundenzähler1FBup', Wert1);\n    \n    \nvar Time = global.set('StundeFBup',day+' '+dayX+'.'+monthX+'.'+year+' '+hour+':'+minute+':'+second); \n\n\n\n\n\n\nreturn msg;\n\n    ","outputs":1,"noerr":0,"x":900,"y":40,"wires":[["5545d23f.caea74"]]},{"id":"5545d23f.caea74","type":"function","z":"c8832cea.c6494","name":"erstellt String","func":"(function() {\n    var days = ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'];\n\n    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];\n\n    Date.prototype.getMonthName = function() {\n        return months[ this.getMonth() ];\n    };\n    Date.prototype.getDayName = function() {\n        return days[ this.getDay() ];\n    };\n})();\nvar now = new Date();\nvar day = now.getDayName();\nvar month = now.getMonthName();\n\n// Variablen für Objekt\nvar Wert1 = global.get('Stundenzähler1FBup') ||0;\nvar Time = global.get('StundeFBup') ||0;\n\n\n\n\n// laden des Objekts\nvar m =Wert1+','+Time;\n\n\nreturn {payload:[m]};","outputs":1,"noerr":0,"x":1290,"y":40,"wires":[["f5333221.9b2658"]]},{"id":"f5333221.9b2658","type":"csv","z":"c8832cea.c6494","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"","skip":"0","x":1450,"y":40,"wires":[["55264aea.943134","20667830.21c6f"]]},{"id":"55264aea.943134","type":"file","z":"c8832cea.c6494","name":"","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"false","x":1590,"y":40,"wires":[]},{"id":"20667830.21c6f","type":"debug","z":"c8832cea.c6494","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1610,"y":80,"wires":[]}]
#10

You are passing the CSV node an array of strings. From the CSV node’s help text:

If the input is an array of simple values, it builds a single line CSV string.

You are passing in [ "114,Montag 25.06.2018 16:04:16"] - an array that contains a String. So the CSV node is treating that whole string as a single value when it generates the CSV output - probably by putting 'quotes' around it due to the fact it contains a ,.

In other words, you are generating CSV in your Function node and then asking the CSV node to re-encode it again.

You have two options:

  1. get rid of the CSV node as it isn’t helping you
  2. fix the Function to return an array of values and let the CSV node do its work.

The fix for the Function is:

return {payload:[Wert1, Time]};