Sorting positive and negative digits

Hi,
i have mqtt message payload "-1" and "1", i try switch with two output - not working. Both digits going on one or other output... no metter use "==" ">" "<" or "otherwise" etc. if use other digits - positive all work fine. i cant change mqtt payload digits, its from rotary encoder array on esp8266, mqtt payload format is "10:2:-1" changing last part - "-1" or "1" . How i can sort on different outputs "1" and "-1"?

These are strings, not numbers. You would need to ether send them as numbers or convert them to numbers before doing any numerical comparisons

You can convert the property in the switch node using a JSONata expression.
e.g.

[{"id":"b15ec1c0.45137","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"str","x":150,"y":3980,"wires":[["62b7b58.d85804c"]]},{"id":"62b7b58.d85804c","type":"switch","z":"c74669a0.6a34f8","name":"","property":"$number(payload)","propertyType":"jsonata","rules":[{"t":"lt","v":"0","vt":"num"},{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":330,"y":4020,"wires":[["b2f576f7.07073"],["8e0f836.ea41a8"],["4855cece.93e67"]]},{"id":"a57c140b.edfb1","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":150,"y":4060,"wires":[["62b7b58.d85804c"]]},{"id":"4a4651cd.ab011","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":150,"y":4020,"wires":[["62b7b58.d85804c"]]},{"id":"4855cece.93e67","type":"debug","z":"c74669a0.6a34f8","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":4060,"wires":[]},{"id":"b2f576f7.07073","type":"debug","z":"c74669a0.6a34f8","name":"-1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":3980,"wires":[]},{"id":"8e0f836.ea41a8","type":"debug","z":"c74669a0.6a34f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":4020,"wires":[]}]

Work great, just im think as Gunner say, first - string need convert to number. Im split mqtt payload "3:2:-1"

var str = msg.payload;
var parts = str.split(":");
msg.payload = {
  dir: parseInt(parts[2]),
}
return msg;

and get in output only last part "1 or -1", after conecting switch with JSONata expression i got :
"Invalid JSONata expression: Argument 1 of function "number" does not match function signature".
This is what i think, string convert to number and only than input to switch?
P.S. Sorry if my question seems stupid, im try anderstand node red just short time.

As payload is now a object and msg.payload.dir is a number, the switch property would be msg.payload.dir. If it is still set to $number(payload) then an error will be shown as payload is an object, and $number() accepts strings or numbers.

... not great - but if they are strings then you could just use a switch node with "contains" set to look for -

Perfect as you say. Set in switch property "$number(payload.dir)" and all work now. If i get time, try read some manual of node red. Thanks.

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