Irrigation with multiple zones
Moderator: leecollings
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Irrigation with multiple zones
Hi
I would like to implement a script to manage my multi-zones irrigation system.
I have four zones and I would like to activate them one after the other for a specific number of minutes.
I was thinking to use the BakSeeDaa´s Google Calendar for Domoticz as starting point (when and how much time) combined with weather forecast to avoid irrigation if it's raining or has recently rained.
The thing that I don't know how to implement is the activation of all the valves one after the other.
I was thinking to use the text in the Google Calendar event as a combination of values to understand which valve to activate.
Any suggestion?
Thanks
I would like to implement a script to manage my multi-zones irrigation system.
I have four zones and I would like to activate them one after the other for a specific number of minutes.
I was thinking to use the BakSeeDaa´s Google Calendar for Domoticz as starting point (when and how much time) combined with weather forecast to avoid irrigation if it's raining or has recently rained.
The thing that I don't know how to implement is the activation of all the valves one after the other.
I was thinking to use the text in the Google Calendar event as a combination of values to understand which valve to activate.
Any suggestion?
Thanks
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
-
georgesattali
- Posts: 84
- Joined: Saturday 05 March 2016 16:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: France
- Contact:
Re: Irrigation with multiple zones
what would you think of a "device" script : when the first stops the second starts, ...
and only the first is started by Google Calendar.
and only the first is started by Google Calendar.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Irrigation with multiple zones
Or no calendar but rain expectation?
For example:
This php script will ask buienradar.nl the rain expectation for the next 2 hours.
If the total expected rain is less than 10 ask domoticz the status of switches in roomplan 3.
Depending on the time of day and status of those switches sent switch commands.
In my PHP Floorplan script there's also Google Calendar integration.
Then it would be appointments like switch zone1 on and switch zone1 off.
Or switch a dummy switch and have the rest been done depending on that one.
For example:
This php script will ask buienradar.nl the rain expectation for the next 2 hours.
If the total expected rain is less than 10 ask domoticz the status of switches in roomplan 3.
Depending on the time of day and status of those switches sent switch commands.
Code: Select all
$time=$_SERVER['REQUEST_TIME'];
$domoticzurl='http://127.0.0.1:8084/';
function Schakel($idx,$cmd,$name=NULL) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command¶m=switchlight&idx='.$idx.'&switchcmd='.$cmd.'&level=0&passcode='),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
$rains=file_get_contents('http://gps.buienradar.nl/getrr.php?lat=50.892880&lon=3.112568');
$rains=str_split($rains, 11);
$totalrain=0;
foreach($rains as $rain) {
$totalrain=$totalrain+substr($rain,0,3);
}
if($totalrain <= 10) {
$domoticz=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&plan=3'),true);
if($domoticz) {
foreach($domoticz['result'] as $dom) {
$name=$dom['Name'];
if(substr($dom['Data'],0,2)=='On') ${'S'.$name}='On';
else if(substr($dom['Data'],0,3)=='Off') ${'S'.$name}='Off';
else if(substr($dom['Data'],0,4)=='Open') ${'S'.$name}='Open';
else ${'S'.$name}=$dom['Data'];
${'SI'.$name}=$dom['idx'];
${'ST'.$name}=strtotime($dom['LastUpdate']);
}
if($time>=strtotime('6:00') && $time<=strtotime('6:20')) {
if($Szone1=='Off') Schakel($SIzone1,'On');
} else {if($Szone1=='On') Schakel($SIzone1,'Off');}
if($time>=strtotime('6:20') && $time<=strtotime('6:40')) {
if($Szone2=='Off') Schakel($SIzone2,'On');
} else {if($Szone2=='On') Schakel($SIzone2,'Off');}
if($time>=strtotime('6:40') && $time<=strtotime('7:00')) {
if($Szone3=='Off') Schakel($SIzone3,'On');
} else {if($Szone3=='On') Schakel($SIzone3,'Off');}
if($time>=strtotime('7:00') && $time<=strtotime('7:20')) {
if($Szone4=='Off') Schakel($SIzone4,'On');
} else {if($Szone4=='On') Schakel($SIzone4,'Off');}
}
}
Then it would be appointments like switch zone1 on and switch zone1 off.
Or switch a dummy switch and have the rest been done depending on that one.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Irrigation with multiple zones
Thinking some more...
You should also check if it has rained in the past x hours.
Or even better: use a ground moisture sensor.
You should also check if it has rained in the past x hours.
Or even better: use a ground moisture sensor.
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
Thank all.
This is what I have in mind.
In Google calendar I set a calendar event with a CSV like this:
10,0,8,4
I will decode it and put in four variables, one for each zone.
This will stand for minutes for each irrigation zones. If 0 no irrigation at that time.
When the calendar event will activate, before doing anything it should check if it has rained in the past xx hours or if it will rain in the upcoming ones (still need to find a way to check it for Italy).
If it's ok, it should start a switching On the first relay (I will most probably use an ESP8266 connected to a 4-relay module). And after the first finish it should start the next one that has a value higher than 0 for that specific time.
What I am not sure how to implement, is how to let the second (and higher) switch to turn On only after the first one has finished.
What do you think?
This is what I have in mind.
In Google calendar I set a calendar event with a CSV like this:
10,0,8,4
I will decode it and put in four variables, one for each zone.
This will stand for minutes for each irrigation zones. If 0 no irrigation at that time.
When the calendar event will activate, before doing anything it should check if it has rained in the past xx hours or if it will rain in the upcoming ones (still need to find a way to check it for Italy).
If it's ok, it should start a switching On the first relay (I will most probably use an ESP8266 connected to a 4-relay module). And after the first finish it should start the next one that has a value higher than 0 for that specific time.
What I am not sure how to implement, is how to let the second (and higher) switch to turn On only after the first one has finished.
What do you think?
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Irrigation with multiple zones
How will you create the events in the calendar? Is it possible to automate that?
- Westcott
- Posts: 423
- Joined: Tuesday 09 December 2014 17:04
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: UK - Glos
- Contact:
Re: Irrigation with multiple zones
Apparently Buienradar only works for the Benelux region.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
I was thinking to create them as repeating calendar event (like programming my actual Orbit irrigation system).Egregius wrote:How will you create the events in the calendar? Is it possible to automate that?
With the possibility to change them from anywhere and to bypass an event based on weather
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Irrigation with multiple zones
I guess there are other sites/api's available around the world.Westcott wrote:Apparently Buienradar only works for the Benelux region.
Sound straightforward.woody4165 wrote: I was thinking to create them as repeating calendar event (like programming my actual Orbit irrigation system).
With the possibility to change them from anywhere and to bypass an event based on weather
If you have some php knowledge you should be able to combine the script I posted above with my Google Calendar script.
I do think that just using the appointment timings should do it. Like create 4 repeating appointments: zone1 from 6:00 to 6:10, zone2 from 6:11 to 6:19 etc.
- Westcott
- Posts: 423
- Joined: Tuesday 09 December 2014 17:04
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: UK - Glos
- Contact:
Re: Irrigation with multiple zones
Wunderground and forecast.io have APIs
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
This is what, if possible, I would like to avoid. Creating n different calendar event for each zone is difficult to mantain if I want to modify them.Egregius wrote:I guess there are other sites/api's available around the world.Westcott wrote:Apparently Buienradar only works for the Benelux region.
Sound straightforward.woody4165 wrote: I was thinking to create them as repeating calendar event (like programming my actual Orbit irrigation system).
With the possibility to change them from anywhere and to bypass an event based on weather
If you have some php knowledge you should be able to combine the script I posted above with my Google Calendar script.
I do think that just using the appointment timings should do it. Like create 4 repeating appointments: zone1 from 6:00 to 6:10, zone2 from 6:11 to 6:19 etc.
Regarding PHP, I have no knowledge of it, sorry
As well as for Lua, but it's easier for me.
Meanwhile, I have just wrote some parts to use it in a script.
The first one is going to get precipitation forecast from ForecastIo
Code: Select all
-- get data from ForecastIo and decode it in forecastio
local jsondata = assert(io.popen('curl "https://api.forecast.io/forecast/xxxxxxxx/xx.xxxxxx,yy.yyyyy"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local forecastIo = json:decode(jsondevices)
-- read precipitation forecast hourly for next 12 hours
for i = 1, 12, 1 do
print(os.date("%X", forecastIo.hourly.data[i].time))
print(forecastIo.hourly.data[i].precipIntensity)
print(forecastIo.hourly.data[i].precipProbability)
end
Then, searching here and there I used this split string function to separate the text in the title of the calendar event
Code: Select all
-- Title is like "10,8,0,4"
local_uservar = uservariables['GCal88NextEvent']
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
local mySchedule = local_uservar:split(",")
for i = 1, #mySchedule do
print(mySchedule[i]) -- This will print the for schedules
end
I'm confused...
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
I found Weather Underground more complete since it has the possibility to get history data based on a specific date.
Using JSON.lua I have decoded the generate output, but I need to know how many table elements there are and go reverse, since it contains informations on a 30 minutes basis ordered from older to newer.
I would like to test only last 12 hours, so I need to test 24 table elements starting from the latest.
What is the right loop to do do it?
I get an error
The element I'm testing is
Using JSON.lua I have decoded the generate output, but I need to know how many table elements there are and go reverse, since it contains informations on a 30 minutes basis ordered from older to newer.
I would like to test only last 12 hours, so I need to test 24 table elements starting from the latest.
What is the right loop to do do it?
I get an error
Code: Select all
2016-04-16 15:44:59.869 Error: EventSystem: in irrigation2: [string "-- ..."]:71: attempt to index field '?' (a nil value)
Code: Select all
wundergound.history.observations[x].rainCubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
- Westcott
- Posts: 423
- Joined: Tuesday 09 December 2014 17:04
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: UK - Glos
- Contact:
Re: Irrigation with multiple zones
When I try it in a browser I get 15 hours of history at 5 minute intervals.
No 'rain' but -
precip_ratem: "0.0",
precip_ratei: "0.00",
precip_totalm: "0.0",
precip_totali: "0.00",
No 'rain' but -
precip_ratem: "0.0",
precip_ratei: "0.00",
precip_totalm: "0.0",
precip_totali: "0.00",
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
Strange!
I've used the call
and I get every 30 mins, for example
If I set the date as today, it goes back a certain amount of hours, if I set yesterday I get more data.
I would like to go with the today date, since I want to evaluate last 12 hours starting from now.
I've used the call
Code: Select all
http://api.wunderground.com/api/xxxxxx/history_20160416/q/zmw:xxxxxxx.json
and I get every 30 mins, for example
Code: Select all
{
"date": {
"pretty": "12:50 AM CEST on April 15, 2016",
"year": "2016",
"mon": "04",
"mday": "15",
"hour": "00",
"min": "50",
"tzname": "Europe/Rome"
},
"utcdate": {
"pretty": "10:50 PM GMT on April 14, 2016",
"year": "2016",
"mon": "04",
"mday": "14",
"hour": "22",
"min": "50",
"tzname": "UTC"
},
"tempm":"11.0", "tempi":"51.8","dewptm":"8.0", "dewpti":"46.4","hum":"82","wspdm":"5.6", "wspdi":"3.5","wgustm":"-9999.0", "wgusti":"-9999.0","wdird":"0","wdire":"Variable","vism":"-9999.0", "visi":"-9999.0","pressurem":"1015", "pressurei":"29.98","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precipm":"-9999.00", "precipi":"-9999.00","conds":"Clear","icon":"clear","fog":"0","rain":"0","snow":"0","hail":"0","thunder":"0","tornado":"0","metar":"METAR LIRF 142250Z VRB03KT CAVOK 11/08 Q1015 NOSIG" },
{
"date": {
"pretty": "1:20 AM CEST on April 15, 2016",
"year": "2016",
"mon": "04",
"mday": "15",
"hour": "01",
"min": "20",
"tzname": "Europe/Rome"
},
"utcdate": {
"pretty": "11:20 PM GMT on April 14, 2016",
"year": "2016",
"mon": "04",
"mday": "14",
"hour": "23",
"min": "20",
"tzname": "UTC"
},
"tempm":"9.0", "tempi":"48.2","dewptm":"7.0", "dewpti":"44.6","hum":"87","wspdm":"3.7", "wspdi":"2.3","wgustm":"-9999.0", "wgusti":"-9999.0","wdird":"10","wdire":"North","vism":"-9999.0", "visi":"-9999.0","pressurem":"1015", "pressurei":"29.98","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precipm":"-9999.00", "precipi":"-9999.00","conds":"Clear","icon":"clear","fog":"0","rain":"0","snow":"0","hail":"0","thunder":"0","tornado":"0","metar":"METAR LIRF 142320Z 01002KT CAVOK 09/07 Q1015 NOSIG" },
{
"date": {
"pretty": "1:50 AM CEST on April 15, 2016",
"year": "2016",
"mon": "04",
"mday": "15",
"hour": "01",
"min": "50",
"tzname": "Europe/Rome"
},
"utcdate": {
"pretty": "11:50 PM GMT on April 14, 2016",
"year": "2016",
"mon": "04",
"mday": "14",
"hour": "23",
"min": "50",
"tzname": "UTC"
},
I would like to go with the today date, since I want to evaluate last 12 hours starting from now.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
- Westcott
- Posts: 423
- Joined: Tuesday 09 December 2014 17:04
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: UK - Glos
- Contact:
Re: Irrigation with multiple zones
I used .../history/q/pws:IENGLAND873.json
This is my PWS and it gave today's history at 5 min intervals.
No 'rain' though, but various 'preci's
Perhaps you need -
wundergound.history.observations[x].precipi
although the value of -9999.00 does not look very good.
This is my PWS and it gave today's history at 5 min intervals.
No 'rain' though, but various 'preci's
Perhaps you need -
wundergound.history.observations[x].precipi
although the value of -9999.00 does not look very good.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
I get always that negative value.
I tried with a date when was raining, but I get same data.
Maybe Wunderground doesn't collect this data for Italy.
I wasn't correct saying that ForecastIO does not provide history, but does not provide, at least for my region, precip measurement, but just if has rained or not.
It got info for forecast.
I tried with a date when was raining, but I get same data.
Maybe Wunderground doesn't collect this data for Italy.
I wasn't correct saying that ForecastIO does not provide history, but does not provide, at least for my region, precip measurement, but just if has rained or not.
It got info for forecast.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
- Westcott
- Posts: 423
- Joined: Tuesday 09 December 2014 17:04
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: UK - Glos
- Contact:
Re: Irrigation with multiple zones
Perhaps you could try a nearby PWS:
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
Can I switch on a device from a Lua script setting at the same time "on delay" and "stop after"?
Inviato con Tapatalk
Inviato con Tapatalk
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
So I wrote my script, actually is made of 2 Lua and 1 bash script.
I'm sure it can be optimized, sorry I am not a programmer and know just a little bit of coding.
The script is for managing a 4 zone irrigation system thru an ESP8266 and a 4 relay board.
First of all, I've created four new Dummy Switch, one for each irrigation zone.
The main script uses the fantastic Google Calendar for Domoticz by BakSeeDaa (thanks!) to check for a specific calendar event.
It will check for an event that start with IRRIG and separated by comma with the irrigation time for each zone (ie. IRRIG,10,4,0,8)
If the event is correct, it will check for previous and upcoming rainfall forecast, to avoid irrigation. Using Weather Underground.
If all is ok, it can call the bash script that will manage zone activation in a serialized way (sleep).
The third script is "listening" for activation of one of the Dummy Switch and will send http commands to the ESP, to switch off all the relay except the one I want to activate, to be sure that only one is active.
Here is the code:
Main Script
Bash Script
Executing script
And now some help request:
I have defined the function
This function doesn't need (?) a parameter, but I haven't found a way to declare without it and to call it without.
Main issue instead is related to the bash script call. I've done it thru
The os.execute do nothing or it does not call the bash script
I've tried copying the value of "comando" and execute it into a terminal and it works.
It's like "/home/pi/domoticz/scripts/irrigation.sh 10 8 0 4"
Weird thing is this.
I tried grabbing only some code from the main one and it works.
Only difference is that the "local_uservar" is not taken from Calendar Event, but is in the same format.
What can be the issue?
I'm sure it can be optimized, sorry I am not a programmer and know just a little bit of coding.
The script is for managing a 4 zone irrigation system thru an ESP8266 and a 4 relay board.
First of all, I've created four new Dummy Switch, one for each irrigation zone.
The main script uses the fantastic Google Calendar for Domoticz by BakSeeDaa (thanks!) to check for a specific calendar event.
It will check for an event that start with IRRIG and separated by comma with the irrigation time for each zone (ie. IRRIG,10,4,0,8)
If the event is correct, it will check for previous and upcoming rainfall forecast, to avoid irrigation. Using Weather Underground.
If all is ok, it can call the bash script that will manage zone activation in a serialized way (sleep).
The third script is "listening" for activation of one of the Dummy Switch and will send http commands to the ESP, to switch off all the relay except the one I want to activate, to be sure that only one is active.
Here is the code:
Main Script
Code: Select all
---------------------------------
--Main script for manage a 4 zone irrigation system thru an ESP8266
--Is based on Gmail Calendar event (thanks to BakSeeDaa for his Google Calendar for Domoticz).
--It will check for an event that start with Irrig and separated by comma with the irrigation time for each zone (ie. Irrig,10,4,0,8)
--If the event is correct, it will check for previous and upcoming rain, to avoid irrigation.
--If all is ok, it will call a bash script that will manage zone activation in a serialized way (sleep)
--Author : woody4165
--Date : 17 April 2016
---------------------------------
commandArray = {}
if (devicechanged['GCalTicz'] == 'On') then
local innaffia = 1
else
return
end
local innaffia = 1
oggi = os.date("%Y%m%d")
-- The irrigation event must be this format "IRRIG,xx,xx,xx,xx" where xx are the activation minutes for each zone. Can be also 0.
local_uservar = uservariables['GCal88NextEvent']
-- function to split the string comma separated
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
-- Split the Event to check if this is an irrigation event and grab the activation minutes per zone
local CalEvent = local_uservar:split(",")
-- If the first element is not "IRRIG" or if does not contain all the parameters, it's not an irrigation event. Exit
if ((CalEvent[1] ~= "IRRIG") or (#CalEvent ~= 5)) then
return
end
--need to find "<" in the calendar event string to cut the last parameter at that position
w = string.find(CalEvent[5], "<")
w = w-1
local minuti_irrig1 = 0
local minuti_irrig2 = 0
local minuti_irrig3 = 0
local minuti_irrig4 = 0
-- If all is ok, define minutes per zone
minuti_irrig1 = tostring(CalEvent[2]*60)
minuti_irrig2 = tostring(CalEvent[3]*60)
minuti_irrig3 = tostring(CalEvent[4]*60)
minuti_irrig4 = (string.sub(CalEvent[5], 1, w)*60)
-- Now can control weather
function checkWeather(fake)
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
-- Check if it has rained
-- get data from Weather Underground history of current day
local jsondata = assert(io.popen('curl "http://api.wunderground.com/api/xxxxxxx/history_'..oggi..'/q/zmw:00000.x.xxxx.json"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local rained = json:decode(jsondevices)
pioggia = tonumber(rained.history.dailysummary[1].precipm)
-- If has rained exit
if (pioggia > 0) then
-- print("Ha piovuto ieri "..pioggia.."mm")
innaffia = 0
return
end
-- Check if it's going to rain
-- get data from Weather Underground forecast
local jsondata = assert(io.popen('curl "http://api.wunderground.com/api/xxxxxx/forecast/q/zmw:00000.x.xxxxx.json"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local willrain = json:decode(jsondevices)
local piove_mattina = 0
local piove_sera = 0
local precip_oggi_mattina = 0
local precip_oggi_sera = 0
-- Grab only the forecast data for today
precip_oggi_mattina = tonumber(willrain.forecast.simpleforecast.forecastday[1].qpf_day.mm)
precip_oggi_sera = tonumber(willrain.forecast.simpleforecast.forecastday[1].qpf_night.mm)
if ((precip_oggi_mattina == nil) or (precip_oggi_mattina == 0)) then
piove_mattina = 1
end
if ((precip_oggi_sera == nil) or (precip_oggi_sera == 0)) then
piove_sera = 1
end
-- If is going to rain, exit
if ((piove_mattina == 0) or (piove_sera == 0)) then
innaffia = 0
print("Pioverà la mattina "..precip_oggi_mattina.."mm e la sera "..precip_oggi_sera.."mm")
return
end
end
checkWeather(fake)
-- Now call the bash irrigation script with irrigation time per zone
comando="/home/pi/domoticz/scripts/irrigation.sh "..minuti_irrig1..' '..minuti_irrig2..' '..minuti_irrig3..' '..minuti_irrig4
os.execute(comando)
return commandArray
Bash Script
Code: Select all
#!/bin/bash
MIN_ZONE1=$1
MIN_ZONE2=$2
MIN_ZONE3=$3
MIN_ZONE4=$4
DOMO_IP="192.168.x.xx" # Domoticz IP Address
DOMO_PORT="xxxx" # Domoticz Port
IDX_ZONE1="xx"
IDX_ZONE2="xx"
IDX_ZONE3="xx"
IDX_ZONE4="xx"
if expr "$MIN_ZONE1" '>' 0
then
echo $MIN_ZONE1
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE1&switchcmd=On" > /dev/null
sleep $MIN_ZONE1
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE1&switchcmd=Off" > /dev/null
fi
if expr "$MIN_ZONE2" '>' 0
then
echo $MIN_ZONE2
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE2&switchcmd=On" > /dev/null
sleep $MIN_ZONE2
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE2&switchcmd=Off" > /dev/null
fi
if expr "$MIN_ZONE3" '>' 0
then
echo $MIN_ZONE3
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE3&switchcmd=On" > /dev/null
sleep $MIN_ZONE3
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE3&switchcmd=Off" > /dev/null
fi
if expr "$MIN_ZONE4" '>' 0
then
echo $MIN_ZONE4
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE4&switchcmd=On" > /dev/null
sleep $MIN_ZONE4
curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=switchlight&idx=$IDX_ZONE4&switchcmd=Off" > /dev/null
fi
Code: Select all
---------------------------------
--Script to activate a 4 relay board connected to an ESP8266 to control 4 irrigation zones
--Author : woody4165
--Date : 17 April 2016
---------------------------------
commandArray = {}
-- When one of the 4 Irrigation Dummy Switch will change Status
-- For each zone activation, first switch Off all the others.
if (devicechanged['IrrigZone1'] == 'On') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,0"')
elseif (devicechanged['IrrigZone1'] == 'Off') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
end
if (devicechanged['IrrigZone2'] == 'On') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,0"')
elseif (devicechanged['IrrigZone2'] == 'Off') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
end
if (devicechanged['IrrigZone3'] == 'On') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,0"')
elseif (devicechanged['IrrigZone3'] == 'Off') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
end
if (devicechanged['IrrigZone4'] == 'On') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,0"')
elseif (devicechanged['IrrigZone4'] == 'Off') then
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
os.execute('curl "http://192.168.x.xx/control?cmd=GPIO,xx,1"')
end
return commandArray
I have defined the function
Code: Select all
function checkWeather(fake)
Main issue instead is related to the bash script call. I've done it thru
Code: Select all
-- Now call the bash irrigation script with irrigation time per zone
comando="/home/pi/domoticz/scripts/irrigation.sh "..minuti_irrig1..' '..minuti_irrig2..' '..minuti_irrig3..' '..minuti_irrig4
os.execute(comando)
I've tried copying the value of "comando" and execute it into a terminal and it works.
It's like "/home/pi/domoticz/scripts/irrigation.sh 10 8 0 4"
Weird thing is this.
I tried grabbing only some code from the main one and it works.
Only difference is that the "local_uservar" is not taken from Calendar Event, but is in the same format.
What can be the issue?
Code: Select all
commandArray = {}
local_uservar = "IRRIG,10,0,8,4"
-- function to split the string comma separated
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
-- Split the Event to check if this is an irrigation event and grab the activation minutes per zone
local CalEvent = local_uservar:split(",")
-- If the first element is not "Irrig" it's not an irrigation event. Exit
if ((CalEvent[1] ~= "IRRIG") or (#CalEvent ~= 5)) then
return
end
local minuti_irrig1 = 0
local minuti_irrig2 = 0
local minuti_irrig3 = 0
local minuti_irrig4 = 0
-- If all is ok, define minutes per zone
minuti_irrig1 = tostring(CalEvent[2])
minuti_irrig2 = tostring(CalEvent[3])
minuti_irrig3 = tostring(CalEvent[4])
minuti_irrig4 = tostring(CalEvent[5])
-- Now call the bash irrigation script with irrigation time per zone
comando="/home/osmc/domoticz/scripts/irrigation.sh "..minuti_irrig1..' '..minuti_irrig2..' '..minuti_irrig3..' '..minuti_irrig4
os.execute(comando)
return commandArray
Last edited by woody4165 on Sunday 17 April 2016 15:28, edited 1 time in total.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
-
woody4165
- Posts: 476
- Joined: Monday 14 March 2016 13:55
- Target OS: Linux
- Domoticz version: beta
- Location: Rome, Italy
- Contact:
Re: Irrigation with multiple zones
Found the issue on os.execute
The last parameter was "dirty" since there was also calendar date.
I had to find "<" and made a substring before it.
I have updated previous post
The last parameter was "dirty" since there was also calendar date.
I had to find "<" and made a substring before it.
I have updated previous post
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
Who is online
Users browsing this forum: No registered users and 1 guest