Add date to 'save to file' node

Hello,
i want that the ‘save to file’ node uses the current date in the filename, so i added function in which i add ‘date’ property to the msg, and then in the save node i wrote: “{{msg.date}}.csv”, but the node is not reading the date property and it stores the file with this name: “{{msg.date}}.csv”.
i hope that you can help.

You cannot use the {{ }} syntax in the file name field. You need to generate the filename you want and pass it into the node as msg.filename - as described in the info sidebar of the File node.

1 Like

It would be helpful if you provided the code you used in the function…

Thank you.
that’s the code i wrote in the function:

var d = new Date();
var t = d.getTime();
var year = d.getFullYear();
var month = d.getMonth()+1; 
if(month.toString().length == 1) {
var month = '0'+month;
}
var day = d.getDate();
var hour  = d.getHours();
msg.date = t;
msg.filename = "measurements/"+year+month+day+".csv";
return msg;

and it is working well.

1 Like

Hi all

Can you please provide the code with the node

Thanks

Did you manage to find the right syntax for the node code ?

it shows error, does not work.

Show us the error.

It just says error in javascript code on this line:
var month = '0' + month;

Please export the function node that is showing the error and paste it here. Use the </> button at the top of the forum entry window when pasting it in, to prevent the forum messing with the format.

Also stop node red and start it again in a command window and paste the startup log here. Use the </> button again when pasting that.

It is not an error - more of a "there is something not right".

The problem is the code you have used is "not good". It is declaring var month twice - that is the warning.

Here is an updated version:

const d = new Date()
const year = d.getFullYear()
const month = (d.getMonth() + 1).toString().padStart(2, '0')
const day = d.getDate().toString().padStart(2, '0')
msg.date = d.getTime()
msg.filename = `measurements/${year}-${month}-${day}.csv`
return msg

Thanks, no error message but it still does not save the file name and use it as an external link. :
What am I missing here?
Plan was originally when someone presses Doorbell, it takes a snapshot and send a rich notification to HA with an external link to the jpg. But internet decided to cache the jpg file so I get the same image over and over. Second plan was to use a timestamp that saves every visitor (better plan), but can't find a solution that works with filename and external link. :frowning: I get doorbell_.jpg nonstop

[{"id":"934a2575fef20641","type":"inject","z":"b0334f5a6d891812","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":990,"wires":[["fa0b37c43d26efd1"]]},{"id":"18610cc8d82ca6da","type":"api-call-service","z":"b0334f5a6d891812","name":"Doorbell Snapshot","server":"f6a86a23.b6858","version":1,"debugenabled":false,"service_domain":"camera","service":"snapshot","entityId":"camera.doorbell_sub","data":"{\"filename\":\"/config/www/doorbell_{{ msg.date }}.jpg\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":480,"y":990,"wires":[["9db16d0408606e97"]]},{"id":"fa0b37c43d26efd1","type":"function","z":"b0334f5a6d891812","name":"Get Date","func":"const d = new Date()\nconst year = d.getFullYear()\nconst month = (d.getMonth() + 1).toString().padStart(2, '0')\nconst day = d.getDate().toString().padStart(2, '0')\nmsg.date = d.getTime()\nmsg.filename = `measurements/${year}-${month}-${day}.csv`\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":990,"wires":[["18610cc8d82ca6da"]]},{"id":"9db16d0408606e97","type":"function","z":"b0334f5a6d891812","name":"External Notification","func":"msg.payload = {\n    data: {\n        \"message\":\"Csengettek !\",\n        \"data\":\n            {\n            \"image\": \"https://internet.com/local/doorbell_{{ msg.date }}.jpg\",\n            \"channel\":\"Front Doorbell\",\n            \"priority\":\"high\",\n            \"ttl\": 0,\n            \"actions\":\n                [{\n                \"action\": \"URI\",\n                \"title\": \"Kamera megnyitás\",\n                    \"uri\": \"app://com.mcu.reolink\"\n                }]\n            }\n    }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":990,"wires":[["86f6cad23ef87d09"]]},{"id":"86f6cad23ef87d09","type":"api-call-service","z":"b0334f5a6d891812","name":"Phone notification","server":"f6a86a23.b6858","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_m2102k1g","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":920,"y":990,"wires":[[]]},{"id":"f6a86a23.b6858","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Since you are using HA - I have no idea. AFAIK, most folk here prefer to do everything in node-red.

This is what I (and most other on this forum) see when i import your flow:

As you can see, I have no idea what those nodes are and nor do i known settings they have


NOTE: The original intention of this thread (that is, BTW, over 5 years old), was to generate a dynamic filename in msg.filename - probably for sending to the file node. As you can see from my screenshot does just that.

Just because the msg now has a filename property does NOT mean poking that into a HA node will somehow change a filename somewhere in the bowels of HA.

My only suggestion at this point would be start a new thread and be explicit with what you are doing and that you are using HA etc - OR (probably better) - head over to the HA forum where you can get HA advise on the HA parts of your flow.

1 Like