One weather entity to rule them all

:cloud: 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.

:question: 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.

:chart_with_upwards_trend: 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

:computer_mouse: 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.

:technologist: 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

:balance_scale: 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

:hammer_and_wrench: 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: 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!

Buy Me A Coffee

You can also use PayPal

9 Likes

Just added it my system and it seems to be working well so far.

thanks for providing this.

Worked really well initially, but sometimes I don’t get the hourly or daily data under the forecast. In other words, instead of this:

I get this:

I’m using the Android companion app, and I can’t reproduce the problem at will, I’m afraid.

Edit: The same issue appears in two different dashboards, though.

Any issue under the template integration in your log? And what does sensor.weather_forecast_entities show in developer tools > states?

Problem’s gone now (after a restart for something else). I’ll keep an eye on it and get back to you if it returns.

What changes would I need to make to replace the “Combined” attribution under the forecast. Can’t see anything simple (which is about my level).

Latte? Americano? How much is coffee in your part of the world?

Cappuccino at breakfast, Americano during the day :wink:

The modern format doesn’t seem to have a configuration key for attribution.

I just realized that the “Weather Combined Max Temp Today” & the “Weather Combined Max Temp Tomorrow” sensors also need their units adjusted as well.

I know global warming is bad but a max temp of 159F is even a bit much for that situation. :grinning_face_with_smiling_eyes:

Ah right, let me mention that in the start post!

I’ve updated the config so YAML anchors are used now. The template sensors will always use the same unit as the weahter entity.

2 Likes