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.

For loops
#1
Code:
local thermostatCount = 18 -- Number of thermostats
local heating_cooling_sources = '12/1/211'

--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


local script_lock = '12/1/210'
local holidays = '6/1/3' -- Thermostat Mode
local S4_ON_OFF = {} --   AUTO / OFF
local S4_ON_OFF_FB = {} --   AUTO/OFF feedback
local overheat = {} -- Overheat status
local climate_status = {} -- Thermostat heating status
local Force_heat = {} --  FORCED POS
local PIDBLOCK = {} --  PID block
local thermostat_block = {} -- Thermostat block
local blocking_Fb = {} --  blocking FB
local frost_heat_FB = {} -- Termostato Frost/Heat protection Feedback

-- Initialize thermostat addresses and other variables
for i = 1, thermostatCount do
  S4_ON_OFF[i] = '12/1/' .. i
  S4_ON_OFF_FB[i + 100] = '12/1/' .. (i + 100)
  overheat[i] = '2/4/' .. i
  climate_status[i] = '3/6/' .. i
  Force_heat[i] = '3/1/' .. i
  PIDBLOCK[i] = '3/5/' .. i
  thermostat_block[i + 100] = '3/5/' .. (i + 100)
  blocking_Fb[i] = '12/0/' .. i
  frost_heat_FB[i] = '3/4/' .. i

end





for i = 1, thermostatCount do
  os.sleep(0.2)
    if grp.getvalue(overheat[i])  or grp.getvalue(holidays) == 3 or not grp.getvalue(S4_ON_OFF[i]) or not grp.getvalue(climate_status[i]) then
      grp.checkwrite(PIDBLOCK[i], true)
    else
      grp.checkwrite(PIDBLOCK[i], false)
    end
  end
 




if grp.getvalue(heating_cooling_sources) == 1 then
   
  for i = 1, thermostatCount do
    local index2 = i + 100
    if grp.getvalue(overheat[i])  then
      grp.checkwrite(Force_heat[i], true)
      --grp.checkwrite(PIDBLOCK[i], true)
      grp.checkwrite(thermostat_block[index2], true)
      -- Log thermostat block action
      log('Termostat block enabled for thermostat ' .. i)
    else
      grp.checkwrite(Force_heat[i], false)
     -- grp.checkwrite(PIDBLOCK[i], false)
      grp.checkwrite(thermostat_block[index2], false)
     
      -- Log thermostat block disabled action
      log('Termostat block disabled for thermostat ' .. i)
    end
  end
end










if grp.getvalue(heating_cooling_sources) == 2 then

  -- Šildymo vožtuvų ir termostaų blokavimas
  for i = 1, thermostatCount do
  os.sleep(0.2)
    if grp.getvalue(overheat[i]) then
      grp.checkwrite(Force_heat[i], true)
      --grp.checkwrite(PIDBLOCK[i], true)
      -- Log vožtuvo block action
      log('Vožtuvas block enabled for thermostat ' .. i)
    else
      grp.checkwrite(Force_heat[i], false)
      --grp.checkwrite(PIDBLOCK[i], false)
      -- Log vožtuvo block disabled action
      log('Vožtuvas block disabled for thermostat ' .. i)
    end
  end
end


 



  for i = 1, thermostatCount do
  os.sleep(0.2)
    if grp.getvalue(overheat[i])  then
     -- if grp.getvalue(S4_ON_OFF[i]) then
        grp.checkwrite(blocking_Fb[i], true)
        -- Log visualization block action
        log('Visualization block enabled for thermostat ' .. i)
      else
        if not grp.getvalue(overheat[i])  then
  -- if grp.getvalue(S4_ON_OFF[i]) then
        grp.checkwrite(blocking_Fb[i], false)
        -- Log visualization block disabled action
        log('Visualization block disabled for thermostat ' .. i)
           -- end
         -- end
      end
    end
  end




  for i = 1, thermostatCount do
  os.sleep(0.2)
    local index = i + 100 -- Calculate the corresponding index for S4_ON_OFF_FB
    if grp.getvalue(frost_heat_FB[i]) or grp.getvalue(holidays) ~= 1 or not grp.getvalue(S4_ON_OFF[i])  or grp.getvalue(thermostat_block[index]) then
    grp.checkwrite(S4_ON_OFF_FB[index], false)

   

  else
   if not grp.getvalue(frost_heat_FB[i]) and  grp.getvalue(holidays) == 1 and  grp.getvalue(S4_ON_OFF[i])  and not grp.getvalue(thermostat_block[index])  then
    grp.checkwrite(S4_ON_OFF_FB[index], true)
   
 
end
end
end
I have written a script that controls the thermostats and contains five for loops. Each thermostat is associated with four separate addresses that can trigger this script. I utilize this script for 18 thermostats. However, when I activate all the thermostats within a minute, a significant number of for loops are executed, consuming 100% of the processor's capacity. Consequently, not all the addresses maintain the correct values, leading to an unstable system. All 18 thermostat addresses share the same tag.

My goal is to employ a single script to control all the thermostats, given that they are identical. This approach would enhance the scalability of the script and facilitate error correction. In other words, the same script would be applicable to all thermostats, and any changes made would immediately apply to all thermostats. This eliminates the need to modify 18 tags individually.

How I can lower processor load?
Reply
#2
You should check which group address triggered the script and update only the relevant thermostat, not all of them. You also have a lot of grp.getvalue() calls to the same group addresses. You should cache these values to improve performance.
Reply
#3
(08.08.2023, 12:38)admin Wrote: You should check which group address triggered the script and update only the relevant thermostat, not all of them. You also have a lot of grp.getvalue() calls to the same group addresses. You should cache these values to improve performance.
Could you please explain the concept of caching these values?

I've observed that when the script is triggered by address 3/6/1, the for loop runs twice when the value changes, for example, from 4 to 1. However, if I send the same value, the for loop only runs once. Additionally, when I use another address with a boolean data type, the for loop runs only once, regardless of whether the value changes.

Do you have any insight into why this might be happening?

For test I'm using only this part of code.
Code:
local thermostatCount = 1 -- Number of thermostats
local heating_cooling_sources = '12/1/211'

--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


local script_lock = '12/1/210'
local holidays = '6/1/3' -- Thermostat Mode
local S4_ON_OFF = {} --   AUTO / OFF
local S4_ON_OFF_FB = {} --   AUTO/OFF feedback
local overheat = {} -- Overheat status
local climate_status = {} -- Thermostat heating status
local Force_heat = {} --  FORCED POS
local PIDBLOCK = {} --  PID block
local thermostat_block = {} -- Thermostat block
local blocking_Fb = {} --  blocking FB
local frost_heat_FB = {} -- Termostato Frost/Heat protection Feedback

-- Initialize thermostat addresses and other variables
for i = 1, thermostatCount do
  S4_ON_OFF[i] = '12/1/' .. i
  S4_ON_OFF_FB[i + 100] = '12/1/' .. (i + 100)
  overheat[i] = '2/4/' .. i
  climate_status[i] = '3/6/' .. i
  Force_heat[i] = '3/1/' .. i
  PIDBLOCK[i] = '3/5/' .. i
  thermostat_block[i + 100] = '3/5/' .. (i + 100)
  blocking_Fb[i] = '12/0/' .. i
  frost_heat_FB[i] = '3/4/' .. i

end

This might not seem like a significant issue at first glance, but when considering that I have 18 thermostats and each one triggers the script twice, the impact accumulates.

Attached Files Thumbnail(s)
   
Reply
#4
Check that you don't have other scripts that can cause an extra write or that the script itself does not change a group address that is mapped to the same tag.

A better approach is to split the script into several parts so only relevant code is run when a certain value changes. And check event.dst to determine which thermostat should be checked.

As for caching something like this can be used instead of grp.getvalue():
Code:
local values = {}

function getvalue(addr)
  if values[ addr ] == nil then
    values[ addr ] = grp.getvalue(addr)
  end

  return values[ addr ]
end
Reply


Forum Jump: