# Using environment variable during another environment variable definiton in flow properties - issue

**URL:** https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316
**Category:** General
**Created:** [9 November 2022 11:14 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316 "2022-11-09T11:14:44Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)
#### Post date: [9 November 2022 11:14 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/1 "2022-11-09T11:14:44Z")

</div>

Dear all NodeRed enthusiastics,  
I notified I am not able to use environment variable in another environment variable definition in flow properties similar to example shown above. I don't know why, but the first variable (writen in format ${ID} in next variable definition) is empty string, despite ID variable was defined in special line above definition as string ...  
You can see the pictures of successful and unsuccessful environment variables definitions ...  
1] when I am NOT using ${ID} and ${TOPIC}, TOPIC = "/vamet1x02/", TOPIC\_C0 = "/vamet1x02/c0/", TOPIC\_C1 = "/vamet1x02/c1/", TOPIC\_C2 = "/vamet1x02/c2/" as expected 🙂  
2] when I am uising ${ID} and ${TOPIC}, TOPIC = "/vamet1x/", TOPIC\_C0 = "c0/", TOPIC\_C1 = "c1/", TOPIC\_C2 = "c2/" ☹  
Would you be so kind and could you advise me, what I am doing wrong, please?  
Thx in advance. Regards, Josef.  
 ![Works](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a8ef86d0131e475f45cad1cfa9d2fb4ad874d33.png)  
 ![DoesntWork](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb4000155543c04352672c000d569af5d9d0825c.png)

---

<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: [9 November 2022 11:41 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/2 "2022-11-09T11:41:46Z")

</div>

If I understand correctly then possibly all you need in the subflow properties is to provide the ID. Then inside the subflow build the TOPIC strings from the id and fetch the values from the environment variables.

---

<div class="post-metadata">

### Author: ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)
#### Post date: [9 November 2022 11:49 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/3 "2022-11-09T11:49:51Z")

</div>

Dear Colin,  
thank you very much for your response.  
Yes, exactly. All what I need is to enter ID (as string) and I would like to have all appropriate flow nodes properties / values after that derived from this environment variable.  
BTW, "TOPIC\_0x" are used for example in "mqtt in" nodes for "Topic" property.  
Regards, Josef.

---

<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: [9 November 2022 12:03 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/4 "2022-11-09T12:03:58Z")

</div>

I meant remove the subflow properties except for ID and build the other values inside the subflow from the ID. So, for example, in a Function node

```auto
msg.topic = "/vamet1x" + env.get("ID") + "/"

```

or using template string literals

```auto
msg.topic = `/vamet1x${env.get("ID")}/`

```

---

<div class="post-metadata">

### Author: ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)
#### Post date: [10 November 2022 04:52 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/5 "2022-11-10T04:52:14Z")

</div>

Dear Colin,  
thanks again for quick and valuable advise, I am using already this approach 😉.  
Unfortunately, it is not easy to do in case of mqtt in node, as there is no input option (except mqtt itself) and therefore setting up the parameters by code is impossible (or I am not experienced enough).  
And what is frustrating for me ... as description of using environment variables on link [Using environment variables : Node-RED](https://nodered.org/docs/user-guide/environment-variables) says, it should be possible in TypedInput widget (like flow properties are) ... and there is indicator it is / was possible: [Using subflow environment variable to set mqtt topic - #7 by tinamore](https://discourse.nodered.org/t/using-subflow-environment-variable-to-set-mqtt-topic/15331/7) ... knolleary used this and tinamore confirmed it is / was working ...  
Therefore, is it relating to version of my NodeRed? Or was the attitude of knolleary solution misleading?  
Hmmm ... still unclear for me.  
Regards, Josef.

 ![knolleary_QM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7486959d8ea9c9f872b48c937dab62c3a6765a2.png)

---

<div class="post-metadata">

### Author: ![phoddie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phoddie/32/64232_2.png) [@phoddie](https://discourse.nodered.org/u/phoddie)
#### Post date: [10 November 2022 06:31 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/6 "2022-11-10T06:31:51Z")

</div>

FWIW – I haven't been able to use one environment variable from within another. I tried to duplicate the example that @jopl posted from @knolleary, but that didn't work either. The output for `TOPIC1` is `"stat//power2"` instead of the expected `"stat/ROOM1/power2`". Here's the simplified test flow:

```auto
[
    {
        "id": "34ee1d1f3ef8a9d2",
        "type": "tab",
        "label": "env test",
        "disabled": false,
        "info": "",
        "env": [
            {
                "name": "ROOM",
                "value": "ROOM1",
                "type": "str"
            },
            {
                "name": "TOPIC1",
                "value": "stat/${ROOM}/power2",
                "type": "env"
            }
        ],
        "_mcu": {
            "mcu": false
        }
    },
    {
        "id": "9a997e58a5bca39d",
        "type": "inject",
        "z": "34ee1d1f3ef8a9d2",
        "name": "",
        "props": [
            {
                "p": "ROOM",
                "v": "ROOM",
                "vt": "env"
            },
            {
                "p": "TOPIC1",
                "v": "TOPIC1",
                "vt": "env"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "_mcu": {
            "mcu": false
        },
        "x": 290,
        "y": 180,
        "wires": [
            [
                "003785f01356c9da"
            ]
        ]
    },
    {
        "id": "003785f01356c9da",
        "type": "debug",
        "z": "34ee1d1f3ef8a9d2",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 490,
        "y": 180,
        "wires": []
    }
]

```

---

<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: [10 November 2022 14:11 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/7 "2022-11-10T14:11:06Z")

</div>

This simple example works for me.

```auto
[{"id":"78e2f572.71f74c","type":"subflow","name":"room_pir","info":"","category":"","in":[{"x":137,"y":179.0000057220459,"wires":[{"id":"d715137.ff4d2f"}]}],"out":[],"env":[{"name":"ROOM","type":"str","value":""},{"name":"SENSOR_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_sensor"},{"name":"LIGHTS_SET_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_lights/set"},{"name":"LIGHTS_GET_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_lights/get"}]},{"id":"d715137.ff4d2f","type":"mqtt out","z":"78e2f572.71f74c","name":"publish to ${LIGHTS_SET_TOPIC}","topic":"${LIGHTS_SET_TOPIC}","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"adf2dac3.b20fe8","x":400,"y":180,"wires":[]},{"id":"adf2dac3.b20fe8","type":"mqtt-broker","name":"localhost","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"tigger/LWT","birthQos":"0","birthPayload":"Online","birthMsg":{},"closeTopic":"tigger/LWT","closeQos":"0","closePayload":"Offline","closeMsg":{},"willTopic":"tigger.LWT","willQos":"0","willPayload":"Offline","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"1bd984a7.b656db","type":"subflow:78e2f572.71f74c","z":"b08cb6bc45dd4866","name":"kitchen_pir","env":[{"name":"ROOM","value":"kitchen","type":"str"},{"name":"SENSOR_TOPIC","value":"test/${ROOM}_sensor","type":"env"},{"name":"LIGHTS_SET_TOPIC","value":"test/${ROOM}_lights/set","type":"env"},{"name":"LIGHTS_GET_TOPIC","value":"test/${ROOM}_lights/get","type":"env"},{"name":"flexdash_grid","value":"","type":"str"}],"x":410,"y":280,"wires":[]},{"id":"2e51ab08b72022a2","type":"inject","z":"b08cb6bc45dd4866","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":280,"wires":[["1bd984a7.b656db"]]},{"id":"6dfce52f05474ff2","type":"mqtt in","z":"b08cb6bc45dd4866","name":"","topic":"test/kitchen_lights/set","qos":"1","datatype":"auto-detect","broker":"adf2dac3.b20fe8","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":380,"wires":[["e54004041812fc03"]]},{"id":"e54004041812fc03","type":"debug","z":"b08cb6bc45dd4866","name":"debug 34","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":420,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)
#### Post date: [10 November 2022 19:50 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/8 "2022-11-10T19:50:34Z")

</div>

Dear Colin,  
thank you again.  
As I can see, your definition using environment variables chaining is working. Have you any idea, where I did a mistake?  
Regards, Josef.

---

<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: [11 November 2022 08:11 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/9 "2022-11-11T08:11:26Z")

</div>

> [@jopl](#):
>
> Have you any idea, where I did a mistake?

Can you post a simple failing example, similar to the one I posted?

---

<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: [11 November 2022 09:38 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/10 "2022-11-11T09:38:16Z")

</div>

In particular, how have you configured the MQTT node?

---

<div class="post-metadata">

### Author: ![jopl](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jopl](https://discourse.nodered.org/u/jopl)
#### Post date: [12 November 2022 11:10 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/11 "2022-11-12T11:10:35Z")

</div>

Dear Colin,  
I am so sorry for delay, I reinstalled everything for starting from "clean" environment ... no change.  
Sending the example :-).  
Thank you very much for your help.  
Regards, Josef.  
[flow\_example.json](https://discourse.nodered.org/uploads/short-url/5Mqn68PtlEZaobW5t2ROraQj5uN.json) (2.4 KB)

---

<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: [12 November 2022 11:25 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/12 "2022-11-12T11:25:04Z")

</div>

I thought this was about subflow properties as in the post you linked to and the example I posted.

Edit: For the future please post flows inline as described in this canned message.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 January 2023 11:25 UTC](https://discourse.nodered.org/t/using-environment-variable-during-another-environment-variable-definiton-in-flow-properties-issue/70316/13 "2023-01-11T11:25:35Z")

</div>

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