Downloading Latest File from Dashboard

Hey all,

I'm trying to create a simple flow to download the latest .csv file generated to the user's machine.

Context: When the user turns logging on, some other flows log external measurements to a local .csv file stored in the folder "/home/pi/log/". The file name varies based on the time they started measuring, but the full path is stored in global variables 'global.a-filename' and 'global.b-filename'.

I've tried integrating a version of this flow but can't get the flow to work with my variables, as they include the full path and using them without a default path seems to cause issues.

If i remove the path in the variable, leaving just the file name, and insert the path into the base path, I can get it to download. Perhaps I can just edit the string I'm inputting to remove the file path.

Here's an example of my variable:
global.a-filename="/home/pi/log/A_Power_Log20220427-11:42:32.csv"
And here's what I'm using currently in place of it:
"/A_Power_Log20220427-11:42:32.csv"

Any suggestions? I am by no means a node-red pro, so struggling a bit!

Here's what I've come up with...

[{"id":"1c47c3c0.9e4ecc","type":"function","z":"af4bb936.890328","name":"Set base path","func":"// using basepath = \"\" doesn't work as I'd hoped\n// example global.a-filename = /home/pi/log/A_Power_Log20220427-11:42:32.csv\n\nvar basePath = \"/home/pi/log/\";\nvar filename = msg.req.params.fn;\n\nmsg.filename = basePath + filename;\nreturn [msg, null];\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1280,"y":1640,"wires":[["ad804a0d.4310e8"],["ea82bad4.101d98"]]},{"id":"ea82bad4.101d98","type":"http response","z":"af4bb936.890328","name":"","statusCode":"","headers":{},"x":1650,"y":1680,"wires":[]},{"id":"7d2d8318.53d4bc","type":"catch","z":"af4bb936.890328","name":"","scope":null,"uncaught":false,"x":1120,"y":1720,"wires":[["bfbc1050.6708a","747edb6e.69d3b4"]]},{"id":"bfbc1050.6708a","type":"function","z":"af4bb936.890328","name":"Set 404","func":"msg.payload = msg.error;\nmsg.statusCode = 404;//resource not found\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1500,"y":1720,"wires":[["ea82bad4.101d98"]]},{"id":"747edb6e.69d3b4","type":"debug","z":"af4bb936.890328","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1150,"y":1760,"wires":[]},{"id":"a32f0ed2.38111","type":"ui_template","z":"af4bb936.890328","group":"dfb4a60f.d788f8","name":"ui_temlplate - present download links on dashboard","order":0,"width":0,"height":0,"format":"<div >\n    <a href=\"/A_Power_Log20220427-11:42:32.csv\"> Download File A</a>\n    <a href=\"/B_Power_Log20220427-11:42:32.csv\">Download File B</a>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":1490,"y":1800,"wires":[[]]},{"id":"8d759c0.04f7a68","type":"http in","z":"af4bb936.890328","name":"","url":"/:fn","method":"get","upload":false,"swaggerDoc":"","x":1080,"y":1640,"wires":[["1c47c3c0.9e4ecc"]]},{"id":"ad804a0d.4310e8","type":"file in","z":"af4bb936.890328","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":1490,"y":1620,"wires":[["ea82bad4.101d98"]]},{"id":"dfb4a60f.d788f8","type":"ui_group","name":"Data Export","tab":"48418b79.0f5834","order":1,"disp":true,"width":"12"},{"id":"48418b79.0f5834","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1}]

It's not clear to me what you are asking ?

There is no reference to any global vars in this flow ?

I notice that the 2nd output of the function node is never used, so not sure what its for ?

Hey, my apologies. The flow I've included is currently using some fixed text in place of where i'd like the variable to sit, within the UI template node:

I'd like to change:
<a href="/A_Power_Log20220427-11:42:32.csv"> Download File A</a>
to
<a href=(global.a-filename)> Download File A</a>

But no amount of formatting or adjustment of the associated variables can get it to behave.

The 2nd output is just a leftover component of the template flow I used to get started

Hope this makes more sense..

So why not remove the full path before saving the global, or before passing to file node ?

This will remove /home/pi/log/ from any payload sent in.

[{"id":"8eb4572f803114c5","type":"function","z":"b0ac7c4e8a64590a","name":"","func":"msg.payload = msg.payload.replace('/home/pi/log/', '');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":640,"wires":[["8a46e7325be84d5d"]]},{"id":"f80811ebe5eda0da","type":"inject","z":"b0ac7c4e8a64590a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/home/pi/log/A_Power_Log20220427-11:42:32.csv","payloadType":"str","x":870,"y":640,"wires":[["8eb4572f803114c5"]]},{"id":"8a46e7325be84d5d","type":"debug","z":"b0ac7c4e8a64590a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1190,"y":640,"wires":[]}]

Thanks! I’ll give this a crack. I had wondered if something like this would work, just wasn’t sure if it was the best way to go about it

Much appreciated

Unfortunately after some tinkering, this doesn't work. It seems that it doesn't even reach the function node when '/home/pi/log/' is included. I'm wondering if this is a problem with how the http nodes are set up?

Can you post the flow you have tried ?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.