Airflow - problem with script

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

Moderator: leecollings

Post Reply
rmacuda
Posts: 3
Joined: Tuesday 25 May 2021 18:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Airflow - problem with script

Post by rmacuda »

Hi
i tried to adopt one of script to my purposes:

Code: Select all

-- Temp control

local scriptVar = 'tempControl3'

return
{
    on =
    {
        timer =
        {
            'every minute',   -- domoticz events based on timers are triggered once a minute max.
        },
        customEvents = -- used to increase frequency to once every 30 seconds.
        {
            scriptVar,
        }
    },

        logging =   {
                        level         =   domoticz.LOG_DEBUG, -- If set to domoticz.LOG_ERROR you will only see something if dzVents encountered an error
                                                               -- So best to start with domoticz.LOG_DEBUG until your script works as expected.
                        marker        =   scriptVar,
                    },

           data =   {
                        lowTemperature  = { initial = true },
                    },

    execute = function(dz, item)

        if item.isTimer then
            dz.emitEvent(scriptVar).afterSec(30) -- this will trigger the customEvent in the On section and therewith increase the frequency to every 30 seconds
        end


        local insideTemperature = dz.devices('TempInside').temperature
        local outsideTemperature = dz.devices('TempOutside').temperature
        local stfan = dz.devices('FanIn')
        local safan = dz.devices('FanOut') -- this second use of 'switch' as varname does overwrite the previous one.

        local stfanIsOff = stfan.state == 'Off'
        local safanIsOff = safan.state == 'Off'
       
        local lowerTemperature = 25
        local higherTemperature = 45
        local comingFromLow = dz.data.lowTemperature
        local temperatureInRange = insideTemperature > lowerTemperature and insideTemperature < higherTemperature


        dz.data.lowTemperature = insideTemperature < lowerTemperature                   -- remember coming from low

        if stfanIsOff and safanIsOff and temperatureInRange and comingFromLow then              -- between 25-45 and coming from < 25 ==>> switchOn
          if insideTemperature < outsideTemperature then stfan.switchOn()
          elseif insideTemperature > outsideTemperature then safan.switchOn()
              end
        elseif not temperatureInRange then                    -- > 24 or < 20 switchOff
                if not stfanIsOff or not safanIsOff then 
                    safan.switchOff() 
                    stfan.switchOff() 
                end
            if insidetemperature > higherTemperature then                               -- No longer coming from low
                dz.data.lowTemperature = false
            end
        end
    end
}                           
After I manualy switch on fans and temperature(inside) falls below 25 degrees script can switch off, but there is no way to automaticaly switch them on after reaching 25 degrees
The setup is:
2 temperature sensors, and 2 fans ... the trick is that need to compare temperatures inside room and outside not to pump warmer air inside room in that case need to run fan pumping air from basement


Believe you can help
regards
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Airflow - problem with script

Post by waaren »

rmacuda wrote: Thursday 27 May 2021 19:00 I tried to adopt one of script to my purposes:
First: please do not crosspost. Crossposts will be deleted by a moderator.

If you need to debug a script a first step is always to look at log (and add some debug log statements when helpful)
Please try below script a couple of runs and share the produced loglines. Also explain what you expected to happen against what the script does (or does not)

Code: Select all

-- Temp control

local scriptVar = 'tempControl3'

return
{
    on =
    {
        timer =
        {
            'every minute',   -- domoticz events based on timers are triggered once a minute max.
        },
        customEvents = -- used to increase frequency to once every 30 seconds.
        {
            scriptVar,
        }
    },

        logging =   {
                        level         =   domoticz.LOG_DEBUG, -- If set to domoticz.LOG_ERROR you will only see something if dzVents encountered an error
                                                               -- So best to start with domoticz.LOG_DEBUG until your script works as expected.
                        marker        =   scriptVar,
                    },

           data =   {
                        lowTemperature  = { initial = true },
                    },

    execute = function(dz, item)

        if item.isTimer then
            dz.emitEvent(scriptVar).afterSec(30) -- this will trigger the customEvent in the On section and therewith increase the frequency to every 30 seconds
        end

        dz.utils.dumpTable(dz.data)

        local s = {}
        s.insideTemperature = dz.devices('TempInside').temperature
        s.outsideTemperature = dz.devices('TempOutside').temperature
        local stfan = dz.devices('FanIn')
        local safan = dz.devices('FanOut') -- this second use of 'switch' as varname does overwrite the previous one.

        s.stfan = stfan.state
        s.safan = safan.state
        s.stfanIsOff = stfan.state == 'Off'
        s.safanIsOff = safan.state == 'Off'

        s.lowerTemperature = 25
        s.higherTemperature = 45
        s.comingFromLow = dz.data.lowTemperature
        s.temperatureInRange = s.insideTemperature > s.lowerTemperature and s.insideTemperature < s.higherTemperature


        dz.data.lowTemperature = s.insideTemperature < s.lowerTemperature                   -- remember coming from low

        if s.stfanIsOff and s.safanIsOff and s.temperatureInRange and s.comingFromLow then              -- between 25-45 and coming from < 25 ==>> switchOn
            dz.log('------------ 1 ', dz.LOG_DEBUG)
            if s.insideTemperature < s.outsideTemperature then
                dz.log('------------ 2 ', dz.LOG_DEBUG)
                stfan.switchOn()
            elseif s.insideTemperature > s.outsideTemperature then
                dz.log('------------ 3 ', dz.LOG_DEBUG)
                safan.switchOn()
            end
        elseif not s.temperatureInRange then                    -- > 24 or < 20 switchOff
            dz.log('------------ 4', dz.LOG_DEBUG)
            if not s.stfanIsOff or not s.safanIsOff then
                dz.log('------------ 5 ', dz.LOG_DEBUG)
                safan.switchOff()
                stfan.switchOff()
            else
                dz.log('------------ 6 ', dz.LOG_DEBUG)
            end
        else
            dz.log('------------ 7 ', dz.LOG_DEBUG)
        end

        dz.utils.dumpTable(s)
        dz.utils.dumpTable(dz.data)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rmacuda
Posts: 3
Joined: Tuesday 25 May 2021 18:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Airflow - problem with script

Post by rmacuda »

waaren wrote: Thursday 27 May 2021 20:27 If you need to debug a script a first step is always to look at log (and add some debug log statements when helpful)
Please try below script a couple of runs and share the produced loglines. Also explain what you expected to happen against what the script does (or does not)
Hi
log output:

2021-05-28 21:45:00.340 Status: dzVents: Info: tempControl3: ------ Start internal script: TestScript:, trigger: "every minute"
2021-05-28 21:45:00.342 Status: dzVents: Info: tempControl3: ------ Finished TestScript
2021-05-28 21:45:00.343 Status: EventSystem: Script event triggered: /domoticz/dzVents/runtime/dzVents.lua
2021-05-28 21:45:00.342 Error: dzVents: Error: (3.1.7) tempControl3: An error occurred when calling event handler TestScript
2021-05-28 21:45:00.342 Error: dzVents: Error: (3.1.7) tempControl3: /domoticz/dzVents/runtime/Utils.lua:579: bad argument #1 to 'pairs' (table expected, got boolean)
rmacuda
Posts: 3
Joined: Tuesday 25 May 2021 18:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Airflow - problem with script

Post by rmacuda »

Hi I've returned to somewhat my initial script and found that problem is in 'commingFromLow' logic .. in fact I also needed some hysteresis to avoid frequent on/offs near cut temperature so lets say:

I need to run one of fans when temperature reaches lest say 27 degrees but to avoid on/off actions beetween 26.9 and 27.1 degrees I needed to set offTemprature at 26 degrees. I also check if outside temperature is not higher than inside -then run InFan in other case i run OutFan.
So I tried this script and it is somehow working

Code: Select all

-- Temp control

local scriptVar = 'tempControl3'

return
{
    on =
    {
        timer =
        {
            'every minute',   -- domoticz events based on timers are triggered once a minute max.
        },
        customEvents = -- used to increase frequency to once every 30 seconds.
        {
            scriptVar,
        }
    },

        logging =   {
                        level         =   domoticz.LOG_DEBUG, -- If set to domoticz.LOG_ERROR you will only see something if dzVents encountered an error
                                                               -- So best to start with domoticz.LOG_DEBUG until your script works as expected.
                        marker        =   scriptVar,
                    },

 --          data =   {
 --                       lowTemperature  = { initial = true },
 --                   },

    execute = function(dz, item)

        if item.isTimer then
            dz.emitEvent(scriptVar).afterSec(30) -- this will trigger the customEvent in the On section and therewith increase the frequency to every 30 seconds
        end


        local insideTemperature = dz.devices('TempInside').temperature
        local outsideTemperature = dz.devices('TempOutside').temperature
        local stfan = dz.devices('FanIn')
        local safan = dz.devices('FanOut') -- this second use of 'switch' as varname does overwrite the previous one.

        local stfanIsOff = stfan.state == 'Off'
        local safanIsOff = safan.state == 'Off'
       
        local onTemperature = 27
        local offTemperature = 26
--        local comingFromLow = dz.data.lowTemperature
--        local Temperature = insideTemperature > lowerTemperature and insideTemperature < higherTemperature


--        dz.data.lowTemperature = insideTemperature < lowerTemperature                   -- remember coming from low

        if insideTemperature > onTemperature then              -- between 25-45 and coming from < 25 ==>> switchOn
          if safanIsOff and insideTemperature < outsideTemperature then safan.switchOn() 
                elseif stfanIsOff and insideTemperature > outsideTemperature then stfan.switchOn()
              end
        elseif insideTemperature < offTemperature and ( not stfanIsOff or not safanIsOff ) then                    -- > 24 or < 20 switchOff
                if not stfanIsOff then 
                    stfan.switchOff() 
                    end
                if not safanIsOff then
                    safan.switchOff() 
                    end
--            if insideTemperature > higherTemperature then                               -- No longer coming from low
--                dz.data.lowTemperature = false
--            end
        end
    end
}

This seems to work
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest