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