home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.15k stars 29.83k forks source link

Sensor template for weather.get_forecasts appears to return a String instead of an array #123751

Closed mcowell closed 1 month ago

mcowell commented 1 month ago

The problem

Hello,

I have the following template for use with Tileboard to fetch hourly weather from Environment Canada.

template:      
  - trigger:                                              
      - platform: time_pattern
        hours: "/4"                                               
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    action:                   
      - service: weather.get_forecasts
        data:           
          type: daily
        target:                                                                                                
          entity_id: weather.ottawa_kanata_orleans
        response_variable: daily
    sensor:
      - name: Weather Forecast Daily
        unique_id: weather_forecast_daily
        state: "{{ states('weather.ottawa_kanata_orleans') }}"
        attributes:                                           
          temperature_unit: "{{ state_attr('weather.ottawa_kanata_orleans', 'temperature_unit') }}"
          forecast: "{{ daily['weather.ottawa_kanata_orleans'] }}"

Using the following

{{state_attr('sensor.weather_forecast_daily','forecast')}} The above used to give me an array of objects back, but now I just get a string.

{'forecast': [{'datetime': datetime.datetime(2024, 8, 12, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 30, 'condition': 'lightning-rainy', 'temperature': 22.0, 'templow': 13.0}, {'datetime': datetime.datetime(2024, 8, 13, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 0, 'condition': 'sunny', 'temperature': 27.0, 'templow': 15.0}, {'datetime': datetime.datetime(2024, 8, 14, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 30, 'condition': 'rainy', 'temperature': 28.0, 'templow': 14.0}, {'datetime': datetime.datetime(2024, 8, 15, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 0, 'condition': 'sunny', 'temperature': 28.0, 'templow': 15.0}, {'datetime': datetime.datetime(2024, 8, 16, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 0, 'condition': 'partlycloudy', 'temperature': 28.0, 'templow': 18.0}, {'datetime': datetime.datetime(2024, 8, 17, 15, 0, tzinfo=tzutc()), 'precipitation_probability': 60, 'condition': 'rainy', 'temperature': 24.0, 'templow': 18.0}]}

Thus, when I try to get the [0]th element of the above I get undefined since it is not actually an array. This becomes an issue on Tileboard as I try to iterate though the first nth elements of the forecast.

Seemingly another user ran into the issue here also : https://community.home-assistant.io/t/need-help-with-recent-daily-weather-forecast-error/759166

Please let me know if I can provide any more information or do any testing for you.

Thanks!

What version of Home Assistant Core has the issue?

core-2024.8.0

What was the last working version of Home Assistant Core?

core-2024.7.4

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Environment Canada

Link to integration documentation on our website

https://www.home-assistant.io/integrations/environment_canada/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 month ago

Hey there @gwww, @michaeldavie, mind taking a look at this issue as it has been labeled with an integration (environment_canada) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `environment_canada` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign environment_canada` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


environment_canada documentation environment_canada source (message by IssueLinks)

gwww commented 1 month ago

What version were you running when this was last working?

I think this is an upstream issue for something in templates. I've asked for some help on Discord.

mcowell commented 1 month ago

Thanks @gwww ... I was not certain it is related to the weather integration either. It could certainly be that something changed with templates.

This broke when I moved to 2024.8.0 ... I believe I was running the last 2024.7.x version before that which I think was core-2024.7.4.

gwww commented 1 month ago

get_forecasts is working properly and returns a dict type. This can be shown by changing the last line of your template to:

forecast: "{{ daily['weather.ottawa_kanata_orleans'].forecast[0] }}"

That returns the dict entry forecast, which is an array and the [0] returns the first element of the array. So, the Environment Canada integration is working properly.

I'm not a template expert and can't offer a solution. My suggestion would be to post on the forums and see what people there have to say. Feel free to tag this problem and tag me. I'm curious how this worked in previous versions but does not work now.

gwww commented 1 month ago

@home-assistant close

gwww commented 1 month ago

@home-assistant reopen

gjohansson-ST commented 1 month ago

Fixed by #123960