Asynchronous shell command example
Posted: Monday 11 January 2021 20:06
This is my example that seems to work very well: I have a python plugin that reads data from a solar controller and sent to domoticz, sometimes service lost connection from server and don´t restart, using the help of dzvents and asynchronous shell command now you can restart the service daemon if, temperature from storage tank or solar collector do not update at the time you determine.
It can be used for other uses. Here it is::
It can be used for other uses. Here it is:
Code: Select all
local scriptVar = 'Restart service daemon'
return
{
on =
{
timer =
{
'every 30 minutes', -- for DEBUG
},
shellCommandResponses =
{
"myTrigger"
},
httpResponses =
{
scriptVar .. '*',
},
},
logging =
{
level = domoticz.LOG_DEBUG ,
marker = scriptVar,
},
execute = function(dz, item)
local temperature = dz.devices(594) -- your temp device you want control
local lastUpdate = 0
local function makeTimeStamp(dateString, pattern)
local pattern = pattern or "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+)" -- %d+ = 1 or more digits , %.* = 0 or more any character
local year, month, day, hour, minute = dateString:match(pattern)
return os.time({year = year, month = month, day = day, hour = hour, min = minute})
end
local function triggerJSON(id, delay)
url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=temp&idx=' .. id .. '&range=day'
dz.openURL({ url = url, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
--/json.htm?type=graph&sensor=temp&idx=IDX&range=day
end
-- main
if item.isTimer then
triggerJSON(temperature.id)
return
elseif item.isShellCommandResponse then
dz.log ("Response object: " .. item.data, dz.LOG_INFO)
end
if item.ok and item.isJSON then
rt = item.json.result
for index, record in ipairs(rt) do
if index == #rt then
lastUpdate = dz.time.dDate - makeTimeStamp(record.d)
if lastUpdate > 420 then -- lastUpdate higher than 420 second, 7 minutes
dz.executeShellCommand({
command = "sudo systemctl restart nabto",
callback = "myTrigger",
timeout = 2, -- max execution time in seconds
})
end
end
end
else
dz.log('Problem retrieving data..',dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}