Join two payloads

Looking for some help on how to accomplish the following: I will explain what I am trying to accomplish then paste my flow below. I want to capture some data at the start and end of a test period, then combine this data in a single payload and write it to a csv file. The data is contained in separate float tables for the start period, and the end period. When the period starts, the operator clicks a button on the HMI, which reads values into the float table, and also sets an I32 variable equal to 1. The rbe node allows flow to continue once the value is 1 and gets the start data from the float table. The end period is done the same way. I end up with two payloads, how can they be combined into a single one to use the csv node?

[{"id":"af669554.ee2258","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"2a54dbcb.a786e4","type":"inject","z":"af669554.ee2258","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":60,"wires":[["585d796a.620e58","ae4187fb.d699f8"]]},{"id":"6b10b0f5.2f5c3","type":"pac-read","z":"af669554.ee2258","device":"31b5623c.c2558e","dataType":"float-table","tagName":"MatBalstart_data","tableStartIndex":"0","tableLength":"10","value":"payload","valueType":"msg","topic":"","topicType":"none","name":"","x":500,"y":160,"wires":[["c78c58d6.2af158"]]},{"id":"3537ab78.4243a4","type":"rbe","z":"af669554.ee2258","name":"","func":"deadbandEq","gap":"1","start":"","inout":"in","property":"payload","x":320,"y":160,"wires":[["6b10b0f5.2f5c3"]]},{"id":"e45aa1a.87f9b6","type":"pac-read","z":"af669554.ee2258","device":"31b5623c.c2558e","dataType":"float-table","tagName":"MatBalend_data","tableStartIndex":"0","tableLength":"10","value":"payload","valueType":"msg","topic":"","topicType":"none","name":"","x":500,"y":240,"wires":[["c78c58d6.2af158"]]},{"id":"224bc154.347d2e","type":"rbe","z":"af669554.ee2258","name":"","func":"deadbandEq","gap":"1","start":"","inout":"in","property":"payload","x":320,"y":240,"wires":[["e45aa1a.87f9b6"]]},{"id":"585d796a.620e58","type":"pac-read","z":"af669554.ee2258","device":"31b5623c.c2558e","dataType":"int32-variable","tagName":"MatBalstart_flag","tableStartIndex":"","tableLength":"","value":"","valueType":"msg.payload","topic":"","topicType":"none","name":"","x":140,"y":160,"wires":[["3537ab78.4243a4"]]},{"id":"ae4187fb.d699f8","type":"pac-read","z":"af669554.ee2258","device":"31b5623c.c2558e","dataType":"int32-variable","tagName":"MatBalend_flag","tableStartIndex":"","tableLength":"","value":"","valueType":"msg.payload","topic":"","topicType":"none","name":"","x":140,"y":240,"wires":[["224bc154.347d2e"]]},{"id":"f7476990.bee2c8","type":"function","z":"af669554.ee2258","name":"","func":"var start_data = msg.start;\nstart_data = context.get(\"start_data\")||[]; //array holding the start data\nvar end_data = msg.end;\nend_data = context.get(\"end_data\")||[]; //array holding the end data\nvar final_data = [];\nfinal_data = context.get(\"final_data\")||[]; //array holding the final data\n\nfinal_data[0] = start_data[0];\nfinal_data[1] = start_data[1];\n\n\ncontext.set(\"start_data\", start_data);\ncontext.set(\"end_data\", end_data);\ncontext.set(\"final_data\", final_data);\n\nmsg.payload = final_data;\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":260,"wires":[[]]},{"id":"142d8dd3.8a9452","type":"debug","z":"af669554.ee2258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":910,"y":200,"wires":[]},{"id":"c78c58d6.2af158","type":"join","z":"af669554.ee2258","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":720,"y":200,"wires":[["142d8dd3.8a9452"]]},{"id":"31b5623c.c2558e","type":"pac-device","z":"","address":"10.0.4.10","protocol":"http"}]

Out of curiosity, is there a reason you’re not using a PAC strategy to calculate this? And is this a RIO or EPIC application?

But regarding this flow, I understand why you can’t just get one table after another in order, it being timing-related, but I see you have a function node in this flow using context variables, which is not wired in. I think saving each set of values as a flow or global variable is the better approach so other nodes can reference them as-needed.
The intention there being that saving the “start_data” and “end_data” at the flow / global level rather than context, each on separate wires, and then combining them in a third flow that references both would be a great way around the trouble of combining data or dealing with sync issues. Then, I believe the final “MatBalend_flag” toggling to 1 would be the point where you check both variables, do the calculation, and then send that result to the csv node.
(Node-RED has more details on their documentation site, if you haven’t already checked that out: Working with context : Node-RED)

It is an EPIC application. I want to get the data into an excel spreadsheet, where I can use an excel node in the node red application.
I didn’t realize I need to use global, rather than context and I think that is my problem.
Actually, after thinking about it you are right. It would be much easier to simply put all the data into one float table in the Pac strategy after the MatBalend_flag is 1. Then it comes in as one payload and I do not need to sync anything in node red.
Thanks Terry

1 Like