call_delay on a global function set?

Hey guys, wondering if someone would be willing to help with an issue I came up with. I’m trying to do basically a recursive call_delay. So the function basically calls itself. Problem is, it works when I’m testing the Lua, but it doesn’t work when I put it in “myLua” file and send it up. Says that the function is nil. Any ideas?

Thank you,

We may be able to help if you post the code and explain how you are testing it.

If you have a function in an LUA file … it is not in the global table.
It’s a global in the table that represents the interface for your LUA file.

call_delay looks for the function as a GLOBAL function.
To enter this, in the file where you define the function named XXXX, add the statement:
_G.XXXX = XXXX

Also remember that LUA is a “just in time” type compiler. Another words, things are not compiled and visible until they have to. (And all that compiled stuff gets discarded on reboot or LUA reload.)

So if you are referencing a function from a place that has not been executed yet, it won’t be known to LUA. For example defining a function in a scene, and referencing that function from another scene. Would not work unless the first scene is always run first.

The solution to that is to place that function in the Start up LUA code section. The start up code is always executed when the LUA engine starts (like after a reboot/LUA reload).

Thank you very much guys! Basically I was looking to adjust my RGBW LED strips very slowly… so I have a simple function that sets the lights dim value, then if the target was not met goes on to call it’s function again. What Richard posted was exactly what I needed.

The function looks like this :

function set_dim_target(device,target) 
	luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = tonumber(target)}, device)
end

function roll_up(device)
	set_dim_target(device,LEDLightRampSpeed)
	LEDLightRampSpeed = LEDLightRampSpeed + 1
	if (tonumber(LEDLightRampSpeed) < 100) then
		luup.call_delay('roll_up', 120)
	end
end

Going to play some tonight and try the global that Richard mentioned. Thank you again guys!

So it almost works. Code :

function roll_up(device)
	set_dim_target(device,LEDLightTarget)
	LEDLightTarget = LEDLightTarget + 1
	if (tonumber(LEDLightTarget) < 100) then
		_G.roll_up = roll_up
		luup.call_delay('roll_up', 1, device)
	end
end

When calling it, the first call works, then I seem to get :
GetLuaInterface can’t find device type: 4/0xabf798 str: 142

If I put “device = 142” in the first line, so I force it, it works as it’s supposed to. So something appears to be botching the device call after the first one.

Any ideas?

Suspect that you need to look at what is a number and what is a string and how they are consequently handled: eg newLoadlevelTarget and LEDLightTarget/LEDLightRampSpeed.

Looks like this would push the stack down by 100 iterations - not sure if that is OK either. Others will have a better clue on this.

[quote=“bigbasec, post:6, topic:184666”]So it almost works. Code :

function roll_up(device)
	set_dim_target(device,LEDLightTarget)
	LEDLightTarget = LEDLightTarget + 1
	if (tonumber(LEDLightTarget) < 100) then
		_G.roll_up = roll_up
		luup.call_delay('roll_up', 1, device)
	end
end

When calling it, the first call works, then I seem to get :
GetLuaInterface can’t find device type: 4/0xabf798 str: 142

If I put “device = 142” in the first line, so I force it, it works as it’s supposed to. So something appears to be botching the device call after the first one.

Any ideas?[/quote]

The device number in luup.call_action(…) needs to be a number type variable. If it is a string, it is interpreted as a UDN. This would fix it:

function set_dim_target(device,target) luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = tonumber(target)}, tonumber(device)) end