It is going out of hand...
Latest additions and changes:
Version 0.5
- MaxRainFall changed, it is now a average of your desired "Buienradar" max rain fall input to prevent the switch to flip flop
- Optimized Moist sensor script as it now triggers irrigation on a minimum value and shutsdown on a maximum value
- Added extra loggin option
- Added option to use Buienradar or not
- Added option to use Dutch or English translation
This script is still in BETA as i just wrote it and haven't fully tested it.
Be aware that i'm not a coder and it's maybe the cleanest script ever, but who cares as it works
I hope it can be of use to you and feel free to adapt it.
Bug fixes, patches, and improvements are hapiliy accepted of course.
Same counts for LUA optimizations as this is the first time i wrote,copy/past a LUA script.
When i started this i didn't had a clue what i was doing
Code: Select all
--[[
script_time_irrigation.lua
@author: Siewert Lameijer
@since: 2015-06-30
@version: 0.5 (BETA)
@Garden Irrigation lua script
Credits:
- Nicky Bulthuis -/- for the dusk sensor script
- Hans van der Heijden -/- for the IsItGonnaRain script
- anonymous user -/- for the "Reading weather station data" script
- dannybloe -/- for Humidity Control script
- And to all those who i forgot to mention
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
This script has the intention to control and maintain your garden soil humidity.
And is it is a hodgepodge of different scripts found on the Domoticz forum.
I wrote this script for my own little garden project and to beable to automate irrigation while i'm gone.
The topic can be found here: https://www.domoticz.com/forum/viewtopic.php?f=38&t=6904
Somehow it got out of hand and got big.
So i thought that maybe some of you could have a benefith of this.
Although the script is intended for irrigation.
You could adapt it for other projects.
I do assume that when you want to use this script you already have the knowhow of Lua and Domoticz.
As i won't explain the entire installation ;-)
The script does the following:
- It scraps WeatherUnderground data and processing it usable data
- It monitors buienradar (optional) to see if there are any showers upcoming 24hr
- It scraps temperature and humidity from your moist sensor and processing it usable data
- It lets you choose when (Day of the week, month and time of the day) to irrigate
- By an extensive Settings Menu you can set various settings to control when to irrigate
settings ranges from: Min/Max Temp, Min/Max Humidity pct, Amount of rain, wind, UV, day, month to stop or start irrigating.
The Hardware i use:
- DIY Garden irrigation piping as seen in the topic
- Washer 220v Watervalve 2-way
- KaKu ACM-3500-3 3-in-1 unit (controlling the 2 valves) 1 reserve for pond protector to scare the cats ;-)
This script is still in BETA as i just wrote it and haven't fully tested it.
Be aware that i'm not a coder and it's maybe the cleanest script ever, but who cares as it works ;-)
I hope it can be of use to you and feel free to adapt it.
Bug fixes, patches, and improvements are hapiliy accepted of course.
Same counts for LUA optimizations as this is the first time i wrote,copy/past a LUA script.
When i started this i didn't had a clue what i was doing ;-)
Siewert Lameijer
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Necessities:
1x Virtual switch to be able to manual override the irrigation program (Manual Override)
1x Soil Moist & Humidity sensor
1x Active WeatherUnderground account
1x Switch to be able to control your waterpump/valve which is somehow wireless connected to domoticz
1x Garden with irrigation hardware xD
The following UserVariable have to be create:
- VarManOverride (type=integer, value=0)
- VarTimeCon (type=integer, value=0)
- VarWeatherCon (type=integer, value=0)
- VarTimeOfDayCurrent (type=integer, value=0)
- VarMayFollowProgram (type=integer, value=0)
- VarFollowsProgram (type=integer, value=0)
- VarMaxRunTime (type=integer, value=0)
- VarMoistOnline (type=integer, value=0)
- VarMoistOnlineHum (type=integer, value=0)
- VarMoistConTemp (type=integer, value=0)
- VarMoistConHum (type=integer, value=0)
- VarProgramOverride (type=integer, value=0)
- VarProgramRun (type=integer, value=0)
- VarTimeFrame (type=integer, value=0)
- VarManOverrideCon (type=integer, value=0)
- VarBuienRadarCon (type=integer, value=0)
- VarTimeFrameMon (type=integer, value=0)
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Changelog:
Version 0.5
- Buienradar MaxRainFall changed, it is now a average of your desired max rain fall input to prevent the switch to flip flop
- Optimized Moist sensor script as it now triggers irrigation on a minimum value and shutsdown on a maximum value
- Added extra loggin option
- Added option to use Buienradar or not
- Added option to use Dutch or English translation
Version 0.4
- Changed Days, Months to text instead of numbers as input
- Optimized irrigation program ending and keeping it that until the next time schedule
Version 0.3
- Added Buienradar (IsItGonnaRain) script
- Optimized Max irrigation time script
- Clean some code
- Reorderd printed text
Version 0.2
- Added counter for Max irrigation time
- Added manual override
- Optimized WeatherUnderground scraping data
- Optimized Moist sensor scraping data
- Optimized user input Time Of The Day, Days and Months
Version 0.1
- Added WeatherUnderground and scraping data
- Added Moist sensor scraping Temp & Humidity data
- Added Moist sensor online check
- Added user input for Time Of The Day where irrigation may start
- Added user input for days and months where the irrigation may start
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
--]]
time = os.date("*t")
commandArray = {}
--
-- **********************************************************
-- Settings Begin
-- **********************************************************
--
-- Set your desired language (English, Dutch)
local lang = 'Dutch'
-- Lua script interval check, change the number 1 to every desired number in minutes
-- Logging:
-- 0 = None
-- 1 = Only critical
-- 2 = All
local logging = 2
--
-- **********************************************************
-- Location and minimum Rain Fall Variables For BuienRadar
-- **********************************************************
--
-- Do you want to use BuienRadar? Yes or No
local Buienradar = 'No' --(Dutch weather, rain prediction)
-- Set your location by adding Lat/Lon
local lat = '53.338023'
local lon = '6.303114'
-- Set your minimum rain fall for in a periode of 24hr
local VarMaxRainFall = '20'
--
-- **********************************************************
-- WeatherUnderGround Variables
-- **********************************************************
--
-- Name of your Weatherstation widget which contains humidity,uv,temp and pressure
local weatherstation = 'Barometer'
-- Name of your Weatherstation Wind widget
local wind = 'Wind'
-- Name of your Weatherstation Rain widget
local rain = 'Regenmeter'
-- Name of your Weatherstation UV widget
local uv = 'UV'
--
-- **********************************************************
-- Switches and Virtual Switches Variables
-- **********************************************************
--
-- Name of your manual override virtual switch
local VitManOverride = 'Irrigatie Manual Override'
-- Name of your irrigation switch for water valve/pump
local VarPump = 'Irrigatie 1 Check'
--
-- **********************************************************
-- Moist Sensor Variables
-- **********************************************************
--
-- Name of your Moist Sensor device
local MoistSensor = 'Vocht Sensor (border 1)'
-- Your target soil humidity percentage
local VarTarHum = '80'
-- Your minumim soil humidity percentage from which the irrigation will start
local VarMinHum = '55'
-- Your Minumum soil temperature, Lower won't trigger irrigation
local VarTarTempMin = '12'
-- Your Maximum soil temperature, Higher won't trigger irrigation
local VarTarTempMax = '25'
-- Amount of minuten/times we want to switch OFF irrigation after the program has started.
local VarMaxRunTime_off = 60
--
-- **********************************************************
-- Day, Month and Time Variables
-- **********************************************************
--
-- Set your desired TimeOfDay for irrigation (Morning=0, Afternoon=1, Evening=2 or Night=3)
local VarTimeOfDay = '2'
-- Set your desired irrigation day, leave blank for no irrigation
-- Set Mon, Tue, Wed, Thu, Fri, Sat or Sun
local VarDay1 = 'Mon'
local VarDay2 = ''
local VarDay3 = 'Wed'
local VarDay4 = ''
local VarDay5 = 'Fri'
local VarDay6 = ''
local VarDay7 = 'Sun'
-- Set your desired irrigation month, leave blank for no irrigation,
-- Set months like: Jan, Feb, Apr, May, Jun, Jul, Aug, Sep, ect ect
local VarMonth1 = ''
local VarMonth2 = ''
local VarMonth3 = ''
local VarMonth4 = 'Apr'
local VarMonth5 = 'May'
local VarMonth6 = 'Jun'
local VarMonth7 = 'Jul'
local VarMonth8 = 'Aug'
local VarMonth9 = 'Sep'
local VarMonth10 = ''
local VarMonth11 = ''
local VarMonth12 = ''
--
-- **********************************************************
-- Weather Variables
-- **********************************************************
--
-- Set your prefered max rain fall, higher won't trigger irrigation
local RainCurrent = '0'
-- Set your prefered minimum outside temperature for irrigation, Lower won't trigger irrigation
local WeatherTempMin = '13'
-- Set your prefered maximum outside temperature for irrigation, Higher won't trigger irrigation
local WeatherTempMax = '24'
-- Set your prefered maximum UV for irrigation, Higher won't trigger irrigation
local UVMax = '4'
-- Set your prefered max windspeed km/h, higher won't trigger irrigation
local WindSpeed = '50'
-- Set your prefered max windgust km/h, higher won't trigger irrigation
local WindGust = '150'
--
-- **********************************************************
-- Settings END
-- **********************************************************
--
--[[
--> Don't edit anything below this line or you could break the script!
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
--]]
week = tonumber(os.date("%V"));
month = tostring(os.date("%b"));
dayno = tonumber(os.date("%d"));
dayofweekno = tonumber(os.date("%w"));
day = tostring(os.date("%a"));
hour = tonumber(os.date("%H"));
min = tonumber(os.date("%M"));
--
-- **********************************************************
-- Scrapping Weather Data from WeatherUnderground
-- **********************************************************
--
--
-- Weatherstation data:
--
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[weatherstation]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWeatherTemp = tonumber(sWeatherTemp);
sWeatherHumidity = tonumber(sWeatherHumidity);
sWeatherUV = tonumber(sWeatherUV);
sWeatherPressure = tonumber(sWeatherPressure);
sWeatherUV2 = tonumber(sWeatherUV2);
if logging == 1 then
print("--------------------------------------------------------------------------------------")
if lang == 'Dutch' then
print("Irrigatie [HET WEER]");
print("-- Irrigatie ==> [WeerStation] - Temperatuur: " .. sWeatherTemp .. " ");
else
print("Irrigation [WEATHER]");
print("-- Irrigation ==> [WeatherStation] - Temperature: " .. sWeatherTemp .. " ");
end
end
if logging >= 2 then
print("--------------------------------------------------------------------------------------")
if lang == 'Dutch' then
print("Irrigatie [HET WEER]");
print("-- Irrigatie ==> [WeerStation] - Temperatuur: " .. sWeatherTemp .. " ");
print("-- Irrigatie ==> [WeerStation] - Luchtvochtigheid: " .. sWeatherHumidity .. " ");
print("-- Irrigatie ==> [WeerStation] - Barometer: " .. sWeatherPressure .. " ");
else
print("Irrigation [WEATHER]");
print("-- Irrigation ==> [WeatherStation] - Temperature: " .. sWeatherTemp .. " ");
print("-- Irrigation ==> [WeatherStation] - Humidity: " .. sWeatherHumidity .. " ");
print("-- Irrigation ==> [WeatherStation] - Pressure: " .. sWeatherPressure .. " ");
end
end
--
-- Windmeter data:
--
sWindDirectionDegrees, sWindDirection, sWindSpeed, sWindGust, sWindTemperature, sWindFeel = otherdevices_svalues[wind]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWindDirectionDegrees = tonumber(sWindDirectionDegrees);
sWindDirection = (sWindDirection);
sWindSpeed = tonumber(sWindSpeed);
sWindGust = tonumber(sWindGust);
sWindTemperature = tonumber(sWindTemperature);
sWindFeel = tonumber(sWindFeel);
if logging == 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [Windmeter] - Windsnelheid: " .. sWindSpeed .. " ");
print("-- Irrigatie ==> [Windmeter] - Windvlaag: " .. sWindGust .. " ");
else
print("-- Irrigation ==> [Windmeter] - Windspeed is: " .. sWindSpeed .. " ");
print("-- Irrigation ==> [Windmeter] - Windgust is: " .. sWindGust .. " ");
end
end
if logging >= 2 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [Windmeter] - Windrichting (in graden): " .. sWindDirectionDegrees .. " ");
print("-- Irrigatie ==> [Windmeter] - Windrichting: " .. sWindDirection .. " ");
print("-- Irrigatie ==> [Windmeter] - Windsnelheid: " .. sWindSpeed .. " ");
print("-- Irrigatie ==> [Windmeter] - Windvlaag: " .. sWindGust .. " ");
print("-- Irrigatie ==> [Windmeter] - Windtemperatuur: " .. sWindTemperature .. " ");
print("-- Irrigatie ==> [Windmeter] - Gevoelstemperatuur: " .. sWindFeel .. " ");
else
print("-- Irrigation ==> [Windmeter] - Winddirection (in degrees) is: " .. sWindDirectionDegrees .. " ");
print("-- Irrigation ==> [Windmeter] - Winddirection is: " .. sWindDirection .. " ");
print("-- Irrigation ==> [Windmeter] - Windspeed is: " .. sWindSpeed .. " ");
print("-- Irrigation ==> [Windmeter] - Windgust is: " .. sWindGust .. " ");
print("-- Irrigation ==> [Windmeter] - Windtemperature is: " .. sWindTemperature .. " ");
print("-- Irrigation ==> [Windmeter] - Windfeel is: " .. sWindFeel .. " ");
end
end
--
-- Rainmeter data:
--
sRainmeterCurrent, sRainmeterTotal = otherdevices_svalues[rain]:match("([^;]+);([^;]+)")
sRainmeterCurrent = tonumber(sRainmeterCurrent);
sRainmeterTotal = tonumber(sRainmeterTotal);
if logging == 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [Regenmeter] - Regenval nu: " .. sRainmeterCurrent .. " ");
print("-- Irrigatie ==> [Regenmeter] - Totale Regenval: " .. sRainmeterTotal .. " ");
else
print("-- Irrigation ==> [Rainmeter] - Actual rain is: " .. sRainmeterCurrent .. " ");
print("-- Irrigation ==> [Rainmeter] - Total rain is: " .. sRainmeterTotal .. " ");
end
end
--
-- UV data:
--
sUV, sSolar = otherdevices_svalues[uv]:match("([^;]+);([^;]+)")
sUV = tonumber(sUV);
sSolar = tonumber(sSolar);
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [UV] - UV Sterkte: " .. sUV .." ");
else
print("-- Irrigation ==> [UV] - UV Strength is: " .. sUV .." ");
end
end
if sRainmeterCurrent >= tonumber (RainCurrent)
and sWeatherTemp >= tonumber (WeatherTempMin)
and sWeatherTemp <= tonumber (WeatherTempMax)
and sUV <= tonumber (UVMax)
and sWindSpeed <= tonumber (WindSpeed)
and sWindGust <= tonumber (WindGust)
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [HET WEER] - Weer conditie lijkt goed om te irrigeren");
else
print("-- Irrigation ==> [WEATHER] - Weather conditions are looking good and ready for irrigation");
end
end
if(uservariables['VarWeatherCon'] == 0) then
commandArray['Variable:VarWeatherCon']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [INFO] ==> Weer conditie lijkt niet goed voor irrigatie");
else
print("-- Irrigation [INFO] ==> Weather conditions aren't good for irrigation");
end
end
if(uservariables['VarWeatherCon'] == 1) then
commandArray['Variable:VarWeatherCon']= '0'
end
end
if logging >= 1 then
print("--------------------------------------------------------------------------------------")
end
--
-- **********************************************************
-- Scrapping Temparature and moist humidity from moist sensor
-- **********************************************************
--
--
-- Moist Sensor data:
--
smoistsensorTemp, smoistsensorHumidity = otherdevices_svalues[MoistSensor]:match("([^;]+);([^;]+);([^;]+)")
smoistsensorTemp = tonumber(smoistsensorTemp);
smoistsensorHumidity = tonumber(smoistsensorHumidity);
if logging >= 1 then
if lang == 'Dutch' then
print("Irrigatie [GROND CONDITIE]");
print("-- Irrigatie ==> [VochtSensor] - Grond Temperatuur: " .. smoistsensorTemp .. " ");
print("-- Irrigatie ==> [VochtSensor] - Grond Vochtigheid: " .. smoistsensorHumidity .. " ");
else
print("Irrigation [SOIL CONDITION]");
print("-- Irrigation ==> [MoistSensor] - Soil Temperature: " .. smoistsensorTemp .. " ");
print("-- Irrigation ==> [MoistSensor] - Soil Humidity: " .. smoistsensorHumidity .. " ");
end
end
--
-- **********************************************************
-- Moist Sensor online check
-- **********************************************************
--
if (smoistsensorTemp == 0 or smoistsensorTemp == nil) then
if logging >= 1 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [VochtSensor] - [ERROR] Vocht sensor geeft een temperatuur aan van 0 of nil, irrigatie word gestaakt')
else
print('-- Irrigation ==> [MoistSensor] - [ERROR] Moist Sensor reports Temp is 0 or nil. Skipping Irrigation')
end
end
if(uservariables['VarMoistOnline'] == 1) then
commandArray['Variable:VarMoistOnline']= '0'
end
else
if(uservariables['VarMoistOnline'] == 0) then
commandArray['Variable:VarMoistOnline']= '1'
end
end
if (smoistsensorHumidity <= 6 or smoistsensorHumidity >= 99 or smoistsensorHumidity == nil) then
if logging >= 1 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [VochtSensor] - [Error] ==> Vocht sensor geeft een verkeerde vochtigheids waarde aan,, irrigatie word gestaakt')
else
print('-- Irrigation ==> [MoistSensor] - [Error] ==> Moist Sensor reports wrong Humidity values. Skipping Irrigation')
end
end
if(uservariables['VarMoistOnlineHum'] == 1) then
commandArray['Variable:VarMoistOnlineHum']= '0'
end
else
if(uservariables['VarMoistOnlineHum'] == 0) then
commandArray['Variable:VarMoistOnlineHum']= '1'
end
end
--
-- **********************************************************
-- Checking Grond Temperature Conditions
-- **********************************************************
--
if smoistsensorTemp >= tonumber (VarTarTempMin)
and smoistsensorTemp <= tonumber (VarTarTempMax)
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond temperatuur is goed voor irrigatie");
else
print("-- Irrigation ==> [MoistSensor] - Soil Temp conditions are fine for irrigation");
end
end
if(uservariables['VarMoistConTemp'] == 0) then
commandArray['Variable:VarMoistConTemp']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond temperatuur is niet goed voor irrigatie");
else
print("-- Irrigation ==> [MoistSensor] - Soil Temp conditions aren't good for irrigation");
end
end
if(uservariables['VarMoistConTemp'] == 1) then
commandArray['Variable:VarMoistConTemp']= '0'
end
end
if smoistsensorTemp <= tonumber (VarTarTempMin)
then
if logging >= 2 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond temperatuur is te laag...");
else
print("-- Irrigation ==> [MoistSensor] - Soil Temp is to low...");
end
end
end
if smoistsensorTemp >= tonumber (VarTarTempMax)
then
if logging >= 2 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond temperatuur is te hoog...");
else
print("-- Irrigation ==> [MoistSensor] - Soil Temp is to high...");
end
end
end
--
-- **********************************************************
-- Checking Grond Humidity Conditions
-- **********************************************************
--
if smoistsensorHumidity >= tonumber (VarMinHum) or smoistsensorHumidity <= tonumber (VarTarHum) or smoistsensorHumidity >= tonumber (VarTarHum)
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond vochtigheid is goed, geen irrigatie nodig");
else
print("-- Irrigation ==> [MoistSensor] - Soil humidity is fine and doesn't need irrigation");
end
end
if(uservariables['VarMoistConHum'] == 1) then
commandArray['Variable:VarMoistConHum']= '0'
end
end
if smoistsensorHumidity <= tonumber (VarMinHum)
then
if logging >= 2 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond is droog...");
else
print("-- Irrigation ==> [MoistSensor] - Soil is dry...");
end
end
if(uservariables['VarMoistConHum'] == 0) then
commandArray['Variable:VarMoistConHum']= '1'
end
end
if smoistsensorHumidity >= tonumber (VarTarHum)
then
if logging >= 2 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [VochtSensor] - Grond is nat...");
else
print("-- Irrigation ==> [MoistSensor] - Soil is wet...");
end
end
end
--
-- **********************************************************
-- Checking Buienradar
-- **********************************************************
--
if Buienradar == 'Yes' then
if logging >= 1 then
print("--------------------------------------------------------------------------------------")
print("Irrigatie [BUIENRADAR]");
end
tempfilename = '/var/tmp/rain.tmp'
totalrain = 0
rainlines = 0
function IsItGonnaRain( minutesinfuture )
url='http://gps.buienradar.nl/getrr.php?lat='..lat..'&lon='..lon
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
file = io.open(tempfilename, "r")
while true do
line = file:read("*line")
if not line then break end
linetime=string.sub(tostring(line), 5, 9)
linetime2 = os.time{year=os.date('%Y'), month=os.date('%m'), day=os.date('%d'), hour=string.sub(linetime,1,2), min=string.sub(linetime,4,5), sec=os.date('%S')}
difference = os.difftime (linetime2,os.time())
if ((difference > 0) and (difference<=minutesinfuture*60)) then
rain=tonumber(string.sub(tostring(line), 0, 3))
totalrain = totalrain+rain
rainlines=rainlines+1
end
end
file:close()
averagerain=totalrain/rainlines
return(averagerain)
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
--
-- **********************************************************
-- Is it gonna rain for the upcoming 24hr YES/NO
-- **********************************************************
--
local VarMaxRainFallPlus = VarMaxRainFall * 0.5 + VarMaxRainFall
minutes=1440
RainPrediction = IsItGonnaRain(minutes)
RainmmHour=10^((RainPrediction-109)/32)
if RainPrediction >= tonumber (VarMaxRainFallPlus) then
verw = 3
if (uservariables['VarBuienRadarCon']) == 1 then
commandArray['Variable:VarBuienRadarCon']= '0'
end
if logging >= 1 then
print('-- Irrigatie ==> [REGEN] - Verwacht '..RainPrediction..' binnen 24 uur.')
end
else
verw = 2
if (uservariables['VarBuienRadarCon']) == 0 then
commandArray['Variable:VarBuienRadarCon']= '1'
end
if logging >= 1 then
print('-- Irrigatie ==> [REGEN] - Geen of weinig regen verwacht binnen 24 uur.')
end
end
end
if logging >= 1 then
print("--------------------------------------------------------------------------------------")
end
--
-- **********************************************************
-- Checking Time Frame
-- **********************************************************
--
if logging >= 1 then
if lang == 'Dutch' then
print("Irrigatie [TIMEFRAME]");
else
print("Irrigation [TIMEFRAME]");
end
end
if day == VarDay1 or day == VarDay2 or day == VarDay3 or day == VarDay4 or day == VarDay5 or day == VarDay6 or day == VarDay7
then
if logging >= 1 then
if lang == 'Dutch' then
print(os.date("-- Irrigatie ==> [TimeFrame] - Vandaag is het %A en aangemerkt als irrigatie dag"))
else
print(os.date("-- Irrigation ==> [TimeFrame] - Today is %A and it is a irrigation day"))
end
end
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [TimeFrame] - Ingeroosterde dagen: '..VarDay1..' '..VarDay2..' '..VarDay3..' '..VarDay4..' '..VarDay5..' '..VarDay6..' '..VarDay7..'')
else
print('-- Irrigation ==> [TimeFrame] - Scheduled days are: '..VarDay1..' '..VarDay2..' '..VarDay3..' '..VarDay4..' '..VarDay5..' '..VarDay6..' '..VarDay7..'')
end
end
if(uservariables['VarTimeFrame'] == 0) then
commandArray['Variable:VarTimeFrame']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print(os.date("-- Irrigatie ==> [TimeFrame] - Vandaag is het %A en niet aangemerkt als irrigatie dag"))
else
print(os.date("-- Irrigation ==> [TimeFrame] - Today is %A and isn't a scheduled irrigation day"))
end
end
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [TimeFrame] - Ingeroosterde dagen: '..VarDay1..' '..VarDay2..' '..VarDay3..' '..VarDay4..' '..VarDay5..' '..VarDay6..' '..VarDay7..'')
else
print('-- Irrigation ==> [TimeFrame] - Scheduled days are: '..VarDay1..' '..VarDay2..' '..VarDay3..' '..VarDay4..' '..VarDay5..' '..VarDay6..' '..VarDay7..'')
end
end
if(uservariables['VarTimeFrame'] == 1) then
commandArray['Variable:VarTimeFrame']= '0'
end
end
if month == VarMonth1 or month == VarMonth2 or month == VarMonth3 or month == VarMonth4 or month == VarMonth5 or month == VarMonth6 or month == VarMonth7 or month == VarMonth8 or month == VarMonth9 or month == VarMonth10 or month == VarMonth11 or month == VarMonth12
then
if logging >= 1 then
if lang == 'Dutch' then
print(os.date("-- Irrigatie ==> [TimeFrame] - Vandaag is het een dag in %B, en deze maand is aangemerkt als irrigatie maand"))
else
print(os.date("-- Irrigation ==> [TimeFrame] - Today is a day in %B, and is scheduled for irrigation"))
end
end
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [TimeFrame] - Ingeroosterde maanden: '..VarMonth1..' '..VarMonth2..' '..VarMonth3..' '..VarMonth4..' '..VarMonth5..' '..VarMonth6..' '..VarMonth7..' '..VarMonth8..' '..VarMonth9..' '..VarMonth10..' '..VarMonth11..' '..VarMonth12..'')
else
print('-- Irrigation ==> [TimeFrame] - Scheduled months are: '..VarMonth1..' '..VarMonth2..' '..VarMonth3..' '..VarMonth4..' '..VarMonth5..' '..VarMonth6..' '..VarMonth7..' '..VarMonth8..' '..VarMonth9..' '..VarMonth10..' '..VarMonth11..' '..VarMonth12..'')
end
end
if(uservariables['VarTimeFrameMon'] == 0) then
commandArray['Variable:VarTimeFrameMon']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print(os.date("-- Irrigatie ==> [TimeFrame] - Vandaag is het een dag in %B, en deze maand is niet aangemerkt als irrigatie maand"))
else
print(os.date("-- Irrigation ==> [TimeFrame] - Today is a day in %B, and isn't scheduled for irrigation"))
end
end
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [TimeFrame] - Ingeroosterde maanden: '..VarMonth1..' '..VarMonth2..' '..VarMonth3..' '..VarMonth4..' '..VarMonth5..' '..VarMonth6..' '..VarMonth7..' '..VarMonth8..' '..VarMonth9..' '..VarMonth10..' '..VarMonth11..' '..VarMonth12..'')
else
print('-- Irrigation ==> [TimeFrame] - Scheduled months are: '..VarMonth1..' '..VarMonth2..' '..VarMonth3..' '..VarMonth4..' '..VarMonth5..' '..VarMonth6..' '..VarMonth7..' '..VarMonth8..' '..VarMonth9..' '..VarMonth10..' '..VarMonth11..' '..VarMonth12..'')
end
end
if(uservariables['VarTimeFrameMon'] == 1) then
commandArray['Variable:VarTimeFrameMon']= '0'
end
end
--
-- **********************************************************
-- checking time of the day so irrigation may proceed
-- **********************************************************
--
if (time.hour >= 6) and (time.hour <= 11) then
if (uservariables['VarTimeOfDayCurrent'] ~= 0) then
commandArray['Variable:VarTimeOfDayCurrent']= '0'
end
end
if (time.hour >= 12) and (time.hour <= 19)then
if (uservariables['VarTimeOfDayCurrent'] ~= 1) then
commandArray['Variable:VarTimeOfDayCurrent']= '1'
end
end
if (time.hour >= 20) and (time.hour <= 23)then
if (uservariables['VarTimeOfDayCurrent'] ~= 2) then
commandArray['Variable:VarTimeOfDayCurrent']= '2'
end
end
if (time.hour >= 0) and (time.hour <= 5)then
if (uservariables['VarTimeOfDayCurrent'] ~= 3) then
commandArray['Variable:VarTimeOfDayCurrent']= '3'
end
end
if (uservariables['VarTimeOfDayCurrent']) == tonumber (VarTimeOfDay) then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [TimeFrame] - Dit dagdeel komt overeen met jouw voorkeurs dagdeel voor irrigatie")
else
print("-- Irrigation ==> [TimeFrame] - Time of the Day matches your desired irrigation value")
end
end
if (uservariables['VarTimeCon']) == 0 then
commandArray['Variable:VarTimeCon']= '1'
end
end
if (uservariables['VarTimeOfDayCurrent']) ~= tonumber (VarTimeOfDay) then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie ==> [TimeFrame] - Dit dagdeel komt niet overeen met jouw voorkeurs dagdeel")
else
print("-- Irrigation ==> [TimeFrame] - Time of the Day doesnt match your criteria.")
end
end
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie ==> [TimeFrame] - Programma wacht op het geschikte dagdeel')
else
print('-- Irrigation ==> [TimeFrame] - Waiting for the right Time of the Day')
end
end
if (uservariables['VarTimeCon']) == 1 then
commandArray['Variable:VarTimeCon']= '0'
end
end
if logging >= 1 then
print("--------------------------------------------------------------------------------------")
end
if logging >= 1 then
if lang == 'Dutch' then
print("Irrigatie [PROGRAMMA]");
else
print("Irrigation [Program]");
end
end
--
-- **********************************************************
-- Giving the irrigation program a Go or NoGo
-- **********************************************************
--
if Buienradar == Yes then
if (uservariables['VarTimeCon']) == 1
and (uservariables['VarTimeFrame']) == 1
and (uservariables['VarTimeFrameMon']) == 1
and (uservariables['VarWeatherCon']) == 1
and (uservariables['VarMoistOnline']) == 1
and (uservariables['VarMoistOnlineHum']) == 1
and (uservariables['VarMoistConTemp']) == 1
and (uservariables['VarMoistOnlineHum']) == 1
and (uservariables['VarBuienRadarCon']) == 1
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Condities] ==> Alle condities lijken goed, en het irrigatie programma mag vervolgt worden")
else
print("-- Irrigation [Conditions] ==> All conditions seems to be okay, Irrigation may if needed continue program")
end
end
if (uservariables['VarMayFollowProgram']) == 0
then
commandArray['Variable:VarMayFollowProgram']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Condities] ==> Niet alle condities lijken goed, en het irrigatie programma mag niet vervolgt worden")
else
print("-- Irrigation [Conditions] ==> Not all conditions seems to be okay, Irrigation may NOT continue program")
end
end
if (uservariables['VarMayFollowProgram']) == 1
then
commandArray['Variable:VarMayFollowProgram']= '0'
end
end
else
if (uservariables['VarTimeCon']) == 1
and (uservariables['VarTimeFrame']) == 1
and (uservariables['VarTimeFrameMon']) == 1
and (uservariables['VarWeatherCon']) == 1
and (uservariables['VarMoistOnline']) == 1
and (uservariables['VarMoistOnlineHum']) == 1
and (uservariables['VarMoistConTemp']) == 1
and (uservariables['VarMoistOnlineHum']) == 1
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Condities] ==> Alle condities lijken goed, en het irrigatie programma mag vervolgt worden")
else
print("-- Irrigation [Conditions] ==> All conditions seems to be okay, Irrigation may if needed continue program")
end
end
if (uservariables['VarMayFollowProgram']) == 0
then
commandArray['Variable:VarMayFollowProgram']= '1'
end
else
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Condities] ==> Niet alle condities lijken goed, en het irrigatie programma mag niet vervolgt worden")
else
print("-- Irrigation [Conditions] ==> Not all conditions seems to be okay, Irrigation may NOT continue program")
end
end
if (uservariables['VarMayFollowProgram']) == 1
then
commandArray['Variable:VarMayFollowProgram']= '0'
end
end
end
--
-- **********************************************************
-- Irrigatie Program Active
-- **********************************************************
--
if (uservariables['VarMayFollowProgram']) == 1
and (uservariables['VarProgramRun']) == 0
and (uservariables['VarProgramOverride']) == 0
and (uservariables['VarManOverrideCon']) == 0
then
if (uservariables['VarFollowsProgram']) == 0 then
commandArray['Variable:VarFollowsProgram']= '1'
end
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Programma] ==> Irrigatie Programma Geactiveerd...")
print("-- Irrigatie [Programma] ==> Irrigatie pomp zal spoedig starten...")
print("--------------------------------------------------------------------------------------")
else
print("-- Irrigation [Program] ==> Irrigation Program Activated.")
print("-- Irrigation [Program] ==> Irrigation pump will start in a minute...")
end
end
end
--
-- **********************************************************
-- Irrigatie stopped after a full runtime cycle
-- **********************************************************
--
if (uservariables['VarProgramRun']) == 1
and (uservariables['VarProgramOverride']) == 1
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Programma] ==> Irrigatie Gestopt, Pomp heeft de maximale looptijd overschreden...")
print("-- Irrigatie [Programma] ==> Wachten op een nieuwe TimeFrame...")
print("--------------------------------------------------------------------------------------")
else
print("-- Irrigation [Program] ==> Irrigation Stopped, Maximum RunTime Exceeded...")
print("-- Irrigation [Program] ==> Awaiting new TimeFrame...")
end
end
end
--
-- **********************************************************
-- New timeframe so irrigation reactivated
-- **********************************************************
--
if (uservariables['VarTimeCon']) == 0
and (uservariables['VarMayFollowProgram']) == 0
and (uservariables['VarProgramOverride']) == 1
then
if (uservariables['VarProgramOverride']) == 1 then
commandArray['Variable:VarProgramOverride']= '0'
commandArray['Variable:VarProgramRun']= '0'
end
if logging >= 1 then
print("-- Irrigatie [Programma] ==> Irrigatie Programma Gereactiveerd...")
print("--------------------------------------------------------------------------------------")
end
end
--
-- **********************************************************
-- Irrigatie stoppen, Regen Verwacht
-- **********************************************************
if Buienradar == 'Yes' then
local VarMaxRainFallAv = VarMaxRainFallPlus * 0.5 + VarMaxRainFall
if (uservariables['VarBuienRadarCon']) == 1 and (uservariables['VarProgramRun']) == 1 and RainPrediction >= tonumber (VarMaxRainFallAv)
then
if (uservariables['VarProgramOverride']) == 0 then
commandArray['Variable:VarProgramOverride']= '1'
commandArray['Variable:VarMaxRunTime']= '-1000'
commandArray['Variable:VarProgramRun']= '0'
commandArray[VarPump]='Off'
if logging == 1 then
print("-- Irrigatie [Programma] ==> Irrigatie Programma gestopt, Regen Verwacht...")
print("--------------------------------------------------------------------------------------")
end
end
end
end
--]]
-- **********************************************************
-- Irrigatie stopped as the moist humidity exceed your desired value
-- **********************************************************
--
if smoistsensorHumidity >= tonumber (VarTarHum)
and (uservariables['VarProgramRun']) == 1
then
if (uservariables['VarProgramOverride']) == 0 then
commandArray['Variable:VarProgramOverride']= '1'
commandArray['Variable:VarMaxRunTime']= '-1000'
commandArray['Variable:VarProgramRun']= '0'
commandArray[VarPump]='Off'
end
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Programma] ==> Irrigatie Programma Gestopt, Grond vochtigheid heeft zijn maximale waarde behaald...")
print("--------------------------------------------------------------------------------------")
else
print("-- Irrigation [Program] ==> Irrigation Program Stopped, Moist Humidity exceeded your desired value...")
end
end
end
--
-- **********************************************************
-- Irrigatie stoppen, Manual Override
-- **********************************************************
--
if otherdevices[VitManOverride] == 'On'
and (uservariables['VarManOverrideCon']) == 0
and (uservariables['VarProgramRun']) == 1
then
if (uservariables['VarProgramOverride']) == 0 then
commandArray['Variable:VarProgramOverride']= '1'
commandArray['Variable:VarMaxRunTime']= '-1000'
commandArray['Variable:VarProgramRun']= '0'
commandArray[VarPump]='Off'
commandArray['Variable:VarManOverrideCon']= '1'
end
end
--
-- **********************************************************
-- Irrigatie stoppen, Manual Override
-- **********************************************************
--
if otherdevices[VitManOverride] == 'On'
and (uservariables['VarManOverrideCon']) == 1
then
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Programma] ==> Irrigatie Programma Manueel gestopt!")
print("--------------------------------------------------------------------------------------")
else
print("-- Irrigation [Program] ==> Irrigation Program Stopped, Manual Override")
end
end
end
if otherdevices[VitManOverride] == 'Off'
and (uservariables['VarManOverrideCon']) == 1
then
if (uservariables['VarProgramOverride']) == 1 then
commandArray['Variable:VarProgramOverride']= '0'
commandArray['Variable:VarMaxRunTime']= '-1000'
commandArray['Variable:VarProgramRun']= '0'
commandArray['Variable:VarFollowsProgram']= '0'
commandArray['Variable:VarManOverrideCon']= '0'
commandArray[VarPump]='Off'
if logging >= 1 then
if lang == 'Dutch' then
print("-- Irrigatie [Programma] ==> Irrigatie Programma Gereactivated!")
else
print("-- Irrigation [Program] ==> Irrigation Program Reactivated, Manual Override OFF")
end
print("--------------------------------------------------------------------------------------")
end
end
end
--
-- **********************************************************
-- Irrigatie Counter MaxRunTime and validate check
-- **********************************************************
--
--
-- Debug logging.
--
function debug(msg)
if logging >= 1 then
if lang == 'Dutch' then
print('-- Irrigatie [DEBUG] ==> ' .. msg)
else
print('-- Irrigation [DEBUG] ==> ' .. msg)
end
end
end
--
-- Info logging.
--
function info(msg)
if logging >= 1 then
if lang == 'Dutch' then
print('-- Irrigatie [Programma] ==> ' .. msg)
else
print('-- Irrigation [Program] ==> ' .. msg)
end
end
end
--
-- Error logging.
--
function error(msg)
if logging >= 2 then
if lang == 'Dutch' then
print('-- Irrigatie [ERROR] ==> ' .. msg)
else
print('-- Irrigation [ERROR] ==> ' .. msg)
end
end
end
--
-- Validates the configuration
--
function validate()
result = true
if not (uservariables['VarMaxRunTime']) then
if lang == 'Dutch' then
error('Gebruikers Variable [' .. VarMaxRunTime .. '] bestaat niet.')
else
error('User Variable [' .. VarMaxRunTime .. '] doesnt exist.')
end
result = false
end
if not otherdevices[VarPump] then
if lang == 'Dutch' then
error('Apparaat [' .. VarPump .. '] bestaat niet.')
else
error('Device [' .. VarPump .. '] doesnt exist.')
end
result = false
end
if not (uservariables['VarFollowsProgram']) then
if lang == 'Dutch' then
error('Gebruikers Variable [' .. VarFollowsProgram .. '] bestaat niet.')
else
error('User Variable [' .. VarFollowsProgram .. '] doesnt exist.')
end
result = false
end
return result
end
--
-- Tests if irrigation may start.
--
function may_update(VarIsPump)
counter = (uservariables['VarMaxRunTime'])
current_counter = counter
-- set the counter.
if VarIsPump and counter >= 0 then
counter = math.min(1, counter + 1)
elseif not VarIsPump and counter <= 0 then
counter = math.max(VarMaxRunTime_off * -1, counter - 1)
else
counter = 0
end
if counter ~= current_counter then
commandArray['Variable:VarMaxRunTime']= tostring(counter)
end
result = false
if VarIsPump and counter >= 1 then
result = true
elseif not VarIsPump and math.abs(counter) >= VarMaxRunTime_off then
result = true
end
return result
end
--
-- Start irrigation
--
function on_off(VarIsPump)
update_allowed = may_update(VarIsPump)
if lang == 'Dutch' then
info('Irrigatie Pomp = [' .. otherdevices[VarPump] .. ']')
else
info('Irrigation Pump = [' .. otherdevices[VarPump] .. ']')
end
if update_allowed then
if otherdevices[VarPump] == 'Off' and VarIsPump then
commandArray[VarPump]='On'
commandArray['Variable:VarFollowsProgram']= '0'
commandArray['Variable:VarProgramRun']= '1'
elseif (otherdevices[VarPump] == 'On' and not VarIsPump) then
commandArray[VarPump]='Off'
commandArray['Variable:VarProgramOverride']= '1'
end
end
end
--
-- Is Irrigatie On.
--
function is_VarFollowsProgram_aan()
result = false
result = (uservariables['VarFollowsProgram'] == 1)
return result
end
--
-- **********************************************************
-- Starting Irrigatie
-- **********************************************************
--
if validate() then
on_off(is_VarFollowsProgram_aan())
end
if logging >= 1 and (uservariables['VarMayFollowProgram']) == 0
and (uservariables['VarFollowsProgram']) == 0
and (uservariables['VarProgramOverride']) == 0
then
if lang == 'Dutch' then
print("-- Irrigatie [Standby] ==> Irrigatie Programma op Standby.")
else
print("-- Irrigation [Standby] ==> Irrigation Program on Standby")
end
end
if logging >= 1 and (uservariables['VarMayFollowProgram']) == 1
and (uservariables['VarFollowsProgram']) == 0
and (uservariables['VarProgramOverride']) == 1
then
if lang == 'Dutch' then
print("-- Irrigatie [Standby] ==> Er is al geirrigeerd vandaag, Irrigatie Programma op Standby.")
else
print("-- Irrigation [Standby] ==> Already Irrigated today, Irrigation Program on Standby and awaiting new cycle")
end
end
if logging >= 1 then
print("--------------------------------------------------------------------------------------")
end
return commandArray
--[[
THE END!
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
--]]