# Switch not switching no matter what I try (newbie)

**URL:** <https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364>\
**Category:** General\
**Created:** [26 March 2022 08:35 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364 "2022-03-26T08:35:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [26 March 2022 08:35 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/1 "2022-03-26T08:35:40Z")

</div>

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:

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 March 2022 08:50 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/2 "2022-03-26T08:50:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [26 March 2022 10:52 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/3 "2022-03-26T10:52:31Z")

</div>

26/03/2022, 21:49:35[node: 50b44f415edb8a3d](http://homeassistant.local:8123/api/hassio_ingress/YEfCOnrdemkT0zD7tldaj4_eH8gBh1fruHva5mmiqHY/#)sensors/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"

---

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [26 March 2022 11:05 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/4 "2022-03-26T11:05:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 March 2022 12:42 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/5 "2022-03-26T12:42:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [26 March 2022 13:59 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/6 "2022-03-26T13:59:28Z")

</div>

inject node

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 March 2022 14:33 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/7 "2022-03-26T14:33:45Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e9d08597434e641ad7dbb71107456e8eacd301b.png)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/220def2c7a9ef98e95248bab45a393812133e605.png)

next the msg goes into a `change` node  
 ![Screen Shot 2022-03-26 at 10.18.56 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c8f6c77903969c12cf2b0ae2f024cfe74a92fbfb.png)  
and comes out looking like this:  
 ![Screen Shot 2022-03-26 at 10.18.28 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/7119e5dc1ac83cc15af935fec74280d637951ca7.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57187e3f7e3d21c484f01ae661f84a6abac45d08.png)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e664b6a6a0156b4f475043d7ad743c714d7edf2d.png)  
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.

---

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [27 March 2022 01:02 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/8 "2022-03-27T01:02:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 March 2022 01:07 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/9 "2022-03-27T01:07:48Z")

</div>

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](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![RustyV](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RustyV](https://discourse.nodered.org/u/RustyV)\
**Post date:** [27 March 2022 02:18 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/10 "2022-03-27T02:18:39Z")

</div>

That makes things a lot clearer thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 May 2022 02:19 UTC](https://discourse.nodered.org/t/switch-not-switching-no-matter-what-i-try-newbie/60364/11 "2022-05-26T02:19:11Z")

</div>

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