I'm into node-red for some weeks now and one problem keeps coming up again and again for me.
I have a dozen sequences and multiple flows/tabs and the nodes in there shall rely on specific settings I want to maintain in a single place (or for each flow/tab separately).
So I like to set the email server, mqtt-prefixes, watch folder paths, http-endpoint uris etc. in a dedicated "config" location and pick bits out of the config via function node e.g., but most nodes do not seem to support injecting required properties via msg (which I use whenever possible). I even went as far as writing a custom "watch" node, which is able to be read folder-paths to watch at from msg.filepath, but I obviously cannot come up with custom nodes for all the native nodes in node-red just to make configuration possible.
What I am basically looking for is something like this:
inject object like { myProject : { emailserver : "mymail.com" }} into global context at flow startup.
(I seem to have accomplished this with "node-red-contrib-config" at least.)
enter {global['myProject.emailserver']} as "Server" value in the email-node editor (e.g.)
(This would also avoid "useless" setup/change/function nodes on front of many nodes.)
Is there something like this?
Thank you! o)
ps: I've seen that you can use ENV vars like this, but maintaining ENV vars for the project externally is not really the way to go in my opinion and I have hundreds of configurable items (ip-cameras, ip-addresses, folders, etc). I cannot list or edit ENV vars from within the node-red environment in a "settings-container" like fashion easily either.
You can do the first part using an object node and the basic change node where you can set
a global variable to be an object. No need for any contrib nodes
This is exctly what I use global contect variables for. You have tab 1 (I rename the tab to 'Initialize') the place you set the variables and then the other tabs can grab the values when they need to use them.
Or in the globals section of the settings.js file. To make management easier, you can leverage the node.js modules feature to create your own single-file module that you can then require into the globals. That way you can keep your own settings in your own file.
If the Config node is Active, the configuration properties will be applied at startup before any flows are started.
Having the settings ready before the flows start is quite important, I was just looking for an alternative to the contrib node. Using settings.js is difficult, settings in there do not export well together with the flows.
Regarding 2) Any ideas here? This is the real difficulty. How to supply settings to nodes, which do not support incoming msg. As mentioned, I had hopes to be able to use the node editor with some curly-bracket syntax or something to inject settings, like it's possible with ENV vars.
Today, env var are the only option available to do this sort of thing.
As of 1.0, it is possible to define and manage env vars within a subflow, but not generally.
There are some thoughts about adding something to the projects feature to make it possible to manage settings for different environments. But they are only thoughts and there's no specific plan to deliver it.
I created a topic regarding this "expression" type of syntax in node editors, not much response back then.
I also read about JSONata support in more recent versions, but failed to find a single example in the docs and this forum about where these JSONata expressions can be used and IF they can be used in any node editor (which would solve my problem!).
Thank you! o)
ps: Side topic question:
What does "Environment Variable" mean in Node-Red context? An "Environment Variable" for me, is something I set on OS or process level, like "PATH". Is this the kind of variables Node-Red is talking about, when it says "environment variable" in the docs or is it referring to some javascript / nodejs / flow context variables? I don't get it! o)
Not that I know of. You might though be able to do something with Node-RED's pseudo env vars - in a group box for example. Not sure.
Node-RED has the Typed Input control available to Nodes in the Editor which includes options to use JSONata which can do complex expressions. Other inputs can do deep object selections.
JSONata inputs have to be built into a nodes editor panel. However, you can also use it in a function node if needed (though often easier to simply use JavaScript in that case).
The core change node, has typed inputs and so is a generic way to potentially do what you want.
Environment Variables do indeed normally refer to something at the OS level. Such variables, when used in an application like Node-RED are invariant between start-ups.
But Node-RED has something else called Environment Variables as well. While somewhat confusing, I believe they are named that way because you use them in flows in the same way as OS env vars.
This 2nd type are set in flow tabs, groups and sub-flows and can be consumed within the structure that defines them.