Heating control program

Hi

I have managed to program my first LUA program to Vera! I would like to share it in case someone is doing something similar.

Case:
Here in Nordic the price of the electricity can vary a lot depending the hour you need to consume it.

[url=http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/SE/Hourly/?view=table]Nord Pool Maintenance

Basicly the electricity is cheap during night time and expencive at day time. But if you have a house like mine you can not reserve the heat to the house so your only option is to avoid the peak hours. Finaly I get tired to turn the heating ON and OFF manually so it was time to buy vera.

In order to automate this I did set up an table to to startup LUA to manage the parameters so they can be used around all the system.

GLOBAL_T_MIN = {} GLOBAL_T_MAX = {} GLOBAL_HEATING = {}

GLOBAL_HEATING[0] = “ON”
GLOBAL_HEATING[1] = “ON”
GLOBAL_HEATING[2] = “ON”
GLOBAL_HEATING[3] = “ON”
… etc to 24
GLOBAL_T_MIN[0] = “16”
GLOBAL_T_MIN[1] = “16”
GLOBAL_T_MIN[2] = “16”
GLOBAL_T_MIN[3] = “16”
GLOBAL_T_MIN[4] = “17”
… etc to 24
GLOBAL_T_MAX[0] = “18”
GLOBAL_T_MAX[1] = “18”
GLOBAL_T_MAX[2] = “18”
GLOBAL_T_MAX[3] = “18”
GLOBAL_T_MAX[4] = “18”
GLOBAL_T_MAX[5] = “20”
GLOBAL_T_MAX[6] = “20”
GLOBAL_T_MAX[7] = “19”
…etc to 24

Via the GLOBAL_HEATING[X] I am now marking the peak hours (dayly) to vera. This need to be changed if I never learn a way to download the pricelist from the internet.

Then I did set up and scene to run every 30 minutes (eaven and half our).

That will control the heating via 3 Fibaro relay conneted to house main electricity boads (electrician did the work on that area). All my radiators are linked to three different zones.

In the LUA code in scene looks like this

-- House heating By 16.1.2016 by Marko Einamo -- local TEMP_DEV = 9 local TEMP_SID = "urn:upnp-org:serviceId:TemperatureSensor1" local RoomTemp1 = luup.variable_get(TEMP_SID,"CurrentTemperature", TEMP_DEV ) -- get time local tNow = os.date("*t") local i = tNow.hour local dNow = os.date("%A")

– write log
heatingLog (“Heating: ==================”)
heatingLog ( "now Date : " … dNow)
heatingLog ( "now hour : " … tNow.hour )
heatingLog ( "T_MIN: " … GLOBAL_T_MIN[i] )
heatingLog ( "T_MAX: " … GLOBAL_T_MAX[i] )
heatingLog ( "Heating : " … GLOBAL_HEATING[i] )
heatingLog ( "Room Temp : " … RoomTemp1 )

— is it weekend ?
if ((dNow == “Saturday”) or (dNow == “Sunday”)) then
heatingLog ( "It is weekend so heating shoud be ON and we go with the MAX all day !!! " )
GLOBAL_HEATING[i] = “ON”
end
— the main logig

if (( RoomTemp1 < GLOBAL_T_MAX[i]) and (GLOBAL_HEATING[i] == “ON”)) then

heatingLog  ( "Room Temp under max set and heating is set ON -> heating ON" )
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },47)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },53)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },56)

elseif RoomTemp1 < GLOBAL_T_MIN[i] then

heatingLog  ( "Temp under global min -> HEAT ON anyway. It's getting too cold here ... " )
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },47)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },53)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },56)

else
heatingLog ( "Setting heating OFF " )
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },47)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },53)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },56)
end

So, now every thing been running for 2-3 weeks. I did notice that the weekend parameters need to be different from weekday parameter so I did add the weekend option to the code as a quick fix last weekend.

As a summary , that was not so difficult. Via cut and paste the examples here in forum you can archive a lot :slight_smile: My former programing experience is from 80-90’s cobol and Fortran languages in mainframe wold.

The log issue is also handled via function . I did but that also to startup lua. The function it self I did copy from the forum also so thank you for that:

function heatingLog(message, clear) local socket = require("socket") local time = socket.gettime() or os.time() local tms = string.format(".%03d ",math.floor (1000 * (time % 1))) local stamp = os.date("%d %b %Y %T",math.floor(time)) .. tms local mode = "a+" if clear then mode = "w+" end local file = io.open("/www/heating.log.", mode) file:write(stamp .. (message or "") .. "\n") file:close() end