# Reference env variable in property name

**URL:** <https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015>\
**Category:** General\
**Created:** [24 May 2022 17:03 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015 "2022-05-24T17:03:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [24 May 2022 17:03 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/1 "2022-05-24T17:03:03Z")

</div>

Hi everyone, I am trying to generalize a subflow so that I can use it in multiple rooms of my place.

Some of my global scope properties look something like `global.currentscene.livingroom` or `global.currentscene.bedroom`.

What I am now doing is that I am setting an env variable `$room` in my subflow in the attempt to be able to set global properties dynamically from inside the subflow. In other words, I am trying to then do something like:

Change Node -\> Property -\> `global.currentscene.$room` -\> Assign value

I have read the docs but it is still unclear to me whether I can achieve something like this. [This page](https://nodered.org/docs/user-guide/environment-variables) mentions not being able to reference partial stuff, but does that mean that I should instead assign a value such as `global.currentscene.livingroom` to my env, rather than `livingroom`, and then just use `$room` ?

Thanks

---

<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:** [24 May 2022 17:30 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/2 "2022-05-24T17:30:35Z")

</div>

You could set the env var to a message then reference that var.  
e.g.

```auto
[{"id":"62b0db36.5664cc","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":160,"y":120,"wires":[{"id":"18db26f9.bc2c09"}]}],"out":[{"x":440,"y":140,"wires":[{"id":"18db26f9.bc2c09","port":0}]}],"env":[{"name":"room","type":"str","value":"","ui":{"type":"input","opts":{"types":["str"]}}}],"meta":{},"color":"#DDAA99"},{"id":"18db26f9.bc2c09","type":"change","z":"62b0db36.5664cc","name":"","rules":[{"t":"set","p":"global_name","pt":"msg","to":"room","tot":"env"},{"t":"set","p":"test[msg.global_name]","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":180,"wires":[[]]},{"id":"13c60667.bee79a","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":1880,"wires":[["1e3ddcce.248b13"]]},{"id":"1e3ddcce.248b13","type":"subflow:62b0db36.5664cc","z":"bf9e1e33.030598","name":"","env":[{"name":"room","value":"tester","type":"str"}],"x":340,"y":1880,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [24 May 2022 21:26 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/3 "2022-05-24T21:26:33Z")

</div>

When I try and import your code I get the error `Error: TypeError: o is undefined`

---

<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:** [24 May 2022 22:01 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/4 "2022-05-24T22:01:03Z")

</div>

Not sure what happened there, as imports fine for me. Its a simple flow.

The sub flow just contains a change node.

 ![chrome_screenshot_1653429706993](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c06553a6c3b18044a54bd98f449fb74f70ec4caf.png)

```auto
[{"id":"18db26f9.bc2c09","type":"change","z":"62b0db36.5664cc","name":"","rules":[{"t":"set","p":"global_name","pt":"msg","to":"room","tot":"env"},{"t":"set","p":"test[msg.global_name]","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":180,"wires":[[]]}]

```

The subflow enviroment variable is set like this

 ![chrome_screenshot_1653429186733](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d36e7ef6b426beeb41ec627bd388336b2335dda.png)  
The injected payload is saved to `global.test.[$room]`

---

<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:** [23 July 2022 22:01 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/5 "2022-07-23T22:01:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 August 2022 16:28 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/6 "2022-08-02T16:28:25Z")

</div>

Reopened at request of OP

---

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [2 August 2022 16:41 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/7 "2022-08-02T16:41:42Z")

</div>

@E1cid thanks for the help and sorry about the late reply, hadn't had time to look into it.

I am bumping on an issue here:

my globals are names `global.currentscene.ROOMNAME` and this allows me to neatly group things together in debug because of the dot notation.

what you suggest to do is to do something like `global.currentscene.[msg.room_name]` but it seems like this is not allowed by NR _because_ of the dot. I can, in fact, do `global.currentscene_[msg.room_name]` and NR stops complaining.

I am wndering if there is any smart way for me to keep the dot notation, though it's obviously not the end of the world and I can accept losing it.

---

<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 August 2022 16:47 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/8 "2022-08-02T16:47:32Z")

</div>

> [@AlexKalopsia](#):
>
> `global.currentscene[msg.room_name]`

Is valid in a change node, from node-red 1.3 i believe. I use it with no issues.  
this works and creates a global

```auto
[{"id":"9700255f.3906c","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":3500,"wires":[["18db26f9.bc2c09"]]},{"id":"18db26f9.bc2c09","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"global_name","pt":"msg","to":"room","tot":"str"},{"t":"set","p":"test.test[msg.global_name]","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":3500,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [2 August 2022 16:51 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/9 "2022-08-02T16:51:13Z")

</div>

I'm on 3.0.0

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36e4919b7339d7161e175af00e0881d604d856a3.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a3fdf77192b98b9c08ac4d07e4f7d29b4378b08.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:** [2 August 2022 16:54 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/10 "2022-08-02T16:54:51Z")

</div>

You would not have the dot before the bracket, the bracket replaces the dot  
e.g. `test.test.test` would be `test.test[msg.payload]`

---

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [2 August 2022 17:01 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/11 "2022-08-02T17:01:17Z")

</div>

Oh wow, I didn't get that at all, that seems fairly unexpected, but maybe it's documented somewhere? I would not expect the content of a message to also include dot notation. I will try that out, thank you

---

<div class="post-metadata">

**Author:** ![AlexKalopsia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexkalopsia/32/7195_2.png) [@AlexKalopsia](https://discourse.nodered.org/u/AlexKalopsia)\
**Post date:** [2 August 2022 23:25 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/12 "2022-08-02T23:25:31Z")

</div>

I can confirm that this worked! I still find it weird that if `msg.payload` is `my_string`, then `test[msg.payload]` would translate to `test.my_string` rather than `testmy_string`.

I guess is that by intuition `[msg.payload]` is just a placeholder for the string value (and this might be my wrong assumption), so I would totally expect `test.[msg.payload]` to be accepted and translate to `test.my_string`

Thanks again for the help

---

<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:** [3 August 2022 06:05 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/13 "2022-08-03T06:05:43Z")

</div>

Dot and bracket notation are fundamentals of javascript. See [Working with objects - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects)

---

<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:** [17 August 2022 06:06 UTC](https://discourse.nodered.org/t/reference-env-variable-in-property-name/63015/14 "2022-08-17T06:06:00Z")

</div>

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