Getting Back To The Legacy 'forecasts' System for Weather

This topic has come up several times and I’ve had to copy and paste my code every time I answer, so I thought it would be easier to just put it on a topic instead.

As you know if you are reading this, the forecast attribute on weather entities was phased out quite some time ago, and finally removed from HA officially a few releases ago.

:warning: NOTE

The reason the forecast attribute got removed was to reduce unnecessary database bloat, due to the way attributes are stored in the database, and carrying all of that data in Home Assistant’s state machine. Keep that in mind when using this solution.

Getting at that weather now is a bit more difficult, particularly if you are just trying to get access to it easily rather than having to make a service call to get it, store it into a variable and then finally access the data in an entirely different way. Plus, the new method is useless in template editor testing.

What this does is create a sensor template that mimics the original way things were, it takes a few minutes to set up and then you have a sensor entity (not a weather entity) where forecast is again an attribute. You can make this fancier by adding the rest of your weather data too if you want, such as current condition, wind speed, etc.

The sensor is a triggered template, it updates when you start HA or every 5 minutes - change this to suit your needs. At that time it calls the get_forecasts (which is the second iteration of the new method to get forecasts), stores it into a variable and then you use that variable to populate the sensor attributes:

trigger:
  - platform: time_pattern
    minutes: /5
  - platform: homeassistant
    event: start

action:
  - service: weather.get_forecasts
    data:
      type: daily
    target:
      entity_id: weather.openweathermap
    response_variable: owm_daily

sensor:
  - name: Legacy Forecasts
    state: "{{ states('weather.openweathermap') }}"

    attributes:
      forecast: "{{ owm_daily['weather.openweathermap'].forecast }}"

Change the above weather entities and variable names to suit your needs, obviously. You can also set it to pull Hourly forecasts if your weather integration supports it, as well as Day/Night. Personally I pull all three into attributes called “daily”, “hourly” and “daynight” then I’m covered on all bases.

The only real change you have to make is to the domain of your old stuff that was using the forecast attribute from weather to sensor:

{{ state_attr('weather.openweathermap', 'forecast') }}

Now becomes:

{{ state_attr('sensor.whatever_you_named_it', 'forecast') }}

Purists might argue that this isn’t embracing the change, and I understand that, but for me I actually use the weather for a lot of automations and scripts and like to noodle it out sometimes in template editor, which can’t really be done any longer. It’s also nice to just have a single line to access the data rather than having to go through the hoops. I use both methods, but this gives me the ability for easy and immediate access when I need it.

6 Likes

What a great idea! :slight_smile: :+1:

And it’s a great way for people, who still want to rely on the “old technique”. It doesn’t make much sense, but it’s a free choice! :slight_smile:

But, I have a two part question. :laughing: Isn’t triggering every minute quite a lot? And as additional question, can’t there be a trigger for the last_changed or last_updated attribute of the weather entity?

I suppose you can make it however you want. I’ve been running this template for a while and don’t notice any degradation from running every 5, it’s only grabbing the forecast from a device that has it, not polling the weather provider itself.

As for triggers, the world is your oyster!

The issue is that you are now storing huge needless amounts of data in the recorder database. Precisely negating the reason the new method was introduced to solve.

I understand that and know that was the reason for the change. So far it hasn’t caused me undue problems and the solution for this might change over time, but it’s useful for now - even if counter to the reason it was removed.

1 Like

Nice write-up, but I had the same thought as Tom, so I added it as a note.

1 Like

I can’t really see a reason one would ever need record the data for a “forecast” template sensor. Anyone using this method (I do something similar) should probably just exclude it from the recorder by default and avoid the issue all together.

I want to use the hourly forecast to turn on anti-frost heaters. It should be possible to put this into an automation, but I don’t know how to test it:

alias: 06e. Anti-Frost
description: ""
triggers:
  - trigger: time_pattern
    hours: /3
conditions: []
actions:
  - action: weather.get_forecasts
    target:
      entity_id: weather.openweathermap
    data:
      type: hourly
    response_variable: owm_hourly
  - if:
      - condition: template
        value_template: "{{ state_attr('weather.openweathermap','forecast')|float|min < 1.0 }}"
    then:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.greenhouseheater
mode: single

You could test it by creating a trigger for a toggle or button press or have it run every 3 minutes to make sure it works.

I don’t think PhillyGilly1’s automation is following your first post’s advice. Its Template Condition attempts to reference the (now non-existent) forecast attribute of the weather entity.

1 Like

I wasn’t even looking at that, just giving a means to test - but you are absolutely correct, they are not looking at the proper entity.

Yes. I didn’t pay enough attention to the action call part which should be:

action: weather.get_forecasts
data:
  type: hourly
target:
  entity_id: weather.forecast_home
response_variable: owm_hourly

When I run this in the Developer tools I get:


I was also struggling with the nested attributes, so how do I interrogate the response to find if the lowest temperature is below 1C? Thanks

alias: 06e. Anti-Frost
triggers:
  - trigger: time_pattern
    hours: /3
actions:
  - action: weather.get_forecasts
    target:
      entity_id: weather.openweathermap
    data:
      type: hourly
    response_variable: owm_hourly
  - if:
      - condition: template
        value_template: >
          {{
            owm_hourly['weather.openweathermap'].forecast
              | map(attribute='temperature')
              | min < 1
          }}
    then:
      - action: switch.turn_on
        target:
          entity_id: switch.greenhouseheater
mode: single
1 Like

I think I am so close to getting this to work but just can’t complete the last step. Anyone know what goes instead of the ??? to display tomorrows temperature?

automation:

  - triggers:
      - trigger: time_pattern
        minutes: /5
      - trigger: homeassistant
        event: start
    actions:
      - action: notify.android_tv_fire_tv
        data:
          message: "Getting Weather forecast"
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.met_office_manchester
        response_variable: own_daily
      - action: notify.android_tv_fire_tv
        data:
          message: "tomorrow's temperature is ?????"

The get forcast action seems to work if called from the action facility in the debugger and I can see tomorros temperature