How to build an Jsonata for this switch node?

Hi,
i am new on NR and building up the first flows. Every thing is working, but some points can be made better, I think.

One question to start. I have an switch node to select the outputs based on the topics. For now I have 6 outputs, but would like to have the 3+4 and 5+6 on the same output. And its not enough to have an filter only on the part "cool_" or "heat_", this will match with to much topic.
Is this a point to use Jsonata?

I think a regular expression test would do it, as you can match on cool_ followed by target or diff. I am not going to tell you how to do it though, as I don't use regex much and would have to look it up and then play with it till it worked. So I will leave that for you to do yourself :slight_smile:

Actually, if you order them correctly, which you already have, and select that it stops on the first match (the drop down at the bottom), then you just need to check for Contains heat_ and Contains cool_ as the other options will already have been ruled out. Unless there are other matching strings that you want to ignore completely that is.

I connect switch outputs together when I want them both to be treated as being the same

image

image

note the stopping after first match setting

1 Like

That's what I am using now. But I hope to find a solution for an thing like "if 'play' or 'pause' then use output" -> 3

Hm, thanks. But to select that it stops on the first match is not an option for my flow.

Why not?

Each message can only have one value with one msg.topic, so why do you need to carry on looking for matches?

I don't understand, how can it match more than one?

As @Colin said, regex is your "friend". Kind of... as it is not the friendliest stuff to use.
However if you want to send 3 and 4 to the same output, you can do:

If you want to experiment with regex, you can go to https://regex101.com/ it will help you to build the expression.

^ is beginning of string
$ is end of string
| is "or"

1 Like

Thanks, this is the solution for me. Testet and it works. Have to spend some time on regex.

You have not told us why the example selecting Stop at first match does not work? I suspect you may misunderstand what that does.

Right, my understanding for now is not deep enought. If you like, please give me some hints.

Stop at first match means that it tests the first condition, if that is satisfied then it sends the message to output 1 and does nothing else. If the first test does not match then it tries the second test, and if that matches it sends the message to output 2 and does nothing else, and so on for each test. So in your original configuration if you selected this option then if it gets as far as test 5 then you would know that it was not cool_switch_state so need only test for Contains cool_ to identify both cool_target and cool_diff.
If you select Check all rules then it tests all the rules every time and will send the message to all outputs that match, not just the first one, which is obviously a waste of time in your original configuration as only one can match each time.

I understand. But There are more topics not shown in my first post also starting with cool_ or heat_ to implement later. So I have to take care on the order in this switch.
But vor now its right to use the first match only.

1 Like

OK, in that case the regex method may be safer and easier to follow when you come back to look at it in a years time :slight_smile:
Though still it should be first match as that saves it wasting time when a match has been found.
I think first match would be better as the default, but that is another matter.

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