How to switch based on array value

Hi

i am new to node red and would like to know how to do the following.

I have 8 digital input values( true or false ) that are coming in on msg.payload as an array [false,false,false,false,false,false,false,false] and i would like to trigger outputs based on the true or false value of each array index.

You could do a few ways. Off the top of my head:

  • via a function node set to have 8 output and the code set to return msg.payload.map(e => {return {payload:e}})
  • wire the msg to 8 change nodes that SETs the value of msg.payload to msg.payload[x] (where x is 0~7)

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Here's a quick demo using Steve's Map Method...

And here's the Node-RED flow, so you can Import it and try it out...

[{"id":"d0d670bd0a5b6772","type":"tab","label":"Map function demo","disabled":false,"info":"","env":[]},{"id":"8959eb7e7a67cc21","type":"function","z":"d0d670bd0a5b6772","name":"Array Mapping","func":"// return msg.payload.map(e => ({ payload: e }));\n\n// Return only if an input is true, null is false\n// Uncomment the next line and comment-out the first line\n \nreturn msg.payload.map(e => e ? { payload: e } : null);","outputs":8,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":280,"wires":[["e0b650c1687fc97f"],["2676a4a1c86798e0"],["7087deea270ed147"],["815e704f0cc8b762"],["d4e24d66b9a144bc"],["d335eb5abfe68df1"],["b89a22a259ce35ea"],["c70a2db23ac35081"]]},{"id":"2267674ab1c3bbd6","type":"inject","z":"d0d670bd0a5b6772","name":"Test","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":210,"y":280,"wires":[["de7de090fd54ad38"]]},{"id":"de7de090fd54ad38","type":"function","z":"d0d670bd0a5b6772","name":"Array pattern","func":"msg.payload = [true,false,false,false,false,false,false,false];\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":280,"wires":[["8959eb7e7a67cc21"]]},{"id":"e0b650c1687fc97f","type":"debug","z":"d0d670bd0a5b6772","name":"MSB","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":140,"wires":[]},{"id":"2676a4a1c86798e0","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":180,"wires":[]},{"id":"7087deea270ed147","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3576","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":220,"wires":[]},{"id":"815e704f0cc8b762","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3577","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":260,"wires":[]},{"id":"d4e24d66b9a144bc","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3578","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":300,"wires":[]},{"id":"d335eb5abfe68df1","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3579","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":340,"wires":[]},{"id":"b89a22a259ce35ea","type":"debug","z":"d0d670bd0a5b6772","name":"debug 3580","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":380,"wires":[]},{"id":"c70a2db23ac35081","type":"debug","z":"d0d670bd0a5b6772","name":"LSB","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":420,"wires":[]},{"id":"25dfae1fd97e1bfd","type":"comment","z":"d0d670bd0a5b6772","name":"Using .map method","info":"","x":570,"y":200,"wires":[]}]


The JavaScript in the function node can be adapted so only inputs that are 'true' are shown on the outport port(s) by using this...

return msg.payload.map(e => e ? { payload: e } : null);

There may be a simpler way, exactly how do you trigger an output?

Just interested to hear if you have had time to try out the various suggestions?

Hi

No this did not work.........I did have a solution that i found online that used a split node before the switch node that worked but i managed to delete it and now cant find the solution i found online

1 Like

Using a debug node show us what is going into the function (screenshot, expand any objects shown there first) and Export the function node and paste it here.

[
    {
        "id": "7e8a445cd9f1f429",
        "type": "modbus-read",
        "z": "d0d670bd0a5b6772",
        "name": "",
        "topic": "",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Input",
        "adr": "0",
        "quantity": "1",
        "rate": "2",
        "rateUnit": "s",
        "delayOnStart": false,
        "enableDeformedMessages": false,
        "startDelayTime": "",
        "server": "733fb84059b76a0f",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 130,
        "y": 300,
        "wires": [
            [],
            [
                "637f6e872ed32b51"
            ]
        ]
    },
    {
        "id": "637f6e872ed32b51",
        "type": "split",
        "z": "d0d670bd0a5b6772",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "property": "payload",
        "x": 310,
        "y": 320,
        "wires": [
            [
                "328eea9b0acbf03e"
            ]
        ]
    },
    {
        "id": "328eea9b0acbf03e",
        "type": "switch",
        "z": "d0d670bd0a5b6772",
        "name": "",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "parts[0]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[1]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[2]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[3]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[4]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[5]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[6]",
                "vt": "msg"
            },
            {
                "t": "eq",
                "v": "parts[7]",
                "vt": "msg"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 8,
        "x": 470,
        "y": 320,
        "wires": [
            [
                "30c4cb7230d3622f",
                "c9f1b293ac7d7bcf"
            ],
            [
                "ddedc2863e52e014",
                "2963765f6c05b830"
            ],
            [
                "e43deb24212749ad",
                "47a729a6de098d2c"
            ],
            [
                "c2e9b0405d4e31fd",
                "13f6efe2e10c5de9"
            ],
            [
                "945773ec01f32003",
                "aff3470865645d7b"
            ],
            [
                "822b7da1a72e5afc",
                "89b9bafba6b24bf3"
            ],
            [
                "98ec17257870c06f",
                "43dc7c3b02aed743"
            ],
            [
                "23f184ad51532e4b",
                "92c7c889b73b55d3"
            ]
        ]
    },
    {
        "id": "30c4cb7230d3622f",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 140,
        "wires": []
    },
    {
        "id": "ddedc2863e52e014",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 220,
        "wires": []
    },
    {
        "id": "e43deb24212749ad",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 300,
        "wires": []
    },
    {
        "id": "c2e9b0405d4e31fd",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 380,
        "wires": []
    },
    {
        "id": "945773ec01f32003",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 460,
        "wires": []
    },
    {
        "id": "822b7da1a72e5afc",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 540,
        "wires": []
    },
    {
        "id": "98ec17257870c06f",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 7",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 620,
        "wires": []
    },
    {
        "id": "23f184ad51532e4b",
        "type": "debug",
        "z": "d0d670bd0a5b6772",
        "name": "debug 8",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 700,
        "wires": []
    },
    {
        "id": "c9f1b293ac7d7bcf",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 1",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "0",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 180,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "2963765f6c05b830",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 2",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "1",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 260,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "47a729a6de098d2c",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 3",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "2",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 340,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "13f6efe2e10c5de9",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 4",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "3",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 420,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "aff3470865645d7b",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 5",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "4",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 500,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "89b9bafba6b24bf3",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 6",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "5",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 580,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "43dc7c3b02aed743",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 7",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "6",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 660,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "92c7c889b73b55d3",
        "type": "modbus-write",
        "z": "d0d670bd0a5b6772",
        "name": "Relay 8",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "Coil",
        "adr": "7",
        "quantity": "1",
        "server": "733fb84059b76a0f",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 680,
        "y": 740,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "733fb84059b76a0f",
        "type": "modbus-client",
        "name": "",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "192.168.1.101",
        "tcpPort": 502,
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": 9600,
        "serialDatabits": 8,
        "serialStopbits": 1,
        "serialParity": "none",
        "serialConnectionDelay": 100,
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": true,
        "showErrors": false,
        "showWarnings": true,
        "showLogs": true
    },
    {
        "id": "72033a3f75b427cc",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-contrib-modbus": "5.45.2"
        }
    }
]

Does your Modbus really output an array like you stated in your initial posting?
That is what I based my demo flow on.
Did you try the Demo Flow as it worked on my Raspberry Pi?

You have not done that. In fact also show us what is coming out of the modbus node.

If you are splitting a array with the split node, you should be using msg.parts.index and == number 0,1,2,3, etc

e.g

yes i tried that but did not work....no the output is looking for a boolean value.....in modbus input is [false,false,false,false,false,false,false,false] which is the current state of each input but goes to true when an input is closed

this is what is coming in on the modbus input

18/03/2026, 14:36:35node: debug 4polling : msg : Object

object

topic: "polling"

payload: object

data: array[8]

0: false

1: false

2: false

3: false

4: false

5: false

6: false

7: false

buffer: buffer[1]

0: 0x0

values: array[8]

0: false

1: false

2: false

3: false

4: false

5: false

6: false

7: false

input: object

topic: "polling"

from: ""

payload: object

queueLengthByUnitId: object

queueUnitId: 1

unitId: 1

sendingNodeId: "8f3dfaf360c57a2f"

_msgid: "9d6fb8e854565892"

Hi

Have done that but still no joy !!!

Hi, when you copy data from the debug, please use the "Copy Value" button to get good valid JSON representation

BX00Cy7yHi

Since the value appears to be in payload.data, you can simply do what I suggested in my first post

but using msg.payload.data of course

Demo

chrome_CHNrnMYghl

return msg.payload.data.map(e => { return { payload: e } })

demo flow (use CTRL+I to import)

[{"id":"fb165d75c3c39be2","type":"function","z":"7ecf422396bf31b4","name":"function 2","func":"return msg.payload.data.map(e => { return { payload: e } })\n","outputs":8,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1140,"wires":[["0498ae635bb28812"],["69f92c42d33f1b17"],["4fa9344a11984b75"],["78e2347950aac4db"],[],[],[],[]]},{"id":"b358ec1e5705c108","type":"inject","z":"7ecf422396bf31b4","name":"fake modus array of coils","props":[{"p":"payload.data","v":"[true, false, true, false,true, false, true, false]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":260,"y":1140,"wires":[["fb165d75c3c39be2"]]},{"id":"0498ae635bb28812","type":"debug","z":"7ecf422396bf31b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":790,"y":1000,"wires":[]},{"id":"69f92c42d33f1b17","type":"debug","z":"7ecf422396bf31b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":790,"y":1060,"wires":[]},{"id":"4fa9344a11984b75","type":"debug","z":"7ecf422396bf31b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":790,"y":1120,"wires":[]},{"id":"78e2347950aac4db","type":"debug","z":"7ecf422396bf31b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":790,"y":1180,"wires":[]}]
1 Like

What does a debug node after the split node reveal?

As Steve has pointed out, it looks like you should be in msg.payload.data.

Either,

move msg,payload.data to msg.payload, your individual items of data would be in msg.payload after the split.

or

split msg.payload.data, your individual values would then be in msg.payload.data after the split.