Nowadays, it is becoming increasingly important to use the vital resource of water sparingly. Inspired by the book "Let there be water" by Seth Siegel and the increased periods of drought in Germany, I set out to find a more sustainable solution for my garden Irrigation. Since I am a friend of Python programming, my solution could not consist of a simple rain gauge, but instead had to include a smart socket, an OpenWeather API and a few lines of Python code. Joking aside, in contrast to a simple rain gauge, OpenWeather offers many other useful weather features that could be used fruitfully in conjunction with a smart socket. For the sake of simplicity, I will only use the probability of rain in this article. But you could also, for example, add evaluation of the solar radiation and adjust shading systems accordingly. The possibilities are manifold thanks to OpenWeather.
Background:
While I am on my summer vacation, my garden is watered by a timer. The setup is as follows: the timer supplies power to a pump for 15 minutes every evening. This pump thereby pumps water from a water tank and waters the flowers through a hose. The pump is activated every day, regardless of whether it rains or not. This procedure is now to be replaced by a smarter method. It is to be checked whether it will rain in the next 24 hours or not. And only if no rain is predicted, the pump is to be supplied with electricity for 15 minutes in order to supply the garden with water.
In case you are looking for a deeper technical solution, I can highly recommend Chris’ Github repository. Although Chris lives in Norway, not necessarily a country known for its water scarcity, he has developed an impressive Python application that combines weather forecasts to control garden irrigation to save water.

Solution:
In contrast to Chris’ engineering approach, I’ll take a more mundane one: I connect the irrigation system (the water pump) to a smart plug. Smart in this case means that the plug is connected to the Internet. That plug will only be turned on when there’s no rain predicted within the next day.
My point is not to advertise an "American everything seller", but to provide you with a balanced solution that works reliably without requiring engineering skills. That’s why I decided to use Alexa’s smart plug together with an application called "Virtual Smart Home". That app makes it super easy to set up URL triggers for Alexa’s smart plug routines:
*Virtual Smart Home™ URL Routine Trigger for Alexa Routines**
First of all on that website "virtualsmarthome.xyz", we need to create a new trigger. I have entered "GardenHose" as the name:

Now just click on the save button and you’re done:


Now in your Alexa app, choose your smart plug and create a routine that will turn on the power whenever the URL is activated:

Click on "Create a Routine" and tell that each time "GardenHose is pressed" (which means there is a call to that URL):

…the power for the plug must be turned on:

Now repeat the above steps for a second trigger. That will be used to turn the smart plug off:

The only difference is that for this trigger "Garden Hose Off" the smart plug will be turned off:

…when that URL was hit:

Now the programming part starts. Since we will call the API of OpenWeather, we first need to create a free account (if not already done). Go to OpenWeather, create an account and confirm your mail.
After that the activation of your api key could take some minutes, so please be patient. To check if your API is working, try the beginning of the code:
from pprint import pprint
import requests, json
r = requests.get('https://api.Openweathermap.org/data/2.5/forecast?q=Hamburg,de&cnt=9&APPID=YourApiKey')
pprint(r.json())
If your API Keys works, then your output should look something similar to this:

Congratulations, we have now received the weather forecast for Hamburg (q=Hamburg) for the next 24 hours (cnt=9). Because the above Json format is a little difficult to read we convert it into a pandas dataframe using json_normalize:
df = pd.json_normalize(r.json())

The relevant weather forecasts are stored in the column "list".

It is the field "pop" which tells us about the probability of precipitation. The values of the parameter vary between 0 and 1, where 0 is equal to 0%, 1 is equal to 100%. The time distance between every "pop" is three hours, that’s why we end up having 9 "pops" in total for the next 24 hours (the first pop is the current time when the API has been called).
Since we are mainly interested in rainfall probabilities for the next 24 hours, we extract these into 9 columns.
df['list'] = df['list'].astype(str)
df = df[['list']].join(df['list'].str.extractall(r"(?<='pop': )([^'pop']+)(?=,)").unstack().droplevel(0, axis=1).rename(lambda x: 'RainProbability_' + str(x+1), axis=1))

For our example let’s estimate that it will rain if any of the rain probabilities is above 80%. Otherwise we expect that it is unlikely to rain:
import numpy as np
conditions = [(df['RainProbability_1'] > '0.8') |
(df['RainProbability_2'] > '0.8') |
(df['RainProbability_3'] > '0.8') |
(df['RainProbability_4'] > '0.8') |
(df['RainProbability_5'] > '0.8') |
(df['RainProbability_6'] > '0.8') |
(df['RainProbability_7'] > '0.8') |
(df['RainProbability_8'] > '0.8') |
(df['RainProbability_9'] > '0.8')]
choices = ['RainLikely']
df['RainProbability'] = np.select(conditions, choices, default='RainUnlikely')

We will combine this output with our Smart Plug. The Smart Plug should only turn on (and thus activate the pump to water the garden) if it is unlikely to rain in the next 24 hours. After 15 minutes of watering, the pump is supposed to turn off. On the other hand, the plug should remain off when the weather forecast predicts rain.
import webbrowser
import time
b = df['RainProbability'].iloc[0]
new = 2 # open in a new tab, if possible
if b =='RainUnlikely':
url = "https://www.virtualsmarthome.xyz/url_routine_trigger/activate.php?trigger=YourTriggerToTurnSmartPlugOn&response=html"
webbrowser.open(url, new=new)
time.sleep(900)
url="https://www.virtualsmarthome.xyz/url_routine_trigger/activate.php?trigger=YourTriggerToTurnSmartPlugOff&response=html"
webbrowser.open(url, new=new)
else:
print ('No need to water since there will be rain within the next 24h')


Below you’ll find the complete code:
Summary:
Congratulations, you have set up a smart irrigation system. That was another simple step towards a more sustainable world. Certainly our smart plug is using only sustainable electricity sources (atomic energy does not count, does it, France?:-)).
Many thanks for reading! I hope this article is helpful for you. Feel free to connect with me on LinkedIn, Twitter or Workrooms.
Special thanks also to Katherine Prairie for her valuable feedback!