Using Data stored in a message for a function

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!

[{"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}]


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...

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

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

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.

@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 :joy::joy::joy:

1 Like

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

1 Like

Well JavaScript does have defined, inherent restrictions and so ... :wink:

1 Like

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.

Actually, you appear to be correct!

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

1 Like