88weighed 88weighed - 1 month ago 50
Node.js Question

Node-red output a message to CSV

For the past couple of days I have been trying and reading to get something very specific done in Node-Red: I want to send the (LoRa) message to a CSV.
This CSV should contain the following items:


  • topic

  • date

  • payload



I can insert the date using a function node:

var str1 = Date();


I have been playing around with CSV node, but I can't get it to output comma separated values. All this has probably to do with my lack of java programming skills, which is why I turn to you.

Can you help me out?

Edit:
I'm still looking for the answer, which has brought me the following:
Function node:

var res = Date() + "," + msg.topic + "," + msg.payload; return [ { payload: res } ];


Output:

[{"col1":"Mon Oct 17 2016 17:10:20 GMT+0200 (CEST)","col2":"1/test/1","col3":"string1"}]


All I want now is to lose the extra information such as column names and [{}]

Answer

The CSV node works only on the msg.payload field so you will have to copy the extra data into the payload object to get it to output what you want.

So to format the data correctly you need to place a function node with the following before the CSV node:

var originalPayload = msg.payload;
var newPayload = {};
newPayload.date = new Date().toString();
newPayload.topic = msg.topic;
newPayload.payload = originalPayload;
msg.payload = newPayload;

return msg;

And configure the CSV node to output columns "date,topic,payload"

Comments