# Global Context in Node

**URL:** <https://discourse.nodered.org/t/global-context-in-node/49700>\
**Category:** General\
**Created:** [13 August 2021 23:12 UTC](https://discourse.nodered.org/t/global-context-in-node/49700 "2021-08-13T23:12:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:12 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/1 "2021-08-13T23:12:37Z")

</div>

So pretty new to node-red but I've been learning a lot in the last few weeks... that being said I was using node-red with the Huemagic node to control my lights. I've recently changed to a zigbee2mqtt setup. Only problem is, the node for z2m contains the settings for the lights within the node? not sure if I'm stating it correctly, however the pictures will show what I mean.

Before with huemagic I passed on the settings with a change node then to the light. Now within the node to control the light I specify the settings. No big deal... except for the lights where I control the temperature with a global variable that changes. How do I recall the global context within that message? I'm sure I'm not stating it correctly, so hopefully the pictures will help...

 ![Screenshot 2021-08-13 13.34.09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffaee62f2829e0e9b8d8e75bde091afd73c76966.png)  
Old way with the change node

 ![Screenshot 2021-08-13 13.10.31](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/50dbde3afef2b0884c1e86e7ceba70fa90eb3e03.png)

 ![Screenshot 2021-08-13 13.11.40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c03a389d8e4cb208700ee2784ae6e833bfd0c6f.png)  
New way with the new node

Thanks for any help!

---

<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:** [13 August 2021 23:20 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/2 "2021-08-13T23:20:10Z")

</div>

What options do you have in the type field for payload, other than json {}?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 August 2021 23:21 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/3 "2021-08-13T23:21:00Z")

</div>

Hi.

I'm not an expert and don't use those nodes.

Something to maybe help though is:  
rather than post screen shots of the nodes, also include the actual node. (Export it)

Yes, that means people will have to have that node installed and - yes I am one of them - people may not be keen on installing nodes.

But I think it will help get the bigger message through to people who are interested.

---

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:25 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/4 "2021-08-13T23:25:43Z")

</div>

msg., flow., global., string, number, and then JSON

I believe I'm using the JSON to set all the parameters instead of just one? To be completely honest the documentation on the node is quite lacking... found the format I needed in Issues on the github rather than in the readme...

---

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:26 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/5 "2021-08-13T23:26:59Z")

</div>

Sure thing!

````auto
[{"id":"3763d256.7bb84e","type":"zigbee2mqtt-out","z":"42731f5d.a5d2f","name":"Closet Bright","server":"a9e92c14.0a08e","friendly_name":"Closet Ceiling","device_id":"0x0017880108e450d9","command":"payload","commandType":"msg","payload":"{\"state\":\"ON\",\"brightness\":254,\"color_temp\":233,\"transition\":2}","payloadType":"json","transition":0,"x":710,"y":180,"wires":[]},{"id":"a9e92c14.0a08e","type":"zigbee2mqtt-server","name":"zigbee2mqtt","host":"localhost","mqtt_port":"1883","mqtt_username":"echo29mqtt","mqtt_password":"","tls":"","usetls":false,"base_topic":"zigbee2mqtt"}]```
````

---

<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:** [13 August 2021 23:29 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/6 "2021-08-13T23:29:41Z")

</div>

So the only option is to use msg and a change or function node to construct the object.

---

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:31 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/7 "2021-08-13T23:31:56Z")

</div>

So how I was doing it before then, with Huemagic? I was just confused because the node wanted the parameters within the node itself...

---

<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:** [13 August 2021 23:35 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/8 "2021-08-13T23:35:00Z")

</div>

Yes but, there are some slight differences to the property names i.e transtion.time becomes transition.

---

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:36 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/9 "2021-08-13T23:36:22Z")

</div>

Right... I got that. Just not the variable color change. Alright I'll tinker with that. Thank you!

---

<div class="post-metadata">

**Author:** ![derail\_green](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@derail\_green](https://discourse.nodered.org/u/derail_green)\
**Post date:** [13 August 2021 23:56 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/10 "2021-08-13T23:56:16Z")

</div>

Function node was it, thanks so much! Think I got confused with the lack of documentation and considering it was all inside the node itself.

---

<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:** [14 August 2021 00:13 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/11 "2021-08-14T00:13:49Z")

</div>

You can also use a template node, here is an example

```auto
[{"id":"21f5866d.471d3a","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":780,"wires":[["2341e933.f676e6"]]},{"id":"2341e933.f676e6","type":"template","z":"b779de97.b1b46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n\"state\":\"ON\",\n\"brightness\":254,\n\"color_temp\": {{global.colorTemp}},\n\"transition\": 2\n}","output":"str","x":370,"y":740,"wires":[["fa3c27d1.9fbb8"]]},{"id":"fa3c27d1.9fbb8","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":640,"wires":[]}]

```

---

<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:** [13 October 2021 00:14 UTC](https://discourse.nodered.org/t/global-context-in-node/49700/12 "2021-10-13T00:14:11Z")

</div>

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