Need Help with Recent Daily Weather Forecast Error

I use the following sensors to retrieve weather forecast data from Environment Canada. These are really beyond my skill level and only exist due to the kind help of community members. This was created back when the forecast attribute was changed to the get_forecast(s) service (now action).

Recently, (after updating to 2024.8.0 and continuing in 2024.8.1) I noticed my daily forecast was not rendering on my dashboard and I had an error relating to it in the logs. The error clearly pointed me to the offending code. I copied the code into Developer Tools and successfully reproduced the error. I can post the entire log error if needed.

I then decided to see what, exactly, was being returned by the daily forecast sensor and it doesn’t look right.

The hourly forecast sensor returns expected results.

I tried the Daily get_forecasts action in Developer Tools and it returns proper results.

This is where my skill level peaks out. I have no idea why the daily forecast is returning a string while the hourly returns a list. I have no idea why get_forecasts works in Developer Tools, but not in the template sensor.

Any help is greatly appreciated.

MrGrey.

Good day!

I have raised a CORE bug for this issue - Sensor template for weather.get_forecasts appears to return a String instead of an array · Issue #123738 · home-assistant/core · GitHub

Hopefully somebody on the Home Assistant team can figure it out. I will update this thread if I learn more.

Thanks!

1 Like

Good morning!

So @gwww has shown that the issue is not with the Environment Canada integration, which is good.

So it would seem that something with templates has changed.

Not sure if I can raise an issue against templates?

Do you have data/images from when this was working?

I would have to roll back to before 2024.8.0.

However, as we can see above, hourly works, but daily does not. Here are two examples:

Hourly

temperature_unit: °C
forecast:
  - datetime: "2024-08-14T16:00:00+00:00"
    condition: sunny
    precipitation_probability: 0
    temperature: 25
  - datetime: "2024-08-14T17:00:00+00:00"
    condition: sunny
    precipitation_probability: 0
    temperature: 27
  - datetime: "2024-08-14T18:00:00+00:00"
    condition: sunny
    precipitation_probability: 0
    temperature: 28
  - datetime: "2024-08-14T19:00:00+00:00"
    condition: rainy
    precipitation_probability: 40
    temperature: 28

Daily

temperature_unit: °C
forecast: >-
  [{'datetime': datetime.datetime(2024, 8, 14, 15, 0, tzinfo=tzutc()),
  'precipitation_probability': 40, 'condition': 'rainy', 'temperature': 29.0,
  'templow': 16.0}, {'datetime': datetime.datetime(2024, 8, 15, 15, 0,
  tzinfo=tzutc()), 'precipitation_probability': 30, 'condition': 'rainy',
  'temperature': 29.0, 'templow': 15.0}, {'datetime': datetime.datetime(2024, 8,
  16, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 0, 'condition':
  'cloudy', 'temperature': 28.0, 'templow': 18.0}, {'datetime':
  datetime.datetime(2024, 8, 17, 15, 0, tzinfo=tzutc()),
  'precipitation_probability': 0, 'condition': 'rainy', 'temperature': 23.0,
  'templow': 18.0}, {'datetime': datetime.datetime(2024, 8, 18, 15, 0,
  tzinfo=tzutc()), 'precipitation_probability': 0, 'condition': 'rainy',
  'temperature': 24.0, 'templow': 17.0}, {'datetime': datetime.datetime(2024, 8,
  19, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 60, 'condition':
  'rainy', 'temperature': 23.0, 'templow': 16.0}]

Thanks. I didn’t look at the images too closely. I will play some more and see if I can help find the answer.

I sort of wonder if that datetime.datetime() has something to do with it … it looks … odd in there.

The [ ] notation seems correct for a list and each element of the list is contained in a { } but the whole thing is seen as a string instead of a list.

I can of course solve this by creating 5 daily weather entities but it’s so clunky :slight_smile:

I’m really curious what changed to cause this to break. Feels like an issue with templates but I don’t know them well enough to prove it yet.

I have a suspicion as to what is going on, and yes it’s the Environment Canada integration.

You gave me the hint when you mentioned the datetime looking odd. A fix was added to 2024.8 around the timestamps in the daily weather. Going to look at the change… (but will be a few hours before I can get to looking).

Interesting. I am looking at the change in weather.py on commit 6645932.

I do see that ATTR_FORECAST_TIME changes from

dt_util.now() + datetime.timedelta(days=days)).isoformat()

to

fcst[0]["timestamp"]

I cannot see right now where fcst is coming from. The code is cleaner but did we perhaps miss the .isoformat()? I’m thinking if we do not get a nice string back then something in template things the whole thing is a string and that breaks it?

An isoformat does appear to be missing. I added it and things work. However, tests break and I looking into why.

2 Likes

Awesome! Thanks so much for your help!

Thank you for all of the time and effort put into this; both here and on GitHub. What an obscure bug to track down. You folks (@dr_torch, @gwww and everyone over at GitHub) are amazing!

MrGrey.

1 Like

I’m having a similar issue with this… but my error comes from template.yaml coding:
I receive an error in the line of - service: weather.get_forecasts with this message:

"Strings does not match the pattern of “LEGACY_SYNTAX”

  - trigger:
      - trigger: time_pattern
        hours: "/1"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweather
        response_variable: hourly

Does anybody know how to fix this?

I found the fix… “service” is now called “action”

  - trigger:
      - trigger: time_pattern
        hours: "/1"
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweather
        response_variable: hourly