really simple code works unexpectedly

I define a scene and add some simple code. I click run and the code runs twice. I can see this fron reviewing the logs. Has anyone an explanation?

Here is the code:

luup.log(“startup testscene”)
local i = 1
luup.log(i)

luup.sleep(30000)
luup.log(“woken up”)

i = i +1
luup.log(i)
until i > 10
luup.log(i)
luup.log(“finished startup”)

I am not sure I know what you mean by runs twice … since you did not include the log … But putting a sleep for 30 seconds will likely cause Vera to restart …

Here’e part of the log. It runs twice. I thought it should stop at the end of the code.

09/15/13 13:07:54.196 luup_log:0: 1 <0x2e0ad680>
50 09/15/13 13:08:24.220 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:08:24.220 luup_log:0: 2 <0x2e0ad680>
50 09/15/13 13:08:54.250 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:08:54.250 luup_log:0: 3 <0x2e0ad680>
50 09/15/13 13:09:24.280 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:09:24.280 luup_log:0: 4 <0x2e0ad680>
50 09/15/13 13:09:54.310 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:09:54.310 luup_log:0: 5 <0x2e0ad680>
50 09/15/13 13:10:24.340 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:10:24.340 luup_log:0: 6 <0x2e0ad680>
50 09/15/13 13:10:54.370 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:10:54.370 luup_log:0: 7 <0x2e0ad680>
50 09/15/13 13:11:24.400 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:11:24.400 luup_log:0: 8 <0x2e0ad680>
50 09/15/13 13:11:54.430 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:11:54.430 luup_log:0: 9 <0x2e0ad680>
50 09/15/13 13:12:24.460 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:12:24.460 luup_log:0: 10 <0x2e0ad680>
50 09/15/13 13:12:54.490 luup_log:0: woken up <0x2e0ad680>
50 09/15/13 13:12:54.490 luup_log:0: 11 <0x2e0ad680>
50 09/15/13 13:12:54.490 luup_log:0: 11 <0x2e0ad680>
50 09/15/13 13:12:54.490 luup_log:0: finished startup <0x2e0ad680>
50 09/15/13 13:12:54.495 luup_log:0: startup testscene <0x2eac7680>
50 09/15/13 13:12:54.495 luup_log:0: 1 <0x2eac7680>

The Log does not match your code … It is printing woken up multiple times between printing the numbers as you increment them. Your code fragment does not show this.

This is probably old code … Since the following is bad code:

i = i +1 luup.log(i) until i > 10 luup.log(i)

Until is only valid as part of a Repeat construct. Repeat would need to be at the beginning of this fragment.

I would recommend getting a LUA intererpretter and play with it there.
See: Google Code Archive - Long-term storage for Google Code Project Hosting.

apologies…

Anyway I’ve replaced luup.sleep(30000) with

os.execute(“sleep 30”)

and my code snippet only runs once now(as it should)…

well sometimes only once… can it have something to do with ‘sleep’.

Am I using the right function, I just want the code to do nothing for a period of time, then continue from where it left off.

luup.log(“Harvester test startup”)
local i = 1
repeat
os.execute(“sleep 30”)
i = i +1;
until i > 10
luup.log(“finished startup”)
– **********

9/15/13 17:00:43.300 luup_log:0: Harvester test startup <0x2e689680>
50 09/15/13 17:05:43.460 luup_log:0: finished startup <0x2e689680>
50 09/15/13 17:05:43.470 luup_log:0: Harvester test startup <0x2d889680>
50 09/15/13 17:08:59.953 luup_log:17: NorthQ Monitor: Data 397.154 to 397.156 in 308 = 23 <0x2c089680>
50 09/15/13 17:10:43.641 luup_log:0: finished startup <0x2d889680>

Using sleep, in any form, in luup code is deadly.

Vera will “restart” if it sees luup code that takes longer than 60 seconds (UI5) or 30 seconds (UI4) to complete. It restarts the entire engine in order to avoid Vera locking up. You might get lucky, but the lockup detection code is implemented in a variety of the entry points.

Set a timer (call_timer), and have it call you back periodically on a public fn until the “activity” is complete. This avoids Vera ever seeing code running for a long time.

Ok so I now play with luup.call_timer but it’s behaviour is unexpected. The statement gets issued but the program continues and only later does the funtion get executed. This for me makes writing the logic difficult.

I cant figure out how to execute some code 10 times with 30 seconds intervals using the call_timer call. I think I may be missing a key concept here, but exactly what is my program supposed to be doing while it’s waiting for the timer to expire. I don’t want it to do anything, but I do want it to start executing the statement after the call to call_timer.

Can some one put me out of my misery please?

Your main program returns right away and does NOT block Vera from doing other useful things.
[hr]
Welcome to the world of event driven code … This is the world that most developers on small devices or devices witch interface to a user live.

It is mutch more complicated because you need to know/control the order of events. The current state is often communicated via arguments to the event.

In your case the information is the current count.
Your timer code is the event driven code … and it is called at a timer interval (30 Seconds) from now. You call it first with an argument of 0 (Countup, or 10 for a Countdown)

When the timer runs … it increments/decrements the counter as appropriate … does it’s work for that time interval … in your case log a message … than if it’s not the end of the sequence … it reschedules itself with the new count.