16.01.2025, 11:42
(This post was last modified: 16.01.2025, 11:43 by jerryhenke.
Edit Reason: added code
)
(04.12.2018, 13:45)merel Wrote: Hello.Wow. Thanks! Used this today and made some small changes.
You can try this:
Code:function linear_by_table(input,curve,offset)
local FDY = offset or 0
fk1= (curve["y"][1] - curve["y"][2]) / (curve["x"][1] - curve["x"][2])
fk2= (curve["y"][2] - curve["y"][3]) / (curve["x"][2] - curve["x"][3])
fk3= (curve["y"][3] - curve["y"][4]) / (curve["x"][3] - curve["x"][4])
if input < curve["x"][1] then
out = curve["y"][1] + FDY;
elseif input >= curve["x"][1] and input < curve["x"][2] then
out = fk1 * (input - curve["x"][1]) + curve["y"][1] + FDY
elseif input >= curve["x"][2] and input < curve["x"][3] then
out = fk2 * (input - curve["x"][2]) + curve["y"][2] + FDY
elseif input >= curve["x"][3] and input < curve["x"][4] then
out = fk3 * (input - curve["x"][3]) + curve["y"][3] + FDY
else
out = curve["y"][4] + FDY
end
return out
end
curve = {}
curve["x"] = {-20,-10,0,20}
curve["y"] = {80,70,65,18}
--offset=2
--log(linear_by_table(20,curve,offset))
log(linear_by_table(-15,curve))
My code looks like this and runs nicely.
Code:
function linear_by_table(input, curve, offset)
local FDY = offset or 0
local n = #curve["x"] -- Number of points
if n ~= #curve["y"] then
error("x and y lists must have the same number of values")
end
local out
if input < curve["x"][1] then
out = curve["y"][1] + FDY
elseif input >= curve["x"][n] then
out = curve["y"][n] + FDY
else
for i = 1, n - 1 do
if input >= curve["x"][i] and input < curve["x"][i + 1] then
local fk = (curve["y"][i + 1] - curve["y"][i]) / (curve["x"][i + 1] - curve["x"][i])
out = fk * (input - curve["x"][i]) + curve["y"][i] + FDY
break
end
end
end
return out
end
-- Define the curve for interpolation
local curve = {x = {-20, -10, 0, 10, 20}, y = {42, 34, 32, 25, 15}}
-- Adjust offset to shift the entire curve
local offset = 0
-- Get outdoor temperature from KNX address 0/3/0
local outdoor_temp = grp.getvalue('0/3/0')
-- Calculate the flow setpoint based on the outdoor temperature
local flow_setpoint = linear_by_table(outdoor_temp, curve, offset)
-- Write the calculated value to KNX address 5/5/0
grp.write('5/5/0', flow_setpoint)
-- Log the results
log('Outdoor temperature: ' .. outdoor_temp .. ', Flow setpoint: ' .. flow_setpoint)