# Multiple String Outputs from Function

**URL:** <https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441>\
**Category:** General\
**Tags:** function-node\
**Created:** [27 June 2023 14:15 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441 "2023-06-27T14:15:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [27 June 2023 14:15 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/1 "2023-06-27T14:15:58Z")

</div>

_I'm sorry if the question is redundent, but I've been through the forum and still can't quite get my function working._

I have the following Pixel Code examples #1 and# 2 which are working independently of each other, and as expected. The output flows into a .py text file and works fine.

Pixel Code #1:  
if ( fruit =='apples' ) {msg.payload = 'pixels[19]=(255,255,255) ' } else...

Pixel Code #2:  
=if ( vegetable =='sprouts' ) {msg.payload = 'pixels[20]=(255,0,0) ' } else...

**Here's the trouble...**  
I would like to combine multiple if's with multiple outputs into one statement. Something like the example below, but I can't get the syntax of the outputs correct.

=if ( fruit =='apples' && vegetable =='sprouts' ) {msg.payload = 'pixels[19]=(255,0,0)' ,pixels[20]=(255,0,0) } else...

**Desired Output (string):**  
pixels[1]=(255,0,0)  
pixels[20]=(255,0,0)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 June 2023 14:39 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/2 "2023-06-27T14:39:36Z")

</div>

1. Is this "Pixel Code" a Node-red function?
2. Where do the values of variables "fruit" and "vegetable" come from - a single message or multiple messages?
3. Are you using the Write FIle node to create something.py?
4. Does your output file have to contain "pixels[1]" or "pixels[19]"?
5. Must there be a new line between the two values?
6. Is the first "=" in the snippet of code a mistake?
7. Why do you have single quotes around the first part of your assignment to msg.payload but not the second part?

> [@dandspach](#):
>
> ```auto
> =if ( fruit =='apples' && vegetable =='sprouts' ) {
> msg.payload = 'pixels[19]=(255,0,0)' ,pixels[20]=(255,0,0) 
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 June 2023 15:17 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/3 "2023-06-27T15:17:32Z")

</div>

To get the new line use a [template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) or `\n`

```auto
if ( fruit =='apples' && vegetable =='sprouts' ) {
  msg.payload = `pixels[19]=(255,0,0)
  pixels[20]=(255,0,0)` 
}
//or
msg.payload = 'pixels[19]=(255,0,0)\npixels[20]=(255,0,0)' 

```

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [27 June 2023 15:47 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/4 "2023-06-27T15:47:06Z")

</div>

> [@jbudd](#):
>
> - Is this "Pixel Code" a Node-red function? - **It is inside a Node-Red function. The code produces a text string when then is used to build a .py file to turn on and off pixels.**
> - Where do the values of variables "fruit" and "vegetable" come from - a single message or multiple messages? **Single payload, but different Onjects.**
> - Are you using the Write FIle node to create something.py? **Yes, a text file, which is called by a cron job to run the .py at certain intervals. Node-red gets the latest data and creates the .py file.**
> - Does your output file have to contain "pixels[1]" or "pixels[19]"? **Yes, the format 'pixels[19]' denotes the pixel to turn on or off.**
> - Must there be a new line between the two values? **Yes, and I know that the '\n' command will create the new line.**
> - Is the first "=" in the snippet of code a mistake? **yes, I think I instinctivly added it in the forum post, there is no = in the code.**
> - Why do you have single quotes around the first part of your assignment to msg.payload but not the second part? **I'm sorry, I don't understand. Can you clarify?**

Overall, the code works well when I want to test for one condition and one output (light up one led), but now I'm trying to test for two conditions and have two outputs (light up two seperate leds).

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 June 2023 16:02 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/5 "2023-06-27T16:02:57Z")

</div>

Wow, every question answered, thank you!

I'm pretty sure that @E1cid's suggestions will solve the problem but your detailed response deserves a proper answer too.

> [@jbudd](#):
>
> Why do you have single quotes around the first part of your assignment to msg.payload but not the second part  
> **I'm sorry, I don't understand. Can you clarify?**

This is the code you posted.

```auto
msg.payload = 'pixels[19]=(255,0,0)' ,pixels[20]=(255,0,0) 

```

`pixels[19]=(255,0,0`) is wrapped in single quotes `'pixels[19]=(255,0,0)'` but `pixels[20]=(255,0,0)` is not. I was not sure if it was a coding error or an error copying the code to here.

I asked if you are processing a single message or multiple ones because it's a common (beginners) error to think you can combine data from two different payloads in the same function code. But you are not in this situation so all is good.

I asked if you are using the Write File node beause it has the ability to append a new line after each message output. That means you could send the pixels[19] and pixels[20] individually with node.send(). It's not necessary though because you can use @E1cid's code to send them as one.

> [@dandspach](#):
>
> Overall, the code works well when I want to test for one condition and one output (light up one led), but now I'm trying to test for two conditions and have two outputs (light up two seperate leds).

I presume you have confirmed that the python file with both lines in does light both LEDs. 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 June 2023 16:26 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/6 "2023-06-27T16:26:19Z")

</div>

> [@jbudd](#):
>
> I'm pretty sure that @E1cid's suggestions will solve the proble

Not so sure myself as -

> [@dandspach](#):
>
> **Desired Output (string):**  
> pixels[1]=(255,0,0)  
> pixels[20]=(255,0,0)

and

> [@dandspach](#):
>
> d have two outputs (light up two seperate leds).

seem to contradict each other  
@dandspach if you want two outputs you may want to look at [node.send()](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

It may become clearer if you link to the node you are using for these pixel commands.

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [27 June 2023 16:38 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/7 "2023-06-27T16:38:24Z")

</div>

After reading the replies and making the suggested changes to the function, it all is working as it should. Pairs of LED's are lighting up when both conditions are met. I'm posting the solution here for others who may have a similar issue.

_if ( msg.payload.fruit =='apples' && msg.payload.vegetable == 'sprouts') {msg.payload = 'pixels[19]=(255,255,255)\npixels[20]=(255,0,0)' } else_...

Thank you both for your assistance!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 July 2023 16:38 UTC](https://discourse.nodered.org/t/multiple-string-outputs-from-function/79441/8 "2023-07-11T16:38:25Z")

</div>

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