Trigger a message payload based on input from parallel port

Put a debug node showing what is going into the function node and another showing what is coming out. Configer the second on to show complete msg. Make a screenshot showing the input and output messages.

1 Like

I will do tomorrow as soon as I will get back here at work. Now I need to gather all my stuff and punch out.
. For now I want to thank you @Colin and @Steve-Mcl for all the help..

Have a wonderful Day/Night

you wont be catching anything on your switch

as @Colin said, put a debug before and after the function node. Inspect what goes in & compare that to the case's you have created.

I remember earlier in this thread you said there were newlines in the string. Did you look at removing all whitespace like i suggested?

try this...


/* 
strip whitespace etc. 
e.g.  turn this...
  "  tr

  ch

  @123  "

into this...

  "trch@123" 

... then you will have a better chance of matching.
*/

//the code...

//replace everything that isnt     a~Z   0~9  @     with nothingness
let alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); 
//use switch to populate msg.output based on content of alphaNumeric
switch (alphaNumeric) {
  case "@1T@1XR@1Y10":
  msg.output = "1";
  break;
  case "@1T@1XR@1Y236":
  msg.output = "2";
  break;
  default:
  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);
  return null;//halt flow of msg - dont trigger output of this node
}
return msg;//return msg so next node is triggered

Ok before I was using socat and sed for the spaces, but I like msg.payload.replace.

I did try this function but the source stays the same no matter what.
Also the debug node does not give me any error.

I am trying to inject the payload with the inject node (it is quicker) but still the payload goes through unchanged.

The payload will always go through unchanged, because you have no code to change msg.payload. You are setting msg.output. That is why I suggested setting the debug node to show the complete message, then you would see if msg.output is being set.

1 Like

Hmmm. I suspect with the lack of screen shots or exported flow, we are misunderstanding each other.

Try this...

  1. Add debug node before and after the function node
    1. SET the DEBUG nodes to output 'complete msg'
  2. Replace the function node code with below.
  3. In the debug window, expand the resulting msg objects
  4. Take Screenshots and post here.
    1. Include the debug window
    2. Include an image showing (at minimum) the region of interest of your flow

Why..
We need to see all properties of the msg so we can see why it gets past the case statements. So in the debug window we should see what is in msg.alphaNumeric.
I would bet my left nut and several packet of my favorite crisps that the value in msg.alphaNumeric IS NOT a match to any of the case entries you have.

//remove all non alphanumeric (and the @) from the input 
//NOTE: store it in msg object so we can inspect it in the debug window
msg.alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); 
//use switch to populate msg.output based on content of alphaNumeric
switch (msg.alphaNumeric) {
  case "@1T@1XR@1Y10":
  msg.output = "1";
  break;
  case "@1T@1XR@1Y236":
  msg.output = "2";
  break;
  default:
  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);
  return null;//halt flow of msg - dont trigger output of this node
}
return msg;//return msg so next node




Lastly, if you want further help, it may be beneficial if you post your flow and sample data. How to share code or flow json

Good point.

Actually, I suspect everything is working.

When the OP said he got no error (i.e. the default was never reached) I hadn't considered the OP may not be inspecting the right part of the result.

you are both right... if I set the debug note to catch debug.output the function works perfectly... so now how can I set the output in the right way?

What do you mean 'the right way'? Do you want to set msg.payload instead of msg.output? If so then just put msg.payload = ... instead of msg.output = ...

Also, you still have spaces and newlines in you string.
Also you did not output complete msg after BEFORE and AFTER function node

We still cannot help you if you wont do what we ask.

Ah I'm getting impatient in my old age.

I had hoped you would follow our advice and figure it out (learn from trial and error).

Never mind.

If you had did what I said - this is what youd have...

The code I took the time to write for you works & by looking at pictures you posted (where spaces and newlines can be seen) you didnt bother to use my code or follow the steps I set out. :frowning:

Here is my flow for reference...

[{"id":"7569f1ce.da9e1","type":"inject","z":"673afa83.cf1d04","name":"","topic":"","payload":"  @1T @1XR @1Y10 ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":786,"y":144,"wires":[["3092f4d3.c8fb0c","be16b96e.74f838"]]},{"id":"3092f4d3.c8fb0c","type":"function","z":"673afa83.cf1d04","name":"convert msg","func":"//remove all non alphanumeric (and the @) from the input \n//NOTE: store it in msg object so we can inspect it in the debug window\nmsg.alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); \n//use switch to populate msg.output based on content of alphaNumeric\nswitch (msg.alphaNumeric) {\n  case \"@1T@1XR@1Y10\":\n  msg.output = \"1\";\n  break;\n  case \"@1T@1XR@1Y236\":\n  msg.output = \"2\";\n  break;\n  default:\n  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);\n  return null;//halt flow of msg - dont trigger output of this node\n}\nreturn msg;//return msg so next node","outputs":1,"noerr":0,"x":1054,"y":192,"wires":[["b3d26da3.3c93e"]]},{"id":"cd1fb152.bb90a","type":"inject","z":"673afa83.cf1d04","name":"","topic":"","payload":"@1T @1XR @1Y236","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":796,"y":192,"wires":[["3092f4d3.c8fb0c","be16b96e.74f838"]]},{"id":"be16b96e.74f838","type":"debug","z":"673afa83.cf1d04","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1034,"y":240,"wires":[]},{"id":"b3d26da3.3c93e","type":"debug","z":"673afa83.cf1d04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1202,"y":240,"wires":[]},{"id":"be0cd7b7.c383f8","type":"inject","z":"673afa83.cf1d04","name":"","topic":"","payload":"@wont- work -no @case","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":806,"y":264,"wires":[["3092f4d3.c8fb0c","be16b96e.74f838"]]}]
1 Like

I did an oopsie ehehe sorry for that.
I am going to post the code as soon as I get on the raspberry pi. I am reading the guidelines on how to export the nodes and post them in the appropriate way.

Oh no the right way for me is msg.output :slight_smile:

This is my flow:

[{"id":"94f5aaf1.dd7478","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"95ea4fc2.371a2","type":"serial in","z":"94f5aaf1.dd7478","name":"","serial":"14fa8305.476bfd","x":130,"y":140,"wires":[["f6106e64.cbeac","a3d13068.0f127"]]},{"id":"2c887c0c.675924","type":"inject","z":"94f5aaf1.dd7478","name":"","topic":"","payload":"@1T @1XR @1Y236","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":140,"y":340,"wires":[["f6106e64.cbeac","c90a02ea.9cad3"]]},{"id":"f6106e64.cbeac","type":"function","z":"94f5aaf1.dd7478","name":"better filter","func":"let alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); \n//use switch to populate msg.output based on content of alphaNumeric\nswitch (alphaNumeric) {\n  case \"@1T@1XR@1Y10\":\n  msg.output = \"1\";\n  break;\n  case \"@1T@1XR@1Y236\":\n  msg.output = \"2\";\n  break;\n  default:\n  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);\n  return null;//halt flow of msg - dont trigger output of this node\n}\nreturn msg;//return msg so next node is triggered","outputs":1,"noerr":0,"x":370,"y":300,"wires":[["727922a7.a2d37c"]]},{"id":"a3d13068.0f127","type":"debug","z":"94f5aaf1.dd7478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":360,"y":80,"wires":[]},{"id":"727922a7.a2d37c","type":"debug","z":"94f5aaf1.dd7478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"output","targetType":"msg","x":570,"y":200,"wires":[]},{"id":"c90a02ea.9cad3","type":"debug","z":"94f5aaf1.dd7478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":370,"y":400,"wires":[]},{"id":"746dc34b.166b5c","type":"catch","z":"94f5aaf1.dd7478","name":"Debug","scope":["f6106e64.cbeac"],"uncaught":false,"x":350,"y":260,"wires":[["69a50aa4.e72c24"]]},{"id":"c0124ff9.3a91c","type":"status","z":"94f5aaf1.dd7478","name":"","scope":null,"x":740,"y":220,"wires":[[]]},{"id":"69a50aa4.e72c24","type":"debug","z":"94f5aaf1.dd7478","name":"test","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":590,"y":260,"wires":[]},{"id":"14fa8305.476bfd","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"9","bin":"false","out":"count","addchar":"","responsetimeout":"10000"}]

I did try to use the one @Steve-Mcl posted but when I add the node to the arduino I still get the whole payload instead of the object "1" or "2" that I need for the Arduino to light up the specific set of leds

Ok I think I got it adding a change function

[{"id":"714f58a9.946ac8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"bd4b5a0.20216a8","type":"inject","z":"714f58a9.946ac8","name":"","topic":"","payload":"  @1T @1XR @1Y10 ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":160,"wires":[["31311d91.73a682","1a22b267.c69f7e"]]},{"id":"31311d91.73a682","type":"function","z":"714f58a9.946ac8","name":"convert msg","func":"//remove all non alphanumeric (and the @) from the input \n//NOTE: store it in msg object so we can inspect it in the debug window\nmsg.alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); \n//use switch to populate msg.output based on content of alphaNumeric\nswitch (msg.alphaNumeric) {\n  case \"@1T@1XR@1Y10\":\n  msg.output = \"1\";\n  break;\n  case \"@1T@1XR@1Y236\":\n  msg.output = \"2\";\n  break;\n  default:\n  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);\n  return null;//halt flow of msg - dont trigger output of this node\n}\nreturn msg;//return msg so next node","outputs":1,"noerr":0,"x":478,"y":208,"wires":[["8d77016.235ab","a02dc229.65926"]]},{"id":"1863da45.f47a86","type":"inject","z":"714f58a9.946ac8","name":"","topic":"","payload":"@1T @1XR @1Y236","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":208,"wires":[["31311d91.73a682","1a22b267.c69f7e"]]},{"id":"1a22b267.c69f7e","type":"debug","z":"714f58a9.946ac8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":458,"y":256,"wires":[]},{"id":"8d77016.235ab","type":"debug","z":"714f58a9.946ac8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":626,"y":256,"wires":[]},{"id":"f3de9fc2.62caf","type":"inject","z":"714f58a9.946ac8","name":"","topic":"","payload":"@wont- work -no @case","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":280,"wires":[["31311d91.73a682","1a22b267.c69f7e"]]},{"id":"3492f4b7.06d0bc","type":"serial out","z":"714f58a9.946ac8","name":"arduino","serial":"59dec7e8.7f4548","x":700,"y":180,"wires":[]},{"id":"10d75f01.a41b01","type":"debug","z":"714f58a9.946ac8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":700,"y":80,"wires":[]},{"id":"a02dc229.65926","type":"change","z":"714f58a9.946ac8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"output","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":120,"wires":[["10d75f01.a41b01"]]},{"id":"59dec7e8.7f4548","type":"serial-port","z":"","serialport":"/dev/ttyACM0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"500"}]

Change node is unnecessary.

If you read the code in the function you can clearly see msg.output is set. Simply change that to msg.payload like @Colin said ...

For example...

switch (msg.alphaNumeric) {
  case "@1T@1XR@1Y10":
  msg.payload = "1";  //<<<<<<<< SET msg.payload INSTEAD OF msg.output!!!
  break;
  case "@1T@1XR@1Y236":
  msg.payload = "2";  //<<<<<<<< SET msg.payload INSTEAD OF msg.output!!!
  break;
  default:
  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.output`, msg);
  return null;//halt flow of msg - dont trigger output of this node
}

Ok so with the changes suggested nothing is working anymore...

[{"id":"b69086aa.ed1058","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"271f4c3.6eeceb4","type":"inject","z":"b69086aa.ed1058","name":"","topic":"","payload":"  @1T @1XR @1Y10 ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":140,"wires":[["10322b90.247df4","4c5c7ece.16a05"]]},{"id":"10322b90.247df4","type":"function","z":"b69086aa.ed1058","name":"convert msg","func":"//remove all non alphanumeric (and the @) from the input \n//NOTE: store it in msg object so we can inspect it in the debug window\nmsg.alphaNumeric = msg.payload.replace(/[^a-z0-9@+]+/gi, ''); \n//use switch to populate msg.output based on content of alphaNumeric\nswitch (msg.alphaNumeric) {\n  case \"@1T@1XR@1Y10\":\n  msg.payload = \"1\";\n  break;\n  case \"@1T@1XR@1Y236\":\n  msg.payload = \"2\";\n  break;\n  case \"@1T@1XR@1Y219\":\n  msg.payload = \"3\";\n  break;\n  default:\n  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.payload`, msg);\n  return null;//halt flow of msg - dont trigger output of this node\n}\nreturn msg;//return msg so next node","outputs":1,"noerr":0,"x":478,"y":208,"wires":[["8a76d1fa.60dd9","b7c00d44.8b1f7","b0a89545.b2f5b8"]]},{"id":"3adf3e51.fef132","type":"inject","z":"b69086aa.ed1058","name":"","topic":"","payload":"@1T @1XR @1Y236","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["10322b90.247df4","4c5c7ece.16a05"]]},{"id":"4c5c7ece.16a05","type":"debug","z":"b69086aa.ed1058","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":458,"y":256,"wires":[]},{"id":"8a76d1fa.60dd9","type":"debug","z":"b69086aa.ed1058","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":626,"y":256,"wires":[]},{"id":"986f82bc.f22df","type":"inject","z":"b69086aa.ed1058","name":"","topic":"","payload":"@wont- work -no @case","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["10322b90.247df4","4c5c7ece.16a05"]]},{"id":"b0a89545.b2f5b8","type":"serial out","z":"b69086aa.ed1058","name":"arduino","serial":"59dec7e8.7f4548","x":700,"y":180,"wires":[]},{"id":"a549fd1b.78b35","type":"debug","z":"b69086aa.ed1058","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":700,"y":80,"wires":[]},{"id":"b7c00d44.8b1f7","type":"change","z":"b69086aa.ed1058","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"output","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":120,"wires":[["a549fd1b.78b35","b0a89545.b2f5b8"]]},{"id":"69cd710.1e52e9","type":"serial in","z":"b69086aa.ed1058","name":"","serial":"59dec7e8.7f4548","x":140,"y":80,"wires":[["10322b90.247df4","3ade415e.d1f86e"]]},{"id":"3ade415e.d1f86e","type":"debug","z":"b69086aa.ed1058","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":400,"y":60,"wires":[]},{"id":"59dec7e8.7f4548","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"500"}]

The error is UNKNOWN value in payload '@1XR'. Cannot generate value for msg.output

I have no Idea what is going on

Its telling you there was no matching case in the switch & so the default was reached where very clearly the error UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.payload is generated.

Look at comments in the code...

switch (msg.alphaNumeric) {
  case "@1T@1XR@1Y10":  /* <<< ***  MATCH @1T@1XR@1Y10 will hit here ****/
  msg.payload = "1";
  break;
  case "@1T@1XR@1Y236":  /* <<< ***  MATCH @1T@1XR@1Y236 will hit here ****/
  msg.payload = "2";
  break;
  case "@1T@1XR@1Y219":  /* <<< ***  MATCH @1T@1XR@1Y219 will hit here ****/
  msg.payload = "3";
  break;
  default:  /*<<<  *** ANYTHING NOT MATCHED WILL COME HERE - the below will be fired - THIS IS WHATS HAPPENING */
  node.error(`UNKNOWN value in payload '${msg.payload}'. Cannot generate value for msg.payload`, msg);
  return null;//halt flow of msg - dont trigger output of this node
}

...so add another case for @1XR

If you look at the debug output generated by the function node convert msg - it will include the EXACT value in msg.alphaNumeric that you need to MATCH in your additional case statement.

The more I scan the more inconsistency I have with same items... I wonder if it is related with the input from the serial port? I mean for same items I get
0162
R 016
162

I am adding all the cases but I wonder if I could have a little bit more of consistency.

The serial port is set to 9600 because it is the only speed that gives me ascii code instead of binary data and I split the message after 9 characters

Remind us what the data is coming from. Do you have a spec of what it should be?