Saving settings as flatfiles

i have a 'recipe' i am working and i have the basic starting parameters hardcoded into a node. i want to give the client a way to do these things:

*view and then load a settings file out of a directory

*change/make new settings, saving a new file in said directory (with date in filename) , i'm fairly sure i know how to do this part, just added for clarity.

*have the last loaded settings automatically load when starting up from scratch

just curious what the gurus say is the best way, i don't want to download 50 nodes and not find what i'm looking for, i've tried a few of the fs nodes and was unimpressed (read: ignorant) as to how to get the functionality i needed.

Are you talking about node-red or settings for something else?

a flat file i use to set flow variables to run a timing and variable setting loop.

not for node-red settings, a setting to run a 'fancy' loop based on the input parameters in the file

that data format do you require for the file?

Custom?
JSON?
XML
YAML?
INI?
Other?

If JSON or YAML, then you can easily read & write these file using built in nodes. XML is also possible but finnicky to generate JS objects that map nicely to some XML structures

For INI files and other, there are contrib nodes.

If a custom file format, you will probably end up with a bunch of parsing flows or functions for converting TO/FROM file/javascript


Lastly, what uses this file? Another application? Node-red?

really i just need a text/csv and i can sort out the loading and parsing with a file node, unless there is a better way to do it.

the fill just has variables like

zone1HoldTime=20

nothing fancy. it gets parsed by a file node into a function node, and i set those values as flow context to be used a across other function nodes

CSV is one better than a text file (in other words, not a great solution)

As you are working in JavaScript, the sensible choice would be to write JSON files. JSON converts directly to JavaScript objects and JavaScript objects convert directly to JSON. Node-red has built in nodes for doing this. Alternatively, in a function you simply call JSON.stringify or JSON.parse to convert.

Then instead of writing individual context variables just store the object. Your whole variable read/write code becomes about two lines & 4 nodes. And this never changes no matter how many variables as you have.

On top of that, the real benefit of using JSON (or yaml or XML) is that it is an extensible format? What I mean by that is you can add to it without breaking existing format. You cannot do that with a CSV file.

is there by change an example of these 2 lines and 4 nodes?

That was off the top of my head.
I've done similar things in the past.
I'll knock up a sample tomorrow.

In the mean time, read up on persistent context - I suspect that is all you really need.

"Working with context : Node-RED" Working with context : Node-RED.

"A guide to understanding 'Persistent Context' - General - Node-RED Forum" A guide to understanding 'Persistent Context'

As promised - really simple way to read / write a JSON file for storing settings.

chrome_nMgMGiuO7s

flow...

[{"id":"e5b9b9cccb321429","type":"tab","label":"settings file","disabled":false,"info":"","env":[]},{"id":"5bbab672f47f2bb2","type":"group","z":"e5b9b9cccb321429","name":"Read / write settings file \\n NOTE: this never needs to change!","style":{"label":true},"nodes":["abf0187e5c7ba3b3","6b74559a71f0b8e5","1d1a84e30f8f4bd0","cf1762cb6ad612ab","d1d4813a680d91bd","4a642877f344eabb","6829ecc8b2541d99","f506ddd488525490"],"x":34,"y":23,"w":752,"h":138},{"id":"d09213df05084c64","type":"inject","z":"e5b9b9cccb321429","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":260,"wires":[["81e7864349c4ad50"]]},{"id":"81e7864349c4ad50","type":"change","z":"e5b9b9cccb321429","name":"update settings.v1","rules":[{"t":"set","p":"settings.v1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":260,"wires":[[]]},{"id":"57369f189b61f0c4","type":"inject","z":"e5b9b9cccb321429","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":300,"wires":[["a8df0beb9c703763"]]},{"id":"a8df0beb9c703763","type":"change","z":"e5b9b9cccb321429","name":"update settings.another_setting","rules":[{"t":"set","p":"settings.another_setting","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":300,"wires":[[]]},{"id":"7f1f141a17f65bd2","type":"inject","z":"e5b9b9cccb321429","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"settings.v1","payloadType":"global","x":360,"y":400,"wires":[["1211ec5315eab82a"]]},{"id":"1211ec5315eab82a","type":"debug","z":"e5b9b9cccb321429","name":"v1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":630,"y":380,"wires":[]},{"id":"0afc3a38b6ba4ebd","type":"inject","z":"e5b9b9cccb321429","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"settings.another_setting","payloadType":"global","x":400,"y":440,"wires":[["8484b83e5af9649d"]]},{"id":"8484b83e5af9649d","type":"debug","z":"e5b9b9cccb321429","name":"v1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":630,"y":440,"wires":[]},{"id":"d7acbc4fd35e1f24","type":"comment","z":"e5b9b9cccb321429","name":"Store values in settings","info":"","x":380,"y":220,"wires":[]},{"id":"d74c36636fb865af","type":"comment","z":"e5b9b9cccb321429","name":"Get values from settings","info":"","x":390,"y":360,"wires":[]},{"id":"abf0187e5c7ba3b3","type":"inject","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"Save to file","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":80,"wires":[["1d1a84e30f8f4bd0"]]},{"id":"6b74559a71f0b8e5","type":"inject","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"Read from file","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":120,"wires":[["4a642877f344eabb"]]},{"id":"1d1a84e30f8f4bd0","type":"function","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"get settings","func":"msg.payload = global.get(\"settings\") || {}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":80,"wires":[["cf1762cb6ad612ab"]]},{"id":"cf1762cb6ad612ab","type":"json","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"","property":"payload","action":"","pretty":false,"x":450,"y":80,"wires":[["d1d4813a680d91bd"]]},{"id":"d1d4813a680d91bd","type":"file","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"","filename":"user_settings.json","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":640,"y":80,"wires":[[]]},{"id":"4a642877f344eabb","type":"file in","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"","filename":"user_settings.json","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":330,"y":120,"wires":[["6829ecc8b2541d99"]]},{"id":"6829ecc8b2541d99","type":"json","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"","property":"payload","action":"","pretty":false,"x":490,"y":120,"wires":[["f506ddd488525490"]]},{"id":"f506ddd488525490","type":"function","z":"e5b9b9cccb321429","g":"5bbab672f47f2bb2","name":"store loaded settings","func":"global.set(\"settings\", msg.payload || {})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":120,"wires":[[]]}]

NOTE: this becomes pretty much redundant if all you need it to persist values between node-red restarts. As pointed out in previous post - look into persistent context (aka file based context)

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