Page 1 of 1

Asynchronous shell command example

Posted: Monday 11 January 2021 20:06
by javalin
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:

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
}
:

Re: Asynchronous shell command example  [Solved]

Posted: Monday 11 January 2021 20:21
by waaren
javalin wrote: 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.
Any reason why you use the JSON? the lastUpdate is native available in dzVents

Code: Select all

local scriptVar = 'Restart service daemon'

return
{
    on =
    {
        timer =
        {
            'every 30 minutes', -- for DEBUG
        },
 
        shellCommandResponses = 
        { 
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG ,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local temperature = dz.devices(594) -- your temp device you want control

        -- main
        if item.isTimer and temperature.lastUpdate.minutesAgo > 7 then 
            dz.executeShellCommand(
            {
                command = "sudo systemctl restart nabto",
                callback = scriptVar,
                timeout = 2, -- max execution time in seconds
            })
        elseif item.isShellCommandResponse then
            dz.log ("Response object: " .. item.data, dz.LOG_INFO)
        end
    end
}

Re: Asynchronous shell command example

Posted: Monday 11 January 2021 22:56
by javalin
Any reason why you use the JSON? the lastUpdate is native available in dzVents
Thanks waaren. I have not applied it due to my ignorance. It would have saved me a few lines, at least it was fun getting it. :D