Combining arrays into an array of arrays OR removing empty string elements from an array

Hello,

This is more of a coding question then a direct concern with Node-RED however I would greatly appreciate the help.

I am using Node-RED to publish topics to MQTT from a report. As a result I am required to manipulate the data in order to obtain a properly displayed format in MQTT Explorer.

Right now, I have successfully manipulated the data & it comes up correctly in MQTT Explorer however I am trying to implement a 'Delete' function which clears all topics from the client. In order to do this I now need to change the way my data is being Published which requires it to be in either an Array of Arrays OR I must remove the empty string elements from some of the arrays.

This is the code for my publish function:

var versionInf = msg.payload;
flow.set('versionInf', msg.payload);

for (var b = 0; b < versionInf.length; b+=2) {
    var topic = 'FANUC Robot/Version Information/' + versionInf[b];  // Append the index to make the topic unique
    var payload = versionInf[b+1]
    node.send({ topic: topic, payload: payload });
}

return null

The data enters the function node (publish node) in the following format:

["Update Version "," None"]

However when I use the join node once again to attempt to combine the data into an array of arrays, some of the contents give 3 element array while others give a 2 element array as follow:

["DCS","    "," V3.5.12"]

I would like to either: Use a function which removes of the empty string elements within the array or to use a function which combines the arrays into a single array without using the join node which returns some 3 element arrays.

Thank you for your assistance. I can provide further details upon request.

Some device publishes a msg that has a topic and data. The broker then sends that msg to any device that has subscribed to that topic. If you only want to remove that msg from teh broker, you need to publish another msg with that topic and a zero byte payload.

If you want to learn more about MQTT then see this tutorial MQTT Essentials - All Core Concepts Explained

Hello Zenofmud,

I am indeed deleting the topics correctly by sending an empty payload with QoS 0 and retain set to true.

The main issue lies in the organization of my data. I am unaware of how to use an iterative for loop to combine several arrays into either and array of arrays and/or one big array OR how to clear empty elements of the individual arrays so that I can get it into the form i desire.

Thanks for your response.

When you say several arrays can you give me an example of the data?

Actually can you provide the flow with some test data that shows the issue

1 Like

Here is the requested flow & the text file I am pulling the data from.

[
    {
        "id": "743cc0234f1851f1",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f51f30daa9fe8fb7",
        "type": "group",
        "z": "743cc0234f1851f1",
        "name": "MANUAL TRIGGER",
        "style": {
            "stroke": "#ff0000",
            "fill": "#ffffff",
            "label": true,
            "label-position": "n",
            "color": "#000000"
        },
        "nodes": [
            "3f781378483a51f6",
            "182a62f9469aa974",
            "99e9e83ff0d516e3",
            "e345179f4c3923e0"
        ],
        "x": 254,
        "y": 109,
        "w": 192,
        "h": 157
    },
    {
        "id": "3f781378483a51f6",
        "type": "file in",
        "z": "743cc0234f1851f1",
        "g": "f51f30daa9fe8fb7",
        "name": "TEST",
        "filename": "C:\\Users\\PC\\Documents\\TEST.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 350,
        "y": 175,
        "wires": [
            [
                "99e9e83ff0d516e3"
            ]
        ]
    },
    {
        "id": "182a62f9469aa974",
        "type": "inject",
        "z": "743cc0234f1851f1",
        "g": "f51f30daa9fe8fb7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 360,
        "y": 150,
        "wires": [
            [
                "3f781378483a51f6"
            ]
        ]
    },
    {
        "id": "99e9e83ff0d516e3",
        "type": "split",
        "z": "743cc0234f1851f1",
        "g": "f51f30daa9fe8fb7",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 370,
        "y": 200,
        "wires": [
            [
                "e345179f4c3923e0"
            ]
        ]
    },
    {
        "id": "e345179f4c3923e0",
        "type": "join",
        "z": "743cc0234f1851f1",
        "g": "f51f30daa9fe8fb7",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": " ",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 370,
        "y": 225,
        "wires": [
            [
                "d1065ab4c4fd42cd"
            ]
        ]
    },
    {
        "id": "3331fd4615f9b442",
        "type": "function",
        "z": "743cc0234f1851f1",
        "name": "Publish",
        "func": "var versionInf = msg.payload;\nflow.set('versionInf', msg.payload);\n\nfor (var b = 0; b < versionInf.length; b++) {\n            var topic = 'TEST/Information/' + versionInf[b][0];  // Append the index to make the topic unique\n            var payload = versionInf[b][1]\n            node.send({ topic: topic, payload: payload });\n            \n    }\n\nreturn null",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1275,
        "y": 225,
        "wires": [
            [
                "f8934eaf1f046d87"
            ]
        ]
    },
    {
        "id": "d1065ab4c4fd42cd",
        "type": "split",
        "z": "743cc0234f1851f1",
        "name": "Split to new line",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 580,
        "y": 225,
        "wires": [
            [
                "03ff8d8ce3dbbe69"
            ]
        ]
    },
    {
        "id": "6cfe2b4b8cc16828",
        "type": "join",
        "z": "743cc0234f1851f1",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 1090,
        "y": 225,
        "wires": [
            [
                "c3259815b458bb9b"
            ]
        ]
    },
    {
        "id": "fba61b48e8d791d5",
        "type": "function",
        "z": "743cc0234f1851f1",
        "name": "Replacing ",
        "func": "msg.payload = msg.payload.split('        ').join(':');\nmsg.payload = msg.payload.split('     ').join(':');\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 225,
        "wires": [
            [
                "0209096a4abcaad1"
            ]
        ]
    },
    {
        "id": "0209096a4abcaad1",
        "type": "split",
        "z": "743cc0234f1851f1",
        "name": "Split over : ",
        "splt": ":",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 950,
        "y": 225,
        "wires": [
            [
                "6cfe2b4b8cc16828"
            ]
        ]
    },
    {
        "id": "03ff8d8ce3dbbe69",
        "type": "function",
        "z": "743cc0234f1851f1",
        "name": "Replacing ",
        "func": "msg.payload = msg.payload.split('     :').join(':');\nmsg.payload = msg.payload.split('/').join('-');\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 595,
        "y": 250,
        "wires": [
            [
                "fba61b48e8d791d5"
            ]
        ]
    },
    {
        "id": "c3259815b458bb9b",
        "type": "join",
        "z": "743cc0234f1851f1",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 1090,
        "y": 250,
        "wires": [
            [
                "3331fd4615f9b442",
                "70df2e550e8e6350"
            ]
        ]
    },
    {
        "id": "70df2e550e8e6350",
        "type": "debug",
        "z": "743cc0234f1851f1",
        "name": "debug 387",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1170,
        "y": 375,
        "wires": []
    },
    {
        "id": "f8934eaf1f046d87",
        "type": "mqtt out",
        "z": "743cc0234f1851f1",
        "name": "",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "c2f9ab517722afa0",
        "x": 1425,
        "y": 225,
        "wires": []
    },
    {
        "id": "c2f9ab517722afa0",
        "type": "mqtt-broker",
        "name": "",
        "broker": "127.0.0.1",
        "port": "1883",
        "clientid": "MQTT Explorer",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

TEST.txt (623 Bytes)

Let me know if you have further requests/questions regarding the flow or the desired behavior.

Thanks,

Jordan Madgett

That is one messed up text file format, not much consistency.
This example may help

[{"id":"182a62f9469aa974","type":"inject","z":"743cc0234f1851f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":150,"wires":[["e4bf02867c4a6690"]]},{"id":"e4bf02867c4a6690","type":"template","z":"743cc0234f1851f1","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"LR HandlingTool       4324/543\nS/W Serial No.      : 6243    \nController ID       : F888   \nRobot No.           : A8888   \nManufacturing ID    : \nAA Rate abcd/7F, CGES      \nLT Tate 200iD/7F      V1.30D/33 \nAAaor AA          : V10.03    \nTart. DDD. Parameter: V3.00\nANT. Mot. Parameter : V3.00\nDCS                 : V3.5.12\nSt0p pattern        : A\nSoftware Edition No.: D8.SSP/34 \nUpdate Version      : No\nCustomization Ver.  : No\nRoot Version        : V8.30226  \nBoot MONITOR        : V8.30P/31\nTeach Peadant       : 8AAF/11A\nBrdsser Plugins     : V2.34222\nTF Dore Firmware    : V2.304/31\nMedia from AAA        12/36/2034","output":"str","x":360,"y":180,"wires":[["d1065ab4c4fd42cd"]]},{"id":"d1065ab4c4fd42cd","type":"split","z":"743cc0234f1851f1","name":"Split to new line","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":560,"y":180,"wires":[["03ff8d8ce3dbbe69"]]},{"id":"03ff8d8ce3dbbe69","type":"function","z":"743cc0234f1851f1","name":"Replacing ","func":"msg.payload = msg.payload.replace(/:/g, \" \")\nmsg.payload = msg.payload.split(/\\s+\\s+/)\nmsg.topic = \"TEST/Information/\" + msg.payload[0].trim()\nmsg.payload = msg.payload[1].trim()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":180,"wires":[["70df2e550e8e6350"]]},{"id":"70df2e550e8e6350","type":"debug","z":"743cc0234f1851f1","name":"debug 387","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":240,"wires":[]}]

I have simulated the file read with a template node, as it makes life easier when sharing flow examples.

Thanks E1cid.

You sure are right, however unfortunately I do not have control over its format.

This does indeed help give me some ideas however the way I am looking to 'delete' the MQTT topics basically requires me to have my data in an array format. I am looking to split the data, left side vs right side (you could say over the ':' however this trait is not consistent). This is why I was looking to combine them into an array of arrays and to remove of the empty strings.

I am quite new to the scene, are you aware of how to recursively remove of empty string elements within arrays?

I appreciate your help! You triggered some ideas & I will keep slugging. I am tampering with lots of data which I have no control over in order to cleanly publish it to MQTT and to also be able to 'delete' it from MQTT on a timed cycle.

if this is not correct you need to give what outputs you want and what will be skipped, so we are clear.

[{"id":"182a62f9469aa974","type":"inject","z":"743cc0234f1851f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":150,"wires":[["e4bf02867c4a6690"]]},{"id":"e4bf02867c4a6690","type":"template","z":"743cc0234f1851f1","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"LR HandlingTool       4324/543\nS/W Serial No.      : 6243    \nController ID       : F888   \nRobot No.           : A8888   \nManufacturing ID    : \nAA Rate abcd/7F, CGES      \nLT Tate 200iD/7F      V1.30D/33 \nAAaor AA          : V10.03    \nTart. DDD. Parameter: V3.00\nANT. Mot. Parameter : V3.00\nDCS                 : V3.5.12\nSt0p pattern        : A\nSoftware Edition No.: D8.SSP/34 \nUpdate Version      : No\nCustomization Ver.  : No\nRoot Version        : V8.30226  \nBoot MONITOR        : V8.30P/31\nTeach Peadant       : 8AAF/11A\nBrdsser Plugins     : V2.34222\nTF Dore Firmware    : V2.304/31\nMedia from AAA        12/36/2034","output":"str","x":360,"y":180,"wires":[["d1065ab4c4fd42cd"]]},{"id":"d1065ab4c4fd42cd","type":"split","z":"743cc0234f1851f1","name":"Split to new line","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":560,"y":180,"wires":[["03ff8d8ce3dbbe69"]]},{"id":"03ff8d8ce3dbbe69","type":"function","z":"743cc0234f1851f1","name":"Replacing ","func":"msg.payload = msg.payload.replace(/:/g, \" \")\nmsg.payload = msg.payload.split(/\\s+\\s+/)\nmsg.topic = \"TEST/Information/\" + msg.payload[0].trim()\nmsg.payload = msg.payload[1].trim()\nif(msg.payload == \"\"){\n    msg = null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":180,"wires":[["70df2e550e8e6350"]]},{"id":"70df2e550e8e6350","type":"debug","z":"743cc0234f1851f1","name":"debug 387","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":240,"wires":[]}]

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