MQTT configuration from file or env. variable

Hello,

I’m trying to figure out a way to configure my MQTT node’s Client ID , UserName, and Password, outside of the flow. My web portal assigns all the above and they are 32 to 64 characters long. The Client Id is used also within some of my subscription topics, and within an array of values. I would like to either use a file, or environmental variables to fill these credentials in when Node-Red Flows start. Don't want got to 4 different spots in my flows fill these in, for I will have 50plus devices. Thanks in advance for your idea’s and input.

1 Like

The MQTT uses the "configuration node" for setting Client ID, Server, and etc.
There seems to be no way to set up "configuration node" dynamically in run-time.
To set up for any number of devices, e.g. 50plus devices, the only workaround that I find is to modify directly the node-red source code(.json file) with a script.
Would be much better if node-red has a built-in solution.

Thanks for the response Tommy, I assume you mean put the value in the source code flow file? I'm not concerned about it being dynamically changed, I would like to put these property values somewhere to be read at startup. Like in a file. So I would like the property set for the configuration before runtime. So there is no way for these properties to read an environmental variable , they can only read the property value from the flow code?

Hi @Chris24918

you can do this using environment variables.

Any node property can be set to something like $(MY_CLIENT_ID) and the runtime will substitute the value of the env var MY_CLIENT_ID before it gets passed to the node. This cannot be used to insert env vars into part of the value - it has to be the entire value - so $(MY_CLIENT_ID)_FOO will not work.

That is exactly what you want if you are deploying the same set of flows to 50+ devices and you need each to insert its own configuration values.

1 Like

Thanks for your response! I am doing something wrong for it is not working.
I set my client ID in /etc/environment client_id = "5cdc3abcb2d28800084f9f37". Cycled power. Confirmed id is and env. variable, with "echo $client_id" at command line.

had inserted $(client_id) into the Client ID configuration text window within the MQTT node. It will not connect with env. variable.

What did I miss? Also wanted to note I added ```
env: process.env under functionGlobalContext in settings.js file after reading env variables usage in node red.

Are you actually running in the same environment ? Did Node-RED start at boot or from the user environment that you ran the echo from ?

You don’t actually need to do the setting.js part unless you want access from within a function node but it won’t hurt.

Hello dceejay,
Node-red starts at boot. So no I did not start it from the same user environment I ran the echo from. Can you help direct me to learn how to insure its env. variable is available within the running Node-Red environment?
Thanks,

Yes, I just ran command env from within node red with the exec node. The variable is not present there. So is there a place to declare it so its available to all environments?

How does node red start from boot? If it is a systemd script is it the standard Pi one for example?

No its a Moxa IOT gateway. And yes it starts from boot. I have added export client_id= in bash.bashrc and it still isn't getting loaded into node-red's env variables.

It is not clear what questions the No and Yes apply to. The first question was how does it run from boot, to which neither No nor Yes are valid answers. Presumably the No applies to the second question.
You will probably need to find out how it runs from boot and amend that to include the environment var. If it uses systemd then there are standard ways of appending settings to the script without modifying the original.
In order to check whether the env var is getting through at all you could use a change node to set msg.payload to the value and see what comes out.

Thanks, I don't know how it runs from boot, I will try and contact Moxa to get an answer. I have tried both the change node and the exec node (running the env command) and do not get my value back. I only see it from the command prompt via my ssh session.
I now know I need to dig into how node red starts within this device.

Thanks,

A quick search suggests that the OS is Debian in which case (unless it is an old Debian) it will use systemd. Try
sudo systemctl status nodered
if that doesn't find a nodered service it may be called something else. You could try
systemctl list-unit-files|grep node
to find it.

You can also set environment variables in your settings file:

process.env.FOO = "hello";

As the settings file is node.js code, where it sources those env vars from is up to you - it could read from an external file or similar.

1 Like

Yes you are correct it is using systemd. So now I need to figure out where to set the env. variable with it running correct? Thanks!

Ok So if I set it up in the settings file.. like process.env.Foo ="hello" will I still use $(Foo) in the MQTT config node?

Thanks,

Yes. How you set the env var is independent of how you use it.

Thank you , that should work. I'll give it a try and update the results tomorrow..

Thanks process.env.client_id worked. I have a better understanding of environments now.