adding a delay to code

What is the simplest way to add a delay to some luup code? i have read that luup.sleep is not a good candidate since it pauses all execution. i was trying to muck around with luup.call_delay, but it is very limited since i can only pass a single string as a variable. there has to be a better way, but in pouring throught the boards, i cannot seem to find one.

Just for clarification, i am trying to cobble together a couple of calls to sonos to play a mp3 and then speak some text, but since they happen so close together the text call one kills the mp3.

luup.sleep() is indeed to be avoided at all costs. luup.call_delay() is fine for your needs if you simply arrange that the data you want to share is within scope of the called routine. No need to limit yourself to the information you can pass in the single string variable. Equally, you could use that variable if you serialised the data structure you needed with, for example, a json.encode() call.

thanks for the prompt reply! i moved the variables out of the function (i.e. made them global in my file) and then created a function to speak that i call via the luup.call_delay. the code is as follows:

speakText = "Ooops, nothing passed"
speakVol = 50

function saySomething(callText, callVol)
    	speakText = callText

	if callVol == nil then
      	speakVol = 50
    	speakVol = callVol
    luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "PlayURI", {URIToPlay="", Volume=speakVol}, 59)
    luup.call_delay("sonosSpeak", 4, "")

function sonosSpeak()
    luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text=speakText, Language="en-GB", Volume=speakVol}, 59)

for some reason this throws an error that i tried to call a function with a nil value. i suspect it has somethign to do with the scope, but i can’t figure out where i am going wrong with this. here is the error:

LuaInterface::CallFunction_Timer-5 function sonosSpeak failed attempt to call a nil value 

Where are you trying to run this code?
It’s not, perchance, in a chunk starting with a “module” statement, or nested within another function?

Yup, all these functions are in a file i use to hold all my code. the file starts with:

module("sebbyLuaCode", package.seeall)

in my startup lua i call for it as such:

sebbyCode = require("sebbyLuaCode")

So this means that your global function is not truly global, but merely available globally as “sebbyCode.sonosSpeak”. To make the function visible to call_delay, you need to define it as

function _G.sonosSpeak ()

but the delay call should remain as you have it already, simply “sonosSpeak”.

well, that did the trick, THANK YOU!!!

… so if i define a function in a module, it is not truly global? trying to understand the reasoning behind this…

Actually, there really is no such thing as a global… everything is within closures. Within a module, that’s it. Dig into the ‘Programming in Lua’ book*, or the Reference Manual, to go deeper into this. Properly understanding closures and variable scope is really essential.

  • Chapter 14, “The Environment”