Simple thermostat control in lua

For heating/cooling related questions in Domoticz

Moderator: leecollings

Post Reply
pjsgsy
Posts: 26
Joined: Thursday 28 December 2017 20:51
Target OS: Windows
Domoticz version: 3.8153
Contact:

Simple thermostat control in lua

Post by pjsgsy »

Hi,

I spent this am so far upgrading my Domoticz to the new version (Thank you!), and coding a new, simpler thermostat for my heaters. I figure I should share this. It is very simple, but someone might find it useful.

You need to create a virtual thermostat for this script. This will allow you to set the temperatures very easily via the main GUI, including on a timer, for overnight, etc. I have a 'Status' variable in my setup, so my home knows when I am 'Away', 'Home', etc. The script will reduce heating when I am away, but you can simply comment those sections out if you do not have this. The script uses a 'hysteresis' for 'wiggle' room in the temperature. This prevents excessive cycling of the heating.

Additional heaters can be added be duplicating the initial section (I added this in the comments so you get the idea). Would be nicer to do this in an array, and I might do that in future if I can learn a little more lua!

Anyway - I hope someone might find this useful.

Code: Select all

-- print ("*************** Thermostat script ************")

--print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'")
--for key, value in pairs(devicechanged) do
--     print ('Changed devices: ==>> key: ' ..  tostring(key) .. '; value: ==>>  ' .. tostring(value))
--end   

commandArray = {}
-- 
local hysteresis = 0.5                              -- Wiggle room for temprature, esle relays/switch gets used too much
--
local sensor     = 'Study'                          -- Temp sensor name
local switch     = 'Study Heater'                   -- heater switch name
local stat       = 'Study thermostat'               -- thermostat name
--
local temprature = otherdevices_temperature[sensor] -- gets the temprature sensor value
local sp         = otherdevices[stat]               -- gets the setpoint from the termostat

-- we will temporarily overide the thermostat value with a modest value
-- when we are 'Away', to prevent excessively heating an empty house
-- this whole section could be commented out if you do not have a 'Home' status
if (otherdevices['Status'] == "Away") then
    sp = sp - 3
elseif (otherdevices['Status'] == "Holiday") then
    sp = 10
elseif (otherdevices['Status'] == "Night") then
    sp = sp - 5    
end
-- force a min value
if (tonumber(sp) < 10) then
    sp = 10
end

print("Thermostat:'"..sensor.."' temp is " .. tostring(temprature)..", SetPoint is "..tostring(sp)..", switch status is "..otherdevices[switch]..", home status is '"..otherdevices['Status'].."'")  -- for debugging - can comment out

if (temprature < (sp - hysteresis)) then
    print("temprature is less than setpoint-hysteresis of "..tostring((sp - hysteresis)))  -- for debugging - can comment out
    if (otherdevices[switch] == 'Off') then
        commandArray[switch] = 'On'
    end
elseif (temprature > (sp + hysteresis)) then
    print("temprature is more than setpoint+hysteresis of "..tostring((sp + hysteresis)))  -- for debugging - can comment out
    if (otherdevices[switch] == 'On') then
        commandArray[switch] = 'Off'
    end
end

-- next heater/room
--[[
sensor     = 'Study'                          -- Temp sensor name
switch     = 'Study Heater'                   -- heater switch name
stat       = 'Study thermostat'               -- thermostat name
--
temprature = otherdevices_temperature[sensor] -- gets the temprature sensor value
sp         = otherdevices[stat]               -- gets the setpoint from the termostat

-- we will temporarily overide the thermostat value with a modest value
-- when we are 'Away', to prevent excessively heating an empty house
-- this whole section could be commented out if you do not have a 'Home' status
if (otherdevices['Status'] == "Away") then
    sp = sp - 3
elseif (otherdevices['Status'] == "Holiday") then
    sp = 10
elseif (otherdevices['Status'] == "Night") then
    sp = sp - 5    
end
-- force a min value
if (tonumber(sp) < 10) then
    sp = 10
end

print("Thermostat:'"..sensor.."' temp is " .. tostring(temprature)..", SetPoint is "..tostring(sp)..", switch status is "..otherdevices[switch]..", home status is '"..otherdevices['Status'].."'")  -- for debugging - can comment out

if (temprature < (sp - hysteresis)) then
    print("temprature is less than setpoint-hysteresis of "..tostring((sp - hysteresis)))  -- for debugging - can comment out
    if (otherdevices[switch] == 'Off') then
        commandArray[switch] = 'On'
    end
elseif (temprature > (sp + hysteresis)) then
    print("temprature is more than setpoint+hysteresis of "..tostring((sp + hysteresis)))  -- for debugging - can comment out
    if (otherdevices[switch] == 'On') then
        commandArray[switch] = 'Off'
    end
end
--]]


return commandArray

just in case anyone wants to know how I did the status variable, it's just a generic selector switch with these selector levels

0 Off
10 Home
20 Away
30 Night
40 Holiday
Post Reply

Who is online

Users browsing this forum: INendels and 1 guest