Background
As many users of Home Assistant I have automations depending on weather conditions. For example sun covers need to close if the current condition changes to rainy
or you want alerts to close the window if the outside temperature exceeds the inside temperature on a hot day.
However, it could be that the weather integration you are using is (temporarily) unavailable, which will make your automations fail.
To prevent this I created a template weather entity, which combines all data of the other weather integrations you use.
How does it work?
The weather information is updated every hour, by generating a list of all weather entities (excluding this template weather entity) and using the weather.get_forecasts
entity on each entity in this list. It first uses type: daily
and then type: hourly
.
As not all weather integrations support both types, this will generate log entries when the service call fails. Therefor I added a filter on the logger
integration to prevent showing these errors.
For each combination of forecast type and weather entity which were successful, it will send an event which will add the entities to a trigger based template sensor. This will create a list of all weather entities which support either the daily or hourly forecast (or both).
This list will then be used to create a combined mapping with all forecasts, and jinja templates are used to combine it all in one forecast. For all numeric values the median value is used, and for the condition the mode (value with highest occurrence) is used.
Other (binary) sensors
Besides creating a weather entity, it also creates some other sensors which can be useful in automations:
- Highest forecasted temperature of the current day
- Highest forecasted tempearature of the next day
- The forecasted condition of the current day (which is not necessarily the current condition)
- A binary sensor which will be
on
if sun is expected in the next 3 hours
Prerequisites
- Multiple weather integrations (so at least 2)
- All these integrations should use the same settings for the units. I didn’t change those for any integration, so it uses the defaults matching my region. However, if you did change them, they should be same for each integration, otherwise the template will be comparing different units, which will lead to faulty results.
How to install?
The code used can be found on my GitHub repo: combined-weather-new.yaml
I use packages, but this can be placed directly in you configuration.yaml. However, if you already use the template
integration or the logger
integration, you might need to split the code up into the relevant parts, and place it under the existing key.
Then make sure the units used by the template weather are correct. I’ve set them according to the values matching my region, but you might need to change this to your local units. Make sure they match with the units used by the weather integrations. These units need to be set in lines 441 - 445 (see the snippet below for what to look for)
temperature_unit: &temp_unit "°C"
pressure_unit: "hPa"
wind_speed_unit: "km/h"
visibility_unit: "km"
precipitation_unit: "mm"
Make sure to leave in &temp_unit
as it is a YAML anchor which is used later on for the template sensors for highest temperature for today and tomorrow.
After adding the configuration, and updateing the units, you can reload template entities in Developer tools > YAML. If this was the first time you added any template entities, you’ll need to restart HA.
Note that the changes to the logger integration always need a restart, so in case you only reload template entities, you will get error messages in the log.
A restart will also issue an update to for the template weather entity, and the date is updated every hour, 30 seconds after the whole hour. You can also manually update the template weather by sending the update_combined_forecast
event in developer tools > events
Advanced config (providing weight)
In case you trust certain integrations more than others, you can give those more weight. This can be done at the top of the code, using the weight
variable:
- alias: "Define weights for different weather integrations"
variables:
weight:
weather.knmi:
current: 3
daily: 3
hourly: 4
weather.buienradar:
current: 2
daily: 2
You can use different weights for the current conditions, and the daily
and hourly
forecast. If nothing is provided for a weather entity, the weight will be 1
Works with…
It should work with most weather integrations, but not when a funky format for the date is used in the forecast, which can’t be converted to a datetime using as_datetime
(I know this is the case for the Weather Underground integration, which uses a timestamp in ms).
I’ve tested it with:
- Accuweather (core)
- Met.no (core)
- OpenWeatherMap (core)
- OpenMeteo (core)
- PirateWeather (custom)
- RMI (custom - BE)
- Buienradar (custom - NL)
- KNMI (custom - NL)
This is how my sensor to list the supported entities looks like:
hourly:
- weather.met
- weather.knmi
- weather.pirateweather
- weather.tomorrow_io
- weather.openweathermap
- weather.kmi
- weather.open_meteo
daily:
- weather.buienradar
- weather.met
- weather.knmi
- weather.accuweather
- weather.pirateweather
- weather.tomorrow_io
- weather.openweathermap
- weather.thuis
- weather.open_meteo
entities:
- weather.buienradar
- weather.met
- weather.knmi
- weather.accuweather
- weather.pirateweather
- weather.tomorrow_io
- weather.openweathermap
- weather.kmi
- weather.open_meteo
Coffee
If you like this template weather config, please feel free to buy me a coffee (I might spend it on another beverage though).
In case you decide to do so, thanks a lot!
You can also use PayPal