# Grief with the switch node

**URL:** <https://discourse.nodered.org/t/grief-with-the-switch-node/33442>\
**Category:** General\
**Created:** [28 September 2020 11:44 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442 "2020-09-28T11:44:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![little\_rich](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@little\_rich](https://discourse.nodered.org/u/little_rich)\
**Post date:** [28 September 2020 11:44 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/1 "2020-09-28T11:44:45Z")

</div>

Hi,  
I have some data feeding into the switch node which is in this format  
msg.payload.rows[0].current\_price  
msg.payload.rows[0].ref\_price  
msg.payload.rows[0].quantity

I am trying to use the switch command to filter based on whether the current price is \> the ref\_price or not.  
A pass is meant to go out the top output and I have the fail as an 'otherwise' to go out the other output.

From what I can tell the top parameter kind of has to be the msg.payload format

In the next part of the switch I have chosen the \> symbol  
Regarding the parameter to test against, it doesn't seem to matter what I choose the result is always the same. Pass or Fail, they both go out the top or the bottom output.  
(in another use of the switch, I test for null or !=null and it works perfectly)

msg.payload.rows[0].old\_price  
msg.$  
msg.0-9 (looking for a numeric I assume)  
msg.abc looking for text but I thought maybe the numbers were seen as text  
I have even tried {new\_price} type commands.

Also have tried the 'stopping on first match' selector.

Can someone suggest what 'should be the correct method' as I am just trying all combinations and getting nowhere.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4fd39a13fb0977cc45143dd7faf4a125969139ce.png)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [28 September 2020 11:56 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/2 "2020-09-28T11:56:06Z")

</div>

Can you share the input of the switch node using a debug node ?

It is working for me:

```auto
[{"id":"daede0a2.fd909","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"5e7ed0c8.a823b","type":"inject","z":"daede0a2.fd909","name":"is greater","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"rows\":[{\"current_price\":100,\"ref_price\":80}]}","payloadType":"json","x":180,"y":100,"wires":[["6c2fdb3a.35bda4"]]},{"id":"6c2fdb3a.35bda4","type":"switch","z":"daede0a2.fd909","name":"current > ref","property":"payload.rows[0].current_price","propertyType":"msg","rules":[{"t":"gt","v":"payload.rows[0].ref_price","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":460,"y":140,"wires":[["593396f8.b68408"],["3e95ade6.793162"]]},{"id":"593396f8.b68408","type":"debug","z":"daede0a2.fd909","name":">","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":80,"wires":[]},{"id":"3e95ade6.793162","type":"debug","z":"daede0a2.fd909","name":"otherwise","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":160,"wires":[]},{"id":"869d1641.30c798","type":"inject","z":"daede0a2.fd909","name":"is smaller","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"rows\":[{\"current_price\":80,\"ref_price\":100}]}","payloadType":"json","x":180,"y":140,"wires":[["6c2fdb3a.35bda4"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b50f7f135a2c14fca79454e4d67b9b18cb0b3b9.png)

---

<div class="post-metadata">

**Author:** ![little\_rich](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@little\_rich](https://discourse.nodered.org/u/little_rich)\
**Post date:** [28 September 2020 12:02 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/3 "2020-09-28T12:02:11Z")

</div>

Thank you for having a look.  
Does this help?  
It is coming from a database query further back. But I was convinced by looking at the paths to the data that it should work.

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

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [28 September 2020 12:11 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/4 "2020-09-28T12:11:00Z")

</div>

You are comparing strings not numbers.  
So, you should convert string to number.  
This can also be done in switch node using jsonata expression:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b2ec14402c938bde3d10e922f091d3927486772.png)

The flow with updated switch node:

```auto
[{"id":"daede0a2.fd909","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"5e7ed0c8.a823b","type":"inject","z":"daede0a2.fd909","name":"is greater","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"rows\":[{\"current_price\":\"100\",\"ref_price\":\"80\"}]}","payloadType":"json","x":180,"y":100,"wires":[["6c2fdb3a.35bda4"]]},{"id":"6c2fdb3a.35bda4","type":"switch","z":"daede0a2.fd909","name":"current > ref","property":"$number(payload.rows[0].current_price)","propertyType":"jsonata","rules":[{"t":"gt","v":"$number(payload.rows[0].ref_price)","vt":"jsonata"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":460,"y":140,"wires":[["593396f8.b68408"],["3e95ade6.793162"]]},{"id":"593396f8.b68408","type":"debug","z":"daede0a2.fd909","name":">","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":80,"wires":[]},{"id":"3e95ade6.793162","type":"debug","z":"daede0a2.fd909","name":"otherwise","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":160,"wires":[]},{"id":"869d1641.30c798","type":"inject","z":"daede0a2.fd909","name":"is smaller","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"rows\":[{\"current_price\":\"80\",\"ref_price\":\"100\"}]}","payloadType":"json","x":180,"y":140,"wires":[["6c2fdb3a.35bda4"]]}]

```

---

<div class="post-metadata">

**Author:** ![little\_rich](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@little\_rich](https://discourse.nodered.org/u/little_rich)\
**Post date:** [28 September 2020 12:23 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/5 "2020-09-28T12:23:25Z")

</div>

Oh gosh, thank you so much. I really should have asked on Saturday, I have spent hours on this.  
I really appreciate your support.

---

<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:** [27 November 2020 12:23 UTC](https://discourse.nodered.org/t/grief-with-the-switch-node/33442/6 "2020-11-27T12:23:33Z")

</div>

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