Weather template for change in how Weather behaves

In the 2024.4 release the weather forecast was finally removed from the NWS integration and requires an addition to make it get twice-daily forecasts. I’ve tried what the weather page says for configuration, but it doesn’t seem to work. I don’t use the weather cards and had built a custom display in lovelace as I combine information from my Davis Instruments system and the NWS information. Below is what I have included in my configuration.yaml and it works once after it is added and a yaml reload is done, but never updates after that. In fact if I go in and run the service in the developer tool, it only shows the information that was pulled that first time and does not update.

The sensor loads correctly and shows on the page, but only once.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.keug_daynight
        data:
          type: twice_daily
        response_variable: twice_daily
    sensor:
      - name: weather eug data1
        unique_id: weather_eug_data1
        state: "{{ as_timestamp(twice_daily['weather.keug_daynight'].forecast[0].datetime)|timestamp_custom('%A')}}"
        attributes:
          precipitation: "{{ twice_daily['weather.keug_daynight'].forecast[0].precipitation_probability|int }}"
          temperature: "{{ twice_daily['weather.keug_daynight'].forecast[0].temperature|int}}"
          daynight: |
            {%if (twice_daily['weather.keug_daynight'].forecast)[0].is_daytime == true %}
                {{'&nbsp'}}
            {% else %}
                {{'Night'}}
            {% endif %}

I’ve changed your post so the YAML is properly formatted, which makes your code actually possible to read.
Please try to do that yourself next time :slight_smile:

The sensor you created will update every hour on the whole hour, so at 0:00, 1:00, 2:00, 3:00 etc

Below is what I have included in my configuration.yaml and it works once after it is added and a yaml reload is done, but never updates after that. In fact if I go in and run the service in the developer tool, it only shows the information that was pulled that first time and does not update.

Which service in the developer tool are you referring to here?

Sorry about the formatting…wouldn’t it be great if the cut and paste just worked ;). Here is the service:

service: weather.get_forecasts
target:
  entity_id: weather.keug_daynight
data:
   type: twice_daily

Select your code and use this button to apply the code formatting (or just type 3 backticks for opening and closing of the text which should be formatted as code)
image

If the information provided by that service call doesn’t update, you should log an issue for that integration, because then it isn’t working properly.
If that service response doesn’t update, your template sensor won’t update as well.

Good to know, thanks for the pointer!

try weather.kjax_daynight. Its what I’m using and can confirm it does work.

I have the exactly the same issue. All my OpenHASP displays were all great until the last HA update where they changed the format. I’m not sure I fully understand the new method. The first thing I’m trying to get is the detailed forecast. I can create the service call and can see the info, but creating a template sensor is above my pay grade…

service: weather.get_forecasts
target:
  entity_id:
    - weather.keul_daynight
data:
  type: twice_daily

Results in this response. I want the detailed_description:

weather.keul_daynight:
  forecast:
    - detailed_description: Partly cloudy, with a low around 46. West wind around 9 mph.
      datetime: "2024-04-15T01:00:00-06:00"
      precipitation_probability: 0
      is_daytime: false
      condition: partlycloudy
      wind_bearing: 270
      temperature: 46
      dew_point: 40
      wind_speed: 9
      humidity: 74
    - detailed_description: >-
        Partly sunny, with a high near 66. West northwest wind 12 to 20 mph,
        with gusts as high as 29 mph.
      datetime: "2024-04-15T06:00:00-06:00"
Etc, etc, etc

So if I follow the OP’s format it would appear I have to create a template sensor (somewhere? Where? I’ve never used triggers in a template sensor) but this is where I’m stumbling. It will probably be something like this, adding Detailed Description as an “attribute”?

template:
  - trigger:
      - platform: time_pattern
        hours: /12
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.keug_daynight
        data:
          type: twice_daily
        response_variable: twice_daily
    sensor:
      - name: Weather Keul Detailed Description
        unique_id: weather_keul_detailed_forecast
        state: "{{ as_timestamp(twice_daily['weather.keul_daynight'].forecast[0].datetime)|timestamp_custom('%A')}}"
        attributes:
          detailed_description: "{{ twice_daily['weather.keul_daynight'].forecast[0].detailed_description }}"
          precipitation: "{{ twice_daily['weather.keul_daynight'].forecast[0].precipitation_probability|int }}"
          temperature: "{{ twice_daily['weather.keul_daynight'].forecast[0].temperature|int}}"
          daynight: |
            {%if (twice_daily['weather.keug_daynight'].forecast)[0].is_daytime == true %}
                {{'&nbsp'}}
            {% else %}
                {{'Night'}}
            {% endif %}

If I can get it into a sensor I think I can figure out how to use it in OpenHASP. If, as the OP states, it only updates once, can’t I set up a recurring Automation as a hack to update the sensor manually? I’m unclear as to which service I would call to update my new template sensor 2x a day.

BTW, What a total PITA… sometimes it’s best to leave legacy stuff that just works in place.

I got some help on Discord. This seems to work:

- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.keul_daynight
      response_variable: detailed_description
  sensor:
    - name: KEUL Detailed Description 
      unique_id: keul_detailed_description 
      state: "{{ detailed_description['weather.keul_daynight'].forecast[0].detailed_description }}"

To help your understanding, as you seem to have just sprinkled detailed_description everywhere, hoping for the best, here’s the same code as you posted but with a couple of tweaks.

The response_variable can be called whatever you please: here, I’ve changed it to x, and I’ve changed the state to only use bracket notation, rather than the confusing mix of dot and bracket previously used:

- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.keul_daynight
      response_variable: x
  sensor:
    - name: KEUL Detailed Description 
      unique_id: keul_detailed_description 
      state: "{{ x['weather.keul_daynight']['forecast'][0]['detailed_description'] }}"

That response variable will contain an object structure, something like this (greatly simplified):

{"weather.keul_daynight":
  {"forecast":
    [
      {"time": "[first timestamp",
       "detailed_description": "(first description)"},
      {"time": "[second timestamp",
       "detailed_description": "(second description)"}
    ]
  }
}

and the state simply pulls out the first ([0]) detailed description:

1 Like

VERY helpful! Now I can take my ring off!
image

This change to the weather object is very confusing. I need to attack the hourly stuff next, and I think I grasp the concepts a bit better now.

Still struggling… this gives me the detailed_description only. The next 2 sensors are blank. If I remove the detailed_description, the condition then works and so on, so it appears that I have to create a new service call for every attribute? I must be missing something bc that would be totally inefficient.

- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.keul_daynight
      response_variable: x
  sensor:
    - name: KEUL Detailed Description 
      unique_id: keul_detailed_description 
      state: "{{ x['weather.keul_daynight'].forecast[0].detailed_description }}"
    - name: KEUL condition
      unique_id: keul_condition
      state: "{{ x['weather.keul_daynight'].forecast[0].condition }}"
    - name: KEUL is_daytime
      unique_id: keul_is_daytime
      state: "{{ x['weather.keul_daynight'].forecast[0].is_daytime }}"

Edit: all of a sudden they populated.


Am I missing a trigger? Would this be better?

- trigger:
    - platform: time_pattern
      hours: /1
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: weather.keul_daynight

I’m also not sure I understand service calls as they relate to my current time. It’s 7:45am right now. When I make the twice_daily service call I get this returned:

weather.keul_daynight:
  forecast:
    - detailed_description: Clear, with a low around 31. North northwest wind around 7 mph.
      datetime: "2024-04-18T23:00:00-06:00"
      precipitation_probability: 0
      is_daytime: false
      condition: clear-night
      wind_bearing: 337.5
      temperature: 31
      dew_point: 23
      wind_speed: 7
      humidity: 64
    - detailed_description: Sunny, with a high near 63. North wind around 7 mph.
      datetime: "2024-04-19T06:00:00-06:00"
      precipitation_probability: 0
      is_daytime: true
      condition: partlycloudy
      wind_bearing: 0
      temperature: 63
      dew_point: 28
      wind_speed: 7
      humidity: 67
    - detailed_description: Mostly clear, with a low around 37. North northwest wind around 7 mph.
      datetime: "2024-04-19T18:00:00-06:00"

The first timestamp is datetime: “2024-04-18T23:00:00-06:00” and we’re now past that since it’s 7:45am and into the second forecast of datetime: “2024-04-19T06:00:00-06:00”. So why would it keep returning the first one? Is there somewhere I have to specify my local time zone so I get the correct response?

Edit: there is a known bug with the NWS integration, all updates are delayed. This explains it.

For those who are apparently doing “high level” functions with weather, this is a GREAT post. I use OpenHasp and this workaround is exactly what I needed. Very minimal changes to all my plates were required after creating the new entity.

Finally a working forecast on all my wall displays. AccuWeather only returns 5 days so I might look for one that returns more and isn’t so buggy like NWS…