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:
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
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
Code: Select all
(io.popen('curl --retry 1 --connect-timeout 1 -m 3 \"'..cmd..'\"')
with prebuilt cmd string according to http/json API:
Code: Select all
cmd = string.format(urlDz..'/json.htm?type=command¶m=udevice&idx=%d&svalue=%d', otherdevices_idx[dev], math.floor(value1))
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.