Growing Degree Days Script
Posted: Sunday 27 December 2020 6:28
I'm not sure how many people are gardeners out there. I've found gardening and fruit trees to be a nice past time, so I created a script in Domoticz to keep track of Growing Degree Days. Which is a useful measure for figuring out when fruits and vegetables will flower or ripen and also when certain pests will appear and start to attack your crops. It resets your sensors in winter either on Jan 1st or Jul 1st, you pick whether you're in the northern or southern hemispheres respectively. Best time to install the script is BEFORE the beginning of the growing season before it gets above freezing (if possible). If you live in a warm place then before Jan/Jul 1st is best. I live in the US so I've set mine to Fahrenheit but it can be changed to use Celsius.
Oh, it's a time based script.
Anyway I've provided the script below :
EDIT: Updated script. Old script had a bug where it only reset if it was below freezing. Also if Domoticz was lagging or was down for several minutes that time would be missed. New update compensates for that.
Oh, it's a time based script.
Anyway I've provided the script below :
Code: Select all
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function findDevID(name)
return otherdevices_idx[name] or false
end
--NEEDED:
-- 1: External Temperature Sensor
-- 2: 4 new custom DUMMY sensors named GDM 50, GDD 50, GDM 32 and GDD 32. Can be changed if you use Celcius.
-- 3: Unpassworded access for localhost (IP 127.0.0.1)
commandArray = {}
local MaxTemp = 90-- If over this temp, set to this temp
local MinTemp50 = 50-- Turn on GDD50 at this temp
local MinTemp32 = 32-- Turn on GDD32 at this temp
local UserTempScale = "F" --Input variables, are they Celcius (C) or Fahrenheit (F)?
local ODThrmtrNm = 'Outside Temperature' --Thermometer name in Domoticz
local gdm50 = "GDM 50" -- Name of growing degree minutes over 50F custom DUMMY sensor in Domoticz
local gdd50 = "GDD 50" -- Name of growing degree days over 50F custom DUMMY sensor in Domoticz
local gdm32 = "GDM 32" -- Name of growing degree minutes over 32F custom DUMMY sensor in Domoticz
local gdd32 = "GDD 32" -- Name of growing degree days over 32F custom DUMMY sensor in Domoticz
local rstmnt = 1 --the number of the month we reset on. Usually Jan (1) or Jul (7)
local Temp = tonumber(otherdevices[ODThrmtrNm])
local stop1 = string.find(Temp,";")
if(stop1 ~= nil) then --If your sensor does more than Temp
Temp = tonumber(string.sub(Temp,0,stop1-1))
else
Temp = tonumber(Temp)
end
if (UserTempScale == "F") then
Temp = round(Temp*(9/5)+32,2)
else
Temp = round(Temp,2)
end
local s1 = otherdevices_lastupdate[gdm32]
local year1 = string.sub(s1, 1, 4)
local tR = os.time{year=os.date("%Y"), month=rstmnt, day=1, hour=0, min=0, sec=0}
if (Temp >= MinTemp50) then
if (Temp >= MaxTemp) then
Temp = MaxTemp
end
--new code in case the script doesn't fire every minute
local month1 = string.sub(s1, 6, 7)
local day1 = string.sub(s1, 9, 10)
local hour1 = string.sub(s1, 12, 13)
local minutes1 = string.sub(s1, 15, 16)
local seconds1 = string.sub(s1, 18, 19)
local s2 = otherdevices_lastupdate[gdm50]
local year2 = string.sub(s2, 1, 4)
local month2 = string.sub(s2, 6, 7)
local day2 = string.sub(s2, 9, 10)
local hour2 = string.sub(s2, 12, 13)
local minutes2 = string.sub(s2, 15, 16)
local seconds2 = string.sub(s2, 18, 19)
local t0 = os.time()
local t1 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
local t2 = os.time{year=year2, month=month2, day=day2, hour=hour2, min=minutes2, sec=seconds2}
local delta1 = (t0-t1)/60
local delta2 = (t0-t2)/60
if (delta1 > 1440) then
delta1=1
end
if (delta2 > 1440) then
delta2=1
end
print(tostring(delta1) .. ", " .. tostring(delta2))
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdm50) .. '|2|' .. tostring(tonumber(otherdevices[gdm50])+(Temp-MinTemp50)*delta2)})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdm32) .. '|2|' .. tostring(tonumber(otherdevices[gdm32])+(Temp-MinTemp32)*delta1)})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdd50) .. '|2|' .. tostring((tonumber(otherdevices[gdm50])+(Temp-MinTemp50)*delta2)/1440)})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdd32) .. '|2|' .. tostring((tonumber(otherdevices[gdm32])+(Temp-MinTemp32)*delta1)/1440)})
elseif (Temp >= MinTemp32) then
if (Temp >= MaxTemp) then
Temp = MaxTemp
end
--new code in case the script doesn't fire every minute
local month1 = string.sub(s1, 6, 7)
local day1 = string.sub(s1, 9, 10)
local hour1 = string.sub(s1, 12, 13)
local minutes1 = string.sub(s1, 15, 16)
local seconds1 = string.sub(s1, 18, 19)
local t0 = os.time()
local t1 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
local delta1 = (t0-t1)/60
if (delta1 > 1440) then
delta1=1
end
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdm32) .. '|2|' .. tostring(tonumber(otherdevices[gdm32])+(Temp-MinTemp32)*delta1)})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdd32) .. '|2|' .. tostring((tonumber(otherdevices[gdm32])+(Temp-MinTemp32)*delta1)/1440)})
end
if (os.time()-tR <= 300) then--less than five mins after the reset month? then reset!
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdm50) .. '|2|0'})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdm32) .. '|2|0'})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdd50) .. '|2|0'})
table.insert (commandArray, { ['UpdateDevice'] = findDevID(gdd32) .. '|2|0'})
end
return commandArray