# Help with switch node

**URL:** <https://discourse.nodered.org/t/help-with-switch-node/74096>\
**Category:** General\
**Created:** [23 January 2023 21:26 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096 "2023-01-23T21:26:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hattemp](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@hattemp](https://discourse.nodered.org/u/hattemp)\
**Post date:** [23 January 2023 21:26 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/1 "2023-01-23T21:26:34Z")

</div>

I cannot get a switch node to work with my payload.  
have ben trying al sort of things but it will not work.  
I have a reterminal with 4 buttons. it will give payload on the press and the release.  
I only want to do something with the pressed payload of the 4 buttons.  
so F1 does A, F2 does B, F3 does C and 0 does 4

all I got was a reaction from both the payloads of a button.  
so if I press F1 it triggers 2 times.  
on the press and on the release.  
can anybody help me to get the switch node working?

I'm getting this payload from the 4 buttons (F1, F2, F3 and 0:

msg.payload : Object

{ key: "F1", status: "pressed" }

msg.payload : Object

{ key: "F1", status: "released" }

msg.payload : Object

{ key: "F2", status: "pressed" }

msg.payload : Object

{ key: "F2", status: "released" }

msg.payload : Object

{ key: "F3", status: "pressed" }

msg.payload : Object

{ key: "F3", status: "released" }

msg.payload : Object

{ key: "O", status: "pressed" }

msg.payload : Object

{ key: "O", status: "released" }

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2023 21:53 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/2 "2023-01-23T21:53:16Z")

</div>

Welcome to the forum @hattemp

Do you mean that you are trying to configure a Switch node that only passes messages where msg.payload.status is pressed?

If so then configure the switch like this

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86b0964f50fe97cfaafe680a934acd392b08ecbe.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 January 2023 21:55 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/3 "2023-01-23T21:55:10Z")

</div>

I suspect the problem is the `payload` going into the `switch` node is an `object`.  
I'm not sure that is supported.

You could try making ~~a~~ TWO new part(s) of the `msg.` if that structure is needed else where down the flow.

So it could be something like:

```auto
msg = {
    "payload":{"key":key,"stats":status of key},
    "key":key,
    "stats":status of key}

```

I know it is painful, but.....

Then you edit the `switch` node and change `payload` to either `key` or `status` depending on which you want to parse.

But.....  
I'm guessing you will have to use two `switch` nodes.  
One to determine the `key` and one to determine the `status`.  
Actually more than 2, but it is implied to be two LAYERS.  
eg:

```auto
[{"id":"c729e8d30418e78a","type":"switch","z":"d83300ecf7ff5383","name":"Status","property":"status","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"relaesed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":210,"y":2160,"wires":[["04c79baf1ff7bbad"],["c0e008e714aa0ea0"]]},{"id":"04c79baf1ff7bbad","type":"switch","z":"d83300ecf7ff5383","name":"Pressed","property":"key","propertyType":"msg","rules":[{"t":"eq","v":"F1","vt":"str"},{"t":"eq","v":"F2","vt":"str"},{"t":"eq","v":"F3","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":410,"y":2080,"wires":[["6b0d19258d6fcf1e"],["48b6aab087dca819"],["dbc89f74b528d39a"]]},{"id":"c0e008e714aa0ea0","type":"switch","z":"d83300ecf7ff5383","name":"Released","property":"key","propertyType":"msg","rules":[{"t":"eq","v":"F1","vt":"str"},{"t":"eq","v":"F2","vt":"str"},{"t":"eq","v":"F3","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":410,"y":2240,"wires":[["965719ac6288f021"],["d135f2c2e8f78074"],["a6d204638af7e3c8"]]},{"id":"6b0d19258d6fcf1e","type":"debug","z":"d83300ecf7ff5383","name":"F1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2040,"wires":[]},{"id":"48b6aab087dca819","type":"debug","z":"d83300ecf7ff5383","name":"F2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2080,"wires":[]},{"id":"dbc89f74b528d39a","type":"debug","z":"d83300ecf7ff5383","name":"F3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2120,"wires":[]},{"id":"965719ac6288f021","type":"debug","z":"d83300ecf7ff5383","name":"F1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2200,"wires":[]},{"id":"d135f2c2e8f78074","type":"debug","z":"d83300ecf7ff5383","name":"F2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2240,"wires":[]},{"id":"a6d204638af7e3c8","type":"debug","z":"d83300ecf7ff5383","name":"F3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2280,"wires":[]}]

```

![Screenshot from 2023-01-24 08-50-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c8baac33f172c68219c4e03e8aadb72679648da.png)

Or:

```auto
[{"id":"c729e8d30418e78a","type":"switch","z":"d83300ecf7ff5383","name":"Key","property":"key","propertyType":"msg","rules":[{"t":"eq","v":"F1","vt":"str"},{"t":"eq","v":"F2","vt":"str"},{"t":"eq","v":"F3","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":210,"y":2160,"wires":[["04c79baf1ff7bbad"],["c601f7f4b83f4121"],["f859749fba212f3f"]]},{"id":"04c79baf1ff7bbad","type":"switch","z":"d83300ecf7ff5383","name":"Status","property":"status","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"repleased","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":400,"y":2080,"wires":[["6b0d19258d6fcf1e"],["965719ac6288f021"]]},{"id":"c601f7f4b83f4121","type":"switch","z":"d83300ecf7ff5383","name":"Status","property":"status","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"repleased","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":400,"y":2160,"wires":[["48b6aab087dca819"],["d135f2c2e8f78074"]]},{"id":"f859749fba212f3f","type":"switch","z":"d83300ecf7ff5383","name":"Status","property":"status","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"repleased","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":2260,"wires":[["dbc89f74b528d39a"],["a6d204638af7e3c8"]]},{"id":"6b0d19258d6fcf1e","type":"debug","z":"d83300ecf7ff5383","name":"F1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2040,"wires":[]},{"id":"965719ac6288f021","type":"debug","z":"d83300ecf7ff5383","name":"F1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2100,"wires":[]},{"id":"48b6aab087dca819","type":"debug","z":"d83300ecf7ff5383","name":"F2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2140,"wires":[]},{"id":"d135f2c2e8f78074","type":"debug","z":"d83300ecf7ff5383","name":"F2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2190,"wires":[]},{"id":"dbc89f74b528d39a","type":"debug","z":"d83300ecf7ff5383","name":"F3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2230,"wires":[]},{"id":"a6d204638af7e3c8","type":"debug","z":"d83300ecf7ff5383","name":"F3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":2280,"wires":[]}]

```

![Screenshot from 2023-01-24 08-54-11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/4204f10026dcc730d8cd84174479ad74bd7a12cb.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 January 2023 22:37 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/4 "2023-01-23T22:37:50Z")

</div>

> [@Trying\_to\_learn](#):
>
> I suspect the problem is the `payload` going into the `switch` node is an `object`.  
> I'm not sure that is supported.

```auto
[{"id":"2e165ac1725a1ff9","type":"inject","z":"c51f51da7ee02d68","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"switch\":\"pressed\"}","payloadType":"json","x":390,"y":1820,"wires":[["9b6f1fabd3c68a4c"]]},{"id":"9b6f1fabd3c68a4c","type":"switch","z":"c51f51da7ee02d68","name":"","property":"payload.switch","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":570,"y":1840,"wires":[["122134804a8b2c45"],["bc053bcf8c4d3521"]]},{"id":"122134804a8b2c45","type":"debug","z":"c51f51da7ee02d68","name":"debug 54","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1820,"wires":[]},{"id":"bc053bcf8c4d3521","type":"debug","z":"c51f51da7ee02d68","name":"debug 55","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1860,"wires":[]},{"id":"d007c27673d241d1","type":"inject","z":"c51f51da7ee02d68","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"switch\":\"caressed\"}","payloadType":"json","x":390,"y":1860,"wires":[["9b6f1fabd3c68a4c"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22fcfc73575e861e1cfb8df627c5f6906c82f2bf.png)

🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 January 2023 22:47 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/5 "2023-01-23T22:47:37Z")

</div>

I stand corrected. 😉

---

<div class="post-metadata">

**Author:** ![hattemp](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@hattemp](https://discourse.nodered.org/u/hattemp)\
**Post date:** [24 January 2023 18:28 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/6 "2023-01-24T18:28:00Z")

</div>

Yes now it works with the combined flows from @Trying_to_learn & @TotallyInformation  
thank you all for your help and suggestions

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/cac97e21791ae91dc7afae0eaceb98eaf7d17e97.png)

---

<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:** [25 March 2023 18:28 UTC](https://discourse.nodered.org/t/help-with-switch-node/74096/7 "2023-03-25T18:28:54Z")

</div>

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