Edit by @Beno. Clearly you can not just keep saving the trend data to the groov Box over and over forever.
Firstly because it is already stored in the trend, so it is not a backup of any kind and secondly because there is a limit to the amount of data you can store on the hard drive.
Please consider using the second flow in this thread, sending trend data to an email address as a CSV attachment.
Thanks to Mary’s post on how to get a URL to download chart data, and a little use of curl
, you can build a flow that will regularly save trend data from the localhost groov server onto the AR1’s filesystem in CSV format!
Every week (the maximum period a trend will store) this flow injects, sets the URL, gets the CSV data with curl -k
in an exec node, then uses JavaScript to set the filename field.
The function node grabs the timestamps from the first and last entries , stored in epoch format, converts them to ISO format and trims off seconds and miliseconds, then stitches them together with the string --to--
, appends .csv
to the end, and gives the file node that string for the filename.
In my case this gave me 2017-08-16T21:53--to--2017-08-18T21:53.csv
for my 48 hour trend so I know what time period the CSV file covers. In application I would increase the inject to save this file once every two days since that is the length of my trend.
Once the flow has built a filename it uses the file node to permanently save the data to a CSV file in /home/dev
.
To get the URL I strongly recommend reading Mary’s post to ensure that Node-RED can use your URL, since the exec node does not have administrator or editor privileges on its own.
An easy way to get the URL that meets the requirements she goes over is to log in as an admin user and right click the download 'tag'
link in the trend display, and copy that link address to your clipboard.
Before you run the node you will need to edit the URL string and replace your groov box hostname, for example mary.groov.com
, with 127.0.0.1:8443
so that the exec node correctly resolves to the localhost. In the case of Mary’s post she would use the URL string https://127.0.0.1:8443/api/v0/data-logging/683_0_10_2_false/scanned-data.csv?api_key=XcJBX8AAwzJusPuQLauehTFvZ65EC6Vu
.
You are free to have this inject as often or rarely as you would like to store the trend data, so for a 48 hour trend like me you can change it to inject once every two days, or take off the timer and just manually click inject when you want to download the data, it’s totally up to you – feel free to modify this flow to suit your needs.
Happy coding!
[{“id”:“4df4c09d.ef9d5”,“type”:“file”,“z”:“e7108e55.3fa88”,“name”:“”,“filename”:“”,“appendNewline”:false,“createDir”:false,“overwriteFile”:“true”,“x”:710,“y”:1560,“wires”:},{“id”:“a273d3b0.d0b8”,“type”:“exec”,“z”:“e7108e55.3fa88”,“command”:“curl -k”,“addpay”:true,“append”:“”,“useSpawn”:“false”,“timer”:“”,“oldrc”:false,“name”:“”,“x”:370,“y”:1540,“wires”:[[“2a64887f.c5d1b8”],[“32fdec2.a463514”],[“32fdec2.a463514”]]},{“id”:“efaf4f3c.34f23”,“type”:“inject”,“z”:“e7108e55.3fa88”,“name”:“inject 1/week”,“topic”:“”,“payload”:“”,“payloadType”:“date”,“repeat”:“604800”,“crontab”:“”,“once”:false,“x”:220,“y”:1500,“wires”:[[“1a563f2e.bc32a1”]]},{“id”:“32fdec2.a463514”,“type”:“debug”,“z”:“e7108e55.3fa88”,“name”:“”,“active”:false,“console”:“false”,“complete”:“true”,“x”:510,“y”:1560,“wires”:},{“id”:“1a563f2e.bc32a1”,“type”:“function”,“z”:“e7108e55.3fa88”,“name”:“set logging url”,“func”:“// ALWAYS change "host name" to "127.0.0.1:8443" and leave the rest of the string the same.\nreturn { payload : "https://127.0.0.1:8443/api/v0/data-logging/87_0_2880_120_false/scanned-data.csv?api_key=3rWGiMK6ARtxk7AXEY4cXhGByCcAZAGK\” };",“outputs”:1,“noerr”:0,“x”:220,“y”:1540,“wires”:[[“a273d3b0.d0b8”]]},{“id”:“2a64887f.c5d1b8”,“type”:“function”,“z”:“e7108e55.3fa88”,“name”:“filename = "trend-start"–to–"trend-end"”,“func”:“start = msg.payload.indexOf("\\n");\nend = msg.payload.indexOf(",", start+1);\nmsg.first_stamp = parseInt(msg.payload.substring(start+1, end));\n // for UTC-7:00 timestamp offset: <(parseInt(…(…)) - 73600000)>\n \nf = new Date(msg.first_stamp);\nend = msg.payload.lastIndexOf(",");\nstart = msg.payload.lastIndexOf("\\n",end-1);\nl = new Date(parseInt(msg.payload.substring(start+1, end)));\n // for UTC-7:00 timestamp offset: <(parseInt(…(…)) - 73600000)>\n \nmsg.filename = f.toISOString().substring(0,16) + ‘–to–’ + l.toISOString().substring(0,16) + ".csv";\n// filename = "yyyy-mm-ddThh:mm_to_yyyy-mm-ddThh:mm" trend start date_to_trend end date\nreturn msg;”,“outputs”:1,“noerr”:0,“x”:610,“y”:1520,“wires”:[[“4df4c09d.ef9d5”]]}]