# Use Group Env Variable in global variable name

**URL:** <https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907>\
**Category:** General\
**Created:** [1 September 2025 19:06 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907 "2025-09-01T19:06:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 19:06 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/1 "2025-09-01T19:06:13Z")

</div>

I’m trying to utilize a group environment variable as an addendum to variable names throughout a flow. Attached is a simplified version - a group env variable is set to TankNum = T1 and in theory a change node could set a variable name with a combination of a static name + a variable name. The resulting variable name would be TankNameT1. Is there any way of doing this as this method doesn’t seem to work?

```auto
[
    {
        "id": "1e05ea3ac797b66e",
        "type": "group",
        "z": "f6200681b265563b",
        "name": "",
        "style": {
            "label": true
        },
        "nodes": [
            "a17e5330370eca1d",
            "9ad2dc41299e88d4",
            "785e596f500556a3"
        ],
        "env": [
            {
                "name": "TankNum",
                "value": "T1",
                "type": "str"
            }
        ],
        "x": 174,
        "y": 6659,
        "w": 552,
        "h": 82
    },
    {
        "id": "a17e5330370eca1d",
        "type": "inject",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 270,
        "y": 6700,
        "wires": [
            [
                "785e596f500556a3"
            ]
        ]
    },
    {
        "id": "9ad2dc41299e88d4",
        "type": "debug",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 6700,
        "wires": []
    },
    {
        "id": "785e596f500556a3",
        "type": "change",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "TankName${TankNum}",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 6700,
        "wires": [
            [
                "9ad2dc41299e88d4"
            ]
        ]
    }
]

```

![Screenshot 2025-09-01 at 2.01.23 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6d8cfe76e02fd237ec297372fd614af506cd0ed.png)

 ![Screenshot 2025-09-01 at 2.02.00 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/1560c9fa4117923eff9a0349d7d3abfe9bcb9d5b.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 September 2025 19:17 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/2 "2025-09-01T19:17:51Z")

</div>

In the change node add a rule before global rule.  
set `msg.``TankNum`  
to value  
`$``TankNum`

Then in the global rule  
use  
` TankName[msg.TankNum]`

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 19:25 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/3 "2025-09-01T19:25:30Z")

</div>

Thanks for the assist. It’s currently showing me “undefined” on the debug. New code export and image attached.

```auto
[
    {
        "id": "1e05ea3ac797b66e",
        "type": "group",
        "z": "f6200681b265563b",
        "name": "",
        "style": {
            "label": true
        },
        "nodes": [
            "a17e5330370eca1d",
            "9ad2dc41299e88d4",
            "785e596f500556a3"
        ],
        "env": [
            {
                "name": "TankNum",
                "value": "T1",
                "type": "str"
            }
        ],
        "x": 174,
        "y": 6659,
        "w": 552,
        "h": 82
    },
    {
        "id": "a17e5330370eca1d",
        "type": "inject",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 270,
        "y": 6700,
        "wires": [
            [
                "785e596f500556a3"
            ]
        ]
    },
    {
        "id": "9ad2dc41299e88d4",
        "type": "debug",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 6700,
        "wires": []
    },
    {
        "id": "785e596f500556a3",
        "type": "change",
        "z": "f6200681b265563b",
        "g": "1e05ea3ac797b66e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "TankNum",
                "pt": "msg",
                "to": "TankNum",
                "tot": "env"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "TankName[msg.TankNum]",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 6700,
        "wires": [
            [
                "9ad2dc41299e88d4"
            ]
        ]
    }
]

```

 ![Screenshot 2025-09-01 at 2.24.18 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b94bb078e472ccb5c3c5ae40f4c000d7db0bca1e.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 September 2025 19:37 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/4 "2025-09-01T19:37:39Z")

</div>

Can you show the sidebar context where the global TankName is set.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [1 September 2025 19:39 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/5 "2025-09-01T19:39:12Z")

</div>

I didn't use an env variable for TankNum (set msg.TankNum using msg.payload) but this worked for me.  
Just to check, TankNum is a number & TankName is an Array (with number of elements at least as great as TankNum)?

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 19:43 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/6 "2025-09-01T19:43:53Z")

</div>

![Screenshot 2025-09-01 at 2.43.43 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f712e6bb384d91434177775905b2e343b8115b4.png)

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 19:44 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/7 "2025-09-01T19:44:52Z")

</div>

Tank name is going to be, for example:

TankT1  
TankT2  
TankT16  
TankC3

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 September 2025 19:46 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/8 "2025-09-01T19:46:04Z")

</div>

That is where you set the env var.

I am asking to see your sidebar context tab, This will show if there is a global context var and what it is, so I can clearly understand how the global is set up.

I think you may be using separate globals for each tank, it would be simpler to set up a global object for all tanks.

[edit]  
Example of how to set tanks object and how to read value for global using group env

```auto
[{"id":"fd653333f4af8a92","type":"inject","z":"d1395164b4eec73e","name":"set tanks global object","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":2160,"wires":[["1959b4cc2eb0baa2"]]},{"id":"1959b4cc2eb0baa2","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"Tanks","pt":"global","to":"{\"T1\":1,\"T2\":2,\"T16\":16,\"C3\":3}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2160,"wires":[[]]},{"id":"1e05ea3ac797b66e","type":"group","z":"d1395164b4eec73e","name":"","style":{"label":true},"nodes":["a17e5330370eca1d","9ad2dc41299e88d4","785e596f500556a3"],"env":[{"name":"TankNum","value":"T1","type":"str"}],"x":174,"y":2219,"w":552,"h":82},{"id":"a17e5330370eca1d","type":"inject","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":270,"y":2260,"wires":[["785e596f500556a3"]]},{"id":"9ad2dc41299e88d4","type":"debug","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":2260,"wires":[]},{"id":"785e596f500556a3","type":"change","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","rules":[{"t":"set","p":"TankNum","pt":"msg","to":"TankNum","tot":"env"},{"t":"set","p":"payload","pt":"msg","to":"Tanks[msg.TankNum]","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":2260,"wires":[["9ad2dc41299e88d4"]]}]

```

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 20:19 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/9 "2025-09-01T20:19:38Z")

</div>

I have about 15 or so tanks that are being temperature controlled - each one in a flow like the flows below. I’m trying to set a group environment variable that can be used to modify some of the variable names so that when I tweak one of these group flows in the future, it will be a lot easier to replicate to each tank.

 ![Screenshot 2025-09-01 at 3.07.58 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/307174ea26d450439914fb006038c90190b501b6.png)

Your flow creates the following in context data but my version doesn’t create a variable called global.TankNameT1 as I would expect.

![Screenshot 2025-09-01 at 3.13.57 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c71b75c12f3de449740725129738afe23470deb.png)

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [1 September 2025 20:48 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/10 "2025-09-01T20:48:43Z")

</div>

So, for example, any time TankC1 is referenced in the name of different variables, the environment variable can be set on a group level and used in different variable names within that group.

 ![Screenshot 2025-09-01 at 3.43.44 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b39625292d84e4233ad077aefcc4710edb512848.png)

 ![Screenshot 2025-09-01 at 3.43.32 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/0305295adf4e43be561e702a9a0fe3c4bd98f841.png)

 ![Screenshot 2025-09-01 at 3.44.08 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1a18e5598234abe147163764bc3c581ee951b25.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 September 2025 22:20 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/11 "2025-09-01T22:20:59Z")

</div>

I am trying to point out to you that you have made your globals more difficult to work with. Having separate global vars for each tank and then each tanks properties, makes it harder to access each property, as your are now noticing as you application becomes more involved.

Setting the tanks to an object will be a better solution in the long run for you. so instead of trying to create a context using your ( Frankenstein ) design, why not use the power of an already created system and get the most out of the JSON/javascript object, as it is what it was designed for.

another example

```auto
[{"id":"fd653333f4af8a92","type":"inject","z":"d1395164b4eec73e","name":"set tanks global object","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":2220,"wires":[["1959b4cc2eb0baa2"]]},{"id":"1959b4cc2eb0baa2","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"Tanks","pt":"global","to":"{\"T1\":{\"batch\":1,\"sip\":{\"upper\":\"up1\",\"lower\":\"low1\"}},\"T2\":{\"batch\":2,\"sip\":{\"upper\":\"up2\",\"lower\":\"low2\"}}}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":2220,"wires":[[]]},{"id":"1e05ea3ac797b66e","type":"group","z":"d1395164b4eec73e","name":"","style":{"label":true},"nodes":["a17e5330370eca1d","9ad2dc41299e88d4","785e596f500556a3","2074fec0d74f0ad0","667e1ec4078c14a3"],"env":[{"name":"TankNum","value":"T1","type":"str"}],"x":174,"y":2279,"w":632,"h":122},{"id":"a17e5330370eca1d","type":"inject","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":290,"y":2320,"wires":[["785e596f500556a3"]]},{"id":"9ad2dc41299e88d4","type":"debug","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":2320,"wires":[]},{"id":"785e596f500556a3","type":"change","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","rules":[{"t":"set","p":"TankNum","pt":"msg","to":"TankNum","tot":"env"},{"t":"set","p":"payload","pt":"msg","to":"Tanks[msg.TankNum].sip","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2320,"wires":[["9ad2dc41299e88d4"]]},{"id":"2074fec0d74f0ad0","type":"change","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","rules":[{"t":"set","p":"Tanks.T1.sip.upper","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":2360,"wires":[[]]},{"id":"667e1ec4078c14a3","type":"inject","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"set ta property in anks global object","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"upper","payload":"","payloadType":"date","x":360,"y":2360,"wires":[["2074fec0d74f0ad0"]]}]

```

Yes you will have to rethink and edit your already created flows, but in the end your flows will become simpler to create and maintain. With a well thought out object you could probably have one flow do the job of your 15 flows. Removing the need to add a flow each time you add a tank , all you would need to do is add a entry in the global object for the new tank.

p.s. When you finally show me the sidebar context for the global you are trying to read, i may be able to show you the way you can do it, your way, i am not going to guess what your global context vars look like. An example flow containing an inject and change node setting your global, would be nice then i and others would be able to test and provide a example using the correct context name and value/s.

---

<div class="post-metadata">

**Author:** ![ryanprel](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@ryanprel](https://discourse.nodered.org/u/ryanprel)\
**Post date:** [2 September 2025 01:31 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/12 "2025-09-02T01:31:26Z")

</div>

Yeah, “just make it simpler” doesn’t take into account the complexity of all of the data sources coming in and out and dashboard nodes. Sometimes you have to build a complex machine first and then simplify it. I’m in the process of simplifying it and learning how to do that in node-red along the way.

It seems like there isn’t actually a way of using an environment variable from a group in constructing variable names.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 September 2025 05:55 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/13 "2025-09-02T05:55:05Z")

</div>

> [@ryanprel](#):
>
> It seems like there isn’t actually a way of using an environment variable from a group in constructing variable names.

Sure there is, here is an example using JSONata

```auto
[{"id":"785e596f500556a3","type":"change","z":"d1395164b4eec73e","g":"1e05ea3ac797b66e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$globalContext(\"TankName\" & $env(\"TankNum\"))\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2320,"wires":[["9ad2dc41299e88d4"]]}]

```

---

<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:** [16 September 2025 05:55 UTC](https://discourse.nodered.org/t/use-group-env-variable-in-global-variable-name/98907/14 "2025-09-16T05:55:40Z")

</div>

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