Save a daily csv file of sensor data


I connected 2 xiaomi temp sensors to my Node-RED.

They send a msg every 30 min, i want to save the data to a csv file and "close" the file every day.

Every timestamp with data get a row in the csv with the same headers.

I'm pretty new to Node-RED but i think it should be easy task

Thanks, Yair

Do this in small steps, first concentrate on fetching the data, once you have achieved that then move on to the next phase.

Thank you for the answer,

I already fetching the data using json and csv nodes.

I have the data in csv format
Now I want to create every day file that include the date and year on the file name and append every msg from the sensor to the file

And at the end of the day to send the file

OK, again then start with the first step. Work out how to use the File Out node to write the data to a file. Once you have that going then move on to the next phase.

Try the below.

[{"id":"50b1a1d1.ed892","type":"file","z":"4a8e69ed.f81b98","name":"Add Device file location","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":550,"y":80,"wires":[["bda46195.b38b1"]]},{"id":"c6679eea.8efd","type":"function","z":"4a8e69ed.f81b98","name":"Data Capture","func":"var d = new Date();\nvar t = d.getTime();\nvar n = d.getTimezoneOffset();\npayload={\"date/time\": d,\"Time Zone Diffrence in minutes\": n,\"IP\": msg.ip,\"Bacnet\": msg.Bacnet, \"Function\": msg.Function, \"type\": msg.ObjectID,\"App Tag\": msg.Apptag, \"Property ID\": msg.PropertyID, \"Write Value\": msg.Wvalue, \"payload\":msg.payload};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":190,"y":80,"wires":[["eafe30f8.658b"]]},{"id":"eafe30f8.658b","type":"json","z":"4a8e69ed.f81b98","name":"","property":"payload","action":"","pretty":false,"x":350,"y":80,"wires":[["50b1a1d1.ed892"]]},{"id":"bda46195.b38b1","type":"debug","z":"4a8e69ed.f81b98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":80,"wires":[]}]

Hi @YairHach

My take on this is something like:

Use an inject node to pulse an output everyday at a given time.
The file name should be either a flow or global context variable so if ever you want to change it, you can easily.
Though obviously you will need a different file name for each day. But that can be derived from the date.
On reception of the pulse, the flow moves the file from its existing name to the new name which is unique.
There are a few ways of doing that. You can use the exec node to do that or use part of th fs-ops set which have a lot of tools for that sort of stuff.