Setting and using variables


#1

I really thought that I knew how to set and use variables but, for the life of me, I can’t figure out what appears to be a simple issue.

I have defined a variable dynamic FirstLoop. I do not assign any value to this in my definition statement as I have been warned that webCoRE will assign this value to the variable every time the piston runs, effectively overwriting any value that was assigned in the previous loop of the piston.

Then I do an X2 Set variable {FirstLoop} = Yes. Note that there are no quotes around the “Yes” but that is how webCoRE sets it up.

Then do an IF {FirstLoop} is ‘Yes’ Then … Note that there are single quotes around the “Yes” added by webCoRE.

The IF always shows False and the local variable FirstLoop always shows as blank. Somehow I must not be setting the variable properly and any logic that depends on that variable after that does not execute. I have attached a screen shot of part of the piston below. Any thoughts? Thanks.


#2

This is true, but your very first command in the piston also sets the variable to ‘Yes’ each time as well…

temp


As far as setting a variable, I always use an expression, and use quotes for standard text.
Either single quotes, or double quotes will work, but stay consistent.
(I use single quotes unless my string has an apostrophe)


#3

I just noticed that there are no triggers up top. Do you have something else firing up this piston?


#4

@WCmore. Thanks for the quick response. Several comments …

  1. I do not have any triggers. The piston just starts with the definitions, the setting of FirstLoop, ActiveDevice1 and ActiveDevice2 then into the do every other minute section.
  2. In response to your 1st observation that the piston will set the variable to YES every time, it actually seems to do the opposite. The varaible FirstLoop never gets set to YES. I was able to set the variable to YES manually (in the piston summary page) and then the IF statement actually worked. By the way, I have another statement within the IF statement that sets FirstLoop to NO and, when I manually set the variable to YES before starting the piston, then the IF statement runs and then FirstLoop is set to NO as I planned. The issue is it never gets set back to YES unless I do so manually.
  3. To be honest, I thought that the piston would not return to the top of the piston, it would just loop within the do every other minute, so the initial setting of these variables at the top would not happen again. Perhaps I am wrong there but I am still unsure why FirstLoop nevers gets set to YES unless I do it manually.
    Further thoughts?

#5

This is likely because you have no trigger, and the "Set variable to Yes" is outside the "Every" statement.

When a piston has a trigger, it starts at the top of the code, and works top to bottom, looking for true IF blocks, or any unrestricted command to run. Without a trigger, your piston only looks in the “Every other minute” block.


I usually try to avoid using loops (so I am nowhere near an expert), but most people use $index to count the number of loops. (with 0 being the first loop) I would try searching the forums for ‘loop index’ to see a few examples. It should be easy to implement into your piston.