Possible with Regex?

Hey guys,

I never used regex expressions and only saw one in a Youtube guide where someone used one. Now I have a smart switch with 6 different states and I'm hoping I can get something done with regex expression inside a Switch Node.

If I would use a function node it would look like this:

var event = msg.payload;

if (event==1002) {
return [msg, null, null, null];
} else if (event==2002) {
return [null, msg, null, null];
} else if (event==1001 || event==1003) {
return [null, null, msg, null];
} else if (event==2001 || event==2003) {
return [null, null, null, msg];
}

I hope there is a solution for the last two if's.
Can someone help me out?

What does the input payload look like ?

only the number 1001 to 1003 or 2001 to 2003 only one of these numbers :slight_smile:

I made my flow complete for my DimmerSwitch with a dimming flow. I only have to decide if the payload is 1002 || 2002 and otherwise. But how can I get an "if statement" into a regex that decides if 1002 or 2002 is the msg.payload.

If you really want to use a regex, then something like ^(1002|2002)$ should do it.

1 Like

Regex or jsonata would work too in a switch node:

1 Like

Mhhh didn't work

that did the job :slight_smile:

It didn't work because you have selected the 'jsonata expression' type, which isn't a regex.

ah lol :smiley:

thank you! did the job as well ofc

I have an other question belonging to this or even jsonata.

I have a msg.payload json object that looks like this:

msg.payload = {status, color, switch, speed, effect}

These objects should be split into 4 or 5 outputs (one for each).
I got a solution that looks like this:

Instead of using 4 or 5 change nodes that are looking like this:
image

And then have an other switch node that checks if the payload isn't null... I thought there would be a nice solution with only 1 switch node that splits the payload depending on the object?

Well you are using the switch node....albeit with 1 output.

You can use the switch node......or better yet, remove the switch nodes and remove the change nodes.
in the dashboard nodes you can use msg.payload.effect, speed, switch etc directly as input.

I don't know exactly what you mean :confused:

I know how to setup a switch node with 4 outputs but I don't know how to let it check if there is speed, switch and so on.

This doesn't work:
image

I have to split them because some of these payloads got numbers and some of these dashboard nodes use numbers as input :slight_smile:

Can you show the payload ?

That's how it looks like if use function node, but I want to avoid it.

Here is an example payload:
image

2 example flows in 1:

[{"id":"39f2942b.6e0054","type":"inject","z":"ac3a9890.7ebed","name":"","topic":"","payload":"{\"speed\":100,\"switch\":true,\"color\":{\"r\":255,\"g\":255,\"b\":255}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":458,"y":528,"wires":[["e88c6dd9.ad2dd","b287ddc8.5a86d8"]]},{"id":"e88c6dd9.ad2dd","type":"function","z":"ac3a9890.7ebed","name":"","func":"m = msg.payload\n\nspeed = {payload:m.speed}\nsw = {payload:m.switch}\ncolor = {payload:m.color}\n\nreturn [speed,sw,color]","outputs":3,"noerr":0,"x":626,"y":600,"wires":[["8fcc88db.bd968"],["5cc4bfc4.cc903"],["c0e85942.2a48e"]]},{"id":"b287ddc8.5a86d8","type":"switch","z":"ac3a9890.7ebed","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"speed","vt":"str"},{"t":"hask","v":"switch","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":626,"y":456,"wires":[["e0230e77.2efa6"],["1a300a87.85e855"]]},{"id":"8fcc88db.bd968","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":552,"wires":[]},{"id":"5cc4bfc4.cc903","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":600,"wires":[]},{"id":"c0e85942.2a48e","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":648,"wires":[]},{"id":"e0230e77.2efa6","type":"change","z":"ac3a9890.7ebed","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.speed","tot":"msg"},{"t":"delete","p":"payload.switch","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":432,"wires":[["247d4b6b.bb9ac4"]]},{"id":"1a300a87.85e855","type":"change","z":"ac3a9890.7ebed","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.switch","tot":"msg"},{"t":"delete","p":"payload.speed","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":480,"wires":[["99a9d3ed.38ad08"]]},{"id":"247d4b6b.bb9ac4","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":982,"y":432,"wires":[]},{"id":"99a9d3ed.38ad08","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":982,"y":480,"wires":[]}]
1 Like

thank you so far, will take a look at this after launch <3 But I think it would be a solution for my complex shit :smiley:

Ok tryed your flow. I forgot to mention that it is also possible to send a payload with just one of these objects. If the ESP does a fresh boot it will send all 4 objects with the msg.payload and if I just change one of these settings it will only sends one of these objects. There are some special effects that will send 3 of 4 and so on.

Tryed the method with your function node and I'm stuck at this point where I could declare a variable with an other variable and if the other variable is undefined it will set an other value. But this didn't work :frowning:

All of these Values of my dashboard nodes will be saved in flow variables. So I could send the saved value instead of an empty one.

var m = msg.payload,
    effect = {payload: m.effect} || {payload: flow.get('TEST_Switch_Desk_Effect')},
    speed = {payload: m.speed} || {payload: flow.get('TEST_Switch_Desk_Speed')},
    color = {payload: m.color} || {payload: flow.get('TEST_Switch_Desk_Color')},
    sw = {payload: m.switch} || {payload: flow.get('TEST_Switch_Desk')};
    
return [effect, speed, color, sw];

This will always lead to undefined payloads with the debug node :frowning:

Maybe I got it now, think it is not the smartest solution but it works:

var m = msg.payload,
    effect = m.effect,
    speed = m.speed,
    color = m.color,
    sw = m.switch;

if (effect === undefined) {effect = {payload: flow.get('TEST_Switch_Desk_Effect')};}
else {effect = {payload: effect};}

if (speed === undefined) {speed = {payload: flow.get('TEST_Switch_Desk_Speed')};}
else {speed = {payload: speed};}

if (color === undefined) {color = {payload: flow.get('TEST_Switch_Desk_Color')};}
else {color = {payload: color};}

if (sw === undefined) {sw = {payload: flow.get('TEST_Switch_Desk')};}
else {sw = {payload: sw};}

return [effect, speed, color, sw];