Grief with the switch node

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

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

It is working for me:

[{"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"]]}]

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

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

The flow with updated switch node:

[{"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"]]}]

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

1 Like

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