How to check if a value is in array using the switch node

Hello,
I am quite new to Node Red and I need to ask for your help as I cannot solve a simple problem.

I have a flow where the mgs.payload is an array like

["Intrusion","Alarm","Input"]

I would like to introduce a switch depending on the value of the second field of msg.payload.
I have tried to introduce the switch node with property msg.payload[1] and then == string "Alarm" but it won't work.

Can you please help?
Thanks in advance,
Daniele

Its working for me

Example Flow

[{"id":"d61f4d428d068cd7","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Intrusion\",\"Alarm\",\"Input\"]","payloadType":"json","x":260,"y":1800,"wires":[["3f22d122d79c0faf"]]},{"id":"3f22d122d79c0faf","type":"switch","z":"54efb553244c241f","name":"Alarm","property":"payload[1]","propertyType":"msg","rules":[{"t":"eq","v":"Alarm","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":1800,"wires":[["fdf8a1d049967a2d"]]},{"id":"fdf8a1d049967a2d","type":"debug","z":"54efb553244c241f","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1800,"wires":[]},{"id":"82d1b19984d0c573","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Intrusion\",\"Alarm\",\"Input\"]","payloadType":"json","x":270,"y":1900,"wires":[["782adf6649f8460d"]]},{"id":"782adf6649f8460d","type":"switch","z":"54efb553244c241f","name":"Alarmmmm","property":"payload[1]","propertyType":"msg","rules":[{"t":"eq","v":"Alarmmmm","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":430,"y":1900,"wires":[["a73f5ba2dee9358c"]]},{"id":"a73f5ba2dee9358c","type":"debug","z":"54efb553244c241f","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1900,"wires":[]}]

maybe your array is not an array but a string ?

2 Likes

image

1 Like

Are you sure the input is an arrray, not a string?

When I inject that as an array, the switch node recognises msg.payload[1] == "Alarm"

Edit: SNAP!

1 Like

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