`SWITCH` node question about options

This is about the SWITCH node and it's options.

Ok, second attempt and I hope this effort is better than my other question I asked poorly.

Say I have a message that is an object.

eg:

msg = {"payload":"this is the payload","important_piece":"this needs to be parsed"};

I hope that is correct for structure as I am flying on fumes just now.

I need to allows the message to pass ONLY if the important_piece is set/there/valid.

There are times when it is just msg.payload and nothing else. So I do NOT want this message to go through.

So that is where the switch node comes into play.

But what do I put in it?

is not null
or
is not empty

Sorry, it is an elephant.
But I am having trouble getting my head around it because of my other post and the solution used there to get things to work.

Example flow:

[{"id":"bf7938a4d1c0b9fc","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload","tot":"str"},{"t":"set","p":"important_piece","pt":"msg","to":"This needs to be parsed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1900,"y":510,"wires":[["239df70d331c7a67","6051957128a32947"]]},{"id":"43b0a867366e9201","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1740,"y":510,"wires":[["bf7938a4d1c0b9fc"]]},{"id":"239df70d331c7a67","type":"switch","z":"0918ee609bf69fc7","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":2100,"y":530,"wires":[["716870566126c0ad"]]},{"id":"6051957128a32947","type":"switch","z":"0918ee609bf69fc7","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":2100,"y":490,"wires":[["eaf6e8ee778738c7"]]},{"id":"f7d96b70408812c1","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1900,"y":550,"wires":[["239df70d331c7a67","6051957128a32947"]]},{"id":"716870566126c0ad","type":"debug","z":"0918ee609bf69fc7","name":"debug 65","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2320,"y":530,"wires":[]},{"id":"eaf6e8ee778738c7","type":"debug","z":"0918ee609bf69fc7","name":"debug 66","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2320,"y":490,"wires":[]},{"id":"24ea464f7f379813","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1740,"y":550,"wires":[["f7d96b70408812c1"]]}]

try usingis null

is null would pass it if it was not set.

I want to pass it if it IS set.

So is not null would be better - yes? (As I used it)

But could you - please - help me get my head around the difference between the two options I used in the example flow?
between null and empty
There is also set and not set...... confusion awry.

Maybe to better explain here is a better laid out flow.

[{"id":"43b0a867366e9201","type":"inject","z":"0918ee609bf69fc7","name":"Wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1730,"y":510,"wires":[["bf7938a4d1c0b9fc"]]},{"id":"24ea464f7f379813","type":"inject","z":"0918ee609bf69fc7","name":"Not wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1730,"y":550,"wires":[["f7d96b70408812c1"]]},{"id":"716870566126c0ad","type":"debug","z":"0918ee609bf69fc7","name":"debug 65","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2320,"y":530,"wires":[]},{"id":"bf7938a4d1c0b9fc","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload I want to get through","tot":"str"},{"t":"set","p":"important_piece","pt":"msg","to":"This needs to be parsed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1900,"y":510,"wires":[["239df70d331c7a67","6051957128a32947"]]},{"id":"f7d96b70408812c1","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload.  It isn't supposed to get through.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1900,"y":550,"wires":[["239df70d331c7a67","6051957128a32947"]]},{"id":"239df70d331c7a67","type":"switch","z":"0918ee609bf69fc7","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":2100,"y":530,"wires":[["716870566126c0ad"]]},{"id":"6051957128a32947","type":"switch","z":"0918ee609bf69fc7","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":2100,"y":490,"wires":[["eaf6e8ee778738c7"]]},{"id":"eaf6e8ee778738c7","type":"debug","z":"0918ee609bf69fc7","name":"debug 66","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2320,"y":490,"wires":[]}]

Andrew, try this

[{"id":"43b0a867366e9201","type":"inject","z":"ed68efd020b754f0","name":"Wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":130,"y":140,"wires":[["bf7938a4d1c0b9fc"]]},{"id":"24ea464f7f379813","type":"inject","z":"ed68efd020b754f0","name":"Not wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":150,"y":300,"wires":[["f7d96b70408812c1"]]},{"id":"bf7938a4d1c0b9fc","type":"change","z":"ed68efd020b754f0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload I want to get through","tot":"str"},{"t":"set","p":"important_piece","pt":"msg","to":"This needs to be parsed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":300,"y":140,"wires":[["6051957128a32947"]]},{"id":"f7d96b70408812c1","type":"change","z":"ed68efd020b754f0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload.  It isn't supposed to get through.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":340,"y":300,"wires":[["6051957128a32947"]]},{"id":"6051957128a32947","type":"switch","z":"ed68efd020b754f0","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"null"},{"t":"nnull"}],"checkall":"false","repair":false,"outputs":2,"_mcu":{"mcu":false},"x":530,"y":200,"wires":[["a76f6126e533c8d8"],["eaf6e8ee778738c7"]]},{"id":"eaf6e8ee778738c7","type":"debug","z":"ed68efd020b754f0","name":"wanted","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","_mcu":{"mcu":false},"x":720,"y":220,"wires":[]},{"id":"a76f6126e533c8d8","type":"debug","z":"ed68efd020b754f0","d":true,"name":"not wanted","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","_mcu":{"mcu":false},"x":730,"y":160,"wires":[]}]

So what happens is when the msg enters the switch it looks to see if that object is null. If it I it goes out the top output but if it is not nulll (i.e. it exists) it goes out the second output of the switch.

As you will see when you try it, the wanted msg will pass thru to the debug while the not wanted will not.

There still remains the question as to exactly what empty means. I am not certain of that myself.

1 Like

Again, thanks.

But as @Colin also asked:
I am still not understanding what the difference between:
is not null
and
is not empty
The HELP doesn't really cover it.

6 Likes

I think you have posted that before.

Sorry I forgot it.

Top marks.

So how would they work in my example?
Sorry for asking the dumb question.
I need to get something cleared in my head.
Until I can see this I am kinda stuck.

Is 0 empty or not empty?

I am rather surprised by the answer

image

The switch is set to check all the rules.

[{"id":"06b2f69ed137e59f","type":"switch","z":"dd2ba972fa7bbf9b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"empty"},{"t":"nempty"},{"t":"null"},{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":5,"x":330,"y":920,"wires":[["cd6aaa98e70ea381"],["4597ec98110d8208"],["789b9d5cf13204ee"],["519c7f4159739ac6"],["c8df47687c6fe75c"]]},{"id":"cd6aaa98e70ea381","type":"debug","z":"dd2ba972fa7bbf9b","name":"Empty","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":840,"wires":[]},{"id":"c8df47687c6fe75c","type":"debug","z":"dd2ba972fa7bbf9b","name":"Otherwise","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1020,"wires":[]},{"id":"34e02a3186656fc4","type":"inject","z":"dd2ba972fa7bbf9b","name":"No payload","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":900,"wires":[["06b2f69ed137e59f"]]},{"id":"0ad1b3ccc517d2cf","type":"inject","z":"dd2ba972fa7bbf9b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":110,"y":940,"wires":[["06b2f69ed137e59f"]]},{"id":"1f6450ae67933b8d","type":"inject","z":"dd2ba972fa7bbf9b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":110,"y":980,"wires":[["06b2f69ed137e59f"]]},{"id":"789b9d5cf13204ee","type":"debug","z":"dd2ba972fa7bbf9b","name":"Null","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":920,"wires":[]},{"id":"e49d2c09f9e6cea5","type":"inject","z":"dd2ba972fa7bbf9b","name":"Empty string","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":130,"y":1020,"wires":[["06b2f69ed137e59f"]]},{"id":"4597ec98110d8208","type":"debug","z":"dd2ba972fa7bbf9b","name":"NOT Empty","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":880,"wires":[]},{"id":"519c7f4159739ac6","type":"debug","z":"dd2ba972fa7bbf9b","name":"NOT null","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":960,"wires":[]},{"id":"b19cb444e3b4e78c","type":"inject","z":"dd2ba972fa7bbf9b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":860,"wires":[["06b2f69ed137e59f"]]}]
1 Like

That is interesting.

When you inject the [] or {} you get TWO outputs.

How does that happen?
Does the switch node create a new message?

As I said:

So [] is both empty and not null.

Sorry.

My parsing system is not working just now.

More interesting is the fact there is only one output for 0

I am now missing something else.

Why?

To me that is understandable.

It is not empty AND it is not null ..... (AND otherwise?)

Oh, so they should get two or more outputs also - yes?

Interesting.

@Trying_to_learn
you could test for undefined, or use JSONata $exists()

[{"id":"43b0a867366e9201","type":"inject","z":"65617ffeb779f51c","name":"Wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":460,"wires":[["bf7938a4d1c0b9fc"]]},{"id":"bf7938a4d1c0b9fc","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload I want to get through","tot":"str"},{"t":"set","p":"important_piece","pt":"msg","to":"This needs to be parsed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":460,"wires":[["6051957128a32947","7879b086fb12a758"]]},{"id":"6051957128a32947","type":"switch","z":"65617ffeb779f51c","name":"","property":"important_piece","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":440,"wires":[[],["eaf6e8ee778738c7"]]},{"id":"7879b086fb12a758","type":"switch","z":"65617ffeb779f51c","name":"","property":"$exists(important_piece)","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":540,"wires":[["eaf6e8ee778738c7"]]},{"id":"f7d96b70408812c1","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"This is the payload.  It isn't supposed to get through.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":500,"wires":[["6051957128a32947","7879b086fb12a758"]]},{"id":"eaf6e8ee778738c7","type":"debug","z":"65617ffeb779f51c","name":"debug 66","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":440,"wires":[]},{"id":"24ea464f7f379813","type":"inject","z":"65617ffeb779f51c","name":"Not wanted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":500,"wires":[["f7d96b70408812c1"]]}]

Here is a write up that may (?) help you understand

The 'check all rules' doesn't apply to Otherwise, it would be silly if it did.

The anomaly with zero is that it does not match is empty, but it doesn't match is not empty either. So zero is not 'empty' but it is also not 'not empty', which doesn't make sense to me. A sort of anti Schrodinger effect. That sounds like a bug to me.
In fact it isn't only zero this applies to, any number gives the same result.

2 Likes

From help

The is empty and is not empty rules can be used to test the length of Strings, Arrays and Buffers, or the number of properties an Object has

"0" works as expected.

Ah yes, RTFM.
Though actually the FM is not complete, as it goes on to say
"Neither rule will pass if the property being tested has a boolean, null or undefined value."
That list should include numeric.