# If statement within a for loop

**URL:** <https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859>\
**Category:** General\
**Created:** [4 October 2021 21:29 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859 "2021-10-04T21:29:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [4 October 2021 21:29 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/1 "2021-10-04T21:29:13Z")

</div>

Good Evening  
I wondered if someone could assist me please  
I have a node-red function that extracts 5 values from a text file as listed below

var lounge = some value  
var kitchen = some value  
var hallway = some value  
var office = some value  
var conseratory = some value

This is where I get stuck

I have a for loop as below which throws out the expected output

for (var i = 0; i \< 6; i++){  
msg.payload = [i]  
node.send(msg);  
}

What i am trying to do is IF [i] of the for loop = 0 then set the payload to lounge and output then  
IF [i] value is 2 then output kitchen etc etc

Many thanks Andrew

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 October 2021 21:32 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/2 "2021-10-04T21:32:56Z")

</div>

Sorry, but I am not quite understanding that.

Could you post the actual code?

And I'm guessing the _something_ is different for each value and you just didn't make each unique only by mistake.

But in retrospect:  
Nah.. Sorry. Not quite getting the last paragraph.

> [@fogmajor](#):
>
> What i am trying to do is IF [i] of the for loop = 0 then set the payload to lounge and output then  
> IF [i] value is 2 then output kitchen

Hang on.

So you are saying/asking....

```auto
var lounge = some value
var kitchen = some value
var hallway = some value
var office = some value
var conseratory = some value

```

And you get a value `i` coming in.

`if (i == 0)`  
you get the longe value.

3 = hallway.  
4 = office.

Not too hard.

But I want to be sure first.

And the actual code would be helpful.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 October 2021 21:48 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/3 "2021-10-04T21:48:29Z")

</div>

This is some code which does something like what you are doing..

```auto
[{"id":"20743040.34be58","type":"inject","z":"83bf3de4.b9429","g":"6869b2ab.7811d4","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"foo","payloadType":"str","x":310,"y":4420,"wires":[["b2d3ecc.6c0101"]]},{"id":"b2d3ecc.6c0101","type":"change","z":"83bf3de4.b9429","g":"6869b2ab.7811d4","name":"","rules":[{"t":"set","p":"table","pt":"msg","to":"month_times","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":4380,"wires":[["c743e378.7603a8","1e9b6683.00f1b9"]]},{"id":"c743e378.7603a8","type":"moment","z":"83bf3de4.b9429","g":"6869b2ab.7811d4","name":"Month","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"MM","locale":"en_AU","output":"month","outputType":"msg","outTz":"Australia/Sydney","x":750,"y":4380,"wires":[["29bd6547.965a3a","8e98e4ce.ab2278"]]},{"id":"29bd6547.965a3a","type":"function","z":"83bf3de4.b9429","g":"6869b2ab.7811d4","name":"Look up","func":"var m = msg.month;\n\nm = parseInt(m);\n\nvar x = msg.table[m];\n\nmsg.payload = x;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":4380,"wires":[["c126c43.66ee238","f2efd6db.b17fe8"]]}]

```

And the `context` (or flow) is set like this:

```auto
20,24,28,32,36,40,40,36,32,28,24,20

```

So as the month changes from 1 - 12 you get the respective value from the table.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [4 October 2021 21:50 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/4 "2021-10-04T21:50:38Z")

</div>

Hi Many thanks for the prompt replay  
Yes that's it, I just want to output the six variables one by one when the for loop iterates

code below

```auto
//Assign text to xxx
var xxx = msg.payload.toString();
xxx = xxx.replace(/"/g,""); // remove quotes

// find the occurrences of the string "ACTUAL_TEMP" from the large txt file xxx
function getStringReminder(str, substr, occ) {
   let index = str.indexOf(substr);
   let preindex = '';
   let i = 1;
   while (index !== -1) {
      preIndex = index;
      if (occ == i) {
        break;
      }
      index = str.indexOf(substr, index + 1)
      i++;
   }
   return preIndex;
}

// Build the voice strings
// Find the temperature values using xxx.substring and its occurrence
// Assign above mention values to the 7 variable below

var lounge = "The Lounge temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 1) + 12 , + 4)) + " Degrees"
var office = "The Office temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 4) + 12 , + 4)) + " Degrees"
var hallway = "The Hallyway temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 1) + 12 , + 5)) + " Degrees"
var kitchen = "The Kitchen temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 6) + 12 , + 4)) + " Degrees"
var conservatory = "The Conservatory temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 7) + 12 , + 4)) + " Degrees"
var hotwater = (getStringReminder(xxx, 'ACTUAL_TEMP', 2))
var heating = (getStringReminder(xxx, 'ACTUAL_TEMP', 3))

//STUCK HERE

for (var i = 0; i < 6; i++){
    
msg.payload = // first iteration of the loop i will = 0 so assign variable lounge and send
              // next iteration i will = 1 assign variable Office and send
              // etc etc

node.send(msg);

}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 October 2021 21:56 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/5 "2021-10-04T21:56:40Z")

</div>

What you need to do is make a _context variable_ called - eg - ROOMS.

Then you have the values stored from them like this:

```auto
var lounge = "The Lounge temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 1) + 12 , + 4)) + " Degrees"
var office = "The Office temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 4) + 12 , + 4)) + " Degrees"
var hallway = "The Hallyway temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 1) + 12 , + 5)) + " Degrees"
var kitchen = "The Kitchen temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 6) + 12 , + 4)) + " Degrees"

```

I'm using your code for now to help you get your head around it. No offence.

```auto
context.set("ROOMS":lounge,office,hallway,kitchen);

var THIS = ROOMS[i];

msg.payload = THIS;

return msg;

```

Send `i` as the incoming msg.payload and you get the value for that room.  
Given: lounge = 1 office = 2 hallway = 3 kitchen = 4.

Or I may be off by 1 and it starts at 0.

But I hope you get the idea.

---

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [4 October 2021 21:58 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/6 "2021-10-04T21:58:15Z")

</div>

That's a top job thank you, i will give that a try and report back

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 October 2021 22:01 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/7 "2021-10-04T22:01:02Z")

</div>

Actually it won't work.

It only sends ONE message depending on the value of `i`.

Your code seems to send ALL messages, which I don't understand why it has to.

If you are _asking it_ for the .... _value_ of/from a room.

Your original code is kind of expecting the room name to be sent.  
Either directly or not.

But when it matches the room, it sends the respective message.  
But that isn't what the code is doing if you are using `node.send()`.

That sends multiple messages.

My code only sends ONE message - depending on the value of `i`, as I said.

But I do hope it helps you.

---

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [4 October 2021 22:04 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/8 "2021-10-04T22:04:38Z")

</div>

It certainly does and thank you for helping, I am also trying to learn

Cheers  
Andrew

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 October 2021 22:14 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/9 "2021-10-04T22:14:59Z")

</div>

No problems.

I surprised it works.

When I _constructed_ the `ROOM` context, I thought I didn't use the correct construction method.

But it seems I did.

So long as it is working.  
Oh, mark the reply that best helped you as the solution.

---

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [4 October 2021 22:41 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/10 "2021-10-04T22:41:28Z")

</div>

Hi Again  
I modified my If statements and this worked for sending multiple messages from within the loop

I then feed these into text to speech, I will share the flow when its finished

many thanks

Andrew

04/10/2021, 23:34:13[node: 9be67fac9f9266de](http://127.0.0.1:1880/#)msg.payload : string[38]

"The Lounge temperature is 18.9 Degrees"

04/10/2021, 23:34:13[node: 9be67fac9f9266de](http://127.0.0.1:1880/#)msg.payload : string[38]

"The Office temperature is 20.9 Degrees"

04/10/2021, 23:34:13[node: 9be67fac9f9266de](http://127.0.0.1:1880/#)msg.payload : string[39]

"The Hallway temperature is 20.7 Degrees"

04/10/2021, 23:34:13[node: 9be67fac9f9266de](http://127.0.0.1:1880/#)msg.payload : string[39]

"The Kitchen temperature is 19.6 Degrees"

04/10/2021, 23:34:13[node: 9be67fac9f9266de](http://127.0.0.1:1880/#)msg.payload : string[44]

"The Conservatory temperature is 19.7 Degrees"

```auto
var lounge = "The Lounge temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 1) + 12 , + 4)) + " Degrees"
var office = "The Office temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 4) + 12 , + 4)) + " Degrees"
var hallway = "The Hallway temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 5) + 12 , + 4)) + " Degrees"
var kitchen = "The Kitchen temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 6) + 12 , + 4)) + " Degrees"
var conservatory = "The Conservatory temperature is " + (xxx.substr(getStringReminder(xxx, 'ACTUAL_TEMP', 7) + 12 , + 4)) + " Degrees"
var hotwater = (getStringReminder(xxx, 'ACTUAL_TEMP', 2))
var heating = (getStringReminder(xxx, 'ACTUAL_TEMP', 3))

for (var i = 0; i < 6; i++){
    

if (i === 0){

msg.payload = lounge

node.send(msg);

}

if (i === 1){

msg.payload = office

node.send(msg);

}

  if (i === 2){

msg.payload = hallway

node.send(msg);

}  
    

 if (i === 3){

msg.payload = kitchen

node.send(msg);

}  
     
    
  if (i === 4){

msg.payload = conservatory

node.send(msg);

}   

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 October 2021 00:20 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/11 "2021-10-05T00:20:43Z")

</div>

Ok.....

This has a _hidden problem_ which I shall discuss with you as I have been bitten by it a few times.

That works and then one day you want to add more rooms.

Not a big deal as is, but there is another way of doing it so it is better _structured_.

Make an ARRAY with the room names and numbers.  
`{{"Lounge","number":1},{"Other_room","number":2}}`  
Sorry I recently asked for help on this and have forgotten the exact structure.  
You then _convert_ the room's name into a number.  
Then you simply send the number in and do what I showed you earlier.

Then, as you add more rooms it is _easier_ to add them and it is a lot smaller code.

Sorry, I've got a big mess happening just now and need to attend to it.

---

<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:** [19 October 2021 00:20 UTC](https://discourse.nodered.org/t/if-statement-within-a-for-loop/51859/12 "2021-10-19T00:20:47Z")

</div>

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