# MQTT and Environment Variables

**URL:** https://discourse.nodered.org/t/mqtt-and-environment-variables/22426
**Category:** General
**Created:** [29 February 2020 21:02 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426 "2020-02-29T21:02:14Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 21:02 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/1 "2020-02-29T21:02:14Z")

</div>

Hi.  
I've been running node-red on a BeagleBone Black Wireless for some time now and am trying to make some upgrades to the software, mainly to parameterize the MQTT connection.

According to [https://nodered.org/docs/user-guide/environment-variables](https://nodered.org/docs/user-guide/environment-variables) "Any node property can be set with an environment variable by setting its value to a string of the form ${ENV\_VAR} ."

I've set some shell variables  
MQTT\_BROKER  
MQTT\_CLIENT  
MQTT\_PORT  
MQTT\_QOS  
MQTT\_TOPIC  
MQTT\_NAME  
to values that currently are known to work. I then changed the MQTT out node and its broker node to try to use these values as ${MQTT\_BROKER}, etc. I've had several problems in this endeavor:

1. it's not clear how to set these variables before node-red starts automatically, so it's not clear to me whether these the values of these variables are even being set. To get past this problem for the time being, I've stopped node-red, set the variables, tested that they are indeed set by examining the output of  
set | grep MQTT  
and then restarted node-red.
2. Nevertheless, it doesn't appear that the values are used. Indeed, I can't even deploy with the broker's port set to ${MQTT\_PORT}, so I have set it to 1883 for the time being.
3. After restarting node-red, the log shows  
29 Feb 15:46:35 - [info] Starting flows  
29 Feb 15:46:36 - [info] [wifiscan:e07da6ad.5061d8] wifiscan - Initialized  
29 Feb 15:46:36 - [info] Started flows  
29 Feb 15:46:36 - [info] serial port /dev/ttyO4 opened at 2400 baud 8N1  
29 Feb 15:46:36 - [info] [mqtt-broker:BBBW] Connection failed to broker: {MQTT\_CLIENTID}@mqtt://{MQTT\_HOST}:1883  
29 Feb 15:46:51 - [info] [mqtt-broker:BBBW] Connection failed to broker: {MQTT\_CLIENTID}@mqtt://{MQTT\_HOST}:1883  
29 Feb 15:47:06 - [info] [mqtt-broker:BBBW] Connection failed to broker: {MQTT\_CLIENTID}@mqtt://{MQTT\_HOST}:1883  
etc.

The failed attempts continue even after moving a node (to activate the "deploy" option) and then deploying.

Suggestions, please! Thanks,  
Richard

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [29 February 2020 22:01 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/2 "2020-02-29T22:01:14Z")

</div>

Hi, welcome to the forum. Posting your actual flow would help us to help you.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [29 February 2020 22:12 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/3 "2020-02-29T22:12:08Z")

</div>

Are you starting node red in the same environment that you have set the vars? Or to put it another way exactly how are you setting them then starting node-red?

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 22:44 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/4 "2020-02-29T22:44:46Z")

</div>

Hi. Thanks for asking. Here's a listing of the session (with some obfuscation for privacy). The obscured values are ones known to work both before and after this experiment.

root@beaglebone:/home/me# set | grep MQTT  
MQTT\_BROKER=[---]  
MQTT\_CLIENT\_ID=[---]  
MQTT\_PORT=1883  
MQTT\_QOS=1  
MQTT\_TOPIC=[---]  
root@beaglebone:/home/me# service node-red stop  
root@beaglebone:/home/me# service node-red start  
root@beaglebone:/home/me# journalctl -f -u node-red -o cat

Oh, maybe I should mention that I ended up using neither the MQTT\_PORT nor the MQTT\_QOS value, as neither was possible.

Thanks, Richard

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [29 February 2020 22:50 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/5 "2020-02-29T22:50:09Z")

</div>

hmm - that looks like the MQTT node is expecting numerics... and of course all env variables are strings...

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 22:52 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/6 "2020-02-29T22:52:53Z")

</div>

The [---] are strings (without quotes), such as (not the real values)  
MQTT\_BROKER=alpha.beta.gamma  
MQTT\_CLIENT\_ID=Whoever  
MQTT\_TOPIC=Whatever

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 22:54 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/7 "2020-02-29T22:54:02Z")

</div>

And the broker, client, and topic should be strings.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [29 February 2020 22:54 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/8 "2020-02-29T22:54:08Z")

</div>

indeed - and expected to be so... I suspect the node is not handling a port of "1883" when it expects 1883.

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 22:55 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/9 "2020-02-29T22:55:49Z")

</div>

Right, but as I said, I'm not using the MQTT\_PORT environment variable. It happens to be set, but couldn't be used in defining the node.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [29 February 2020 22:57 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/10 "2020-02-29T22:57:43Z")

</div>

Actually looking at the code they can be strings just fine.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 February 2020 22:59 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/11 "2020-02-29T22:59:15Z")

</div>

> [@dceejay](#):
>
> . I suspect the node is not handling a port of "1883" when it expects 1883.

Not quite. The editor side tries to validate the node configuration port value as a number, but the value is `"${MQTT...}"`. That will cause it to flag it up as being invalid, but you can still deploy. But you'd have to manually edit the flow file to add that value anyway.

How are you setting the env vars in the first place? Just because they are set in your local root shell, it doesn't mean they will be set when running NR as a service.

You could add some `console.log` lines in your `settings.js` file to print out the values of `process.env.MQTT_BROKER` etc.

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 23:07 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/12 "2020-02-29T23:07:41Z")

</div>

I'm confused. The reason I stopped the node-red service and then restarted it was to be sure that the variables were defined when the service started.

OK. I've got the settings.js file open, and near the end I see  
// configure the logging output  
logging: {  
...  
}

Where do I put console.log lines, and what would one of them look like:  
printf("process.env.MQTT\_BROKER = %s\n", process.env.MQTT\_BROKER)  
or what?

Thanks.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 February 2020 23:10 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/13 "2020-02-29T23:10:07Z")

</div>

At the start of the file (ie, very first line) add:

```auto
console.log(process.env.MQTT_BROKER);

```

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 23:12 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/14 "2020-02-29T23:12:22Z")

</div>

And to answer your question about how I'm setting the variables, I'm sourcing a file with lines like  
export MQTT\_BROKER=[alpha.beta.com](http://alpha.beta.com)  
export MQTT\_CLIENT\_ID=Whatever

and then I check that the settings took by running

set | grep MQTT

with the results I expected.

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 23:17 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/15 "2020-02-29T23:17:04Z")

</div>

OK. I've done that, and get the result "undefined"

Now what?

Thanks.

---

<div class="post-metadata">

### Author: ![ThisEndUp](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@ThisEndUp](https://discourse.nodered.org/u/ThisEndUp)
#### Post date: [29 February 2020 23:33 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/16 "2020-02-29T23:33:26Z")

</div>

It looks like one possibility is to modify  
/lib/systemd/system/node-red.service  
to export the environment variables I want.

Seems a bit brutal, but maybe that's what's needed.

Or is there something I could put into settings.js that would do the trick?

Thanks.

(Oops...just saw the time. I'll have to duck out for a supper invitation for the next few hours, but I'll be back.)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 February 2020 23:49 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/17 "2020-02-29T23:49:45Z")

</div>

You can set env vars via `process.env` in your settings file. So yes that would be a way of doing it.

It's midnight here. I'll be asleep when you're back from supper. But hopefully you have enough to make some progress.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 March 2020 07:07 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/18 "2020-03-01T07:07:37Z")

</div>

I think the problem is that you are setting the env vars in your terminal session but the service gets a new environment so they are not set. This is as it should be because you wouldn't want a service start to behave differently dependent on what env vars happen to be set in the session you start it from. I think you will need to set them up in the service or settings, or read them from a file in the service start, or something else along those lines.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 March 2020 15:45 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/19 "2020-03-01T15:45:13Z")

</div>

This might be the best way to do it, if it is appropriate. It overrides the service file in a way that will survive updates as the original service file is not changed.

> <https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services>

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [1 March 2020 17:40 UTC](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426/20 "2020-03-01T17:40:46Z")

</div>

I have that configured in my alternate installer as well if you want an example.

[Next page](https://discourse.nodered.org/t/mqtt-and-environment-variables/22426.md?page=2)
