Asynchronous shell command example  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Asynchronous shell command example

Post 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
}
:
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command example  [Solved]

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Asynchronous shell command example

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest