require "xap" require "xap.bsc" module(...,package.seeall) info={ version="1.0", description="Boiler Controller" } updateinterval = 60 -- How often to update the boiler hour=0 -- Time of day boilerstate = "off" -- state of boiler currenttemp = 25 -- room temperature cctemp = 25 -- current cost temp halltemp = 25 -- 1wire temperature minute = 0 -- current minute night_close = 22 -- closedown time at night night_am = 1 -- Other time settings morn_open = 7 evening_start = 16 day_temp_on = 19.1 -- Temp settings day_temp_off = 19.6 eve_temp_on = 19.9 eve_temp_off = 20.4 night_temp_on = 15.2 night_temp_off = 15.7 night = "low" day = "low" eve = "low" function init() local f = xap.Filter() f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:temp") f:add("xap-header","class","xAPBSC.event") f:callback(GetTemp) local f = xap.Filter() f:add("xap-header", "source", "dbzoo.livebox.Controller:1wire.2") f:add("xap-header","class","xAPBSC.event") f:callback(GetTemp1) xap.Timer(updateboiler, updateinterval):start() end function updateboiler(self) -- Run using timer every update interval -- pseudo code. Get the current temperature and time of day and set the boiler to -- on or off as appropriate currenttemp= (cctemp + halltemp)/2 local currenthour = os.date("%H") hour=tonumber(currenthour) if hour >= morn_open and hour <= evening_start then night = "low" day = "high" eve = "low" elseif hour > evening_start and hour <= night_close then night = "low" day = "low" eve = "high" else night = "high" day = "low" eve ="low" end if night=="high" then if currenttemp < night_temp_on then boilerstate = "on" elseif currenttemp > night_temp_off then boilerstate = "off" else -- do nothing end end if day=="high" then if currenttemp < day_temp_on then boilerstate = "on" elseif currenttemp > day_temp_off then boilerstate = "off" else -- do nothing end end if eve=="high" then if currenttemp < eve_temp_on then boilerstate = "on" elseif currenttemp > eve_temp_off then boilerstate = "off" else -- do nothing end end sendboilercode() end function GetTemp(frame) cctemp = tonumber(frame:getValue("input.state","text")) end function GetTemp1(frame) halltemp = tonumber(frame:getValue("input.state","text")) end function sendboilercode() if boilerstate=="on" then send2lcd("Boiler On: " ..tostring(currenttemp)) else send2lcd("Boiler Off: " ..tostring(currenttemp)) end send2plug(boilerstate, boilerstate) end function send2lcd(lcdstr) xap.sendShort(string.format([[xap-header { target=dbzoo.livebox.Controller:lcd class=xAPBSC.cmd } output.state.1 { id=* state=on text=%s }]], lcdstr)) end function send2plug(boilerstate,boilerstate) -- Drayton pulses set up on rf1 of the HAH xap.sendShort(string.format([[xap-header { target=dbzoo.livebox.Controller:rf.1 class=xAPBSC.cmd } output.state.1 { id=* state=%s text=%s }]], boilerstate,boilerstate)) end