Never Get Caught in the Rain Again: Building an Automated Weather Alert System
Imagine getting a text message every morning warning you about rain before you leave home. No more checking weather apps or getting caught without an umbrella. Today, I’ll show you how I built an automated weather alert system that sends SMS notifications when rain is expected.
๐ง๏ธ What Does It Do?
This Python application monitors weather forecasts and automatically sends you an SMS alert via Twilio when rain is expected in the next 12 hours. It’s simple, effective, and runs completely automated.
Key Features:
- โ Fetches real-time weather data from OpenWeatherMap API
- โ Detects rain conditions in upcoming forecast periods
- โ Sends SMS notifications via Twilio
- โ Configurable for any location worldwide
- โ Secure credential management with environment variables
- โ 96% test coverage with comprehensive unit tests
๐ ๏ธ How It Works
The system is built with a clean, modular architecture that separates concerns:
- Weather Service – Connects to OpenWeatherMap API and fetches forecast data
- Rain Detection – Analyzes weather condition codes to determine if rain is expected
- Notification Service – Sends SMS alerts through Twilio when rain is detected
- Configuration – Manages all settings securely via environment variables
๐ Prerequisites
Before you start, you’ll need:
- Python 3.7 or higher
- A free OpenWeatherMap API key
- A Twilio account (free trial available)
๐ Quick Start Guide
Step 1: Install Dependencies
pip install -r requirements.txt
This installs:
requests– For API callstwilio– For SMS notificationspython-dotenv– For environment variable managementpytest&pytest-cov– For testing
Step 2: Configure Your Environment
Copy the example environment file and add your credentials:
cp .env.example .env
Edit .env with your details:
# Twilio Configuration
TWILIO_ACCOUNT_SID=your_account_sid_here
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+1234567890
# Weather API Configuration
WEATHER_API_KEY=your_weather_api_key_here
# Location Configuration
LATITUDE=30.1575
LONGITUDE=71.5249
FORECAST_COUNT=4
# Recipient Configuration
RECIPIENT_PHONE_NUMBER=+1234567890
LOCATION_NAME=Multan
Step 3: Run the Application
python main.py
That’s it! If rain is expected, you’ll receive an SMS like:
๐๏ธ Project Structure
โโโ config.py # Configuration management
โโโ interfaces.py # Abstract interfaces
โโโ weather_service.py # Weather API implementation
โโโ notification_service.py # SMS notification service
โโโ weather_alert.py # Main alert logic
โโโ main.py # Application entry point
โโโ test_weather_alert.py # Unit tests
โโโ requirements.txt # Dependencies
โโโ .env # Your credentials (not in git)
โโโ .env.example # Template for credentials
โโโ .gitignore # Protects sensitive files
๐งช Testing & Quality
The application comes with comprehensive unit tests covering all functionality:
pytest test_weather_alert.py --cov=. --cov-report=html
๐ Test Coverage Results:
- Overall Coverage: 96%
- Total Tests: 18 (all passing)
- Implementation Files: 100% coverage
๐ Security Best Practices
This project follows security best practices:
- โ No hardcoded credentials in source code
- โ
All sensitive data stored in
.envfile - โ
.envexcluded from version control via.gitignore - โ
.env.exampleprovided as a safe template
.env file to GitHub or share your API keys publicly!โ๏ธ Customization Options
You can easily customize the system for your needs:
Change Location
Update LATITUDE and LONGITUDE in your .env file. Find coordinates for any city using LatLong.net.
Adjust Forecast Window
Modify FORECAST_COUNT to check more or fewer forecast periods (each period is 3 hours).
Customize Alert Message
Edit the message in weather_alert.py to personalize your notifications.
๐ค Automation Ideas
Take it to the next level by automating the script:
Linux/Mac (Cron Job)
# Run every day at 7 AM
0 7 * * * cd /path/to/project && python main.py
Windows (Task Scheduler)
Create a scheduled task to run python main.py at your preferred time each day.
Cloud Deployment
Deploy to AWS Lambda, Google Cloud Functions, or Heroku for serverless execution.
๐ก Real-World Use Cases
- ๐ด Cyclists planning their morning commute
- ๐๏ธ Construction managers monitoring weather for outdoor work
- ๐ Event planners tracking conditions for outdoor events
- ๐พ Farmers monitoring rainfall for crop management
- ๐ธ Photographers planning outdoor shoots
๐ฎ Future Enhancements
Here are some ideas to extend the functionality:
- Add support for multiple recipients
- Include temperature alerts (heat waves, freezing conditions)
- Send weather summaries even when no rain is expected
- Add email notifications as an alternative to SMS
- Create a web dashboard to view forecast history
- Support for multiple locations
๐ What I Learned
Building this project taught me valuable lessons about:
- Working with external APIs (OpenWeatherMap, Twilio)
- Secure credential management
- Writing testable, modular code
- Dependency injection and interface-based design
- Achieving high test coverage
๐ฏ Conclusion
This weather alert system is a practical example of how a simple Python script can solve real-world problems. With just a few lines of code and two free APIs, you can build a personal assistant that helps you plan your day better.
The best part? Once it’s set up, it runs completely on autopilot. No more checking weather apps or getting caught in unexpected rain!
๐ Get the Code
The complete source code is available on GitHub. Feel free to fork, modify, and make it your own!
Have you built something similar? What features would you add? Let me know in the comments below!
๐ฌ Stay Updated
If you enjoyed this tutorial, subscribe to my newsletter for more Python projects, coding tips, and automation ideas delivered to your inbox.