Vera plugins may have variables and to monitor when variable values change you can register a (callback) function using variable_watch Luup function call.
For example, the call luup.variable_watch("var_watch", "urn:schemas-zerobrane-com:serviceId:SimplyVirtual1", nil, 4)
registers a callback function var_watch
that will be
called when any variable (nil
is passed for a variable name)
for the specified service (urn:schemas-zerobrane-com:serviceId:SimplyVirtual1
)
and specified device (4
) changes its value.
The callback function gets device, serviceid, and variable name as its first three parameters and also gets the old and the new values for the variable.
Debugging Lua code in a watch (variable_watch
) callback
The IDE provides two methods to debug Lua code for events triggered from the Vera device: scenes, plugins, watch, request, and others.
In this example we will be using the simpler method that doesn’t require specifying the IP address of the computer that runs the IDE:
- Open a new editor tab in the IDE, add the following code and save it in a file. In this example I am using device 4 and a particular serviceId, but you can use whatever device and service you have.
function var_watch(lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
require('mobdebug').start() --<-- start debugging
print(lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)
require('mobdebug').done() --<-- stop debugging
end
luup.variable_watch("var_watch", "urn:schemas-zerobrane-com:serviceId:SimplyVirtual1", nil, 4)
-
Start debugging in the IDE by selecting
Project | Start Debugging
and then run the script usingProject | Continue
. -
Point your browser to the URL that sets new variable value (it is using variableset Luup request). Make sure to change the IP address to the address of your Vera device. Note that the callback is only triggered when the value changes, so you may want to change the
Value
parameter in the URL.
You should now see the debugging suspended at the “print” line. You can then step through the code (and will see the values printed to the output window), inspect variables, and use other debugging functions.
This method only works until the Luup engine is restarted (and in some cases it can be restarted by the Vera device without warning). If you can’t get the debugging started in the IDE, try running the second step one more time.