FREE Shipping on orders over $49 USA $99 INTERNATIONAL

Control: Sprinkler Control MQTT OpenHAB ESP8266 Software

Sprinkler Control MQTT OpenHAB ESP8266

Software: 

So ssh into your pi or whatever your server may be.
The first thing we are going to do is create the sprinkler system item.
So type in sudo nano /etc/openhab2/items/home.items and press enter, you may need to type in admin password.
Then type in the comment //Sprinkler System then underneath that we are going to create 2 software switches to control the sprinkler system.
Type in: 
Switch SprinklerCycle "20 Minute Cycle" <water> [ "Switchable" ]
Switch SprinklerSystemSchedule "Sprinkler System Schedule" <calendar> [ "Switchable" ]
and press enter.
Now we are going to create the actual sprinkler system items for the zones type in:
Switch MKSprinklerSystemZone1 "Sprinklers Zone 1" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z1ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z1OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z1ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z1OFF]", autoupdate="false"}
Switch MKSprinklerSystemZone2 "Sprinklers Zone 2" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z2ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z2OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z2ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z2OFF]", autoupdate="false"}
Switch MKSprinklerSystemZone3 "Sprinklers Zone 3" <water> [ "Switchable" ] {mqtt=">[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:ON:Z3ON],>[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1:command:OFF:Z3OFF],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:ON:Z3ON],<[broker:MK-SmartHouse/utilities/MK-SprinklerSystem1/state:state:OFF:Z3OFF]", autoupdate="false"}
and press enter.
 
Now press control x then y and enter.
 
Next up is the sitemap file so we can control the device and our other switches.
 
Type in sudo nano /etc/openhab2/sitemaps/home.sitemap and press enter.
 
It will bring up the sitemap. If you are following along with my series then we have many different frames in our sitemap. I am going to replace what is in the outside frame with our new items.
 
type in:
    
Switch item=MKSprinklerSystemZone1
Switch item=MKSprinklerSystemZone2
Switch item=MKSprinklerSystemZone3
Switch item=SprinklerCycle
Switch item=SprinklerSystemSchedule
and press enter,
 
Now press control x then y and enter.
 
In terminal type in sudo nano /etc/openhab2/rules/home.rules and press enter. 
In the file type in:
//If sprinkler system schedule is on then water each zone at 5 am for 20 min
rule "Sprinkler System Schedule"
when
  Time cron "0 0 5 1/1 * ? *"
then
  if(SprinklerSystemSchedule.state == ON)
  {
      sendBroadcastNotification("SPRINKLER SYSTEM: Daily Watering Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
  }
end
 
Let me explain what this does, it first checks for if the sprinkler system schedule switch is on and if it is then it will individually water each zone for 20 min everyday at 5 am. Also if you have myopenhab could connector connected then it will send you a notification on your device letting you know that it is activating the sprinklers. But if the switch is off then it will not activate the scheduled watering of everyday at 5 am for 20 minutes a zone. Now, what if you did not want it to water everyday at 5 am but every other day. Openhab rules, utilizeses something called cron time and will execute the rule based on what you set.
 
Personally I go to http://www.cronmaker.com/ and set when I want something to happen then copy and paste the cron expression it gives me.
 
Next let’s move on to the 20 minute cycle rule.
 
So paste into the rules file the following:
 
//If 20 Minute Cycle Switch Is On then water each zone for 20 min
rule "20 Minute Cycle"
when
  Item SprinklerCycle received update ON
then
      sendBroadcastNotification("SPRINKLER SYSTEM: 20 Minute Cycle Active")
      sendCommand(MKSprinklerSystemZone1, ON)
      createTimer(now.plusMinutes(20)) [|
           sendCommand( MKSprinklerSystemZone1, OFF)
           sendCommand( MKSprinklerSystemZone2, ON)
           createTimer(now.plusMinutes(20)) [|
               sendCommand( MKSprinklerSystemZone2, OFF)
               sendCommand( MKSprinklerSystemZone3, ON)
               createTimer(now.plusMinutes(20)) [|
                   sendCommand( MKSprinklerSystemZone3, OFF)
               ]
          ]
      ]
      sendCommand(SprinklerCycle, OFF)
end
 
What this does is first send you a notification that the 20 minute cycle is active and then it will individually water each zone for 20 minutes. That is it for the rules, all we did was add a scheduled rule for the sprinkler system and an instant on command rule.
 
Now press control x then y and enter.
 
sudo systemctl restart openhab2.service