# Consolidating incoming data

**URL:** https://discourse.nodered.org/t/consolidating-incoming-data/88000
**Category:** General
**Created:** [16 May 2024 06:51 UTC](https://discourse.nodered.org/t/consolidating-incoming-data/88000 "2024-05-16T06:51:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![parkdev](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@parkdev](https://discourse.nodered.org/u/parkdev)
#### Post date: [16 May 2024 06:51 UTC](https://discourse.nodered.org/t/consolidating-incoming-data/88000/1 "2024-05-16T06:51:44Z")

</div>

Hi,

Hoping someone can help and it may be a bit of a noobie question. But I have a ADAM-6717 module, the Get AI (get anolog inputs) and Get DIO (Digital Input / Outputs). These output data all at the same time when triggered, what I wanted to do was get the information in one payload and ordered correctly. Currently I've added delays to collate the information. But surely there is a better of acomplishing this task.

```auto
[
    {
        "id": "39140a8f02f9cd8f",
        "type": "tab",
        "label": "202391 Post to Losant",
        "disabled": false,
        "info": ""
    },
    {
        "id": "2c7feaa2b0083587",
        "type": "inject",
        "z": "39140a8f02f9cd8f",
        "name": "2 seconds",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "2",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 320,
        "wires": [
            [
                "c2231f55bbf2e15d",
                "4f2c591624eef712"
            ]
        ]
    },
    {
        "id": "c2231f55bbf2e15d",
        "type": "get-ai-value",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "x": 310,
        "y": 180,
        "wires": [
            [
                "636586677e4ef330"
            ],
            [
                "f2fce7de9313309e"
            ],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            []
        ]
    },
    {
        "id": "c401a86aec8ce76b",
        "type": "function",
        "z": "39140a8f02f9cd8f",
        "name": "Consildate Payload",
        "func": "var array = msg.payload;\nvar result = {};\n\n// Create a new object and assign array values to specific keys\nif (array.length == 6) {\n result = {\n data:\n {\n level_1: Math.round(array[0]),\n level_2: Math.round(array[1]),\n pump1_running: array[2],\n pump1_fault: array[3],\n pump2_running: array[4],\n pump2_fault: array[5],\n device: \"10.0.150.254\"\n }\n };\n} else {\n result = {\n data:\n {\n error:true\n }\n };\n}\n\n// Set the payload to the new object\nmsg.payload = result;\n\n// Return the message to continue the flow\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 1110,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "4f2c591624eef712",
        "type": "get-dio-value",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "x": 310,
        "y": 420,
        "wires": [
            [
                "13f62a477c570579"
            ],
            [
                "78fa5a51abe7026d"
            ],
            [
                "a7fa0c7ee6a2e105"
            ],
            [
                "46e12dec0807a453"
            ],
            [],
            [],
            [],
            [],
            []
        ]
    },
    {
        "id": "04fc3726c9ba5fcf",
        "type": "join",
        "z": "39140a8f02f9cd8f",
        "name": "join 6 values",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "0",
        "count": "6",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 890,
        "y": 120,
        "wires": [
            [
                "8f94de12548c8f4c",
                "15714fa4c817c198"
            ]
        ]
    },
    {
        "id": "5e3dc3bb41fbc682",
        "type": "debug",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1330,
        "y": 140,
        "wires": []
    },
    {
        "id": "6efb623c5643184c",
        "type": "delay",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "outputs": 1,
        "x": 690,
        "y": 160,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "13f62a477c570579",
        "type": "delay",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "pauseType": "delay",
        "timeout": "20",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "outputs": 1,
        "x": 690,
        "y": 200,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "78fa5a51abe7026d",
        "type": "delay",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "pauseType": "delay",
        "timeout": "30",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "outputs": 1,
        "x": 690,
        "y": 240,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "a7fa0c7ee6a2e105",
        "type": "delay",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "pauseType": "delay",
        "timeout": "40",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "outputs": 1,
        "x": 690,
        "y": 280,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "46e12dec0807a453",
        "type": "delay",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "pauseType": "delay",
        "timeout": "50",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "outputs": 1,
        "x": 690,
        "y": 320,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "636586677e4ef330",
        "type": "range",
        "z": "39140a8f02f9cd8f",
        "minin": "4",
        "maxin": "20",
        "minout": "0",
        "maxout": "5000",
        "action": "scale",
        "round": false,
        "property": "payload",
        "name": "Scale 0-5000",
        "x": 630,
        "y": 100,
        "wires": [
            [
                "04fc3726c9ba5fcf"
            ]
        ]
    },
    {
        "id": "f2fce7de9313309e",
        "type": "range",
        "z": "39140a8f02f9cd8f",
        "minin": "4",
        "maxin": "20",
        "minout": "0",
        "maxout": "5000",
        "action": "scale",
        "round": false,
        "property": "payload",
        "name": "Scale 0-5000",
        "x": 510,
        "y": 160,
        "wires": [
            [
                "6efb623c5643184c"
            ]
        ]
    },
    {
        "id": "8f94de12548c8f4c",
        "type": "debug",
        "z": "39140a8f02f9cd8f",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1040,
        "y": 360,
        "wires": []
    },
    {
        "id": "15714fa4c817c198",
        "type": "function",
        "z": "39140a8f02f9cd8f",
        "name": "Consolidate Payload v2",
        "func": "var array = msg.payload;\nvar result = {};\n\n// Initialize counters and buffers if they don't exist\nif (typeof context.counters === 'undefined') {\n context.counters = {\n pump1_running: 0,\n pump1_fault: 0,\n pump2_running: 0,\n pump2_fault: 0\n };\n}\nif (!context.buffers) {\n context.buffers = {\n pump1_running: [],\n pump1_fault: [],\n pump2_running: [],\n pump2_fault: []\n };\n}\n\n// Check if array length is 6\nif (array.length == 6) {\n // Create a new object and assign array values to specific keys\n result = {\n data: {\n level_1: Math.round(array[0]),\n level_2: Math.round(array[1]),\n pump1_running: array[2],\n pump1_fault: array[3],\n pump2_running: array[4],\n pump2_fault: array[5],\n device: \"10.0.150.254\"\n }\n };\n \n // Update buffers\n context.buffers.pump1_running.push(array[2]);\n context.buffers.pump1_fault.push(array[3]);\n context.buffers.pump2_running.push(array[4]);\n context.buffers.pump2_fault.push(array[5]);\n \n // Keep only the last 2 elements in the buffers\n if (context.buffers.pump1_running.length > 6) {\n context.buffers.pump1_running.shift();\n }\n if (context.buffers.pump1_fault.length > 15) {\n context.buffers.pump1_fault.shift();\n }\n if (context.buffers.pump2_running.length > 6) {\n context.buffers.pump2_running.shift();\n }\n if (context.buffers.pump2_fault.length > 15) {\n context.buffers.pump2_fault.shift();\n }\n \n // Update counters\n context.counters.pump1_running = context.buffers.pump1_running.every(value => value === true) ? context.counters.pump1_running + 1 : 0;\n context.counters.pump1_fault = context.buffers.pump1_fault.every(value => value === true) ? context.counters.pump1_fault + 1 : 0;\n context.counters.pump2_running = context.buffers.pump2_running.every(value => value === true) ? context.counters.pump2_running + 1 : 0;\n context.counters.pump2_fault = context.buffers.pump2_fault.every(value => value === true) ? context.counters.pump2_fault + 1 : 0;\n \n // Reset counters if any value is false\n if (!array[2]) context.counters.pump1_running = 0;\n if (!array[3]) context.counters.pump1_fault = 0;\n if (!array[4]) context.counters.pump2_running = 0;\n if (!array[5]) context.counters.pump2_fault = 0;\n \n // Set pump values to true if the respective counter has reached 2\n if (context.counters.pump1_running >= 6) {\n result.data.pump1_running = true;\n }\n if (context.counters.pump1_fault >= 15) {\n result.data.pump1_fault = true;\n }\n if (context.counters.pump2_running >= 6) {\n result.data.pump2_running = true;\n }\n if (context.counters.pump2_fault >= 15) {\n result.data.pump2_fault = true;\n }\n} else {\n result = {\n data: {\n error: true\n }\n };\n}\n\n// Set the payload to the new object\nmsg.payload = result;\n\n// Return the message to continue the flow\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1090,
        "y": 260,
        "wires": [
            [
                "5e3dc3bb41fbc682"
            ]
        ]
    }
]

```

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

Thanks,  
Park

---

<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: [16 May 2024 07:36 UTC](https://discourse.nodered.org/t/consolidating-incoming-data/88000/2 "2024-05-16T07:36:57Z")

</div>

Yes. Don't use array mode for the join node. Use the key/value object mode and topics. That way, any items arriving out of order always end up in the same object key. Every time.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![parkdev](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@parkdev](https://discourse.nodered.org/u/parkdev)
#### Post date: [16 May 2024 15:27 UTC](https://discourse.nodered.org/t/consolidating-incoming-data/88000/3 "2024-05-16T15:27:34Z")

</div>

Thanks @Steve-Mcl. Much for effiecient, now I can read them all and the join block allocates the 6 values for me by key. Exactly what I was after. Appreciate the help.

---

<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: [30 May 2024 15:27 UTC](https://discourse.nodered.org/t/consolidating-incoming-data/88000/4 "2024-05-30T15:27:39Z")

</div>

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