Multiple values to CSV

591 views
Skip to first unread message

Nairn Harrison

unread,
Jun 2, 2017, 4:02:39 PM6/2/17
to Node-RED
Hi, Looking for a bit of advice. I'm trying to write about 31 values to a csv file (31 values per entry). my setup is:




the function "build object" at the moment is putting five  values into one message with the following output:



basically I wish to combine the "voltage, current, power, frequency and energy" so these five values go in to one line on the csv file the timestamp is being added also automatically at the start.

the code I had to write one value to a previous csv was in the "add timestamp v2" function and was written as such:

var now = new Date();
var now     = new Date();
var year    = now.getFullYear();
var month   = now.getMonth()+1;
var day     = now.getDate();
var hour    = now.getHours();
var minute  = now.getMinutes();
var second  = now.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}  
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}  
msg.timestamp = day+'-'+month+'-'+year+' '+hour+':'+minute;
if (msg.filename === undefined) {
        msg.filename = "/home/pi/data/nairnformat.csv";
    }
msg.payload = msg.timestamp + "\t" + msg.payload;
return msg;


I'm assuming I need to alter the line msg.payload = msg.timestamp + "\t" + msg.payload; to something far more expanded to take all five values ("voltage, current, power, frequency and energy") in to one line.  I will have 31 values in the final version but would appreciate if someone could assist with how I need to code the final line in getting these five I can then tackle the rest myself.

Nairn



Zenofmud

unread,
Jun 2, 2017, 4:31:09 PM6/2/17
to node...@googlegroups.com
Why not create them as a son object then ass them to the ‘csv’ node?

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/bae1d97a-19bc-4b8c-a758-8794c71dbbc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Jun 2, 2017, 4:47:50 PM6/2/17
to node...@googlegroups.com
well the build object already has a payload that is an object... which is a good start... - so the timestamp wants to add to that...
so the last line needs to be
    msg.payload.timestamp = day+'-'+month+'-'+year+' '+hour+':'+minute;
    return msg;
that will add a timestamp property to the existing payload...
(you don't need the file pieces yet)
Similarly add the other properties you need...

then pass to a csv node...
and in the "columns" section arrange the columns as you want them using the property names.... eg
    timestamp,voltage,power,current....
that should then produce a csv line.... that you can then feed direct to a file node (and just set the name in there)

Zenofmud

unread,
Jun 2, 2017, 5:06:13 PM6/2/17
to node...@googlegroups.com
Here is an example floe you can play with - the first function creates the payload of the five variables and passes it to the CSV node and then to your function.
[{"id":"5f9ea95e.7eb188","type":"function","z":"757d259f.63e454","name":"`","func":"var voltage = 25;\nvar current= 30;\nvar power = 1024;\nvar frequency = 66;\nvar energy = 98.4;\n\nmsg.payload = {\n    \"voltage\":voltage,\n    \"current\":current,\n    \"power\":power,\n    \"frequency\":frequency,\n    \"energy\":energy\n};\n//msg.payload = voltage+','+current+','+power+','+frequency+','+energy;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":132,"wires":[["595ac32c.7f55ac","3ef5c90b.4e8846"]]},{"id":"1b546429.ed86bc","type":"debug","z":"757d259f.63e454","name":"","active":true,"console":"false","complete":"true","x":769,"y":312,"wires":[]},{"id":"73a6c75f.ca6f98","type":"inject","z":"757d259f.63e454","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":128,"y":132,"wires":[["5f9ea95e.7eb188"]]},{"id":"79638adc.516cfc","type":"function","z":"757d259f.63e454","name":"","func":"var now = new Date(); \nvar now     = new Date(); \nvar year    = now.getFullYear();\nvar month   = now.getMonth()+1; \nvar day     = now.getDate();\nvar hour    = now.getHours();\nvar minute  = now.getMinutes();\nvar second  = now.getSeconds(); \nif(month.toString().length == 1) {\nvar month = '0'+month;\n}\nif(day.toString().length == 1) {\nvar day = '0'+day;\n}   \nif(hour.toString().length == 1) {\nvar hour = '0'+hour;\n}\nif(minute.toString().length == 1) {\nvar minute = '0'+minute;\n}\nif(second.toString().length == 1) {\nvar second = '0'+second;\n}   \nmsg.timestamp = day+'-'+month+'-'+year+' '+hour+':'+minute;\nif (msg.filename === undefined) {\n        msg.filename = \"/home/pi/data/nairnformat.csv\";\n    }\n//msg.payload = msg.timestamp + \"\\t\" + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":666,"y":131,"wires":[["1b546429.ed86bc"]]},{"id":"595ac32c.7f55ac","type":"debug","z":"757d259f.63e454","name":"","active":true,"console":"false","complete":"true","x":325,"y":325,"wires":[]},{"id":"3ef5c90b.4e8846","type":"csv","z":"757d259f.63e454","name":"","sep":",","hdrin":"","hdrout":true,"multi":"mult","ret":"\\n","temp":"voltage, current, power, frequency, energy","x":490,"y":132,"wires":[["79638adc.516cfc","ec14897a.15501"]]},{"id":"ec14897a.15501","type":"debug","z":"757d259f.63e454","name":"","active":true,"console":"false","complete":"true","x":625,"y":313,"wires":[]}]

Dave C-J

unread,
Jun 2, 2017, 5:16:15 PM6/2/17
to node...@googlegroups.com
my version... :-)

[{"id":"5a5e1a76.ed8ff4","type":"inject","z":"cc742b03.5c3978","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":100,"y":1300,"wires":[["e54087fc.f54658"]]},{"id":"e54087fc.f54658","type":"function","z":"cc742b03.5c3978","name":"build object","func":"var voltage = 25;\nvar current= 30;\nvar power = 1024;\nvar frequency = 66;\nvar energy = 98.4;\n\nmsg.payload = {\n    \"voltage\":voltage,\n    \"current\":current,\n    \"power\":power,\n    \"frequency\":frequency,\n    \"energy\":energy\n};\n//msg.payload = voltage+','+current+','+power+','+frequency+','+energy;\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":1260,"wires":[["695b2e15.44599","dab87d69.57242"]]},{"id":"695b2e15.44599","type":"debug","z":"cc742b03.5c3978","name":"","active":true,"console":"false","complete":"true","x":270,"y":1380,"wires":[]},{"id":"dab87d69.57242","type":"function","z":"cc742b03.5c3978","name":"add timestamp","func":"var now = new Date(); \nvar now     = new Date(); \nvar year    = now.getFullYear();\nvar month   = now.getMonth()+1; \nvar day     = now.getDate();\nvar hour    = now.getHours();\nvar minute  = now.getMinutes();\nvar second  = now.getSeconds(); \nif(month.toString().length == 1) {\nvar month = '0'+month;\n}\nif(day.toString().length == 1) {\nvar day = '0'+day;\n}   \nif(hour.toString().length == 1) {\nvar hour = '0'+hour;\n}\nif(minute.toString().length == 1) {\nvar minute = '0'+minute;\n}\nif(second.toString().length == 1) {\nvar second = '0'+second;\n}   \n\nmsg.payload.timestamp = day+'-'+month+'-'+year+' '+hour+':'+minute;\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":1260,"wires":[["4aad5a7a.ed81e4"]]},{"id":"4aad5a7a.ed81e4","type":"csv","z":"cc742b03.5c3978","name":"","sep":",","hdrin":"","hdrout":false,"multi":"mult","ret":"\\n","temp":"timestamp,voltage, current, power, frequency, energy","x":550,"y":1300,"wires":[["71e31524.a6a4fc","eb5dd38a.62af4"]]},{"id":"71e31524.a6a4fc","type":"debug","z":"cc742b03.5c3978","name":"","active":true,"console":"false","complete":"true","x":690,"y":1300,"wires":[]},{"id":"eb5dd38a.62af4","type":"file","z":"cc742b03.5c3978","name":"","filename":"/home/pi/data/nairnformat.csv","appendNewline":false,"createDir":false,"overwriteFile":"false","x":590,"y":1360,"wires":[]}]

steve rickus

unread,
Jun 2, 2017, 9:04:45 PM6/2/17
to Node-RED
Another shortcut you can use to create the timestamp you need is this:

var now = new Date();
var timestamp = now.format("dd-MM-yyyy HH:mm"); // outputs "02-06-2017 20:47"

There is also the handy node-red-contrib-moment node that will handle any format or timezone you can imagine. It also does date arithmetic and can humanize time spans into human readable forms (like "about 3 days ago" or "a half hour from now).

If you intend to push this data into a database or web service, I would suggest using the more standard ISO-8601 format --

var iso8601 = now.toISOstring();  // outputs "2017-06-03T00:47:12.622Z"

which has the benefits of being understood (correctly) by most databases, and can be ascii sorted according to calendar time.
--
Steve

Zenofmud

unread,
Jun 3, 2017, 6:37:27 AM6/3/17
to node...@googlegroups.com
Steve - when I use your code,
var now = new Date();
var timestamp = now.format("dd-MM-yyyy HH:mm"); // outputs "02-06-2017 20:47"
it throws:
"TypeError: now.format is not a function”
so there must be something else you have installed??
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/731c19ff-5659-41f5-82bc-844e586861cf%40googlegroups.com.

Zenofmud

unread,
Jun 3, 2017, 6:47:19 AM6/3/17
to node...@googlegroups.com
Also ‘now.toISOstring’ should be ‘now.toISOString’ - the s in ‘string' has to be capitalized - ‘String'
On Jun 2, 2017, at 9:04 PM, steve rickus <sri...@gmail.com> wrote:

now.toISOstring

Zenofmud

unread,
Jun 3, 2017, 6:57:17 AM6/3/17
to node...@googlegroups.com
Well I was just giving a starting point for educational purposes…(yeah, that my story and I’m sticking to it GRIN).

FYI - in Daves example you have to edit the CSV node->Object-to-CVS Options and check the ‘include columnar row’ if you want the column names outputted.

steve rickus

unread,
Jun 5, 2017, 10:35:28 AM6/5/17
to Node-RED
Ah, that is probably browser-specific... I only checked in Chrome, with no special addons

Yes, good catch on the typo -- it should have been getISOString()
Reply all
Reply to author
Forward
0 new messages