How to split flow after x payload characters?

Hello everyone,
I have the following partial flow (original thread Problems with MQTT flow after Excel import and msg modifications - #8 by E1cid ) where I have an array that can contain a lot of characters.

What I would like to do is split after 500 characters.

//EDIT: I tried a second approach of explanation in my next post :slight_smile:

So the first 500 characters should end up having the topic "cmnd/tasmota_45147C/Rule1" and the next 500 should have "cmnd/tasmota_45147C/Rule1 +" and all following batches of 500 characters should also have "cmnd/tasmota_45147C/Rule1 +".

cmnd/tasmota_45147C/Rule1 : msg.topic : string[25]
"cmnd/tasmota_45147C/Rule1"

with (sorry, did not count the characters for this example)

cmnd/tasmota_45147C/Rule1 : msg.payload : string[457]
"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ature<%var16% DO Backlog var16 %value%; status 10; var15 %value%; add15 2; sub16 2 ENDON"

and then

"cmnd/tasmota_45147C/Rule1 +"

with the next 500 charaters of the payload.

So in the end I get multiple sets of topic+payload to publish as one mqtt message

Does anybody know how I can achieve this?

I am not sure why this extract of my big flow stops working after the join node.
The debug after join should output

{"rule1":"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON
","topic":"cmnd/tasmota_45147C"}

But I hope it is clear what it does right now. Happy to explain further or copy more info from the flow.

[
    {
        "id": "4876690d41b4e54a",
        "type": "file in",
        "z": "4917828a0c65f3b4",
        "name": "Select Excel.xlsx  to read as binary buffer",
        "filename": "C:\\00_Home Automation\\IoT_Overview.xlsm",
        "filenameType": "str",
        "format": "",
        "chunk": false,
        "sendError": false,
        "allProps": false,
        "x": 790,
        "y": 450,
        "wires": [
            [
                "470afdf1f5ae8bea"
            ]
        ]
    },
    {
        "id": "470afdf1f5ae8bea",
        "type": "book",
        "z": "4917828a0c65f3b4",
        "name": "Workbook",
        "raw": false,
        "x": 1040,
        "y": 450,
        "wires": [
            [
                "af19443bacd024a6"
            ]
        ]
    },
    {
        "id": "af19443bacd024a6",
        "type": "sheet",
        "z": "4917828a0c65f3b4",
        "name": "Worksheet \"Tasmota\"",
        "sheetName": "Tasmota",
        "x": 1230,
        "y": 450,
        "wires": [
            [
                "1e41d24fa32b08aa"
            ]
        ]
    },
    {
        "id": "ef106fcc4fd092ac",
        "type": "sheet-to-json",
        "z": "4917828a0c65f3b4",
        "name": "topic",
        "raw": "false",
        "range": "",
        "header": "A",
        "blankrows": true,
        "x": 1659,
        "y": 450,
        "wires": [
            [
                "2b599f39186e2c56",
                "90461ae9f9fb8819"
            ]
        ]
    },
    {
        "id": "2b599f39186e2c56",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "topic",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1839,
        "y": 490,
        "wires": []
    },
    {
        "id": "90461ae9f9fb8819",
        "type": "link out",
        "z": "4917828a0c65f3b4",
        "name": "link out topic",
        "mode": "link",
        "links": [
            "b3e51ed4ea0fd757"
        ],
        "x": 1859,
        "y": 450,
        "wires": [],
        "l": true
    },
    {
        "id": "37fbca160225293c",
        "type": "sheet-to-json",
        "z": "4917828a0c65f3b4",
        "name": "rule1",
        "raw": "false",
        "range": "",
        "header": "A",
        "blankrows": true,
        "x": 1680,
        "y": 560,
        "wires": [
            [
                "c874e5cc0fd9938c",
                "9c3f6a4da5156f6f"
            ]
        ]
    },
    {
        "id": "c874e5cc0fd9938c",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "rule1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1860,
        "y": 600,
        "wires": []
    },
    {
        "id": "9c3f6a4da5156f6f",
        "type": "link out",
        "z": "4917828a0c65f3b4",
        "name": "link out rule1",
        "mode": "link",
        "links": [
            "23d2f90904b7b617"
        ],
        "x": 1880,
        "y": 560,
        "wires": [],
        "l": true
    },
    {
        "id": "ed910d310e840287",
        "type": "inject",
        "z": "4917828a0c65f3b4",
        "name": "start rules",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "setting",
                "v": "rules",
                "vt": "str"
            },
            {
                "p": "selectRange",
                "v": "Ax:Ay",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 160,
        "y": 630,
        "wires": [
            [
                "49bdb30230ca9d8e"
            ]
        ]
    },
    {
        "id": "bf1bddec7d155549",
        "type": "file in",
        "z": "4917828a0c65f3b4",
        "name": "Select Excel.xlsx  to read as binary buffer",
        "filename": "C:\\00_Home Automation\\IoT_Overview.xlsm",
        "filenameType": "str",
        "format": "",
        "chunk": false,
        "sendError": false,
        "allProps": false,
        "x": 811,
        "y": 560,
        "wires": [
            [
                "55492284239c1cf1"
            ]
        ]
    },
    {
        "id": "55492284239c1cf1",
        "type": "book",
        "z": "4917828a0c65f3b4",
        "name": "Workbook",
        "raw": false,
        "x": 1061,
        "y": 560,
        "wires": [
            [
                "9a1ca1557968905d"
            ]
        ]
    },
    {
        "id": "9a1ca1557968905d",
        "type": "sheet",
        "z": "4917828a0c65f3b4",
        "name": "Worksheet \"Tasmota\"",
        "sheetName": "Tasmota",
        "x": 1251,
        "y": 560,
        "wires": [
            [
                "55fc55c834cf8a65"
            ]
        ]
    },
    {
        "id": "1e41d24fa32b08aa",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "Change Column to G",
        "rules": [
            {
                "t": "change",
                "p": "selectRange",
                "pt": "msg",
                "from": "A",
                "fromt": "str",
                "to": "G",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1470,
        "y": 450,
        "wires": [
            [
                "ef106fcc4fd092ac"
            ]
        ]
    },
    {
        "id": "55fc55c834cf8a65",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "Change Column to T",
        "rules": [
            {
                "t": "change",
                "p": "selectRange",
                "pt": "msg",
                "from": "A",
                "fromt": "str",
                "to": "T",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1491,
        "y": 560,
        "wires": [
            [
                "37fbca160225293c"
            ]
        ]
    },
    {
        "id": "49bdb30230ca9d8e",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "Cell Range",
        "rules": [
            {
                "t": "change",
                "p": "selectRange",
                "pt": "msg",
                "from": "x",
                "fromt": "str",
                "to": "11",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "selectRange",
                "pt": "msg",
                "from": "y",
                "fromt": "str",
                "to": "11",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 530,
        "wires": [
            [
                "09f033bbd9876bbf"
            ]
        ]
    },
    {
        "id": "09f033bbd9876bbf",
        "type": "switch",
        "z": "4917828a0c65f3b4",
        "name": "Router",
        "property": "setting",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nempty"
            },
            {
                "t": "eq",
                "v": "result",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "ip",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "template",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "settings",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "timers",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "rules",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 7,
        "x": 520,
        "y": 530,
        "wires": [
            [
                "4876690d41b4e54a"
            ],
            [],
            [],
            [],
            [],
            [],
            [
                "bf1bddec7d155549"
            ]
        ]
    },
    {
        "id": "b3e51ed4ea0fd757",
        "type": "link in",
        "z": "4917828a0c65f3b4",
        "name": "link in topic",
        "links": [
            "5b7db8364882eb62",
            "e843196c4aa6e844",
            "74b1cc4a1f7f173e",
            "43f249b71d98e440",
            "6871bbea69ab6f09",
            "90461ae9f9fb8819"
        ],
        "x": 190,
        "y": 740,
        "wires": [
            [
                "eb7bb4d7a11d158c"
            ]
        ],
        "l": true
    },
    {
        "id": "23d2f90904b7b617",
        "type": "link in",
        "z": "4917828a0c65f3b4",
        "name": "link in rule1",
        "links": [
            "4a97bb7898cf4879",
            "9c3f6a4da5156f6f"
        ],
        "x": 100,
        "y": 810,
        "wires": [
            [
                "def562e18bfb43a2"
            ]
        ],
        "l": true
    },
    {
        "id": "600366a0f68799ab",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "rule1 payload",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1900,
        "y": 810,
        "wires": []
    },
    {
        "id": "807c9d936a26caeb",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "rule1 topic",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1890,
        "y": 850,
        "wires": []
    },
    {
        "id": "9665e9441d8dbe60",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "change topic and payload (string array)",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "$keys($$.payload[0])",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$$.payload.*",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 650,
        "y": 810,
        "wires": [
            [
                "c6527056c4870d77"
            ]
        ]
    },
    {
        "id": "c6527056c4870d77",
        "type": "join",
        "z": "4917828a0c65f3b4",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 870,
        "y": 810,
        "wires": [
            [
                "1ef97b5567699f95"
            ]
        ]
    },
    {
        "id": "4b9e9731d9cd6ad6",
        "type": "split",
        "z": "4917828a0c65f3b4",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 1170,
        "y": 810,
        "wires": [
            [
                "fa6f1dbb8b94bb9a"
            ]
        ]
    },
    {
        "id": "fa6f1dbb8b94bb9a",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "move payloads to topic and payload",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "payload.topic",
                "tot": "msg"
            },
            {
                "t": "move",
                "p": "payload.rule1",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1380,
        "y": 810,
        "wires": [
            [
                "5875817a2b85eea9"
            ]
        ]
    },
    {
        "id": "5875817a2b85eea9",
        "type": "delay",
        "z": "4917828a0c65f3b4",
        "name": "rate limit (prevent flooding)",
        "pauseType": "rate",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1660,
        "y": 810,
        "wires": [
            [
                "600366a0f68799ab",
                "807c9d936a26caeb"
            ]
        ]
    },
    {
        "id": "1ef97b5567699f95",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "add \"/Rule1\"",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.topic#$i.{\"topic\": $ & \"/Rule1\", \"rule1\": $$.payload.rule1[$i]}[]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1010,
        "y": 810,
        "wires": [
            [
                "4b9e9731d9cd6ad6"
            ]
        ]
    },
    {
        "id": "def562e18bfb43a2",
        "type": "function",
        "z": "4917828a0c65f3b4",
        "name": "Change key to rule1",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"rule1\": o[0] };\n});\nreturn msg;\n\n\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 810,
        "wires": [
            [
                "9665e9441d8dbe60"
            ]
        ]
    },
    {
        "id": "eb7bb4d7a11d158c",
        "type": "function",
        "z": "4917828a0c65f3b4",
        "name": "Change key to topic",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"topic\": o[0] };\n});\nreturn msg;\n\n//in this output it's an array that contains two indexes and each index contains an object with the key \"IPAddress1 192.168.0.201; savedata; restart 1\"\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 740,
        "wires": [
            [
                "9665e9441d8dbe60"
            ]
        ]
    }
]

It stops because the join node is set to After a number of message parts 2 so is waiting for messages.

Can I ask what you are trying to do with this flow ?

Sorry, you are right. I forgot the "topic" flows.
I updated the flow in my inital post to include all relevant parts.

So I have an excel file with the first row being the actual command. In this case "Rule1".
The cells beneath that header row contain the data for that command for a whole bunch of devices.
Example:

ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON

At the end of the flow I then use an mqtt node to publish the final message to the device(s) and thereby create a rule.

The problem is that my rules have grown so large that the target hardware devices cannot process that one, long message. It needs to be broken down into multiple messages.

Each mqtt command starts with cmnd/"device_id"/"command_name"
The first command name is the header, i.e. "Rule1". To add more data to that same rule, you need to use the command "Rule1 +".

So the device needs to receive 2 (or more) messages like this:

cmnd/tasmota_45147C/Rule1 long string with maximum 500 characters
cmnd/tasmota_45147C/Rule1 +second part of long string with again maximum 500 characters
cmnd/tasmota_45147C/Rule1 +third part of long string with again maximum 500 characters

and so on until all characters have been broken into 500 chars parts.

You could simply split the rule1 using the split node, then use msg.parts.index to determine the first message and noit append a +.
e.g.

[{"id":"5016cc53317e9f4c","type":"inject","z":"f8f32a4a2ead0f46","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"rule1\":\"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON\",\"topic\":\"cmnd/tasmota_45147C\"}]","payloadType":"json","x":120,"y":320,"wires":[["009c9aac2b039239"]]},{"id":"009c9aac2b039239","type":"change","z":"f8f32a4a2ead0f46","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload[0].topic","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload[0].rule1","tot":"msg"},{"t":"set","p":"command","pt":"msg","to":"Rule1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":320,"wires":[["b155c1a51eafbd42"]]},{"id":"b155c1a51eafbd42","type":"split","z":"f8f32a4a2ead0f46","name":"","splt":"500","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":430,"y":320,"wires":[["f769a5410c3a1ec6"]]},{"id":"f769a5410c3a1ec6","type":"delay","z":"f8f32a4a2ead0f46","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"4","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":570,"y":320,"wires":[["e001bffd30d59fc0"]]},{"id":"e001bffd30d59fc0","type":"switch","z":"f8f32a4a2ead0f46","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":170,"y":400,"wires":[["6e1881d7f5398f47"],["8227a449d1ae0d98"]]},{"id":"6e1881d7f5398f47","type":"template","z":"f8f32a4a2ead0f46","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{topic}}/{{command}}","output":"str","x":480,"y":400,"wires":[["71d3b97b9a296662"]]},{"id":"8227a449d1ae0d98","type":"template","z":"f8f32a4a2ead0f46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"+{{{payload}}}","output":"str","x":300,"y":420,"wires":[["6e1881d7f5398f47"]]},{"id":"71d3b97b9a296662","type":"debug","z":"f8f32a4a2ead0f46","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":400,"wires":[]}]
1 Like

Thank you @E1cid :slight_smile:

I am not sure if I misplaced your nodes or if I am missing a piece of the puzzle.

There is one sub-flow for topic and one for rule. They are then joined together.

Here is the combination of yours and mine.
Normally the flow would wait until it receives both the rule1 and the topic and then merge them. With the splitting after 500 chars this does not seem to work anymore.

[
    {
        "id": "4917828a0c65f3b4",
        "type": "tab",
        "label": "test",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "600366a0f68799ab",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "rule1 payload",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1690,
        "y": 690,
        "wires": []
    },
    {
        "id": "807c9d936a26caeb",
        "type": "debug",
        "z": "4917828a0c65f3b4",
        "name": "rule1 topic",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1680,
        "y": 730,
        "wires": []
    },
    {
        "id": "9665e9441d8dbe60",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "change topic and payload (string array)",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "$keys($$.payload[0])",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$$.payload.*",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 690,
        "wires": [
            [
                "c6527056c4870d77"
            ]
        ]
    },
    {
        "id": "c6527056c4870d77",
        "type": "join",
        "z": "4917828a0c65f3b4",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 660,
        "y": 690,
        "wires": [
            [
                "1ef97b5567699f95"
            ]
        ]
    },
    {
        "id": "4b9e9731d9cd6ad6",
        "type": "split",
        "z": "4917828a0c65f3b4",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 960,
        "y": 690,
        "wires": [
            [
                "fa6f1dbb8b94bb9a"
            ]
        ]
    },
    {
        "id": "fa6f1dbb8b94bb9a",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "move payloads to topic and payload",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "payload.topic",
                "tot": "msg"
            },
            {
                "t": "move",
                "p": "payload.rule1",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1170,
        "y": 690,
        "wires": [
            [
                "5875817a2b85eea9"
            ]
        ]
    },
    {
        "id": "5875817a2b85eea9",
        "type": "delay",
        "z": "4917828a0c65f3b4",
        "name": "rate limit (prevent flooding)",
        "pauseType": "rate",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1450,
        "y": 690,
        "wires": [
            [
                "600366a0f68799ab",
                "807c9d936a26caeb"
            ]
        ]
    },
    {
        "id": "1ef97b5567699f95",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "add \"/Rule1\"",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.topic#$i.{\"topic\": $ & \"/Rule1\", \"rule1\": $$.payload.rule1[$i]}[]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 800,
        "y": 690,
        "wires": [
            [
                "4b9e9731d9cd6ad6"
            ]
        ]
    },
    {
        "id": "def562e18bfb43a2",
        "type": "function",
        "z": "4917828a0c65f3b4",
        "name": "Change key to rule1",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"rule1\": o[0] };\n});\nreturn msg;\n\n\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 860,
        "wires": [
            [
                "009c9aac2b039239"
            ]
        ]
    },
    {
        "id": "eb7bb4d7a11d158c",
        "type": "function",
        "z": "4917828a0c65f3b4",
        "name": "Change key to topic",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"topic\": o[0] };\n});\nreturn msg;\n\n//in this output it's an array that contains two indexes and each index contains an object with the key \"IPAddress1 192.168.0.201; savedata; restart 1\"\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 610,
        "wires": [
            [
                "9665e9441d8dbe60"
            ]
        ]
    },
    {
        "id": "5016cc53317e9f4c",
        "type": "inject",
        "z": "4917828a0c65f3b4",
        "name": "rule1",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"rule1\":\"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON\",\"topic\":\"cmnd/tasmota_45147C\"}]",
        "payloadType": "json",
        "x": 100,
        "y": 860,
        "wires": [
            [
                "def562e18bfb43a2"
            ]
        ]
    },
    {
        "id": "009c9aac2b039239",
        "type": "change",
        "z": "4917828a0c65f3b4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "payload[0].topic",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[0].rule1",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "command",
                "pt": "msg",
                "to": "Rule1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 510,
        "y": 860,
        "wires": [
            [
                "b155c1a51eafbd42"
            ]
        ]
    },
    {
        "id": "b155c1a51eafbd42",
        "type": "split",
        "z": "4917828a0c65f3b4",
        "name": "",
        "splt": "500",
        "spltType": "len",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 660,
        "y": 860,
        "wires": [
            [
                "f769a5410c3a1ec6"
            ]
        ]
    },
    {
        "id": "f769a5410c3a1ec6",
        "type": "delay",
        "z": "4917828a0c65f3b4",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "4",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 800,
        "y": 860,
        "wires": [
            [
                "e001bffd30d59fc0"
            ]
        ]
    },
    {
        "id": "e001bffd30d59fc0",
        "type": "switch",
        "z": "4917828a0c65f3b4",
        "name": "",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 950,
        "y": 860,
        "wires": [
            [
                "6e1881d7f5398f47"
            ],
            [
                "8227a449d1ae0d98"
            ]
        ]
    },
    {
        "id": "6e1881d7f5398f47",
        "type": "template",
        "z": "4917828a0c65f3b4",
        "name": "",
        "field": "topic",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{topic}}/{{command}}",
        "output": "str",
        "x": 1130,
        "y": 860,
        "wires": [
            [
                "9665e9441d8dbe60"
            ]
        ]
    },
    {
        "id": "8227a449d1ae0d98",
        "type": "template",
        "z": "4917828a0c65f3b4",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "+ {{{payload}}}",
        "output": "str",
        "x": 1070,
        "y": 920,
        "wires": [
            [
                "6e1881d7f5398f47"
            ]
        ]
    },
    {
        "id": "c83e61e5886f2180",
        "type": "inject",
        "z": "4917828a0c65f3b4",
        "name": "topic",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"G\":\"cmnd/tasmota_45147C\"}]",
        "payloadType": "json",
        "x": 90,
        "y": 610,
        "wires": [
            [
                "eb7bb4d7a11d158c"
            ]
        ]
    }
]

Join the incoming payloads before you split

[{"id":"9369378ae04540c3","type":"inject","z":"1fdeaece8f418434","name":"topic","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"G\":\"cmnd/tasmota_45147C\"}]","payloadType":"json","x":90,"y":610,"wires":[["0ecd0697547595aa"]]},{"id":"0ecd0697547595aa","type":"change","z":"1fdeaece8f418434","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"topic","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"payload[0].G","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":620,"wires":[["237f4d0f02df01c8"]]},{"id":"237f4d0f02df01c8","type":"join","z":"1fdeaece8f418434","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":800,"wires":[["6675cd751bbb4f7d","9806db54e3759789"]]},{"id":"a1dd2d2380802b8b","type":"change","z":"1fdeaece8f418434","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"rule","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"payload[0].rule1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":860,"wires":[["237f4d0f02df01c8"]]},{"id":"6675cd751bbb4f7d","type":"change","z":"1fdeaece8f418434","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.topic","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.rule","tot":"msg"},{"t":"set","p":"command","pt":"msg","to":"Rule1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":860,"wires":[["af48b1454ed814f1"]]},{"id":"9806db54e3759789","type":"debug","z":"1fdeaece8f418434","name":"debug 329","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":720,"wires":[]},{"id":"656a3e599b4df5d9","type":"inject","z":"1fdeaece8f418434","name":"rule1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"rule1\":\"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON\",\"topic\":\"cmnd/tasmota_45147C\"}]","payloadType":"json","x":100,"y":860,"wires":[["a1dd2d2380802b8b"]]},{"id":"af48b1454ed814f1","type":"split","z":"1fdeaece8f418434","name":"","splt":"500","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":660,"y":860,"wires":[["718a49e7e3eb66de"]]},{"id":"718a49e7e3eb66de","type":"delay","z":"1fdeaece8f418434","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"4","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":800,"y":860,"wires":[["915bce70edde81be"]]},{"id":"915bce70edde81be","type":"switch","z":"1fdeaece8f418434","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":950,"y":860,"wires":[["fbdd71ed7f9d47f3"],["1d84c447a6d7fc45"]]},{"id":"fbdd71ed7f9d47f3","type":"template","z":"1fdeaece8f418434","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{topic}}/{{command}}","output":"str","x":1130,"y":860,"wires":[["108b05d9ff8e3457"]]},{"id":"1d84c447a6d7fc45","type":"template","z":"1fdeaece8f418434","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"+ {{{payload}}}","output":"str","x":1070,"y":920,"wires":[["fbdd71ed7f9d47f3"]]},{"id":"108b05d9ff8e3457","type":"debug","z":"1fdeaece8f418434","name":"debug 330","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1290,"y":840,"wires":[]}]

[edit]
Knowing nothing about your subflows, i would suggest you make them output a simple payload and topic.
e.g.

// topic output from subflow
{
  "payload": "my/topic",
  "topic": "topic"
}
// rule output from subflow
{
  "payload": "my rule string",
  "topic": "rule"
}

Then you would not require the extra chnage nodes to move the properties around, or to add topics.

1 Like

Thank you again. :slight_smile:

For some reason, the topic is output incorrectly from the template node
cmnd&#x2F;tasmota_45147C/Rule1

Why is "plain text" not working there?

Change {{topic}} to {{{topic}}}
from template help text

Note: By default, mustache will escape any non-alphanumeric or HTML entities in the values it substitutes. To prevent this, use {{{triple}}} braces.

1 Like

Would it be somehow possible to make the split split the message on the first last space before reaching 500 characters (and removing that space)?

Unfortunately the target adds a space everytime you add to an existing rule.

That then results in an invalid rule
ENDON ON energy#power[2]>=%mem1% D O IF ((var2!=%mem2%))
It should be DO IF

That looks like the receiving app is adding a space where the + is.

You will need to split at a space.
Maybe this example will do it for you. It splits at 500 chars or the nearest space below 500.
It also appends the plus if required and sets the topic.

[{"id":"e09171211ffbfead","type":"inject","z":"b9860b4b9de8c8da","name":"topic","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"G\":\"cmnd/tasmota_45147C\"}]","payloadType":"json","x":70,"y":260,"wires":[["43fe307ae4cf1f61"]]},{"id":"43fe307ae4cf1f61","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"topic","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"payload[0].G","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":260,"wires":[["fbf95049ca9763a2"]]},{"id":"fbf95049ca9763a2","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":250,"y":320,"wires":[["c72488515dd06cac"]]},{"id":"1ea45af6d6227871","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"rule","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"payload[0].rule1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":380,"wires":[["fbf95049ca9763a2"]]},{"id":"c72488515dd06cac","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"let input = msg.payload.rule.split(/\\s/);\nmsg.topic = msg.payload.topic + \"/Rule1\";\nlet output = \"\";\nlet space = \"\";\ninput.forEach(str => {\n    if(output.length + str.length >= 500){\n        msg.payload = output;\n        node.send(msg);\n        output = \"+\" + str + \" \";\n        space = \"\";\n    }else{\n        output = output + space + str;\n        space = \" \";\n    }\n});\nif (output.length > 0){\n    msg.payload = output;\n    return msg;\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":320,"wires":[["718a49e7e3eb66de"]]},{"id":"0ef39fc347e7ebdb","type":"inject","z":"b9860b4b9de8c8da","name":"rule1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"rule1\":\"ON energy#power[1]>=%mem1% DO IF ((var1!=%mem2%)) powerdelta1 %mem2%; var1 %mem2% ENDIF BREAK ON energy#power[1]>=%mem3% DO IF ((var1!=%mem4%)) powerdelta1 %mem4%; var1 %mem4% ENDIF BREAK ON energy#power[1]>=%mem5% DO IF ((var1!=%mem6%)) powerdelta1 %mem6%; var1 %mem6% ENDIF BREAK ON energy#power[1]>=%mem7% DO IF ((var1!=%mem8%)) powerdelta1 %mem8%; var1 %mem8% ENDIF BREAK ON energy#power[1]<%mem7% DO IF ((var1!=%mem10%)) powerdelta1 %mem10%; var1 %mem10% ENDIF ENDON ON energy#power[2]>=%mem1% DO IF ((var2!=%mem2%)) powerdelta2 %mem2%; var2 %mem2% ENDIF BREAK ON energy#power[2]>=%mem3% DO IF ((var2!=%mem4%)) powerdelta2 %mem4%; var2 %mem4% ENDIF BREAK ON energy#power[2]>=%mem5% DO IF ((var2!=%mem6%)) powerdelta2 %mem6%; var2 %mem6% ENDIF BREAK ON energy#power[2]>=%mem7% DO IF ((var2!=%mem8%)) powerdelta2 %mem8%; var2 %mem8% ENDIF BREAK ON energy#power[2]<%mem7% DO IF ((var2!=%mem10%)) powerdelta2 %mem10%; var2 %mem10% ENDIF ENDON ON energy#power[3]>=%mem1% DO IF ((var3!=%mem2%)) powerdelta3 %mem2%; var3 %mem2% ENDIF BREAK ON energy#power[3]>=%mem3% DO IF ((var3!=%mem4%)) powerdelta3 %mem4%; var3 %mem4% ENDIF BREAK ON energy#power[3]>=%mem5% DO IF ((var3!=%mem6%)) powerdelta3 %mem6%; var3 %mem6% ENDIF BREAK ON energy#power[3]>=%mem7% DO IF ((var3!=%mem8%)) powerdelta3 %mem8%; var3 %mem8% ENDIF BREAK ON energy#power[3]<%mem7% DO IF ((var3!=%mem10%)) powerdelta3 %mem10%; var3 %mem10% ENDIF ENDON\",\"topic\":\"cmnd/tasmota_45147C\"}]","payloadType":"json","x":70,"y":380,"wires":[["1ea45af6d6227871"]]},{"id":"718a49e7e3eb66de","type":"delay","z":"b9860b4b9de8c8da","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"4","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":550,"y":320,"wires":[["108b05d9ff8e3457"]]},{"id":"108b05d9ff8e3457","type":"debug","z":"b9860b4b9de8c8da","name":"debug 330","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":260,"wires":[]}]

Thank you once again @E1cid :slight_smile: :slight_smile:

Now everything is running smoothly. And the function is even more elegant.

I will save both approaches for the future :slight_smile:

Again, truly: thank you!

@E1cid, I have run into a small problem.

I usually have more than one set of rules being passed.
So the input into the change node of the rules flow would in fact be something like
image

So not just object 0.

This used to be handled by the change node

[
    {
        "id": "83ec6080bcdf67d4",
        "type": "change",
        "z": "52afbb846e6206f6",
        "name": "change topic and payload (string array)",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "$keys($$.payload[0])",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$$.payload.*",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 680,
        "y": 1070,
        "wires": [
            [
                "05541519c49163d5"
            ]
        ]
    }
]

which from my understanding handles all objects in the array.
That old change node received input from the topic and the rule flow. You separated those into a rule change node and a topic change node.

But I have not been able to change your rule change node (Specific to object 0?) to make it apply to all objects as in the above change node.

And when I try to replace your two change nodes (rule and topic) with just the old one, I get

TypeError: Cannot read properties of undefined (reading 'split')

EDIT:
New part-flow:

[
    {
        "id": "8d8a965d92f9eb04",
        "type": "link in",
        "z": "52afbb846e6206f6",
        "name": "link in rule1",
        "links": [
            "f81d84ed16880709"
        ],
        "x": 80,
        "y": 690,
        "wires": [
            [
                "272dfbb1de4d6fd4"
            ]
        ],
        "l": true
    },
    {
        "id": "272dfbb1de4d6fd4",
        "type": "function",
        "z": "52afbb846e6206f6",
        "name": "Change key to rule1",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"rule1\": o[0] };\n});\nreturn msg;\n\n\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 690,
        "wires": [
            [
                "03e1c09bd63c4cd9"
            ]
        ]
    },
    {
        "id": "72b35938e4a8b327",
        "type": "change",
        "z": "52afbb846e6206f6",
        "name": "change topic",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "topic",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[0].topic",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 550,
        "y": 670,
        "wires": [
            [
                "e2c46c9fdf59478b"
            ]
        ]
    },
    {
        "id": "e2c46c9fdf59478b",
        "type": "join",
        "z": "52afbb846e6206f6",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 700,
        "y": 700,
        "wires": [
            [
                "a29303a15aed73a2"
            ]
        ]
    },
    {
        "id": "03e1c09bd63c4cd9",
        "type": "change",
        "z": "52afbb846e6206f6",
        "name": "change rule1",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "rule",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[0].rule1",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 550,
        "y": 730,
        "wires": [
            [
                "e2c46c9fdf59478b"
            ]
        ]
    }
]

vs old part flow:

[
    {
        "id": "3159c47178afb6a5",
        "type": "link in",
        "z": "ac2f7243c9cf881e",
        "name": "link in rule1",
        "links": [
            "6a9158beaf0da797"
        ],
        "x": 80,
        "y": 720,
        "wires": [
            [
                "3c93179035419089"
            ]
        ],
        "l": true
    },
    {
        "id": "1228c0d736950d0a",
        "type": "change",
        "z": "ac2f7243c9cf881e",
        "name": "change topic and payload (string array)",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "$keys($$.payload[0])",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$$.payload.*",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 630,
        "y": 720,
        "wires": [
            [
                "9f0c2f9e29e20c8a"
            ]
        ]
    },
    {
        "id": "9f0c2f9e29e20c8a",
        "type": "join",
        "z": "ac2f7243c9cf881e",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 850,
        "y": 720,
        "wires": [
            [
                "898b41525c46afd6"
            ]
        ]
    },
    {
        "id": "3c93179035419089",
        "type": "function",
        "z": "ac2f7243c9cf881e",
        "name": "Change key to rule1",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"rule1\": o[0] };\n});\nreturn msg;\n\n\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 720,
        "wires": [
            [
                "1228c0d736950d0a"
            ]
        ]
    }
]

All rather confusing.

Lets use the new flow with function i offered the other day.

Can you give examples of input that is causing issues in this flow, in text format.

Sorry for that.
So, the topic would be

[{"G":"cmnd/tasmota_02B2D1"},{"G":"cmnd/tasmota_02A70A"},{"G":"cmnd/tasmota_228C3D"}]

and the matching rule would be

[{"V":"blah blah1"},{"V":"blah blah2"},{"V":"blah blah3"}]

image

Topic 0 belonging to Rule1 0 etc.

And you want to run through all rules in the array and send mqtt for each object?

How do you determine the rule name to append to mqtt topic?

Well, the first mqtt message will be
msg.topic cmnd/tasmota_02B2D1/Rule1 with msg.payload blah blah1.

The second will be
msg.topic cmnd/tasmota_02A70A/Rule1 with msg.payload blah blah2

So object 0 of topic will end up being the msg.topic of the mqtt message of the payload 0.

EDIT: Sorry, so it is always "/Rule1" that is being appended. Because that flow is always sending rule1 to the different devices.
Rule2 is sent in a separate flow.

Still not clear,.
so first array object has topic .../.../Rule1
and second array object is .../... but no Rule1?

Sorry, edited while you were typing. It should always have /Rule1 appended

EDIT:
If it helps, this is the old flow before you added the 500 character split. The input into the change node are topic and payload (both go into the change node) from here How to split flow after x payload characters? - #14 by AleXSR700

[
    {
        "id": "ecacc9a8fb50df45",
        "type": "function",
        "z": "640c1cd47a364cb1",
        "name": "Change key to rule1",
        "func": "msg.payload = msg.payload.map((obj) => {\n    const o = Object.values(obj);\n    return { \"rule1\": o[0] };\n});\nreturn msg;\n\n\n\n//alternative:\n        // loop over the array\n//msg.payload = msg.payload.map((obj) => {\n        // split the object in an array of key/value pairs\n//    const o = Object.entries(obj);\n        // assign the value to \"IP\" key\n//    return { \"IP\": o[0][1] };\n//});\n\n//return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 720,
        "wires": [
            [
                "39cfa1c54192f165"
            ]
        ]
    },
    {
        "id": "39cfa1c54192f165",
        "type": "change",
        "z": "640c1cd47a364cb1",
        "name": "change topic and payload (string array)",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "$keys($$.payload[0])",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$$.payload.*",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 630,
        "y": 720,
        "wires": [
            [
                "abc21708210b8179"
            ]
        ]
    },
    {
        "id": "abc21708210b8179",
        "type": "join",
        "z": "640c1cd47a364cb1",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 850,
        "y": 720,
        "wires": [
            [
                "f7e2928fc3807489"
            ]
        ]
    },
    {
        "id": "f7e2928fc3807489",
        "type": "change",
        "z": "640c1cd47a364cb1",
        "name": "add \"/Rule1\"",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.topic#$i.{\"topic\": $ & \"/Rule1\", \"rule1\": $$.payload.rule1[$i]}[]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 990,
        "y": 720,
        "wires": [
            [
                "c2f2d5f72ae0a55c"
            ]
        ]
    },
    {
        "id": "c2f2d5f72ae0a55c",
        "type": "split",
        "z": "640c1cd47a364cb1",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 1150,
        "y": 720,
        "wires": [
            [
                "1d4847fc44f994f8"
            ]
        ]
    },
    {
        "id": "1d4847fc44f994f8",
        "type": "change",
        "z": "640c1cd47a364cb1",
        "name": "move payloads to topic and payload",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "payload.topic",
                "tot": "msg"
            },
            {
                "t": "move",
                "p": "payload.rule1",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1360,
        "y": 720,
        "wires": [
            [
                "3be577911a3f4072"
            ]
        ]
    },
    {
        "id": "3be577911a3f4072",
        "type": "delay",
        "z": "640c1cd47a364cb1",
        "name": "rate limit (prevent flooding)",
        "pauseType": "rate",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1640,
        "y": 720,
        "wires": [
            [
                "e4cde7c308278b2d",
                "0b8ee0e38ed1e0d8",
                "704b54d316c54420"
            ]
        ]
    },
    {
        "id": "704b54d316c54420",
        "type": "mqtt out",
        "z": "640c1cd47a364cb1",
        "name": "MQTT rule1",
        "topic": "",
        "qos": "0",
        "retain": "false",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "0d7f426189a0c8d3",
        "x": 1870,
        "y": 670,
        "wires": []
    },
    {
        "id": "0d7f426189a0c8d3",
        "type": "mqtt-broker",
        "name": "MQTT Home Assistant",
        "broker": "192.168.0.2",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

Then the function code needs to be in a loop aswell, and the change nodes need to create arrays for the join.

[{"id":"e09171211ffbfead","type":"inject","z":"b9860b4b9de8c8da","name":"topic","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"G\":\"cmnd/tasmota_02B2D1\"},{\"G\":\"cmnd/tasmota_02A70A\"},{\"G\":\"cmnd/tasmota_228C3D\"}]","payloadType":"json","x":90,"y":120,"wires":[["43fe307ae4cf1f61"]]},{"id":"43fe307ae4cf1f61","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"topic","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"[$$.payload.*]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":120,"wires":[["fbf95049ca9763a2"]]},{"id":"fbf95049ca9763a2","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":270,"y":180,"wires":[["c72488515dd06cac"]]},{"id":"1ea45af6d6227871","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"rule","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"[$$.payload.*]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":240,"wires":[["fbf95049ca9763a2"]]},{"id":"c72488515dd06cac","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"const payload = RED.util.cloneMessage(msg.payload);\npayload.rule.forEach((val, index) => {\n    let input = val.split(/\\s/);\n    msg.topic = payload.topic[index] + \"/Rule1\";\n    let output = \"\";\n    let space = \"\";\n    input.forEach(str => {\n        if(output.length + str.length >= 500){\n            msg.payload = output;\n            node.send(msg);\n            output = \"+\" + str + \" \";\n            space = \"\";\n        }else{\n            output = output + space + str;\n            space = \" \";\n        }\n    });\n    if (output.length > 0){\n        msg.payload = output;\n        node.send(msg);\n    }\n})\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":220,"wires":[["718a49e7e3eb66de"]]},{"id":"0ef39fc347e7ebdb","type":"inject","z":"b9860b4b9de8c8da","name":"rule1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"V\":\"blah blah1\"},{\"V\":\"blah blah2\"},{\"V\":\"blah blah3\"}]","payloadType":"json","x":90,"y":240,"wires":[["1ea45af6d6227871"]]},{"id":"718a49e7e3eb66de","type":"delay","z":"b9860b4b9de8c8da","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"4","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":570,"y":180,"wires":[["108b05d9ff8e3457"]]},{"id":"108b05d9ff8e3457","type":"debug","z":"b9860b4b9de8c8da","name":"debug 330","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":120,"wires":[]}]

[edit] edited change nodes to return arrays even if singleton returned from JSONata expression, ie force array when singe object in incoming messages.

Hmm, did I do something wrong?
TypeError: payload.rule.forEach is not a function