switch node behaviour is confusing #108

Closed
andysc opened this Issue Dec 6, 2013 · 4 comments

Projects

None yet

2 participants

@andysc
andysc commented Dec 6, 2013

it's hard to tell if the contents of msg.payload are actually a number, or a string that looks like a number.
So if you then do numeric comparisons, it doesn't always do the right thing.

Could it do a "Number()" on the input if you use range comparisons?

(not sure what that does if it's really a string and you want to compare strings).
I was comparing what I thought was a number with other numbers in ranges, and it wasn't firing to the pins I was expecting.

@knolleary
Member

Can you provide a specific failing test case? I'm struggling to get it to fail.

@andysc
andysc commented Dec 6, 2013

yes... the flow below compares for <5 or >=5... if you inject 2 it's right (<5), 7 it's right (>=5), but if you inject 200 it does string comparison and says it's <5, which it clearly isn't for numbers.

If you put a node in which does msg.payload = Number(msg.payload), it works as expected.

[{"id":"3224664a.18f16a","type":"inject","name":"","topic":"","payload":"2","repeat":"","crontab":"","once":false,"x":175,"y":921,"z":"d5088c3e.afc89","wires":[["20990256.e20b56"]]},{"id":"20990256.e20b56","type":"switch","name":"","property":"payload","rules":[{"t":"lt","v":"5"},{"t":"gte","v":"5"}],"outputs":2,"x":433,"y":926,"z":"d5088c3e.afc89","wires":[["a2dc96bf.31092"],["3191946d.d1dc9c"]]},{"id":"8b58e3bb.9b6d7","type":"inject","name":"","topic":"","payload":"20","repeat":"","crontab":"","once":false,"x":182,"y":1010,"z":"d5088c3e.afc89","wires":[["20990256.e20b56"]]},{"id":"ad26eabd.0af2c","type":"inject","name":"","topic":"","payload":"7","repeat":"","crontab":"","once":false,"x":180,"y":969,"z":"d5088c3e.afc89","wires":[["20990256.e20b56"]]},{"id":"a2dc96bf.31092","type":"debug","name":"< 5","active":true,"complete":"false","x":639,"y":906,"z":"d5088c3e.afc89","wires":[]},{"id":"3191946d.d1dc9c","type":"debug","name":">= 5","active":true,"complete":"false","x":640,"y":947,"z":"d5088c3e.afc89","wires":[]}]

@knolleary
Member

Hmm ok.

I was going to suggest we could check the type of the property you are comparing against; if it is a Number, then coerce the value you specify in the Switch node to a Number and do the comparison.

The problem comes with the Inject node; it injects a String regardless of what you type in there - we don't know you want it to be a number type.

Need to think the best way to infer what you want. I think we should look at the value ('v') you specify for comparison (the 5 in your example) and decide if it warrants numerical comparison.

If isNaN(Number(v)), then v doesn't represent a number so do string comparison, otherwise do numeric comparison.

@andysc
andysc commented Dec 6, 2013

yeah, that was just an example - my value actually came from a serial port ("1 2 3"), then a function node with a split() in it, to give me the "3", then passed into the switch node. It just happened to also work/break with an inject node to give you an easy example.

I think your idea of looking at the value it's comparing with is a good one.

@knolleary knolleary closed this in d2ce6af Dec 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment