Automating with the Forecast: Leveraging Weather Data in Home Assistant
- #automation

Home Assistant excels at integrating disparate devices and services, bringing them together under one roof. While displaying the current temperature and humidity is a common first step, unlocking the true power of weather data involves leveraging forecasts. Imagine automatically adjusting your irrigation schedule because rain is predicted tomorrow, pre-cooling your house before an impending heatwave, or closing your automated blinds when strong winds are expected. This guide will walk you through integrating weather data, focusing on how to utilize forecast information to build smarter, more responsive automations.
Choosing a Weather Integration
Home Assistant supports numerous weather integrations, each drawing data from different sources. Popular options include:
- Met.no: The default integration, providing data from the Norwegian Meteorological Institute. Reliable, but coverage and features might vary globally.
- OpenWeatherMap: A widely used service offering detailed current and forecast data. Requires an API key (free tier available with limitations).
- AccuWeather: Another popular commercial service with a Home Assistant integration.
- Weatherflow ( Tempest / Sky ): For users with personal weather stations, offering hyper-local data.
- Local Weather Station Integrations: Integrations for pulling data from personal weather stations like Ambient Weather, Netatmo, etc.
For this guide, we'll use OpenWeatherMap as an example, as it provides a good balance of features, ease of setup, and forecast data accessibility, relevant for a global audience. The principles, however, apply to many other integrations that provide forecast entities.
Setting up OpenWeatherMap in Home Assistant
Before you begin, you'll need an API key from OpenWeatherMap. Go to OpenWeatherMap's website, sign up for a free account, and generate an API key. Note that the free tier has usage limits, which are generally sufficient for typical Home Assistant use, but be mindful if you have many locations or frequent polling needs.
- In Home Assistant, navigate to Settings > Devices & Services.
- Click the + Add Integration button.
- Search for OpenWeatherMap and select it.
- A configuration window will appear.
- Enter your API Key.
- Optionally, provide a Name for the integration (e.g., 'Local Weather').
- Choose your Mode: 'Daily' for daily forecasts or 'Hourly' for hourly forecasts. Daily is often sufficient for many forecast-based automations, while hourly provides more granular data.
- Enter your Location. You can use latitude and longitude or search by city name. Using latitude and longitude from your Home Assistant settings is often the most accurate method.
- Select your preferred Units (Metric or Imperial).
- Click Submit.
- If successful, the integration will be added. You might be prompted to assign it to an area.
Troubleshooting Setup:
- Invalid API Key: Double-check the key for typos. Ensure you've waited a few minutes after generating the key on the OpenWeatherMap site, as it might take a moment to become active.
- Location Accuracy: If using city name, ensure it's specific enough. Using lat/lon from Home Assistant's system settings is recommended (`Settings > System > General`).
- Connection Errors: Verify your Home Assistant instance can reach the OpenWeatherMap API servers (check firewalls, network issues).
Understanding Weather Entities and Forecast Data
Once the OpenWeatherMap integration is set up, it will create several entities. The most relevant ones for forecast automation are typically:
weather.YOUR_LOCATION_NAME
: This is the primary weather entity. Its state is the current weather condition (e.g., 'sunny', 'cloudy', 'pouring'). It also has various attributes for current conditions (temperature, humidity, wind speed, etc.).- Forecast Attributes: The magic for future automation lies within the attributes of the main weather entity. Depending on whether you chose 'Daily' or 'Hourly' mode during setup, you will find an attribute named
forecast
. This attribute is a list (an array in programming terms) of forecast data points for the upcoming days or hours.
Each item in the forecast
list represents a specific future period (a day or an hour) and typically contains attributes like:
datetime
: The timestamp for this forecast period.condition
: The predicted weather condition (e.g., 'rainy', 'clear-night', 'partlycloudy').temperature
: The predicted temperature for the period.templow
: The predicted low temperature for daily forecasts.precipitation
: Predicted precipitation amount (e.g., in mm or inches).precipitation_probability
: Predicted probability of precipitation (0-1 range, or 0-100 depending on integration).wind_speed
: Predicted wind speed.wind_bearing
: Predicted wind direction.humidity
: Predicted humidity.
Accessing this nested data within automations requires using Home Assistant's templating engine, specifically Jinja2 templates.
Leveraging Forecast Data in Automations (Examples)
Here are a few practical examples demonstrating how to use forecast data in Home Assistant automations.
Example 1: Pause Irrigation if Rain is Forecasted Tomorrow
This automation checks if the precipitation probability for tomorrow is high and, if so, sends a notification or toggles a helper entity used by your irrigation automations.
automation:
- alias: Pause Irrigation on Forecasted Rain
description: Checks tomorrow's rain forecast and pauses irrigation if needed.
trigger:
- platform: time
at: "23:00:00" # Check late in the evening for the next day
condition: [] # No specific condition needed to run the check
action:
- variables:
tomorrow_forecast:
'{{ state_attr(\'weather.local_weather\', \'forecast\')[1] }}' # [0] is today, [1] is tomorrow
rain_probability_tomorrow:
'{{ tomorrow_forecast.precipitation_probability | default(0) * 100 }}' # Get probability and convert to percentage
- choose:
- conditions:
- '{{ rain_probability_tomorrow > 60 }}' # If rain probability is over 60%
sequence:
- service: persistent_notification.create
data:
title: "Irrigation Alert"
message: "Rain probability tomorrow is {{ rain_probability_tomorrow }}%. Pausing irrigation."
# Add actions here to pause your irrigation system, e.g., toggle a boolean helper
# - service: input_boolean.turn_on
# entity_id: input_boolean.irrigation_pause_rain
default: [] # Do nothing if rain probability is low
Explanation:
- The trigger runs daily at 11 PM.
- We define variables using Jinja2 templates:
tomorrow_forecast
accesses the second item (index[1]
) in theforecast
list attribute of your weather entity (replaceweather.local_weather
with your entity ID).rain_probability_tomorrow
extracts theprecipitation_probability
attribute from the tomorrow forecast item. We use| default(0)
in case the attribute is missing and multiply by 100 if your integration provides a 0-1 value.- The
choose
action checks if the calculated probability is above 60%. - If true, it sends a notification and could perform actions like turning on an
input_boolean
helper that your irrigation automations check before running.
Example 2: Adjust Thermostat Based on Upcoming Temperature Spike
This automation could check if the day after tomorrow is predicted to be significantly hotter than today and pre-cool the house.
automation:
- alias: Pre-cool for Heatwave
description: Check forecast for heat spike the day after tomorrow and pre-cool.
trigger:
- platform: time
at: "08:00:00" # Check in the morning
condition: []
action:
- variables:
today_temp:
'{{ states(\'sensor.outdoor_temperature\') | float(0) }}' # Assuming you have a current outdoor temp sensor
day_after_tomorrow_forecast:
'{{ state_attr(\'weather.local_weather\', \'forecast\')[2] if state_attr(\'weather.local_weather\', \'forecast\') | length > 2 else none }}' # [2] for day after tomorrow
day_after_tomorrow_temp:
'{{ day_after_tomorrow_forecast.temperature | float(0) if day_after_tomorrow_forecast is not none else -100 }}' # Extract temperature
- choose:
- conditions:
# Check if forecast data exists AND if temp spike is significant
- '{{ day_after_tomorrow_forecast is not none }}'
- '{{ day_after_tomorrow_temp > today_temp + 5 }}' # If temp > 5 degrees hotter
- '{{ day_after_tomorrow_temp > 30 }}' # And if the predicted temp is above a threshold (e.g., 30°C or 86°F)
sequence:
- service: climate.set_temperature
target:
entity_id: climate.your_thermostat
data:
temperature: 23 # Set thermostat to a cooler temperature
- service: persistent_notification.create
data:
title: "Climate Alert"
message: "Predicted heatwave day after tomorrow ({{ day_after_tomorrow_temp }}°). Pre-cooling initiated."
default: []
Explanation:
- This checks the forecast for the day after tomorrow (index
[2]
). We include a check that the forecast list is long enough using| length > 2
. - It compares this future temperature to the current outdoor temperature (you'll need an entity for this, like
sensor.outdoor_temperature
from your weather integration or another source). - If the temperature is predicted to be significantly higher (e.g., > 5 degrees C/F) AND above a certain threshold (e.g., 30°C), it sets your thermostat to a pre-cool temperature.
Example 3: Notify on Strong Wind Warning
Check daily for predicted high winds on the next day.
automation:
- alias: Strong Wind Warning Notification
description: Notify if strong winds are predicted for tomorrow.
trigger:
- platform: time
at: "20:00:00" # Check in the evening for the next day
condition: []
action:
- variables:
tomorrow_forecast:
'{{ state_attr(\'weather.local_weather\', \'forecast\')[1] if state_attr(\'weather.local_weather\', \'forecast\') | length > 1 else none }}'
wind_speed_tomorrow:
'{{ tomorrow_forecast.wind_speed | float(0) if tomorrow_forecast is not none else 0 }}'
- choose:
- conditions:
- '{{ tomorrow_forecast is not none }}'
- '{{ wind_speed_tomorrow > 30 }}' # Adjust threshold based on your units and definition of 'strong'
sequence:
- service: persistent_notification.create
data:
title: "Wind Warning"
message: "Strong winds ({{ wind_speed_tomorrow }} mph/kph) predicted for tomorrow. Secure outdoor items."
# Consider adding actions to close automated awnings, etc.
default: []
Explanation:
- Similar structure, but checks the
wind_speed
attribute for tomorrow's forecast. - Triggers a notification if the predicted wind speed exceeds your defined threshold. Remember to use the correct units (mph, kph, m/s) as provided by your weather integration.
Best Practices for Reliable Weather Automation
Building automations based on external data sources like weather requires careful consideration for reliability:
- Understand Your Integration's Data: Familiarize yourself with the specific attributes provided by your chosen integration, the units used, and how frequently the data is updated. Not all integrations provide the same forecast attributes or formats.
- Respect API Limits: If using a service like OpenWeatherMap's free tier, avoid setting overly aggressive polling intervals in the integration settings, as this could lead to exceeding your request limit and temporary service interruptions. The default polling intervals are usually fine.
- Handle Missing Data: Weather services can occasionally be unavailable or return incomplete data. Use Jinja2's
| default(...)
filter or check if forecast objects/attributes exist (as shown in Example 2) to prevent automation errors. - Use Templates Correctly: Accessing nested list attributes like
forecast
requires precise templating. Ensure you are using the correct index ([0]
for today/current period,[1]
for the next, etc.) and attribute names. The Developer Tools > Template editor is invaluable for testing your Jinja2 templates before putting them in automations. - Combine Data Sources: For critical automations (like irrigation), don't rely *solely* on the forecast. Combine it with real-world data from sensors (e.g., a soil moisture sensor) for a more robust system.
- Add Notifications: For important weather-based decisions, include notifications so you are aware of why an automation is or isn't running.
- Have Fallbacks: What happens if the weather data is unavailable? Ensure your critical systems (like heating/cooling or security routines) have fallback logic or manual overrides.
Conclusion
Integrating weather forecast data elevates your Home Assistant setup from reactive to proactive. By understanding the structure of forecast entities and utilizing Jinja2 templating, you can build sophisticated automations that anticipate future conditions, saving energy, protecting property, and enhancing comfort. Start with simple automations and gradually build complexity as you become more comfortable working with dynamic forecast data. Your smart home will thank you for being one step ahead of the weather.

NGC 224
Author bio: