This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Resident script pump circulation
#53
If I can give you some tips:
- tags shouldn't be very, very long because if they are very long you can accidently assign new tag which is misspelled and probably you don't see whole tag/tags in Objects view. In your case you has e.g. "pompa_circolazione_giorno_estate" which means "circulation pump summer day" so you can think how to short this name, maybe you can remove "day" from it, use shortcut name and let e.g. pump_ce1

- please use english in your code, you will do better programs, all people in forums can help
 you faster.

- when you debug code don't be afraid to use a lot of log() function (also like log('Label 1', your_variable)) and also try to check step by step where is the start of the problem because when you have complex script then when something is bad calculated at start then end could be very mess and unpredictable so you should check full path from. You can also log only certain data e.g. when you are logging knx object then you can log full object but more readable would be to log only specific properties like log(obj.name, obj.value).

- use functions for splitting your code into a few simple and easy to test parts which would be also much more readable

- use tables, objects, for loops, tags for multiple zones, don't create another script for it

- for checking simple Lua based thing in code you can check also such tool like https://www.lua.org/demo.html

This are only some tips from me, because I see that you are trying to do better(more advanced) programs.



If it about your code please check step by step every line. I'm strongly recommend you use this last version because it is more readable and more ready for future changes which you will probably do.

1. Please stop all other pump circulation scripts.
2. Please use another script with a name e.g. 'Test' (it must be fresh script)and copy there such script which parallelly creates logs object and at the end ot prints it:
Code:
------------------------------------------------------
-- DEFINE GLOBAL VARIABLES AND ZONES------------------
------------------------------------------------------
seasonGA = '2/0/100'
logging = true

zones = {
    {
        summer = 'pompa_circolazione_giorno_estate',
        winter = 'pompa_circolazione_giorno_inverno',
        pump = '2/0/1',
        offset = 60,
        minimum = 30
    }
}


------------------------------------------------------
-- DONT CHANGE NOTHING BELOW--------------------------
------------------------------------------------------
season = grp.getvalue(seasonGA)
nextCalculationAfter = 30 -- it will be changed by first zone

if logging then logs = {season = season} end -- #TO DELETE

function calcOutOfTheSeason(tag)
    local opened = false
    local firstTimestamp = 0
    local valves = grp.tag(tag)
    
    if logging then logs.zones[#logs.zones].valvesInWinter = {} end -- #TO DELETE
    
    for v = 1, #valves, 1 do
        local valve = valves[v]
        
        if valve.value then
            opened = true
            if firstTimestamp > 0 then
                if firstTimestamp > valve.updatetime then firstTimestamp = valve.updatetime end
            else
                firstTimestamp = valve.updatetime
            end
        end
        
        if logging then logs.zones[#logs.zones].valvesInWinter[v] = {name = valve.name, value = valve.value, tags = valve.tags, opened = opened, firstTimestamp = firstTimestamp} end -- #TO DELETE
    end
    
    return opened, firstTimestamp
end

function calcDuringTheSeason(tag)
    local opened = false
    local firstTimestamp = 0
    local valves = grp.tag(tag)
    
    if logging then logs.zones[#logs.zones].valvesInSummer = {} end -- #TO DELETE
    
    valves = grp.tag(tag)
    for v = 1, #valves, 1 do
      valve = valves[v]
      
      if valve.value then
        opened = true
        if firstTimestamp > 0 then
          if firstTimestamp > valve.updatetime then firstTimestamp = valve.updatetime end
        else
          firstTimestamp = valve.updatetime
        end
      end
      
      if logging then logs.zones[#logs.zones].valvesInSummer[v] = {name = valve.name, value = valve.value, tags = valve.tags, opened = opened, firstTimestamp = firstTimestamp} end -- #TO DELETE
    end
    
    return opened, firstTimestamp
end

if logging then
    logs.season = season
    logs.zones = {}
end -- #TO DELETE

for z, zone in ipairs(zones) do
    local summerTag = zone.summer
    local winterTag = zone.winter
    local pumpGA = zone.pump
    local openingValveTime = zone.offset
    local minInterval = zone.minimum
    
    if logging then
        logs.zones[z] = {
            settings = {
                summerTag = summerTag,
                winterTag = winterTag,
                pumpGA = pumpGA,
                pumpBeforeCmd = grp.getvalue(pumpGA),
                openingValveTime = openingValveTime,
                minInterval = minInterval
            }
        }
    end -- #TO DELETE
    
    -- CALCULATE SINGLE
    
    local opened, firstTimestamp = false, 0
    
    -- Calc valve state and firsttimestamp for update
    if season then
        opened, firstTimestamp = calcDuringTheSeason(winterTag)
    else
        opened, firstTimestamp = calcOutOfTheSeason(summerTag)
    end
    
    if logging then
        logs.zones[z].opened = opened
        logs.zones[z].firstTimestamp = firstTimestamp
    end -- # TO DELETE
    
    -- Find nextZoneCalculationAfter for current zone
    local nextZoneCalculationAfter = 0
    
    if opened then
        local fullyOpened = os.microtime() - firstTimestamp >= openingValveTime
        
        if fullyOpened then
            grp.checkwrite(pumpGA, true)
            nextZoneCalculationAfter = minInterval
            if logging then logs.zones[z].info = 'Turn on pump because valve is fully opened. Next calculation after ' .. nextZoneCalculationAfter .. 's' end -- # TO DELETE
        else
            grp.checkwrite(pumpGA, false)
            nextZoneCalculationAfter = openingValveTime + 1 - (os.microtime() - firstTimestamp) -- run a little after possible change
            if logging then logs.zones[z].info = 'Turn off pump because valve isn't yet fully opened. Next calculation after ' .. nextZoneCalculationAfter .. 's' end -- # TO DELETE
        end
    else
        grp.checkwrite(pumpGA, false)
        nextZoneCalculationAfter = openingValveTime + 1
        if logging then logs.zones[z].info = 'Turn on pump because valve is closed. Next calculation after ' .. nextZoneCalculationAfter .. 's. ' end -- # TO DELETE
    end
    
    -- Update nextCalculationAfter (global)
    if z == 1 or nextCalculationAfter < nextZoneCalculationAfter then
        nextCalculationAfter = nextZoneCalculationAfter
        if logging then logs.zones[z].info = logs.zones[z].info .. 'Variable nextCalculationAfter was changed to ' .. nextZoneCalculationAfter .. '.' end -- # TO DELETE
    end

end

-- log('Next calculation after: ' .. nextCalculationAfter .. 's.')
if nextCalculationAfter and nextCalculationAfter > 5 then
    if logging then
        logs.sleepDescription = 'Calculated ' .. nextCalculationAfter .. 's'
        log(logs)
    end -- # TO DELETE
    os.sleep(nextCalculationAfter)
else
    if logging then
        logs.sleepDescription = 'Default ' .. nextCalculationAfter .. 's (calculated value is 0 or other equal to false)'
        log(logs)
    end -- # TO DELETE
    os.sleep(30)
end
3. Print here the log result.

Please add here also the list of valves in each zones, and I will check if all are calculated.

And if it is about a day when script stopped working correctly, maybe you've done some changes in tags(maybe you've changed multiple tags and accidentally deleted the necessary tag)?
Done is better than perfect
Reply


Messages In This Thread
RE: Resident script pump circulation - by buuuudzik - 25.10.2018, 06:04

Forum Jump: