Use the length of an array as selector for a switch

Hi all,

How can I use the length of a random array (in msg.payload) as determent for a switch?
An example:
If array = empty, follow path 1
If array = 1, follow path 2
If array >= 2, follow path 3

The workaround I use now is to pass the length of the array in msg.topic, as seen here


But I would like a more elegant solution :slight_smile:

Thanks

Hey,

I’m not a Node-Red guru at all. but i think this example is what you have in mind.

[{"id":"feb09fd1.c64ba","type":"inject","z":"51dc3416.631b0c","name":"Ex. array size 4","topic":"","payload":"[\"Storage\", \"Kitchen\", \"Living\", \"Hallway\"] ","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1466.36669921875,"y":137.91665649414062,"wires":[["380c5600.15f6fa"]]},{"id":"380c5600.15f6fa","type":"switch","z":"51dc3416.631b0c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"($count(payload))=0","vt":"jsonata"},{"t":"jsonata_exp","v":"($count(payload))=1","vt":"jsonata"},{"t":"jsonata_exp","v":"($count(payload))=2","vt":"jsonata"},{"t":"jsonata_exp","v":"($count(payload))=3","vt":"jsonata"},{"t":"jsonata_exp","v":"($count(payload))=4","vt":"jsonata"}],"checkall":"false","repair":false,"outputs":5,"x":1654.36669921875,"y":170.91665649414062,"wires":[["1a4764f4.e619eb"],["2739cc04.8cf06c"],["a58aed81.bbbc78"],["1b7150cc.b4f0d7"],["986db2de.e7e3e"]]},{"id":"1a4764f4.e619eb","type":"debug","z":"51dc3416.631b0c","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1908.3667755126953,"y":121.5,"wires":[]},{"id":"2739cc04.8cf06c","type":"debug","z":"51dc3416.631b0c","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1907.0833740234375,"y":164.08334350585938,"wires":[]},{"id":"a58aed81.bbbc78","type":"debug","z":"51dc3416.631b0c","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1906.0833740234375,"y":205.08334350585938,"wires":[]},{"id":"1b7150cc.b4f0d7","type":"debug","z":"51dc3416.631b0c","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1900.0833740234375,"y":247.08334350585938,"wires":[]},{"id":"986db2de.e7e3e","type":"debug","z":"51dc3416.631b0c","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1895.75,"y":293.0833435058594,"wires":[]},{"id":"3ff0ec25.a3d51c","type":"inject","z":"51dc3416.631b0c","name":"Ex. array size 2","topic":"","payload":"[\"Storage\", \"Hallway\"] ","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1467.75,"y":178.08334350585938,"wires":[["380c5600.15f6fa"]]},{"id":"833bcc38.db6e08","type":"inject","z":"51dc3416.631b0c","name":"Ex. array size 0","topic":"","payload":"[] ","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1469.75,"y":219.08334350585938,"wires":[["380c5600.15f6fa"]]}]
1 Like

That’s it.
Thanks!

1 Like

I was looking for something along these lines and came across this, I’m sure I’ve done this in a really simple way in the past, but anyway, I’ve tweaked this a little to simply it and wanted to share incase anyone else is looking for it.