Page 1 of 1

Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 17:03
by havnegata
Hi there!
I've been trying to modify this thermostatscript made by Alexandre DUBOIS to include time. As my skills are low, I'm without success so far. The script starts at anytime and doesn't end when I want it to. Any ideas?

-- Alexandre DUBOIS - 2014
-- This script keeps the room temperature between 19 °C and 21 °C when the virtual switch 'Lounge Thermostat' is on

--------------------------------
------ Variables to edit ------
--------------------------------
local setpoint = 20 --Setpoint temperature
local hysteresis = 0.5 --Threshold to prevent the relay keeps switching in 2 directions
local sensor = 'Soverom' --Name of the temperature sensor
local thermostat = 'DUMMY Soverom' --Name of the virtual switch of the thermostat
local heater = 'Varmeovn soverom' --Name heater to turn on/off
--------------------------------
-- End of variables to edit --
--------------------------------
commandArray = {}
--The sensor emits every 40 seconds. This will be approximately the frequency of execution of the script.
if (devicechanged[sensor]) then
print('Device changed')
temperature = tonumber(devicechanged[sensor..'_Temperature']) --Temperature is raised in the lounge only if the "Thermostat" is on
time = os.date("*t")
if (otherdevices[thermostat]=='On') then
print('--- Termostat styrer oppvarming av soverom ---')

if (temperature < (setpoint - hysteresis)) and ((time.hour >= 15) and (time.hour <= 16)) then
print('Oppvarming av soverom')
commandArray[heater]='On'

elseif (temperature > (setpoint + hysteresis)) or ((time.hour > 16)) then
print('Stopper oppvarming av soverom')
commandArray[heater]='Off'

end
end
end

return commandArray

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 18:28
by blackdog65
I created an additional dummy switch (in my case a multi-position for hot water too) and added it in as an another "if" condition then the dummy switch is controlled by timers.

Good luck

Sean

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 18:31
by Guest
Thanks for the reply. Will try that, but I really would like to learn more about Lua and understand what is wrong with the time code

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 18:31
by havnegata
Thanks for the reply. Will try that, but I really would like to learn more about Lua and understand what is wrong with the time code

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 18:45
by blackdog65
I can't help there as I'm a complete Lua NOOB :-)

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 22:38
by havnegata
It's working now. I was using too many paranthesis.

It was:
elseif (temperature > (setpoint + hysteresis)) or ((time.hour > 16)) then

It should be:
elseif (temperature > (setpoint + hysteresis)) or (time.hour > 16) then

Re: Trying to modify a thermostat script

Posted: Wednesday 10 February 2016 23:00
by havnegata
Seems I was a little quick on cheering... Suddenly the heater turned on again, which it was not supposed to do..

Oh well, back to searching the forum, and hopefully some of you can pinpoint my fault :-)

Re: Trying to modify a thermostat script

Posted: Thursday 11 February 2016 16:15
by havnegata
With the help of jvdz and his example of a time function I managed to achieve my goal. This is a wonderful forum!

function timebetween(s,e)
timenow = os.date("*t")
year = timenow.year
month = timenow.month
day = timenow.day
s = s .. ":00" -- add seconds in case only hh:mm is supplied
e = e .. ":00"
shour = string.sub(s, 1, 2)
sminutes = string.sub(s, 4, 5)
sseconds = string.sub(s, 7, 8)
ehour = string.sub(e, 1, 2)
eminutes = string.sub(e, 4, 5)
eseconds = string.sub(e, 7, 8)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
sdifference = os.difftime (t1, t2)
edifference = os.difftime (t1, t3)
isbetween = false
if sdifference >= 0 and edifference <= 0 then
isbetween = true
end
--~ print(" s:" .. s .. " e:" .. e .. " sdifference:" .. sdifference.. " edifference:" .. edifference)
return isbetween
end


commandArray = {}

if timebetween("09:15:00","09:44:00") then
--- perform action
end

return commandArray