# Trigger a message payload based on input from parallel port

**URL:** <https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017>\
**Category:** Dashboard\
**Created:** [9 June 2019 23:30 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017 "2019-06-09T23:30:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [9 June 2019 23:30 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/1 "2019-06-09T23:30:13Z")

</div>

Hi, I am completely new to Node-RED but I love it already.  
I have an arduino board connected to my raspberry pi, I need to send a one character output from the pi to the Arduino based on another input that comes from a tape dispenser.

Input ----------------- Raspberry Pi --------[read] --- if [variable}-----payload ------ Arduino.

Now I have different inputs coming from my tape dispenser and they are all in the form of @number. I want to trigger a different payload for each possible input.

The total of inputs are 13  
The outputs for the arduino are a 1 character payload [1/2/3/4/5/6/7/8/9/a/b/c/s]

I don't know if a single function can handle it or if I need different functions.

Thank You

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 June 2019 00:03 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/2 "2019-06-10T00:03:41Z")

</div>

Several ways you can do this.

Probably the most "readable"/native way would be to use a function node with a switch statement...

```auto
switch (msg.payload){
  case: 1
  msg.payload = "A";
  break;
  case: 2
  msg.payload = "B";
  break;
}
return msg;

```

There are clever ways (use a lookup object) and even contrib nodes e.g [node-red-contrib-map](https://flows.nodered.org/node/node-red-contrib-map)

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [10 June 2019 00:07 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/3 "2019-06-10T00:07:59Z")

</div>

Iteresting, so case must be the exact input or can I grab only part of it?

I mean, for each case the tape machine gives me:

tr

ch

@123

123 here is only an example. But do I need to specify the tr and ch with the spaces?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 June 2019 00:13 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/4 "2019-06-10T00:13:21Z")

</div>

Case is an exact match.  
So you could strip out the white space first and use "trch@123" as the case.

Search the internet for **JavaScript remove whitespace** for clues

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [10 June 2019 00:21 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/5 "2019-06-10T00:21:13Z")

</div>

Thank you so much for the quick help, I will go and do it immediately.

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [10 June 2019 03:14 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/6 "2019-06-10T03:14:06Z")

</div>

So far I had no luck.

tried with

```auto
switch (msg.payload){
case msg.payload=="@1T @1XR @1Y198":
msg.output = "1";
break;
switch (msg.payload){
case msg.payload=="@1T @1XR @1Y163":
msg.output = "2";
break;

```

But it does not work. all the message gets through unfiltered no matter what I do.  
I also tried with

```auto
var topic=msg.topic;
if (topic=="@1Y198"){
return [null,1]

}
var topic=msg.topic;
if (topic=="@1T"){
return [null]

}
var topic=msg.topic;
if (topic=="@1XR"){
return [null]

}

```

Even in this case the message goes all the way through unfiltered.  
No Idea how to solve this.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [10 June 2019 06:23 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/7 "2019-06-10T06:23:59Z")

</div>

Put a debug node before and after the switch node and see what it being output - i would guess it is probably not a fully formed message depending on how you got it into Node Red in the first place i.e. what is the message flow - how do you interface to the Rpi ports to get the data ? Maybe past your whole flow ?

Craig

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 June 2019 07:29 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/8 "2019-06-10T07:29:46Z")

</div>

Also you have the syntax of the switch statement completely wrong, go back and look at the example @Steve-Mcl posted and you will see that yours is not the same.

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [10 June 2019 23:49 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/9 "2019-06-10T23:49:30Z")

</div>

Hi, I tried with that syntax but the editor was complaining about the "@" that was unexpected.  
The debug node tells me that the output (after the function) is @1T @1XR @1Y198

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 02:00 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/10 "2019-06-11T02:00:34Z")

</div>

Tried different syntax for the node

```auto

switch (msg.payload){
    case: "@1T @1XR @1Y10"
    msg.output = "2";
    break;
    case msg.payload=="@1T @1XR @1Y10":
    msg.output = "3";
    break;
        case msg.payload=="@1T ":
    msg.output = "4";
    break;
        case msg.payload=="@1XR ":
    msg.output = "5";
    break;
}
return msg;

```

But when I use case: case: "@1T @1XR @1Y10"  
the error is Expected ':' and instead saw @1T @1XR @1Y10"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 June 2019 07:32 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/11 "2019-06-11T07:32:36Z")

</div>

> [@krikkit](#):
>
> case msg.payload=="@1T ":

Spot the 2 deliberate errors in that line ^

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 June 2019 07:34 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/12 "2019-06-11T07:34:06Z")

</div>

Actually, every case line is incorrect.

`case: statement;`  
Not  
`case statement:`

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 08:17 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/13 "2019-06-11T08:17:28Z")

</div>

Hi, I don't know what is going on.

Can you try to put

```auto
switch (msg.payload) {
   case: @1T @1XR @1Y10
msg.output = "1";
break;
}
return msg;

```

inside a function?

Node red tells me:  
Unexpected '@'.  
Expected an identifier and instead saw ':'

if I quote everything like this:  
case: "@1T @1XR @1Y10"  
i get  
Expected an identifier and instead saw ':'.  
Expected ':' and instead saw '@1T @1XR @1Y10'.  
Missing ':' on a case clause.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 June 2019 08:20 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/14 "2019-06-11T08:20:20Z")

</div>

> [@krikkit](#):
>
> case: @1T @1XR @1Y10

Now you have lost the quotes, that is the one that you did have right the first time  
`case: "@1T @1XR @1Y10"`  
Strictly there should be a ; on the end but a ; on the end of a line is often optional.

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 08:24 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/15 "2019-06-11T08:24:41Z")

</div>

I see, problem is that even with the quotes and the ';' at the end of the line I have different errors

> [@krikkit](#):
>
> Expected an identifier and instead saw ':'.  
> Expected ':' and instead saw '@1T @1XR @1Y10'.  
> Missing ':' on a case clause.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 June 2019 08:27 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/16 "2019-06-11T08:27:32Z")

</div>

Copy and paste what you have now

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 08:29 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/17 "2019-06-11T08:29:21Z")

</div>

```auto
switch (msg.payload) {
   case: "@1T @1XR @1Y10";
msg.output = "1";
break;
   case: "@1T @1XR @1Y236";
msg.output = "2";
break;
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 June 2019 08:35 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/18 "2019-06-11T08:35:18Z")

</div>

Sorry, I have got it wrong myself now, it should be

```auto
switch (msg.payload) {
   case "@1T @1XR @1Y10":
msg.output = "1";
break;
   case "@1T @1XR @1Y236":
msg.output = "2";
break;
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 08:38 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/19 "2019-06-11T08:38:25Z")

</div>

Let me give it a try 😃

---

<div class="post-metadata">

**Author:** ![krikkit](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@krikkit](https://discourse.nodered.org/u/krikkit)\
**Post date:** [11 June 2019 08:42 UTC](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017/20 "2019-06-11T08:42:41Z")

</div>

Unfortunately all the message goes through the function unfiltered.  
I really have no clues why.

I did try to change  
return msg;  
to  
return msg.output but no luck 😃

[Next page](https://discourse.nodered.org/t/trigger-a-message-payload-based-on-input-from-parallel-port/12017.md?page=2)
