Create daily forecast from hourly forecast using the template weather platform

The OpenWeatherMap integration can be configured only once and thus limits one to either daily or hourly forecasts.

Since the hourly forecast covers the next 48 hours, I’m trying to create a forecast for “tomorrow” (the only full day to get e.g. a high and low temperature for, so it’s the only one that will make sense).

A somewhat related question has been asked before but didn’t lead to a solution and I can’t find any real examples where the forecast attribute for the template weather provider is used.

This is the easy part I have:

weather:
  - platform: template
    name: OpenWeatherMap Daily
    condition_template: "{{ states('weather.openweathermap') }}"
    temperature_template: "{{ state_attr('weather.openweathermap', 'temperature') | float }}"
    humidity_template: "{{ state_attr('weather.openweathermap', 'hunmidity') | float }}"
    attribution_template: "{{ state_attr('weather.openweathermap', 'attribution') }}"
    pressure_template: "{{ state_attr('weather.openweathermap', 'pressure') | int }}"
    wind_speed_template: "{{ state_attr('weather.openweathermap', 'wind_speed') | float }}"
    wind_bearing_template: "{{ state_attr('weather.openweathermap', 'wind_bearing') | float }}"

Scope, variables and for loops are tricky in Jinja2 templates. I can use a namespace to access a variable from the outer scope, but it can only be a primitive type. Thus, I can theoretically build up a JSON string that can then later on be converted to a dictionary (the forecast), but I also need to do some kind of group by and I can’t quite see how.

Perhaps there is a much easier alternative.

To summarise: The built-in and default enabled met.no integration automatically provides hourly and daily weather “devices”. I’m trying to achieve the same with OpenWeatherMap. I’ve also experimented with ClimaCell/tomorrow.io (which does provide both hourly and daily values) but the OpenWeatherMap data seems to correlate better with my environment with less guessing, plus I’m just reaching the API limit way too soon (due to how the integration is built, with sensors and requests for all the individual parts when the number of calls could be reduced if you want anything more than only the weather bits).

Hi there,

usually im just lurking. Yet i registered an account to answer your question - i had the same issue some weeks ago.

It is actually possible to just add a second instance of the OpenWeatherMap integration using the daily forecast. With that i currently have my-location_hourly and my-location_daily forecast sensors.

For tomorrows weather for examle i use:
state_attr('weather.daily_forecast', 'forecast')[1]["condition"] == "sunny"

I hope it helps
~ Ben

1 Like

Thanks for registering then!

How did you do this, because when I tried this I got an error that it already exists (when selecting onecall_hourly and onecall_daily respectively)?

(Also asked my question on Discord and the answer seemed to be that some integrations will allow this while others won’t.)

I tried to redo my steps and was suprised to find the same issue. Digging trough the configuration (.storage/core.config_entries) i found the workaround i used by accident:

My OpenWeatherMap instances use different lat/long coordinates. They point to the same place but are slightly different i.e.:

"title": "Daily Forecast",
    "data": {
        "latitude": 49.18058943132621,
        "longitude": 12.462886591066781,
        # [...]
    },

and

"title": "Hourly Forecast",
    "data": {
        "latitude": 49.180491,
        "longitude": 12.463073,
        # [...]
    },

(translates to a few meters apart)

~Ben

3 Likes

Thanks a mil, Ben.

As I was going about my day, I was actually wondering whether that was the trick. The other option was that maybe you had this configured in YAML previously and the integration then configured both (but I scanned the code and saw one would get a warning in that case.

Your trick works for me.

(For any other readers of this thread: I’m still keen to see real examples of building up a forecast for template weather or how to do the transformation I described in the first post.)

1 Like