Page 1 of 1

Howto? Device ON, execute script after x seconds

Posted: Saturday 12 December 2020 8:59
by BarryT
Hi,

I just want a lua script that will execute a script after turning on an device..
For example, if an device has turned on, it has at least the need for 30 seconds to connect to wi-fi, after that i can execute my script.
Is it possible to add a sleep in lua, or something?

Thanks!!

Re: Howto? Device ON, execute script after x seconds

Posted: Saturday 12 December 2020 10:12
by waaren
BarryT wrote: Saturday 12 December 2020 8:59 For example, if an device has turned on, it has at least the need for 30 seconds to connect to wi-fi, after that i can execute my script.
Is it possible to add a sleep in lua, or something?
In classic Lua you need a dummy device to do this because the script will be canceled by domoticz after 10 seconds. In dzVents (100% Lua) you could do it without an extra dummy, using emitEvent() triggering a customEvent.

Re: Howto? Device ON, execute script after x seconds

Posted: Saturday 12 December 2020 17:22
by BarryT
waaren wrote: Saturday 12 December 2020 10:12
BarryT wrote: Saturday 12 December 2020 8:59 For example, if an device has turned on, it has at least the need for 30 seconds to connect to wi-fi, after that i can execute my script.
Is it possible to add a sleep in lua, or something?
In classic Lua you need a dummy device to do this because the script will be canceled by domoticz after 10 seconds. In dzVents (100% Lua) you could do it without an extra dummy, using emitEvent() triggering a customEvent.
Thanks for answering!
Could you share the script for a classic lua then?
And what is the meaning of the dummy device compared to a normal device to do this?

Re: Howto? Device ON, execute script after x seconds

Posted: Saturday 12 December 2020 21:13
by waaren
BarryT wrote: Saturday 12 December 2020 17:22 What is the meaning of the dummy device compared to a normal device to do this?
A dummy (virtual) device does not send a real command to a physical device.
Could you share the script for a classic lua then?
Could look like this

Code: Select all

local myWifiConnectedDevice = 'WifiConnectedDevice' -- change to name of your wifi connected device
local dummy = 'virtual switch'  -- change to name of your virtual (dummy) helper device

local delay = 30 -- set to required delay
local debug = true -- set to false when everything is OK


commandArray = {}

    local function dPrint(data)

        local function tPrint (t, indent, done)  -- table print function for debugging purposes
            local done = done or {}
            indent = indent or 0
             for key, value in pairs (t) do
                pre = (string.rep (" ", indent)) -- indent it
                if type (value) == "table" and not done[value] then
                    done [value] = true
                    print (pre .. tostring (key) .. ":");
                    tPrint (value, indent + 2, done)
                elseif type(value) == 'function' and not done[value] then
                    print( pre .. (tostring (key) .. "()"))
                else
                    pre = pre .. (tostring (key) .. ": ")
                    print (pre .. tostring(value))
                end
            end
        end
    
        if debug and type(data) ~= 'table' then
            print('Debug: ' .. tostring(data) )
        elseif debug then
            tPrint(data)
        end

    end

    local function state (name)
        if devicechanged[name]  then return devicechanged[name], true
        elseif otherdevices[name] then return otherdevices[name], false
        else return false, false
        end 
     end

    local function isChanged(name)
        return devicechanged[name] ~= nil
    end

    local function afterSec(switch, action, delay)
        commandArray[switch] = action .. ' AFTER ' .. delay
        dPrint(switch .. ' will be set to ' .. action .. ' after ' .. delay .. ' seconds.')
    end

    if isChanged(myWifiConnectedDevice) and tostring( state(myWifiConnectedDevice) ) == 'On' then
        afterSec(dummy, 'On', delay )
        return
    elseif isChanged(dummy) and tostring( state(dummy) ) == 'On' then
        dPrint(devicechanged[dummy])
    else
        return 
    end

-- Rest of your script (to be executed 30 seconds after 'myWifiConnectedDevice' is activated below this line.

return commandArray


Re: Howto? Device ON, execute script after x seconds

Posted: Sunday 13 December 2020 14:48
by BarryT
waaren wrote: Saturday 12 December 2020 21:13
BarryT wrote: Saturday 12 December 2020 17:22 What is the meaning of the dummy device compared to a normal device to do this?
A dummy (virtual) device does not send a real command to a physical device.
Could you share the script for a classic lua then?
Could look like this

Code: Select all

local myWifiConnectedDevice = 'WifiConnectedDevice' -- change to name of your wifi connected device
local dummy = 'virtual switch'  -- change to name of your virtual (dummy) helper device

local delay = 30 -- set to required delay
local debug = true -- set to false when everything is OK


commandArray = {}

    local function dPrint(data)

        local function tPrint (t, indent, done)  -- table print function for debugging purposes
            local done = done or {}
            indent = indent or 0
             for key, value in pairs (t) do
                pre = (string.rep (" ", indent)) -- indent it
                if type (value) == "table" and not done[value] then
                    done [value] = true
                    print (pre .. tostring (key) .. ":");
                    tPrint (value, indent + 2, done)
                elseif type(value) == 'function' and not done[value] then
                    print( pre .. (tostring (key) .. "()"))
                else
                    pre = pre .. (tostring (key) .. ": ")
                    print (pre .. tostring(value))
                end
            end
        end
    
        if debug and type(data) ~= 'table' then
            print('Debug: ' .. tostring(data) )
        elseif debug then
            tPrint(data)
        end

    end

    local function state (name)
        if devicechanged[name]  then return devicechanged[name], true
        elseif otherdevices[name] then return otherdevices[name], false
        else return false, false
        end 
     end

    local function isChanged(name)
        return devicechanged[name] ~= nil
    end

    local function afterSec(switch, action, delay)
        commandArray[switch] = action .. ' AFTER ' .. delay
        dPrint(switch .. ' will be set to ' .. action .. ' after ' .. delay .. ' seconds.')
    end

    if isChanged(myWifiConnectedDevice) and tostring( state(myWifiConnectedDevice) ) == 'On' then
        afterSec(dummy, 'On', delay )
        return
    elseif isChanged(dummy) and tostring( state(dummy) ) == 'On' then
        dPrint(devicechanged[dummy])
    else
        return 
    end

-- Rest of your script (to be executed 30 seconds after 'myWifiConnectedDevice' is activated below this line.

return commandArray

Thanks Waaren, works perfect!