12.12.2018, 22:45
(This post was last modified: 12.12.2018, 22:46 by Erwin van der Zwart.)
Hi,
Here is a more complex script for load balancing, i use it for EV chargers, they can use max 63 Amp and need minimal 8 Amp, based on active chargers and available power the chargers are set to a maximum loader output over modbus (for our EVLink)
All works with TAG's on the binary state (active/not active) object and a offset of 1 in the main address (32/1/1 = State -> 32/1/1 = Set max charging output) so you can easily add multiple chargers (:
BR,
Erwin
Here is a more complex script for load balancing, i use it for EV chargers, they can use max 63 Amp and need minimal 8 Amp, based on active chargers and available power the chargers are set to a maximum loader output over modbus (for our EVLink)
All works with TAG's on the binary state (active/not active) object and a offset of 1 in the main address (32/1/1 = State -> 32/1/1 = Set max charging output) so you can easily add multiple chargers (:
Code:
-- Get maximal available amperage
Max_Amp = grp.getvalue('35/0/10')
-- Get current messured amperage
Current_Amp = grp.getvalue('35/0/11')
-- Calculate available amperage for loaders
Available_Amp = Max_Amp - Current_Amp
-- Minimal amperage output on loader
Min_Loader_Amp = 8
-- Maximal amperage output on loader
Max_Loader_Amp = 63
-- Get all loaders on/off states
All_Loaders = grp.tag('LoaderState')
-- Function to sort table by updatetime
local function tableSortCat (a,b)
return a.updatetime < b.updatetime
end
-- Sort table All_Loaders by updatetime
table.sort(All_Loaders, tableSortCat)
-- Create table with active loader
Active_Loaders = {}
-- Determine amount of active loaders
Number_Active_Loaders = 0
-- Ceeate new table with values and only loader tagged bit objects
for _, item in pairs(All_Loaders) do
if item.datatype == 1 or (item.datatype > 999 and item.datatype < 2000) then
if item.value == true then
Number_Active_Loaders = Number_Active_Loaders + 1
end
table.insert(Active_Loaders, {id = item.id, updatetime = item.updatetime, datatype = item.datatype, address = item.address, value = item.value})
end
end
-- Sort table Active_Loaders by updatetime
table.sort(Active_Loaders, tableSortCat)
-- Determine maximum loaders based on available power
Max_Number_Loaders_Possible = math.floor(Available_Amp / Min_Loader_Amp)
-- Check if more loaders are active then max possible loaders
if Number_Active_Loaders < Max_Number_Loaders_Possible then
Max_Number_Loaders_Possible = Number_Active_Loaders
end
--Set available amperage per loader
Available_Amperage_Loader_Per_Unit = math.round(Available_Amp / Max_Number_Loaders_Possible, 2)
--Check if output exceeds loaders Max_Amp
if Available_Amperage_Loader_Per_Unit > Max_Loader_Amp then
Available_Amperage_Loader_Per_Unit = Max_Loader_Amp
end
-- Set loaders to 0 when current amperage exceeds max amperage
if Current_Amp > Max_Amp then
Max_Number_Loaders_Possible = 0
Available_Amperage_Loader_Per_Unit = 0
end
-- Write values to the output of each loader
Number_Loaders_Value_Written = 0
for _, item in pairs(All_Loaders) do
Address_Table = string.split(item.address, "/")
New_Address = Address_Table[1]+1 .. '/' .. Address_Table[2] .. '/' .. Address_Table[3]
if item.value == true then
if Number_Loaders_Value_Written < Max_Number_Loaders_Possible then
grp.checkupdate(New_Address,Available_Amperage_Loader_Per_Unit)
Number_Loaders_Value_Written = Number_Loaders_Value_Written + 1
else
grp.checkupdate(New_Address,0)
end
else
grp.checkupdate(New_Address,0)
end
end
Erwin