# Set change-node Payloadpath to msg.payload.+global

**URL:** <https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440>\
**Category:** Developing Nodes\
**Created:** [13 December 2020 07:40 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440 "2020-12-13T07:40:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 07:40 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/1 "2020-12-13T07:40:57Z")

</div>

Hello,

i would like to do sth simple i guess, but i cannot figure out how to do it exactly.

I would like to let my msg.payload be sth like "msg.payload.ZbReceived.0xCE25" with a change node. Of course there is no issue with that. But i would like to be able to change the last part "0xCE25" with my stored Global Variable.

I have many things coming from mqtt with different names "0x...." and sometimes i need to change them and it would be easier to just change the Globals instead of change every change node again.

i hope it is understandable what my issue is.

Thanks alot for your help.

Best regards Dominik

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 December 2020 09:05 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/2 "2020-12-13T09:05:52Z")

</div>

> [@Dom19882](#):
>
> i hope it is understandable what my issue is

Not so much 😉  
so .. you want to dynamically pass whatever is in Globals to `msg.payload.ZbReceived` ?  
does Global only have ZbReceived values stored in it .. or does it store other values also ?  
what is the structure of Globals ? send us a screenshot .. a screenshot is a thousand words 😉

[EDIT]  
if you want all Global entries to be set to `msg.payload.ZbReceived` then using a jsonata expression in your **Change node** can do that.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2dc4ff003cb482d2113b89bdf3c0fc41ebdfb8c6.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 09:42 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/3 "2020-12-13T09:42:00Z")

</div>

I think this is one of the cases where a simple function node instead of a change node will make your life much easier. In JavaScript you can use the square brackets to use the value of another var as a key:

```auto
const key = flow.get("test");
msg.payload = msg[key];
return msg;

```

For example would set `msg.payload` to `msg` + whatever the the value of `flow.test` was.

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 09:48 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/4 "2020-12-13T09:48:03Z")

</div>

![Screenshot 2020-12-13 104202](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/72f157937d481203f1bd6a63a0afccd21bd835bb.png)

Hello thanks for reply. I get an MQTT Message with different Topics for different sensors/switches ( ZigbeeGateway). These Things al have a different (Hex?) Number ind this devices for Example 0x631D.  
Sometimes i need to repair the devices and the Hexnumbers are changing then. So i need to go everytime in every node where this Hexnumber is used. I would like to have Globals for all these Hexnumbers. Then i woud not need to change every Changenode and just need to change the globals then. For Example Global BathroomMotion to 0x12345.

Thanks alot

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 09:54 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/5 "2020-12-13T09:54:26Z")

</div>

That’s exactly what the function I posted above does. just adapt it to you global instead of the test flow and add your path:

```auto
const key = global.get("BathroomMotion");
msg.payload = msg.payload.ZbReceived[key];
return msg;

```

Than use a function node with those three lines instead of each of the change nodes.

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 10:00 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/6 "2020-12-13T10:00:50Z")

</div>

> [@JGKK](#):
>
> ```auto
> const key = global.get("BathroomMotion");
> msg.payload = msg.payload.ZbReceived[key];
> return msg;
> 
> ```

Uhuuu. Thanks alot. It works. Would have never found such solution on my own.

Have a great day 😃

---

<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 December 2020 10:26 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/7 "2020-12-13T10:26:18Z")

</div>

And for info this is the same thing in JSONata and change node.

```auto
[{"id":"d58991db.43c948","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"str","x":120,"y":2460,"wires":[["b0b83020.838418"]]},{"id":"b0b83020.838418","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$merge(\t [\t $type(payload) = \"object\" ? payload : {},\t {\t \"ZbReceived\":{$globalContext(\"test\"):\"a value\"}\t }\t]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":2460,"wires":[["8f888c94.94e39"]]},{"id":"8f888c94.94e39","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":2460,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 10:28 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/8 "2020-12-13T10:28:53Z")

</div>

Awesome,

thanks alot

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 10:48 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/9 "2020-12-13T10:48:52Z")

</div>

As much as i like jsonata for a lot of things this is one where i find the syntax just a little bit too convoluted for my liking and prefer the straight forward function node but nice to know the jsonata way too 👍

---

<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 December 2020 10:54 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/10 "2020-12-13T10:54:20Z")

</div>

My example was more convoluted as i assumed msg.payload all ready may have properties, so add some checks. It is simpler if you want to over write msg.payload.

```auto
	   {"ZbReceived":{$globalContext("test"):"a value"}}

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 11:10 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/11 "2020-12-13T11:10:00Z")

</div>

> [@E1cid](#):
>
> `{"ZbReceived":{$globalContext("test"):"a value"}}`

I think your not doing what was requested. Its not about setting `msg.payload.ZBreceived.something` to a specific value. But instead its about setting `msg.payload` to the value of `msg.payload.ZBreceived.something` where the `something` key is dynamic so we need to get it from the value of `global.BathroomMotion` and build the object reference dynamically as the key where what we want `msg.payload` to be can change.

---

<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 December 2020 11:14 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/12 "2020-12-13T11:14:10Z")

</div>

You are mistaken it is setting the key dynamically

 ![Screenshot_20201213-111259](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f17db9374b1d30afb6014c441a99d494a84589c.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 11:18 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/13 "2020-12-13T11:18:55Z")

</div>

![Screenshot 2020-12-13 at 12.17.58](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/248961cbf96ee4b7f0967c75bf253d9b387f9c07.png)

```auto
[{"id":"49e56cf3.3c833c","type":"debug","z":"1b5e73a9.1939fc","name":"what we want","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1820,"y":960,"wires":[]},{"id":"b2136b39.e8ad2","type":"inject","z":"1b5e73a9.1939fc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1140,"y":900,"wires":[["83be35b2.6a4a98"]]},{"id":"83be35b2.6a4a98","type":"change","z":"1b5e73a9.1939fc","name":"","rules":[{"t":"set","p":"BathroomMotion","pt":"global","to":"test","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1370,"y":900,"wires":[[]]},{"id":"82c00fc0.32f318","type":"function","z":"1b5e73a9.1939fc","name":"","func":"const key = global.get(\"BathroomMotion\");\nmsg.payload = msg.payload.ZbReceived[key];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1640,"y":960,"wires":[["49e56cf3.3c833c"]]},{"id":"b105c10f.00e18","type":"debug","z":"1b5e73a9.1939fc","name":"jsonata output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1860,"y":1040,"wires":[]},{"id":"b0b83020.838418","type":"change","z":"1b5e73a9.1939fc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$merge(\t [\t $type(payload) = \"object\" ? payload : {},\t {\t \"ZbReceived\":{$globalContext(\"BathroomMotion\"):\"a value\"}\t }\t]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1660,"y":1040,"wires":[["b105c10f.00e18"]]},{"id":"b75d775.f73cf08","type":"change","z":"1b5e73a9.1939fc","name":"","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.ZbReceived.test","pt":"msg","to":"this is what we want as the msg.payload","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":960,"wires":[["82c00fc0.32f318","b0b83020.838418"]]},{"id":"64b63e60.f389b8","type":"inject","z":"1b5e73a9.1939fc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1140,"y":960,"wires":[["b75d775.f73cf08"]]}]

```

This is what is wanted i think. And your jsonata @E1cid is not doing it as you see. He only wants to set the key and dont set any values. The purpose is to set msg.payload to the value of that dynamically generated key. Maybe you know a jsonata which has exactly the same end result / function as the function node?

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 11:19 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/14 "2020-12-13T11:19:55Z")

</div>

@JGKK

Exactly like this.

I have now a great Function Node where i can change all my HexCodes in One Window. And the other Function Nodes taking the needed Globalvalues from it.

Only small issue i have now is that i get everytime a message in my Debugwindow from the function nodes when no explicit data for the sensor is sended and instead for another.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09dae15c843839e69f4550ddca1e9fb60183eae3.png)

TypeError: Cannot read property '0x1E7F' of undefined

Can i add sth in my function node, that its only "executing" when the payload ist not empty or undefined?

//Just want to add. Everything works like exprected. There are only this errormessages.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 11:29 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/15 "2020-12-13T11:29:58Z")

</div>

adapt the function to:

```auto
const key = global.get("BathroomMotion");
if (msg.payload.ZbReceived.hasOwnProperty(key)) {
    msg.payload = msg.payload.ZbReceived[key];
    return msg;
} else {
    return null;
}

```

It will only send a message if the key matches.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 December 2020 11:33 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/16 "2020-12-13T11:33:38Z")

</div>

I see your Mqtt node is subscribed too all topics #

Copy/paste a msg coming from your Mqtt .. maybe there's a better way to optimize your flow because i dont understand what the point of setting globals and then reading them since when you make a change on the device you still need to go in your "setAllZigbeeDeviceHexCode" function and hard code the new hex 😉

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 11:34 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/17 "2020-12-13T11:34:03Z")

</div>

Thank you

I tried the following way but i think yours is the right way.

Just for information:  
const key = global.get("ZBB\_SZMotionMain");  
msg.payload = msg.payload.ZbReceived[key];  
if(msg.payload !== null && msg.payload !== '')  
{  
return msg;  
}

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 December 2020 11:36 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/18 "2020-12-13T11:36:26Z")

</div>

The js `hasOwnProperty()` function is especially made for this purpose to check if a certain property exists.  
You an read more about it here:

> **[Object.prototype.hasOwnProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)**
>
> The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 December 2020 11:40 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/19 "2020-12-13T11:40:45Z")

</div>

i was thinking of a function with many outputs doing the switching ?  
no need for globals

### Multiple Outputs

> **[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#multiple-outputs)**

---

<div class="post-metadata">

**Author:** ![Dom19882](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dom19882/32/33576_2.png) [@Dom19882](https://discourse.nodered.org/u/Dom19882)\
**Post date:** [13 December 2020 11:41 UTC](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440/20 "2020-12-13T11:41:41Z")

</div>

thanks for the idea. would be possible too i guess.

[Next page](https://discourse.nodered.org/t/set-change-node-payloadpath-to-msg-payload-global/37440.md?page=2)
