Page 1 of 1
Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 15:22
by Firemen112
Hello,
I have a script for my floor heating pump. But the 'nalooptijd' of 10 min. are not in.
Can someone fix this? I have no experience with lua
script:
logging = true
function log (level, message)
file = io.open("/ram/domoticz.log", "a")
file:write(string.format("%s %-5s: LUA : %s\n", os.date("%m/%d/%Y %I:%M:%S %p"),level, message))
io.close(file)
end
AfterOff = 900
Vloer_Retour = 'Vloer_Retour'
Vloer_Aanvoer = 'Vloer_Aanvoer'
Pomp = 'Pomp'
commandArray = {}
PumpOn = tonumber(uservariables['PumpOn'])
-- if one of the sensor temps changed, do our thing
if ( devicechanged[Vloer_Retour] or devicechanged[Vloer_Aanvoer] ) then
time = os.time() -- Current time (integer sec)
dt_in = otherdevices[Vloer_Aanvoer] -- Get in temp
dt_out = otherdevices[Vloer_Retour] -- Get out temp
dt = dt_in - dt_out -- calculate diff
if (logging) then print("INFO",string.format("t_out %.2f t_in %.2f dt %.2f Pomp: %s OnTime: %d", dt_out, dt_in, dt, otherdevices[Pomp], time - PumpOn)) end
-- if temp difference > 1 degree celcius, immediatly tunr on Pump
if (dt > 1) then
-- update time we last turned "On" the pump
PumpOn = os.time()
if (otherdevices[Pomp] == "Off") then
commandArray[Pomp] = "On"
end
else
-- must me lower/equal 1 degree, see if minimum pump "On" time exeeded
if ( (os.time() - PumpOn) >= AfterOff ) then
if (otherdevices[Pomp] == "On") then
commandArray[Pomp] = "Off"
end
end
end
end
commandArray['Variable:PumpOn'] = tostring(PumpOn)
return commandArray
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 15:29
by jannl
Why not use something like Off AFTER?
Verstuurd vanaf mijn SM-G930F met Tapatalk
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 15:39
by Firemen112
Sorry, I mean, if the pump is switched off for >24 hours, then the pump switch on 10 min.
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 16:08
by jvdz
Something like this should work assuming some device will trigger it. I've also moved the updating of the variable inside the if or else it will update each time the script is ran.
Code: Select all
commandArray = {}
logging = true
function log(level, message)
file = io.open("/ram/domoticz.log", "a")
file:write(string.format("%s %-5s: LUA : %s\n", os.date("%m/%d/%Y %I:%M:%S %p"), level, message))
io.close(file)
end
-- Function to return the number of seconds the device was last changed
function datetimedifferencenow(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
difference = os.difftime(t1, t2)
return difference
end
AfterOff = 900
Vloer_Retour = 'Vloer_Retour'
Vloer_Aanvoer = 'Vloer_Aanvoer'
Pomp = 'Pomp'
commandArray = {}
-- if pomp didn't change in the last 24 hours then activate for 10 minutes
if (datetimedifferencenow(Pomp) > 60*60*24) then
commandArray[Pomp] = "On for 10"
end
if (devicechanged[Vloer_Retour] or devicechanged[Vloer_Aanvoer]) then
PumpOn = tonumber(uservariables['PumpOn'])
-- if one of the sensor temps changed, do our thing
time = os.time() -- Current time (integer sec)
dt_in = otherdevices[Vloer_Aanvoer] -- Get in temp
dt_out = otherdevices[Vloer_Retour] -- Get out temp
dt = dt_in - dt_out -- calculate diff
if (logging) then print("INFO", string.format("t_out %.2f t_in %.2f dt %.2f Pomp: %s OnTime: %d", dt_out, dt_in, dt, otherdevices[Pomp], time - PumpOn)) end
-- if temp difference > 1 degree celcius, immediatly tunr on Pump
if (dt > 1) then
-- update time we last turned "On" the pump
PumpOn = os.time()
if (otherdevices[Pomp] == "Off") then
commandArray[Pomp] = "On"
end
else
-- must me lower/equal 1 degree, see if minimum pump "On" time exeeded
if ((os.time() - PumpOn) >= AfterOff) then
if (otherdevices[Pomp] == "On") then
commandArray[Pomp] = "Off"
end
end
end
commandArray['Variable:PumpOn'] = tostring(PumpOn)
end
return commandArray
Jos
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 16:19
by Firemen112
Thanks!!, But now getting a error:
string "commandArray = {} ..."]:20: field 'day' missing in date table
Frank
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 16:31
by jvdz
Ah, I see indeed I made a mistake. It didn't retrieve the lastchanged date/time information for pomp.
I can't test the script myself

... so here is an update for you to test:
Code: Select all
logging = true
function log(level, message)
file = io.open("/ram/domoticz.log", "a")
file:write(string.format("%s %-5s: LUA : %s\n", os.date("%m/%d/%Y %I:%M:%S %p"), level, message))
io.close(file)
end
-- Function to return the number of seconds the device was last changed
function datetimedifferencenow(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
difference = os.difftime(t1, t2)
return difference
end
AfterOff = 900
Vloer_Retour = 'Vloer_Retour'
Vloer_Aanvoer = 'Vloer_Aanvoer'
Pomp = 'Pomp'
commandArray = {}
-- if pomp didn't change in the last 24 hours then activate for 10 minutes
if (datetimedifferencenow(otherdevices_lastupdate[Pomp]) > 60*60*24) then
commandArray[Pomp] = "On for 10"
end
if (devicechanged[Vloer_Retour] or devicechanged[Vloer_Aanvoer]) then
PumpOn = tonumber(uservariables['PumpOn'])
-- if one of the sensor temps changed, do our thing
time = os.time() -- Current time (integer sec)
dt_in = otherdevices[Vloer_Aanvoer] -- Get in temp
dt_out = otherdevices[Vloer_Retour] -- Get out temp
dt = dt_in - dt_out -- calculate diff
if (logging) then print("INFO", string.format("t_out %.2f t_in %.2f dt %.2f Pomp: %s OnTime: %d", dt_out, dt_in, dt, otherdevices[Pomp], time - PumpOn)) end
-- if temp difference > 1 degree celcius, immediatly tunr on Pump
if (dt > 1) then
-- update time we last turned "On" the pump
PumpOn = os.time()
if (otherdevices[Pomp] == "Off") then
commandArray[Pomp] = "On"
end
else
-- must me lower/equal 1 degree, see if minimum pump "On" time exeeded
if ((os.time() - PumpOn) >= AfterOff) then
if (otherdevices[Pomp] == "On") then
commandArray[Pomp] = "Off"
end
end
end
commandArray['Variable:PumpOn'] = tostring(PumpOn)
end
return commandArray
Jos
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 20:13
by Firemen112
Works fine!! Thanks!
Frank
Re: Add "nalooptijd" in a lua script
Posted: Sunday 02 April 2017 21:48
by gerard76
What do you use to read the temperatures? I currently check my Nest to see if the heating is on, but I have to guess the extra time the pump needs to run.