While loop running one too many times


#1

1) Give a description of the problem
I have a while loop that is supposed to toggle my lights every 50 min from sunset till 11pm. At 11pm I want the loop to stop and all my lights turn off. It seems if the time goes past 11pm while the wait is active the lights won’t turn off until the wait is over and the last 2 nights the sms message ran saying the lights toggled after 11pm but they really didnt.


#2

does this work better?

   define
      boolean sms
   end define

    while all of presence sensor 2 and presence sensor 4 presence are not present
          and
          time is between sunset and 11:00pm
       with switches
          toggle
          set sms = true
          wait 50 minutes
       end with
    end while
    if sms == true
       with switches
           turn off
           send sms
           set sms = false
       end with
   end if

#3

I this is a better setup now. I changed it so hopefully the while loop only executes if everyone is away and the time is between sunset and 11pm. Also the loop will only wait 50 minutes if the time is before 10:10pm so the wait activate and cause the loop to run after 11pm. If the time is after 10:10 then the last statements should run turning everything off and send a SMS saying so. This adds the additional benefit of turning everything off at a random time between 10:10 and 11pm hopefully.


#4

ok. heres another way to do the same, i have included the randomness in the wait.

   define
      boolean sms
   end define

    while all of presence sensor 2 and presence sensor 4 presence are not present
          and
          time is between sunset and 11:00pm
       with switches
          toggle
          set sms = true
          wait random(20,50) minutes
       end with
    end while
    if sms == true
       with switches
           turn off
           send sms
           set sms = false
       end with
   end if

#5

heres the example piston if you did want to try it:


#6

What does the random(20, 50) do exactly? Is there a link to something on how random numbers work?


#7

It will generate a number between 20 and 50

https://wiki.webcore.co/Functions#random