If statement within a for loop

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

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.

Hang on.

So you are saying/asking....

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.

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

[{"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:

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.

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

//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);

}

What you need to do is make a context variable called - eg - ROOMS.

Then you have the values stored from them like this:

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.

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.

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

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.

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

Cheers
Andrew

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.

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:13node: 9be67fac9f9266demsg.payload : string[38]

"The Lounge temperature is 18.9 Degrees"

04/10/2021, 23:34:13node: 9be67fac9f9266demsg.payload : string[38]

"The Office temperature is 20.9 Degrees"

04/10/2021, 23:34:13node: 9be67fac9f9266demsg.payload : string[39]

"The Hallway temperature is 20.7 Degrees"

04/10/2021, 23:34:13node: 9be67fac9f9266demsg.payload : string[39]

"The Kitchen temperature is 19.6 Degrees"

04/10/2021, 23:34:13node: 9be67fac9f9266demsg.payload : string[44]

"The Conservatory temperature is 19.7 Degrees"

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

}   

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.

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