Update from using weather attributes with the new service: weather.get_forecasts

Sept 2023 Ha announced that the use of weather attributes will be depreciated in March of 2024 I did not have time to fix this issue until now.
Weather forecast service

If you use the old way of using the weather attributes in a template they would no longer work.

OLD WAY

 - obj: "p5b51" # Forecast time +8h
      properties:
        "text": >
          {%- if not is_state('weather.barb_s_place_hourly','unavailable') %}
          {%- set update = states('sensor.date') %}
          {%- set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {%- set event = as_timestamp(strptime(state_attr('weather.barb_s_place_hourly','forecast')[8]['datetime'], '%Y-%m-%dT%H:%M:%S%z', default='2020-01-00T00:00:00+00:00')) %}
          {%- set delta = ((event - midnight) // 86400) | int %}
          {%- if delta == 0 %}
          Today
          {%- elif delta == 1 %}
          Tomorrow
          {%- endif %}
          {{- event | timestamp_custom(" %-I %p") }}
          {%- endif %}

    - obj: "p5b52" # Forecast temp +8h
      properties:
        "text": "{{ state_attr('weather.barb_s_place_hourly','forecast')[8]['temperature'] if not is_state('weather.barb_s_place_hourly','unavailable') else '-' }}"

    - obj: "p5b53" # Forecast condition +8h
      properties:
        "src": >
          {%- if not is_state('weather.barb_s_place_hourly','unavailable') %}
          L:/w-32-{{ state_attr('weather.barb_s_place_hourly','forecast')[8]['condition'] }}.png
          {%- endif %} 

Solution:

Create a Hourly weather sensor. sensor.weather_hourly useing the service: weather.get_forecasts

This does not work for accuweather I needed to use the Meteorologisk institutt (Met.no) integration.
Just for less confusion I created a new weather service called home forecast
then created new sensor sensor.weather_hourly in my template.yaml

hourly forecast sensor

#weather forcast hourly sensor 
- trigger:
    - platform: time_pattern
      minutes: /1

    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.home
      response_variable: hourly

  sensor:
    - name: Weather Hourly
      state: "{{ states('weather.home') }}"
      attributes:
        temperature: "{{ state_attr('weather.home', 'temperature') }}"
        dew_point: "{{ state_attr('weather.home', 'dew_point') }}"
        temperature_unit: "{{ state_attr('weather.home', 'temperature_unit') }}"
        humidity: "{{ state_attr('weather.home', 'humidity') }}"
        cloud_coverage: "{{ state_attr('weather.home', 'cloud_coverage') }}"
        pressure: "{{ state_attr('weather.home', 'pressure') }}"
        pressure_unit: "{{ state_attr('weather.home', 'pressure_unit') }}"
        wind_bearing: "{{ state_attr('weather.home', 'wind_bearing') }}"
        wind_speed: "{{ state_attr('weather.home', 'wind_speed') }}"
        wind_speed_unit: "{{ state_attr('weather.home', 'wind_speed_unit') }}"
        visibility_unit: "{{ state_attr('weather.home', 'visibility_unit') }}"
        precipitation_unit: "{{ state_attr('weather.home', 'precipitation_unit') }}"
        forecast: "{{ hourly['weather.home'].forecast }}"    

Here is another one using the National weather service integration twice daily.
For testing I recommend changing the

  - platform: time_pattern
    hours: /1 

to minutes or seconds so you don’t have to wait for data update then change back to at 1 hour or more


Below has been edited for USE with the National weather service forecast
action:
- service: nws.get_forecasts_extra
from
action:
- service: weather.get_forecasts

#My templet for weather forecast
#national weather service 
- trigger:
  - platform: time_pattern
    hours: /1
  action:
    - service: nws.get_forecasts_extra
      data:
        type: twice_daily
      target:
        entity_id: weather.pg425
      response_variable: twice_daily
    - variables:
        nextday: "{{ twice_daily['weather.pg425'].forecast[2] }}"
        day: "{{ twice_daily['weather.pg425'].forecast[0] }}"
        evening: "{{ twice_daily['weather.pg425'].forecast[1] }}"
  sensor:
    - name: "Forcast next morning Details NWS"
      unique_id: next_morningdetailed_descriptions
      state: "{{ nextday.detailed_description }}"

    - name: "Forcast TODAY Details NWS"
      unique_id: morningdetailed_descriptions2
      state: "{{ day.detailed_description }}"

    - name: "Forcast evening Details NWS"
      unique_id: eveningdetailed_descriptions2
      state: "{{ evening.detailed_description }}"       

    - name: "Forcast Eve temperature NWS"
      unique_id: eveningtempNWS
      state: "{{ evening.temperature }}"     

    - name: "Forcast today's temperature NWS"
      unique_id: todaytempNWS
      state: "{{ day.temperature }}" 
      
    - name: "Forcast Today chance of rain NWS"
      unique_id: todaychanceofrain
      state: "{{ day.precipitation_probability }}"       
    - name: "Forcast Evening chance of rain NWS"
      unique_id: eveningchanceofrain
      state: "{{ evening.precipitation_probability }}"     
     
    - name: "Forcast Today condition NWS"
      unique_id: todaycondition
      state: "{{ day.condition }}"       
    - name: "Forcast Evening condition NWS"
      unique_id: eveningcondition
      state: "{{ evening.condition }}"
      
    - name: "Forcast Today wind_speed NWS"
      unique_id: todaywind_speed
      state: "{{ day.wind_speed }}"       
    - name: "Forcast Evening wind_speed NWS"
      unique_id: eveningwind_speed
      state: "{{ evening.wind_speed }}"         

New yaml for plates for my openhasp +8 hour data to be displayed. Basicly replace all weather.barb_s_place_hourly' with sensor.weather_hourly

yaml

    - obj: "p5b51" # Forecast time +8h
      properties:
        "text": >
          {%- if not is_state('sensor.weather_hourly','unavailable') %}
          {%- set update = states('sensor.date') %}
          {%- set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {%- set event = as_timestamp(strptime(state_attr('sensor.weather_hourly','forecast')[8]['datetime'], '%Y-%m-%dT%H:%M:%S%z', default='2020-01-00T00:00:00+00:00')) %}
          {%- set delta = ((event - midnight) // 86400) | int %}
          {%- if delta == 0 %}
          Today
          {%- elif delta == 1 %}
          Tomorrow
          {%- endif %}
          {{- event | timestamp_custom(" %-I %p") }}
          {%- endif %}

    - obj: "p5b52" # Forecast temp +8h
      properties:
        "text": "{{ state_attr('sensor.weather_hourly','forecast')[8]['temperature'] if not is_state('sensor.weather_hourly','unavailable') else '-' }}"

    - obj: "p5b53" # Forecast condition +8h
      properties:
        "src": >
          {%- if not is_state('sensor.weather_hourly','unavailable') %}
          L:/w-32-{{ state_attr('sensor.weather_hourly','forecast')[8]['condition'] }}.png
          {%- endif %}

For more on my personal openhasp project.
Home assistant Forums Openhasp on a Lanbon L8 and WT32-SC01 Plus
Gethub openhasp-

sources
Weather forecast service
HA dev Weather forecasts

Unsure How To Use Hourly Forecast Information (Since 2024.3.0) #112628

4 Likes

Thanks for the heads-up. I didn’t see this before, even though apparently it’s been deprecated for a while.

I’ve made a note to check my weather displays when I update to 2024.3.x. Frankly HA isn’t my primary source for weather information, so it’s not that big a deal for me. I do use outside temp in a couple of calculations though.

Am I understanding correctly that the weather data are being moved out of attributes? That would make sense, if the goal is to make better use of the Recorder database. Currently all attributes are stored in a new state_attributes record any time any of them change. That includes not only the values, but the attribute field names, as well. In JSON format. Obviously that’s a lot of duplicated data for all those weather attributes. I’ve had to exclude my weather entities and just extract what I need with templates.

For me it really only screwed up my hourly forecast stuff that came from accuweather and met.no sources . My temps and local stuff is all local from my own weather station and using the Local MQTT AmbientWeather2MQTT to draw from my console . I kept seeing the warnings but did not have time to figure out till it stopped LOL

saved my day after 2024.4 :pray:

Could you please write how did you manage to make ambientweather2mqtt working?
I installed the add-on, my weather station is ws-2902A, I managed to find app and upgrade firmware, enabled custom webserver and entered settings as they are outlined in the ambientweather2mqtt documentation but getting zero data.

Sorry I just saw this
Did you get it going?

@invisible999
I don’t remember
I just followed the instructions and it worked .

Do you see the console in a MQTT explorer?



Yes, this morning it mysteriously started working…

1 Like

Hi,

can you please share your pages.jsonl for your weather forecast widget?

This is already in the original post but
I will copy and paste
For more on my personal openhasp project.
Home assistant Forums Openhasp on a Lanbon L8 and WT32-SC01 Plus
Gethub openhasp-

In original post I Added Forecast sensors from the National weather service integration.
I really like the Forecast Details they give to use for Voice announcements of weather.

A slight chance of showers and thunderstorms before 5am, 
then a slight chance of rain. Partly cloudy. Low around 76, 
with temperatures rising to around 80 overnight. North wind 0 to 5 mph.

That assumes that you find that to be an accurate source of forecast information.

I use both national weather services,
And AccuWeather

update to National weather service template for detailed_description
Only set for twice_daily forecasts. 50% Chance of rain, otherwise partly cloudy with a high of 75F.
the service is now nws.get_forecasts_extra

Action nws.get_forecasts_extra

nws.get_forecasts_extra provides extra data in a form similar to weather.get_forecasts…

1 Like

I’m trying to pull the detailed_description from the NWS forecast using the new(ish) get_forecasts_extra. I’ve been tinkering for hours and keep ending up with empty sensors. This is the code I am using in configuation.yaml for the sensors (my NWS weather entity is weather.kbtm).

template:
  - trigger:
      - platform: time_pattern
        minutes: 10
    action: 
      - service: nws.get_forecasts_extra
        data:
          type: twice_daily
        target:
          entity_id: weather.kbtm
        response_variable: twice_daily
      - variables:
          nextday: "{{ twice_daily['weather.kbtm'].forecast[2] }}"
          day: "{{ twice_daily['weather.kbtm'].forecast[0] }}"
          evening: "{{ twice_daily['weather.kbtm'].forecast[1] }}"
    sensor:
      - name: "NWS Detailed Forecast 2"
        unique_id: forecast_detailed_2
        state: "{{ nextday.detailed_description }}"

      - name: "NWS Detailed Forecast 0"
        unique_id: forecast_detailed_0
        state: "{{ day.detailed_description }}"

      - name: "NWS Detailed Forecast 1"
        unique_id: forecast_detailed_1
        state: "{{ evening.detailed_description }}"

If someone could point out what I’m doing wrong, I would be very appreciative! I can see that the template sensors get created, but they just display “Unknown.” Thanks!

I’m not familiar with the new extra
Or the weather services you are using
But I know with my setups I had to see the weather services docs and look at what is being exposed in HA with developer tools to find the data I wanted .

This is what’s been working for me.

template:
  - trigger:
      - trigger: time_pattern
        minutes: "/5"
      - trigger: homeassistant
        event: start
    action:
      - action: weather.get_forecasts
        data:
          type: twice_daily
        target:
          entity_id: weather.kbtm
        response_variable: daily
      - action: nws.get_forecasts_extra
        data:
          type: twice_daily
        target:
          entity_id: weather.kbtm
        response_variable: weather_forecast
    sensor:
      - name: Daily Forecast
        unique_id: daily_forecast
        state: "{{ now().isoformat() }}"
        icon: mdi:hours-24
        attributes:
          forecast: "{{ daily['weather.kbtm'].forecast }}"
      - name: Daily Forecast Extra
        unique_id: daily_forecast_extra
        state: "{{ now().isoformat() }}"
        icon: mdi:hours-24
        attributes:
          forecast: "{{ weather_forecast['weather.kbtm'].forecast }}"

FYI: NWS integration uses indexed attributes for the forecasts. A sensor template to extract forecast index [0], will return the near-term forecast, day or night. For twice-daily forecasts, the remainder of the indexes will represent day or night forecasts in 12-hour intervals depending on datetime and/or is_daytime attributes. They alternate between day (6 AM to 6 PM) and night (6 PM to 6 AM; which will change the current day at midnight). The behavior might complicate your sensors, however I’m guessing most weather cards use the datetime attribute to determine when each index is valid; it’s doing the work for you.

The two sensors templates I’ve been using populate the entity state with time to indicate when the forecasts change. Except for 6 AM and 6 PM (local time) forecasts, the updates seldom occur at the top of the hour.