Callback functions

Hi,

I am working on a plugin that uses callback functions such as luup.variable_watch(…) and luup.call_delay(…) and the calls looks like (it’s now hardcoded to drill down the problem):

[code]luup.variable_watch(‘watchSensorVariable’, “urn:micasaverde-com:serviceId:EnergyMetering1”, “Watts”, 12)

luup.call_delay(‘publishPing’, 15, “”, false)[/code]

Callback functions looks like:

[code]function publishPing()
luup.log(“Publish ping message”, 1)
– more code
end

function watchSensorVariable(deviceNo, serviceId, variableName, oldValue, newValue)
luup.log(“watchSensorVariable”,1)
– more code
end[/code]

The problem is that the functions aren’t called and the following get logged:

LuaInterface::CallFunction_Variable func: watchSensorVariable Device_Variable 14 urn:micasaverde-com:serviceId:EnergyMetering1:Watts failed attempt to call a nil value ... LuaInterface::CallFunction_Timer-5 function publishPing failed attempt to call a nil value

I have the code in a .lua file referenced in I_.xml and lua file contains
module(“L_MyModule”, package.seeall)

Do you have any idea why it doesn’t work?

Add:

_G.watchSensorVariable = watchSensorVariable

This adds it to the Global Table definition. Which is where Vera is looking for it.
Defining a function in a Module does not add it to the Global table.

Thanks, it’s now solved.
I have tried it before but with different syntax

_G["watchSensorVariable"] = watchSensorVariable

I think the main problem was that I did it on the module level, now I have it in startup method.

Shouldn’t it work to have it on module level?

Yes … but it is position dependent … It needs to be defined at the time you do this.