Running a Statement One Time


#1

What I’m Trying to Accomplish:

When I come home I want the bedroom light to turn on (once per day). If I leave and come back, I don’t want it going back on.

Idea:

I figured I could create a local variable to do this and set it to boolean. So when I first arrive home…the variable is set to var_JustHome=true …and then it gets set to…var_JustHome=false…and then at some point later in the night var_JustHome …goes back to true

Issue:

The problem I’m having is the variable needs an “initial value” to get the process started, but the only way I see to do that is when I define it within the piston. The problem with that is if that happens every time the piston runs, it’s obviously not going to work.

I hope I’m missing something obvious here due to my lack of understanding. Or do I just create a global variable? Or maybe there’s a different way all together…I tried reading through Mike’s examples and got tripped up on this problem. Thanks for any help…

Snapshot Attached:


#2

Some folks just use the $day system variable. Store today’s day in a variable lastRun for example and compare it at run time. If same, skip it, it would be different tomorrow :slight_smile:


#3

What I’ve found through experimentation/observation is that local boolean variables will evaluate to false when uninitialized, and integer variables will evaluate to zero, and datetime variables will evaluate to $now. I don’t know if you can always count on this, but this is what I’ve seen.

So, if you’re using a boolean, write your piston so that false is most likely what it should be when you first start running it.

Or another technique I’ve used (at least for relatively simple pistons) is to disable all the statements in the piston (or at least all the statements that might do the wrong thing when variables are uninitialized) and then add a statement at the end that simply sets all the local variables to what you want them to be initially. Then save and test the piston. The variables will now be initialized. Then edit and re-enable the disabled statements and delete the temporary initialization statements at the end (or disable them in case you think you might ever need them later.) Kind of a kludge, but it works. :slight_smile:


#4

I’m not in front of my PC right now but I’m fairly sure you can set the value of a variable (local) from the piston overview page.

Haven’t tried it but I’m also fairly sure this doesn’t set the value in the define section permanently in the same way as setting it in the piston editor does.


#5

D’oh! Now I feel stupid. You’re absolutely right. And I knew that, too, so why did I waste my time … I guess sometimes one gets too focused on something … yeah, that’s my story and I’m sticking to it.

BTW that reminds me - it would be nice to be able to change global variables that way, too.


#6

Sweet, thanks for the responses guys…very helpful!