Deploy Flood Alert via Google Cloud Functions and Cloud Scheduler

Moritz Geiger
4 min readJul 18, 2021

--

Floods in Southern Germany

Recent news have spread countless of devastating images of flooded homes and destroyed infrastructure in my home country Germany. My brother and his family, who live near a small creek have also been impacted and got surprised by the floods when coming back home. Their house and barn got flooded and left them with a thick layer of mud and soaked, unusable items in the ground floor including important electronic devices. My brother then asked me to build a water level alarm to reduce the severity of such events and pre-warn the family to take preventive measures on time.

TL;DR

I built an email alert which is triggered when certain (publicly available) water levels reach a threshold. I used requests, pandas, seaborn, email and smtplib to run the alarm. It is deployed with Google Cloud Functions. Find source code here:

The Data

The flood intelligence service of Bavaria (Germany) has water measuring points 13 km and 8 km up the river from my brothers house. They measure the water level above the ‘normal’ level and post the information every 15 minutes on their website:

Water level report by flood intelligence service Bavaria

In addition each measuring points have 4 individual notification stages to indicate a warning (German: ‘Meldestufen’). The data is ideal for my email alert tool (although, I’d have wished there was an API).

Fetching the data is rather simple, since its provided in a plain html table with no fancy wrappers or hidden information. Even pandas has an import function to read the table (pd.read_html()):

Fetching water levels and notification stages

Plot Water Levels for the last 12 Hours

I wanted to add a line plot to the email to make simple to read and interpret trends quicker. For that, I used the last 48 rows (12x4) and plotted them on a Seaborn line plot alongside with the official notification stages. The plot has to be html encoded to be attached to the email later on:

Create a plot and return in html

One of the plots looked like this for example:

Plotting the water level development over the last (24) hours (Seaborn)

Assemble and Send Email

Now, I had to build a logic to trigger an email, only when there is a threat. My function table_and_level() already returns the current alert level in the dictionary. Therefore, I could simply check its output and then decide whether to send the email.

The final email contains a header, the current notification level of all measuring sites of the river, a plot of the last 12 hrs water level development and .csv files in the attachment to further study / verify the data. Also, the source urls are provided to directly go and recheck on the water status.

For debugging reasons I added a ‘debug’ parameter to send an email even if no notification stage is reached.

Compiling the email is well discussed on Stackoverflow, you can check out this thread for example. I basically use the email.mime library and attach text and file modules to the email.

To send the mail I use smtplib with a Google Mail email address. The source is here:

Deploy on Google Cloud Functions

The shown functions to get the email ready need to be fed with some input variables to run properly. All the sensitive data can be stored in an .env file when running the app locally. Later, when deploying there is a section for that on GC Functions. Those variables always come as a string. It is important to convert them if needed. To set up a Google Cloud Function and trigger it periodically I’d recommend this article from Edigleysson Silva.

The function I used is here:

Creating an execution function for Google Cloud Functions

Finally, my function source directory looked like this:

Google Cloud Functions built set

The cron job to trigger the function is set in Google Cloud Scheduler and looks like this:

Google Cloud Scheduler Cron Job Metrics (trigger every 15 mins)

--

--

Moritz Geiger
Moritz Geiger

Written by Moritz Geiger

0 Followers

I am Moritz, a data enthusiast who recently graduated from the coding school Le Wagon in the field of Data Science. Before that, I worked in the wine industry.

No responses yet