Multiple String Outputs from Function

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):

  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
  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?
1 Like

To get the new line use a template string or \n

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

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).

1 Like

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.

This is the code you posted.

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.

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

Not so sure myself as -


seem to contradict each other
@dandspach if you want two outputs you may want to look at node.send()

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

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!

1 Like

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