Switch not switching no matter what I try (newbie)

Hi I am trying to detect a particular push button switch being pushed, the payload contains multiple objects. I have used the change node to make a topic sensors/status/index and trying to switch on its value but no matter what I try nothing comes out of the switch.
The code is:

[{"id":"7a8ad30eecd59839","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"5cf054507018f9bd","type":"mqtt out","z":"7a8ad30eecd59839","name":"","topic":"Light0/cmnd/5a205b","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"881ee26fa89baac1","x":420,"y":380,"wires":[]},{"id":"5c6097c9c2c49ef3","type":"inject","z":"7a8ad30eecd59839","name":"Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Light0\\cmnd\\5a205b","payload":" {    \"outputs\": [     { \"index\": 1, \"command\": \"off\" }  ] }","payloadType":"str","x":70,"y":380,"wires":[["5cf054507018f9bd"]]},{"id":"5729648dc441074f","type":"mqtt in","z":"7a8ad30eecd59839","name":"","topic":"Button/stat/bd0577","qos":"2","datatype":"auto","broker":"881ee26fa89baac1","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":60,"wires":[["ba0fe1aab6b1fd5d"]]},{"id":"ba0fe1aab6b1fd5d","type":"json","z":"7a8ad30eecd59839","name":"","property":"payload","action":"","pretty":false,"x":310,"y":100,"wires":[["7e4a82028c0799db"]]},{"id":"3f4d99a9d75fb8f1","type":"debug","z":"7a8ad30eecd59839","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":240,"wires":[]},{"id":"520856cdb58f1b64","type":"inject","z":"7a8ad30eecd59839","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Light0\\cmnd\\5a205b","payload":"","payloadType":"str","x":70,"y":300,"wires":[["7ae8186e29ea686f"]]},{"id":"1fe8bb9a62d32140","type":"sunrise","z":"7a8ad30eecd59839","name":"Warragul Sun Position","lat":"-38","lon":"145","start":"sunrise","end":"sunset","soff":0,"eoff":0,"x":140,"y":500,"wires":[[],[]]},{"id":"592d9e91fa7eeed5","type":"mqtt in","z":"7a8ad30eecd59839","name":"","topic":"Light0/cmnd/5a205b","qos":"2","datatype":"auto","broker":"881ee26fa89baac1","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":440,"wires":[["ae023622b706b61c"]]},{"id":"ae023622b706b61c","type":"debug","z":"7a8ad30eecd59839","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":440,"wires":[]},{"id":"7e4a82028c0799db","type":"split","z":"7a8ad30eecd59839","name":"","splt":"","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"key","x":430,"y":100,"wires":[["e8c69bcf951ad7b5"]]},{"id":"e8c69bcf951ad7b5","type":"change","z":"7a8ad30eecd59839","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"\"sensors/status/\" & key","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":100,"wires":[["2a05dd883c5e1160","91b00d18bed26342"]]},{"id":"0f8e85923da5fd55","type":"switch","z":"7a8ad30eecd59839","name":"sensors/status/index","property":"sensors/status/index","propertyType":"msg","rules":[{"t":"eq","v":"113","vt":"num"},{"t":"eq","v":"112","vt":"num"},{"t":"eq","v":"114","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":320,"y":240,"wires":[["7ae8186e29ea686f","3f4d99a9d75fb8f1"],[],[]]},{"id":"7ae8186e29ea686f","type":"change","z":"7a8ad30eecd59839","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":" {    \"outputs\": [     { \"index\": 1, \"command\": \"on\" }  ] }","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":300,"wires":[["5cf054507018f9bd"]]},{"id":"5b56e9ad21dbe562","type":"inject","z":"7a8ad30eecd59839","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Button/stat/bd0577","payload":"{\"port\":29,\"channel\":1,\"index\":113,\"type\":\"switch\",\"event\":\"on\"}","payloadType":"str","x":90,"y":120,"wires":[["ba0fe1aab6b1fd5d"]]},{"id":"91b00d18bed26342","type":"debug","z":"7a8ad30eecd59839","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":200,"wires":[]},{"id":"2a05dd883c5e1160","type":"change","z":"7a8ad30eecd59839","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"sensors/status/index","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":120,"y":260,"wires":[["0f8e85923da5fd55"]]},{"id":"881ee26fa89baac1","type":"mqtt-broker","name":"","broker":"localhost","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":{},"sessionExpiry":""}]

Thanks in advance.
Rustyv

Put a debug node (set to display the complete msg object) on the output of the change node labeled ‘set msg.payload’ and run the flow.

Now look at the debug output and show me the contents of the variable you are testing is in the debug msg.

26/03/2022, 21:49:35node: 50b44f415edb8a3dsensors/status/index : msg : Object

object

topic: "sensors/status/index"

parts: object

id: "bb853845adc37247"

type: "object"

key: "index"

index: 2

count: 5

key: "index"

_msgid: "0008f52fe13283d4"

There was no mention of it's payload. I originally didn't have that change and was using the set msg.topic which has the payload of 113 but wouldn't operate the switch node, I don't know why.

Did that msg come from the inject node or the MQTT-in node?

inject node

Ok lets look at what you have. Coming out of the inject node you have this:
Screen Shot 2022-03-26 at 10.09.50 AM
and you send that thru a json node (note: you can set the mqtt-in node 'Output' option to 'a parsed JSON object' so you wouldn't need the JSON node - except for the test inject node) and then a split node which results in five msgs in this format:
Screen Shot 2022-03-26 at 10.15.01 AM

next the msg goes into a change node
Screen Shot 2022-03-26 at 10.18.56 AM
and comes out looking like this:
Screen Shot 2022-03-26 at 10.18.28 AM

And you send it into a second change node (do you realize you could combine the two change nodes?)
Screen Shot 2022-03-26 at 10.22.40 AM
but if you look at the previous debug output there is no msg.sensors/status/index so msg.payload will not be changed.

Next the msg goes to the switch node:
Screen Shot 2022-03-26 at 10.30.43 AM
where you are testing msg.sensors/status/index but it does not exist so nothing will come out of the switch node.

If you had added an 'otherwise' condition, you would have gotten output from it.

I realise I could combine the 2 change nodes. I was "clutching at straws" hoping that it would make the message "focused " ( I did say I was new to node red ). if removing the second change node would help then remove it, but I still can't get the sensors/status/index to operate the switch node. Any idea how to do it?

What is sensor/status/index suppose to be? A number? A string”?

You haven’t defined it in the message so you can not test it.

Are you trying to test the ‘index’ value?
If so you don’t need the split node just test msg.payload.index for the value.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

That makes things a lot clearer thank you

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