Help with extracting data longer than 255 characters from NWS Weather integration

Hello, I’m looking from some help on getting data longer than 255 characters out of the NWS Weather integration to use in a voice notification.

Currently I use the following in my template.yaml

- trigger:
    - platform: time_pattern
      # Get most recent forecast at 5 minutes after the hour
      minutes: "/5"
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.kmsp_daynight
      response_variable: daily_forecast

  sensor:
    - name: "Day Forecast 1"
      unique_id: "day_forecast_1"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[0].detailed_description}}"
    - name: "Day Forecast 2"
      unique_id: "day_forecast_2"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[1].detailed_description}}"
    - name: "Day Forecast 3"
      unique_id: "day_forecast_3"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[2].detailed_description}}"
    - name: "Day Forecast 4"
      unique_id: "day_forecast_4"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[3].detailed_description}}"

This works fine 80% of the time, but when the detailed_description is longer than 255 characters the return value is “unknown” . I want to get the full text string without truncating the data.

Thanks in advance

You can’t put more than 256 characters in the state - that’s all it stores. So you could either truncate it to a maximum of 256 characters (or is it 255 - can’t remember), or put it in an attribute, where no such restriction exists.

Hence my question - how do I code it?

  sensor:
    - name: "Day Forecast 1"
      unique_id: "day_forecast_1"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[0].detailed_description[:255] }}"
      attributes:
        full_description: "{{ daily_forecast['weather.kmsp_daynight'].forecast[0].detailed_description }}"

and similarly for the others.

I already have the Template Sensor integrated and working, so I don’t see this actually accomplishes anything over what I already have, I do get the 4 daily forecast descriptions I want unless the description text is longer that 255 characters, only then do I get “unavailable”.

But, what I want is to change the code in such a way that I’ll get the full description. What I’m envisioning is code that will give me the first part of the description and code that will give me the overflow so I can put them back together and have the full description.

raw data from the get_forcasts call gives me this:

    - detailed_description: >-
        Showers and thunderstorms before 10pm, then showers and thunderstorms
        between 10pm and 1am, then a chance of showers and thunderstorms. Mostly
        cloudy, with a low around 52. North wind 15 to 20 mph, with gusts as
        high as 30 mph. Chance of precipitation is 100%. New rainfall amounts
        between 1 and 2 inches possible.

my code:
    - name: "Day Forecast 4"
      unique_id: "day_forecast_4"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[3].detailed_description }}"

gives me: unknown

your code:
    - name: "Day Forecast 11"
      unique_id: "day_forecast_11"
      state: "{{ daily_forecast['weather.kmsp_daynight'].forecast[3].detailed_description[:255] }}"
      attributes:
        full_description: "{{ daily_forecast['weather.kmsp_daynight'].forecast[3].detailed_description }}"

gives me: unknown

Can you try state: "OK" in my code please? See if the long description gets pulled into the attribute?

I assume you’re reloading the YAML between each change? Please also have a look in the logs.

This is really weird - just went into HA and checked the values for my forecast sensors, and now yours shows the detailed description truncated to 255 characters AND the attributes to be the full description, including the words full_description. I can use that !

What’s weird is that I did change state to OK and there was NO change - yes I did reload YAML templates, also ALL YAML CONFIGURATION and restarted HA - nothing changed. But if I can adapt the your code to what I need then all is good. Thank you.

So this is what I finally ended up with:

- trigger:
    - platform: time_pattern
      # Get most recent forecast at 5 minutes after the hour
      minutes: "/5"
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.kmsp_daynight
      response_variable: daily_forecast

  sensor:
    - name: "Daily Forecasts"
      unique_id: "daily_forecasts"
      state: none
      attributes:
        day_zero: "{{ daily_forecast['weather.kmsp_daynight'].forecast[0].detailed_description }}"
        day_one: "{{ daily_forecast['weather.kmsp_daynight'].forecast[1].detailed_description }}"
        day_two: "{{ daily_forecast['weather.kmsp_daynight'].forecast[2].detailed_description }}"
        day_three: "{{ daily_forecast['weather.kmsp_daynight'].forecast[3].detailed_description }}"

And it works beautifully. Thank you again.

1 Like

That’s getting it every 5 minutes. Lose the / to do what the comment says.

If you set the state to "{{ now() }}" you’ll be able to see when it updated last.

Yep, forgot to change it back after getting the data part working - thanks

Doesn’t this generate alternating day/night forecasts because they’re twice daily?

Not exactly, it generates 7 12 hour forecasts, so if you execute the get_forecasts on a twice_daily in the AM you’ll get morning, evening, morning, evening, etc. If you execute get_forecasts in the afternoon, you’ll get evening, morning,…

Realistically the NWS updates the forecast at about 5 minutes after the hour all day long.

Well, that’s the basis for my question. For example, forecast[2] and forecast[3] are not necessarily day_two and day_3. If the first is 6AM to 6PM (is_daytime: true), the second will be 6PM to 6AM the following day (is_daytime: false). With the exception of forecast[0], any forecast showing is_daytime: false will straddle midnight.

Moreover, my experience is the NWS rarely updates shortly after the hour. Take a close look at this:

I agree, my labels are inaccurate, meaning they’re not day0, day1, day2, etc. but then they are just labels and I treat them as such in my use of them - I could have easily called them anything such as forecast_0, forecast_1, and so on. It’s just the way I named them. As far as the frequency goes, it’s close enough, not using them for anything other than creating a morning weather briefing played on a speaker while I’m having breakfast. I used to scrape the forecast from the NWS website but found it wasn’t as reliable as I wanted - that’s still the way I get the announced label such as “Today” , “Tonight”, “Memorial Day”, “Tomorrow Night”, etc.

1 Like

FYI, if you want the full forecast, always use the attribute. For example:

{{ (state_attr(‘sensor.daily_forecast’, ‘forecast’)[2].detailed_description) }}