A bit late but my setup:
I have created a dummy hardware and in my case three devices for the three zones which I have in my garden. I have a script that is initiated on start, on stop and also a dzvents script. All these scripts are calling the python module of
https://github.com/allenporter/pyrainbird.
You need to install this module on your device before you can use it.
My on start invoke script: script:///[path]/rainbird_start.py 2 30 192.168.1.1, where two is the zone, 30 is the time and the last one is the ip adres.
Code: Select all
#!/usr/bin/env python3
from pyrainbird import RainbirdController
import sys
import time
import logging
logging.basicConfig(filename='pypython.log',level=logging.DEBUG)
#gets the given arguments and puts them in the right variable
zone = sys.argv[1]
minutes = sys.argv[2]
ipadress = sys.argv[3]
_LOGGER = logging.getLogger(__name__)
_LOGGER .setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
_LOGGER.addHandler(ch)
#starts irrigate zone for the given zone and minutes
controller = RainbirdController(ipadress,'password')
controller.irrigate_zone(int(zone),int(minutes))
Mind the second last line, there you need to fill in the password of the controller
The off action invoke script: script:///[path]/rainbird_stop.py 192.168.1.1, it only needs the ipadres
Code: Select all
#!/usr/bin/env python3
from pyrainbird import RainbirdController
import sys
import time
import logging
logging.basicConfig(filename='pypython.log',level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)
_LOGGER .setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
_LOGGER.addHandler(ch)
ipadress = sys.argv[1]
#stops all the irrigation
controller = RainbirdController(ipadress,'password')
controller.stop_irrigation()
Mind the second last line, there you need to fill in the password of the controller
And I got a get status dzvents script, but I have not used it for quite sometime because occasionally it did crash domoticz:
Code: Select all
return
{
on =
{
timer =
{
'every minute',
},
},
execute = function(dz)
local GazonAchter = dz.devices('Gazon achter')
local GazonVoor = dz.devices('Gazon voor')
local Druppels = dz.devices('Druppels')
--function to execute a local script with logging
local function osCommand(cmd)
if dz == nil then dz = domoticz end -- make sure dz is declared as domoticz object if not already done earlier
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
if GazonAchter.state == 'On' or GazonVoor.state == 'On' or Druppels.state == 'On'
then
osCommand("python3 /home/pi/pyrainbird/rainbird_get_state.py")
end
end
}
and the python code looks like this:
Code: Select all
#!/usr/bin/env python3
from pyrainbird import RainbirdController
import requests #used for the api calls
import sys
import time
import logging
ipdomoticz = '192.168.1.1' #ipadress of domoticz
portdomoticz = '8080' #port on wich domoticz is running
zones = {
"1": "229", #Gazon achter
"2": "230", #Gazon voor
"3": "228" #Druppels
}
#puts it in a dict so that it can be used in an for loop
zones = zones.items()
logging.basicConfig(filename='pypython.log',level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)
_LOGGER .setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
_LOGGER.addHandler(ch)
#credentials to connect to the controller
controller = RainbirdController('192.168.2.1','password')
#for loop to update the device states in domoticz
for zone, devidswitch in zones:
zonestate = controller.get_zone_state(int(zone))
print(str(zone)+' heeft als status '+str(zonestate))
devreq = requests.get("http://"+ipdomoticz+":"+portdomoticz+"/json.htm?type=devices&rid="+devidswitch).json()
devstatus = devreq["result"][0]["Status"] #gets the status value in the first array of result
if zonestate == False and devstatus == "On":
requests.post("http://"+ipdomoticz+":"+portdomoticz+"/json.htm?type=command¶m=switchlight&idx="+devidswitch+"&switchcmd=Off") # update device
elif zonestate == True and devstatus == "Off":
requests.post("http://"+ipdomoticz+":"+portdomoticz+"/json.htm?type=command¶m=switchlight&idx="+devidswitch+"&switchcmd=On") # update device
It is not really clean, but it does the job, I think this logic could be setup in a python plugin fairly easy, the only problem is that you do need an external library.