# Using Data stored in a message for a function

**URL:** <https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208>\
**Category:** General\
**Created:** [2 October 2019 17:22 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208 "2019-10-02T17:22:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navybrandon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/navybrandon/32/13021_2.png) [@Navybrandon](https://discourse.nodered.org/u/Navybrandon)\
**Post date:** [2 October 2019 17:22 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/1 "2019-10-02T17:22:02Z")

</div>

Hello everyone again. I was wondering how to call upon data that is my msg.object when my function is already using the msg.payload for something else. my goal here is to have the values auto-fill if you will based on the incoming message. Please see below code and pictures. Thanks in advance!

```auto
[{"id":"e2a8b326.7242","type":"tab","label":"Getting help","disabled":false,"info":""},{"id":"921a464b.103708","type":"file in","z":"e2a8b326.7242","name":"","filename":"/home/pi/hvac/NewDevice.log","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":370,"y":300,"wires":[["74e907e7.bef258"]]},{"id":"156aefa6.112d9","type":"ui_button","z":"e2a8b326.7242","name":"","group":"3487420a.cf479e","order":8,"width":0,"height":0,"passthru":false,"label":"Create Device","tooltip":"","color":"red","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"","x":90,"y":300,"wires":[["921a464b.103708"]]},{"id":"74e907e7.bef258","type":"split","z":"e2a8b326.7242","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":610,"y":300,"wires":[["70a53e22.dd283"]]},{"id":"70a53e22.dd283","type":"switch","z":"e2a8b326.7242","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"\"IP\":","vt":"str"},{"t":"cont","v":"\"instance\":","vt":"str"},{"t":"cont","v":"\"Function\":","vt":"str"},{"t":"cont","v":"\"type\":","vt":"str"},{"t":"cont","v":"\"App Tag\":","vt":"str"},{"t":"cont","v":"\"Property ID\":","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":780,"y":300,"wires":[["741268e1.4893c8"],["92d94d2d.495a"],["86b296c1.6ccdf8"],["7baaa4e0.073f9c"],["f4bd8e13.56c7a"],["45923595.c51f9c"]]},{"id":"741268e1.4893c8","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"Dip","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":240,"wires":[["4a447ff7.8cf56"]]},{"id":"92d94d2d.495a","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"Bacnet","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":280,"wires":[["4a447ff7.8cf56"]]},{"id":"86b296c1.6ccdf8","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"Function","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":320,"wires":[["4a447ff7.8cf56"]]},{"id":"7baaa4e0.073f9c","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"ObjectID","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":360,"wires":[["4a447ff7.8cf56"]]},{"id":"f4bd8e13.56c7a","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"Apptag","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":400,"wires":[["4a447ff7.8cf56"]]},{"id":"45923595.c51f9c","type":"change","z":"e2a8b326.7242","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"PropertyID","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":440,"wires":[["4a447ff7.8cf56"]]},{"id":"4a447ff7.8cf56","type":"join","z":"e2a8b326.7242","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1270,"y":320,"wires":[["7c69e0e8.02f11"]]},{"id":"7c69e0e8.02f11","type":"switch","z":"e2a8b326.7242","name":"","property":"Function","propertyType":"jsonata","rules":[{"t":"cont","v":"\"Function\":\"Read Function\"","vt":"str"},{"t":"cont","v":"\"Function\":\"Write Function\"","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1410,"y":320,"wires":[["2829e5a2.94709a","ffeb8f93.8c2b8"],["71c5b0a.35cea5"]]},{"id":"ffeb8f93.8c2b8","type":"function","z":"e2a8b326.7242","name":"Read Funtion","func":"//var msg = {};\nconst bacnet1 = global.get('bacnet');\n// Initialize BACStack\nvar client = bacnet1({adpuTimeout: 6000});\nvar state = msg.payload;\n\nconst values = [\n {objectId: {type: 2, instance: 3001617}, values: [\n {property: {id: 85, index: 4294967295}, value: [{type: bacnet1.enum.ApplicationTags.BACNET_APPLICATION_TAG_REAL, value: state}], priority: 15}\n ]}\n];\n\nclient.readProperty('192.168.1.41', {type: 2, instance: 3001617}, 85, (err, value) => {\n msg.payload = {value: value} ;\n node.send(msg);\n});\nreturn msg;","outputs":1,"noerr":0,"x":1620,"y":300,"wires":[[]]},{"id":"71c5b0a.35cea5","type":"function","z":"e2a8b326.7242","name":"Write Function","func":"//var msg = {};\nconst bacnet1 = global.get('bacnet');\n// Initialize BACStack\nvar client = bacnet1({adpuTimeout: 6000});\nvar state = msg.payload;\n\nconst values = [\n {objectId: {type: 2, instance: 3001617}, values: [\n {property: {id: 85, index: 4294967295}, value: [{type: bacnet1.enum.ApplicationTags.BACNET_APPLICATION_TAG_REAL, value: state}], priority: 15}\n ]}\n];\n\nclient.writePropertyMultiple('192.168.1.41', values, (err, value) => {\n msg.payload = {value: value} ;\n if(err) { msg.payload = {error: err} ; }\n node.send(msg);\n});\n\nreturn msg;","outputs":1,"noerr":0,"x":1620,"y":340,"wires":[[]]},{"id":"2829e5a2.94709a","type":"debug","z":"e2a8b326.7242","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1530,"y":260,"wires":[]},{"id":"3487420a.cf479e","type":"ui_group","z":"","name":"Add Device","tab":"99082a6.ab01ed8","order":2,"disp":true,"width":"10","collapse":true},{"id":"99082a6.ab01ed8","type":"ui_tab","z":"","name":"Testing","icon":"dashboard","order":12,"disabled":false,"hidden":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/99d871f7128efc693029d1ba560ced87ee660b5b.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f47fd727589ee0b1c31813e821992e2aba39e9fc.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 October 2019 01:49 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/2 "2019-10-05T01:49:17Z")

</div>

Put your data somewhere else. E.g `msg.mydata` or `msg.fred`, quite literary infinite options.

You are not restricted to `msg.payload`.

If you absolutely need to output to `msg.payload` but need to keep original payload, then just move it...

```auto
msg.origPayload = msg.payload;
// Do stuff
...
// Etc

//Populate payload property with new data...
msg.payload = newPayloadDataStuffWhateverYouWant;
return msg;

```

---

<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:** [5 October 2019 20:28 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/3 "2019-10-05T20:28:16Z")

</div>

> [@Steve-Mcl](#):
>
> E.g `msg.mydata` or `msg.fred` , quite literary infinite options

Not quite infinite actually. A property name can be any string so it might appear that there are an infinite number of combinations, however, since your computer has (presumably) a limited amount of memory, in practice you cannot have an infinite length string, therefore there are not actually an infinite number of property names you could use. However it is a big number, I mean _really_ big, so for all practical purposes it might at as well be infinite.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 October 2019 21:32 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/4 "2019-10-05T21:32:03Z")

</div>

@Colin I realize your post is tongue in cheek, and you are in fact correct but there were no memory parameters or architecture restrictions stated so I stand by my infinite statement 😂😂😂

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [5 October 2019 22:44 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/5 "2019-10-05T22:44:24Z")

</div>

> [@Steve-Mcl](#):
>
> I realize your post is tongue in cheek, and you are in fact correct but there were no memory parameters or architecture restrictions stated so I stand by my infinite statement

Has anyone tried to run Node-RED on a Turing machine? 😉

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 October 2019 22:58 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/6 "2019-10-05T22:58:07Z")

</div>

Well JavaScript does have defined, inherent restrictions and so ... 😉

---

<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:** [6 October 2019 09:01 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/7 "2019-10-06T09:01:30Z")

</div>

I couldn't find any defined limit on attribute name lengths. I did find references to practical limits in various browsers, but not in the language, so in theory it does seem that the only limit is on memory space.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 October 2019 10:11 UTC](https://discourse.nodered.org/t/using-data-stored-in-a-message-for-a-function/16208/8 "2019-10-06T10:11:29Z")

</div>

Actually, you appear to be correct!

> <https://stackoverflow.com/questions/13367391/is-there-a-limit-on-length-of-the-key-string-in-js-object>

It appears to be down to how the browser (node.js in this case?) handles strings.
