luup.sleep()

Is luup.sleep() (http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_sleep) working for anyone?

That fn was only just added to the Doc, along with a few extra parameters for some methods. I doubt they’re in general release right now.

BTW: I subscribe with an RSS News reader to the RSS Feed hanging off of:

http://wiki.micasaverde.com/index.php/Special:RecentChanges

so I can follow along as stuff is added.

luup.sleep() does work in firmware version 1.1.1155.

Is there an alternative for the current fw?

Perhaps I’m going about this the wrong way?.. I’m looking to turn a lamp module (bedside light) off X minutes after it is turned on manually by pressing the button or via Vera.

So I figured I’d create a scene that watches this lamp module and when it turns on, I’d have the luup script sleep X milliseconds before the “lamp off” command in the scene finally turned off the lamp.

Thx,
BrianAz

Is there an alternative for the current fw?[/quote]

This didn’t seem to work, but it didn’t error on me like sleep() did.

luup.call_delay("luup.inet.wget",1,"http://192.168.1.135/GrowlWebBridge/default.aspx?Title=The+Sun&Text=Set&App=VeraNotify",true) return true

Create a scene triggered by an Event. Select the Event tab, click the Add Event button, pick your device, select the trigger “A device is turned on or off,” give it a name, which mode set to “on.” Now go to the Commands tab, find the device, select “unchanged” or “on” then pick “after X minutes” and “off.”

Thanks! This got the light turning off like I wanted.

Now, the next question… :slight_smile: Is there any way to have the scene run LUUP code after or just before it issues the Off command? I pipe my Vera actions through Growl and it’s sending me the notification when the light is turned on vs when it’s turned off. Would be nice to have a “Luup command” option also.

You could leave the Event in the scene, remove the commands in the Command tab and add the following code in the Luup tab.

[code]luup.call_delay(“switch_off”, 30, “”)

function switch_off (empty)
–perform some other commands here
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=0 },21)
–and/or perform other commands here
end[/code]

Change the 30 to the number of seconds delay and the 21 to the device id you want turned off. Any additional commands you want run after the delay could be added before or after the off command.

Thanks! I’ll give it a try in the morning.

what is the maximum interger you can pass to luup.delay().
I passed 1105358400000, and lua restarted!

How to create long delay. I don’t want to use call_delay()

Thanks

Not sure what you want to do, but having a long delay like that is not good! I would advise against very long delays.

  • Garrett

If a block of Lua code takes longer than 30 seconds (UI4) or 60 seconds (UI5) to complete then Vera will restart the LuaUPnP Process… It’s assuming it’s gotten into a bad state.

Look a few posts up for the example of how to do it using luup.call_delay().

thanks. I read the forum and did that. But now I face a different problem.

I have actually a long scene like:
start()
delay()
stop()

I changed it and no I do it like
start()
call_delay(“stop”)

but now the problem is that my start() function takes more than a minute. The reason is that I’m turning an electric motor on and I need to wait to be able to measure the power to makes sure is has turned on. If it is not on, I need to repeat. This procedure takes more than a minute.

I don’t think I can use call delay on this one.
Is there any other way to do this on Vera?

thanks

If you use luup.call_delay(…) instead of luup.sleep(…) inside your start() function, Vera will not force a restart after 60 seconds. You can use a sequence of functions called by luup.call_delay(…) to implement your process.

inside start() I cannot use call_delay() because call_delay() returns immediately. I actually need to wait 30+ seconds to perform the next operation inside my start() function.
How about calling the whole scene with a call_delay()?. Something like this.

call_delay(“myfunc”, 1) – call myfunc() one second after entering the scene

myfunc()
start() --takes 3 minutes
luup.sleep() --takes few hours
stop() – takes 3 minutes
end

now, the scene returns immediately.

Your scene will never finish if it takes over a minute to run. Vera will issue a restart of the luup engine.

  • Garrett

[quote=“garrettwp, post:16, topic:167282”]Your scene will never finish if it takes over a minute to run. Vera will issue a restart of the luup engine.

  • Garrett[/quote]

Then, how to run a piece of Lua code that takes more than one minute and needs to call sleep() functions?

You break up the single function into a sequence of smaller ones.

The smaller functions should execute very quickly than call luup.delay to call the next step.

This is known as event driven programming. It is often more difficult for non-programmers because of what appears to be the fragmenting of the algorithm. But this is used in almost all modern programming environments.

then how do you exchange information between those smaller functions? use global variables? how do you know a smaller function has run successfully or not?

if yes, where do you keep global variables in Vera to be accessible to multiple scenes?

Windows is event driven, but it has whole support for it. Vera has no mechanism to create, generate and manage events. It may not be very efficient to create and mange your own event driven system, because you want to turn a pump on and you need to make sure it was actually turned on :slight_smile:

Since you are Vera expert, let me ask you a question please. How would you suggest to to run a code like this in Vera?

myfunc()
rc=start() --takes 3 minutes
if(rc==false) return false

t= compute_running_time()
luup.sleep(t) --takes few hours

stop() – takes 3 minutes
end

start()
for i=1, 3 do
start_motor()
wait(one minute)
p= get_power()
if(p>400)
return true
end
return false
end

Thanks

then how do you exchange information between those smaller functions? use global variables? how do you know a smaller function has run successfully or not?
Global variables are a simple solution for this. You could also use a sequence of scenes and only trigger the next scene if the previous function ran successfully.
if yes, where do you keep global variables in Vera to be accessible to multiple scenes?
Variables defined in [i]Startup Lua[/i] will be global and available to all scene Luup code.
How would you suggest to to run a code like this in Vera?
I would use PLEG.

You can do it with Luup, though. As suggested by @RichardTSchaefer, make each step a function that calls luup.call_delay(…) to trigger the next phase. It can all be in one scene’s Luup - the delayed functions will still be run even though the scene’s main code exited after calling the first delay. Instead of using a for loop, set the required count in a global variable and have the start_motor function manage this counter. start_motor calls a delay for check_power which, if the motor isn’t running, calls a delay for start_motor.