Page 1 of 1

greenhouse circulating fan

Posted: Tuesday 29 September 2020 9:07
by abdolhamednik
hello everyone
:)

a circulating fan should work for 5 minutes and stop working for 15 minutes in a greenhouse, only when the exhaust fan is off for some time, i.e. 20 minutes. By the way, the code below is a "time" event, running every minutes
i don't understand why this simple code doesn't work. Can u plz help me

Code: Select all

function timedifference(s)
 year = string.sub(s, 1, 4)
 month = string.sub(s, 6, 7)
 day = string.sub(s, 9, 10)
 hour = string.sub(s, 12, 13)
 minutes = string.sub(s, 15, 16)
 seconds = string.sub(s, 18, 19)
 t1 = os.time()
 t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
 difference = os.difftime (t1, t2)
 return difference
end
--------------------------------------------------------
commandArray = {}

Smart = tonumber(uservariables['Smart'])
if Smart == 0 then --zero means smart system should be inactive
    return commandArray
end

crtoggle = tonumber(uservariables['crtoggle']) --a parameter

    if devicechanged['fan1'] == 'On' then
        commandArray['circule1'] = 'Off'
    elseif otherdevices['fan1'] == 'Off' then
        if timedifference(otherdevices_lastupdate['fan1']) > 1200 then
            if otherdevices['circule1'] == 'On' then
                if crtoggle == 5 then
                    commandArray['circule1'] = 'Off'
                    crtoggle = 0
                end
                crtoggle = crtoggle + 1
            else
                if crtoggle == 15 then
                    commandArray['circule1'] = 'On'
                    crtoggle = 0
                end
                crtoggle = crtoggle + 1
            end
        end
    end
commandArray['Variable:crtoggle'] = tostring(crtoggle)

return commandArray

Re: greenhouse circulating fan

Posted: Tuesday 29 September 2020 10:59
by waaren
abdolhamednik wrote: Tuesday 29 September 2020 9:07 i don't understand why this simple code doesn't work. Can u plz help me
Lua scripts triggered by time do not receive a devicechanged table.
os.difftime is a function so you will have to enclose the time table {year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
in ()
I added some debug print statement in below script. It might help in finding logical errors (if any)

Code: Select all

--[[
        a circulating fan should work for 5 minutes and stop working for 15 minutes in a greenhouse, 
        only when the exhaust fan is off for some time, i.e. 20 minutes. 
        running every minutes.
]]--

local debug = true

local function dPrint(str)
   if debug then print('Debug: *****  ' .. str ) end 
end 

local function timedifference(s, pattern)
    local pattern = pattern or  "(%d+)%-(%d+)%-(%d+)%s(%d+):(%d+):(%d+)"
    local year, month, day, hour, minutes, seconds = s:match(pattern)
    local t1 = os.time()
    local t2 = os.time({year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds})
    return os.difftime (t1, t2)
end
   
--------------------------------------------------------
commandArray = {}

local Smart = tonumber(uservariables['Smart'])
if Smart == 0 then --zero means smart system should be inactive
    dPrint ('--------- Smart is 0; stop script' )
    return commandArray
end

local crtoggle = tonumber(uservariables['crtoggle']) --a parameter

if otherdevices['fan1'] == 'On' then
    commandArray['circule1'] = 'Off'
    dPrint ('--------- fan1 changed to off' )
elseif otherdevices['fan1'] == 'Off' then
    dPrint ('--------- fan1 still off' )
    if timedifference(otherdevices_lastupdate['fan1']) > 1200 then
        dPrint ('--------- fan1 not changed for 1200 seconds' )
        if otherdevices['circule1'] == 'On' then
        dPrint ('--------- circule1 is On' )
            if crtoggle == 5 then
                dPrint ('--------- crtoggle is 5 set circule1 to Off' )
                commandArray['circule1'] = 'Off'
                crtoggle = 0
            end
            crtoggle = crtoggle + 1
        else
            dPrint ('--------- circule1 is Off' )
            if crtoggle == 15 then
                dPrint ('--------- crtoggle is 15 set circule1 to On' )
                commandArray['circule1'] = 'On'
                crtoggle = 0
            end
            crtoggle = crtoggle + 1
        end
    end
end

commandArray['Variable:crtoggle'] = tostring(crtoggle)

-- dump commandArray to log
for key, value in pairs(commandArray) do
   dPrint('commandArray : ' .. tostring(key) .. ': ' .. tostring(value)) 
end    


return commandArray

Re: greenhouse circulating fan

Posted: Tuesday 29 September 2020 13:09
by abdolhamednik
:D
thanks waaren
you are right. it worked fine