I have a LUA script that is triggered by a changed user variable. Works great when I manually change the variable.
Then I have another script that is triggered by time, and in some circumstances changes the user variable in order to trigger the first script.
I can see the user variable being changed by that second script, but the first script is NOT triggered.
Is this an error on my side? Or a bug Or is it "as designed"?
LUA script not triggering other script
Moderator: leecollings
-
- Posts: 643
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: LUA script not triggering other script
I remember I had this kind of issue... Only solution I found at the time was doing the change action using HTTP/JSON API from Lua, not table driven action as usual.
May dig in my scripts to better remember this if needed but your observation ringed a bell...
- jvdz
- Posts: 2206
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: LUA script not triggering other script
Correct, and this is what sort of explains it:
Using a JSON/API link for updating solves that issue.Events
User variables can trigger events, and can also be read and updated through events. Whenever a variable is changed through the API or the webinterface, the event system is triggered. In Lua, scripts in the format script_variable_name.lua will be run, and Blockly will be checked for the relevant variable as well. In the event logic variables can be used and/or updated, but not added. Therefore if you wish to use variables in events, you have to create them first in the user interface. Note that setting a variable through an event does not trigger the event logic again since this can create a nasty loop.
For more info on using variables in events see Events
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 729
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: LUA script not triggering other script
OK, I guess I can try that.
Can some kind soul point me at an example of running a JSON command from within a LUA script?
Can some kind soul point me at an example of running a JSON command from within a LUA script?

Hans
-
- Posts: 643
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: LUA script not triggering other script
Hello,
Not exactly fits your needs because this was for updating a virtual sensor (for electric consumption data) but, to sum-up, use curl with the same arguments you would use trying using a web browser:
In this function, I can use normal commandArray table driven update (if useHttpApi parameter is set to false at function call) or http/json immediate update (if useHttpApi is true).
I use this for virtual kW meters that record true max power (in 10mn time-slices, to avoid domoticz only sampling instant values per 5mn slices in DB/Graphs, so mostly missing true min/max for devices with values that can change a lot in 5mn: Good for temperature, very very bad for power data!) from a power-meter that gets my whole home consumption. As I also use this virtual device to do some load limiting (shutting down heavy power drainers if needed to avoid overlapping my contract power budget & home shutdown), I needed something that triggers other scripts (setting power drainers off for some time) on current time slice intermediate max values & ran in similar issue as yours at the time.
The important call there is the
with prebuilt cmd string according to http/json API:
API documented here for all possible actions:
https://wiki.domoticz.com/Domoticz_API/JSON_URL%27s
All stuff around is just to see if command response from domoticz is OK processing curl command (you may also need to install curl if not present on system) text answer.
As I said, you can test the command from a web browser, just use the LAN IP of your domoticz host instead of the localhost from urlDz in script.
Take care using 'OpenURL' that can be CommandArray driven is commented: This was not working correctly, as stated in Lua code comments. So no way to avoid curl or similar system command...
Domoticz conception try to be a bit foolproof & avoid infinite loops that may result from incorrect use/scripting with such behavior. See also master/slave devices concept that is made almost useless for all in Domoticz to prevent misuses some may do, with no possible workaround than script all devices relationships! Here at least there is a solution.
Not exactly fits your needs because this was for updating a virtual sensor (for electric consumption data) but, to sum-up, use curl with the same arguments you would use trying using a web browser:
Code: Select all
-- Domoticz base url
urlDz='http://127.0.1.1:8080'
--
-- Update virtual sensor (using normal 'UpdateDevice' or http/JSON if useHttpApi set to true),
-- take care only useHttpApi will trigger other device script(s) on a virtual sensor/meter update.
-- Uses urlDz as global base URL variable for Domoticz (f.i: urlDz='http://127.0.1.1:8080').
--
local function updateNum(dev, value1, useHttpApi)
if (useHttpApi == true) then
local cmd = string.format(urlDz..'/json.htm?type=command¶m=udevice&idx=%d&svalue=%d', otherdevices_idx[dev], math.floor(value1))
--print(cmd) -- DEBUG
-- OpenURL (being async) may trigger multiple events if base HW retriggers before API call is processed => use curl.
-- table.insert (commandArray, { ['OpenURL'] = cmd } )
local handle = assert(io.popen('curl --retry 1 --connect-timeout 1 -m 3 \"'..cmd..'\"'))
result = handle:read('*all')
-- Returned JSON format : {"status" : "OK","title" : "Update Device"}
handle:close()
if not string.find(tostring(result), "OK") then
print('ERROR : '..cmd)
print('ERROR : updateNum/useHttpApi returned '..tostring(result))
end
else
local cmd = string.format('%d|0|%d', otherdevices_idx[dev], math.floor(value1))
--print(cmd) -- DEBUG
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
end
I use this for virtual kW meters that record true max power (in 10mn time-slices, to avoid domoticz only sampling instant values per 5mn slices in DB/Graphs, so mostly missing true min/max for devices with values that can change a lot in 5mn: Good for temperature, very very bad for power data!) from a power-meter that gets my whole home consumption. As I also use this virtual device to do some load limiting (shutting down heavy power drainers if needed to avoid overlapping my contract power budget & home shutdown), I needed something that triggers other scripts (setting power drainers off for some time) on current time slice intermediate max values & ran in similar issue as yours at the time.
The important call there is the
Code: Select all
(io.popen('curl --retry 1 --connect-timeout 1 -m 3 \"'..cmd..'\"')
Code: Select all
cmd = string.format(urlDz..'/json.htm?type=command¶m=udevice&idx=%d&svalue=%d', otherdevices_idx[dev], math.floor(value1))
https://wiki.domoticz.com/Domoticz_API/JSON_URL%27s
All stuff around is just to see if command response from domoticz is OK processing curl command (you may also need to install curl if not present on system) text answer.
As I said, you can test the command from a web browser, just use the LAN IP of your domoticz host instead of the localhost from urlDz in script.
Take care using 'OpenURL' that can be CommandArray driven is commented: This was not working correctly, as stated in Lua code comments. So no way to avoid curl or similar system command...
Domoticz conception try to be a bit foolproof & avoid infinite loops that may result from incorrect use/scripting with such behavior. See also master/slave devices concept that is made almost useless for all in Domoticz to prevent misuses some may do, with no possible workaround than script all devices relationships! Here at least there is a solution.
Who is online
Users browsing this forum: No registered users and 0 guests