Rudd-O / homeassistant-meteoswiss

:sun_behind_rain_cloud: :switzerland: Meteo Swiss Integration for Home Assistant (forked from websylv)
90 stars 11 forks source link

Latest update breaks daily weather output #54

Closed badewanne1234 closed 1 month ago

badewanne1234 commented 2 months ago

I'm extracting the weather forecast (daily and now als hourly) into sensors since accessing the forecast data has been changed earlier this year.

The daily output is now broken since the update of the meteoswiss integration yesterday. Interestingly, only the daily forecast is broken, but not the freshly introduced hourly forecast.

Here is my config in the configuration.yaml for daily and for hourly:

#Meteo Schweiz Wettervorhersage als Attribute
  - trigger:
    - platform: state
      entity_id: weather.wetter_mch
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.wetter_mch
        response_variable: daily
    sensor:
      - name: Wettervorhersage MCH Taeglich
        unique_id: weather_forecast_mch_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.wetter_mch'].forecast }}"

#Meteo Schweiz Stündliches Wetter als Attribute
  - trigger:
    - platform: state
      entity_id: weather.wetter_mch
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.wetter_mch
        response_variable: hourly
    sensor:
      - name: Wettervorhersage MCH Stuendlich
        unique_id: weather_forecast_mch_hourly
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ hourly['weather.wetter_mch'].forecast }}"

The output of these sensors is as follows:

Daily:

friendly_name: Wettervorhersage MCH Taeglich
forecast: [{'datetime': '2024-08-25', 'condition': <Condition.pouring: 'pouring'>, 'temperature': 26.0, 'templow': 15.0, 'precipitation': 13.3}, {'datetime': '2024-08-26', 'condition': <Condition.cloudy: 'cloudy'>, 'temperature': 20.0, 'templow': 15.0, 'precipitation': 0.0}, {'datetime': '2024-08-27', 'condition': <Condition.partly_cloudy: 'partlycloudy'>, 'temperature': 25.0, 'templow': 14.0, 'precipitation': 0.0}, {'datetime': '2024-08-28', 'condition': <Condition.sunny: 'sunny'>, 'temperature': 27.0, 'templow': 16.0, 'precipitation': 0.0}, {'datetime': '2024-08-29', 'condition': <Condition.partly_cloudy: 'partlycloudy'>, 'temperature': 29.0, 'templow': 18.0, 'precipitation': 0.0}, {'datetime': '2024-08-30', 'condition': <Condition.partly_cloudy: 'partlycloudy'>, 'temperature': 25.0, 'templow': 17.0, 'precipitation': 1.5}]

Hourly

friendly_name: Wettervorhersage MCH Stuendlich
forecast: 
- datetime: '2024-08-25T08:00:00Z'
  temperature: 16.5
  templow: 15.4
  precipitation: 10.4
- datetime: '2024-08-25T09:00:00Z'
  temperature: 15.3
  templow: 14.1
  precipitation: 2.7

As you can see, the outputting format is broken for the daily sensor, whilst for hourly its OK. I have the exact same sensors (daily + hourly) as well for the met.no weather integration, and it works as it should for both, daily + hourly in the correct format.

oliskippy commented 2 months ago

I confirm the issue with the daily forecast. Same here.

letienne commented 2 months ago

Same. My nspanel stopped displaying weather forecast since the latest update. Appdaemon logs:

fdate = dp.parse(entity.attributes.forecast[item.stype]['datetime'])
TypeError: string indices must be integers
Rudd-O commented 1 month ago

@letienne the attribute forecast was deprecated in HA, this has nothing to do with the integration.

https://community.home-assistant.io/t/removed-deprecated-forecast-attribute-from-weatherentity/727347

Rudd-O commented 1 month ago

@badewanne1234 I don't see any error log in this ticket, nor can I see any error when I run a service call myself — everything just works here:

image

I suspect you are trying to retrieve "daily" attribute from the returned object, which is not an attribute in the response of the get_forecasts action:

image

Sorry but you need to fix the automation / template sensor.

Rudd-O commented 1 month ago

TLDR: remove .daily or .hourly. That's not valid.

letienne commented 1 month ago

Thank you for looking into this. Sorry, but I think you missed the point. I rolled back to an earlier version of your integration and it works fine.

All of us have have already modified our setup to manage the "new" way to handle forecasts a few months back as discussed here.

In my case:

template:
  - trigger:
      - platform: time_pattern
        minutes: /15
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.blabla 
        response_variable: daily
    sensor:
      - name: Weather Forecast Daily
        unique_id: weather_forecast_daily
        state: "{{ states('weather.blabla') }}" 
        attributes:
          temperature: "{{ state_attr('weather.blabla', 'temperature') }}" # change to your weather entity
          temperature_unit: "{{ state_attr('weather.blabla', 'temperature_unit') }}" # change to your weather entity
          forecast: "{{ daily['weather.blabla'].forecast }}" # change to your weather entity 

The following used to work fine, returning a yaml list that other integrations such as this one can handle. forecast: "{{ daily['weather.blabla'].forecast }}".

But the latest commit returns another kind of array (see the brackets "[" instead of the "-" in badewanne's output) : forecast: [{'datetime': '2024-08-25', (...) }].

Whereas the same with an "hourly" type, returns a proper yaml list.

I'm currently happy with the rolled-back code, but if there is anything that can be done in a future release it would be nice :)

Regards

letienne commented 1 month ago

It seems that you do not cast daily and hourly forecasts in the same way. Maybe that could explain the yaml/json mixup?

Daily: fcdata_out = []

Hourly:

fcdata_out: list[Forecast] = []

        (...)

        forecast_data = cast(
            list[HourlyForecast], self._forecastData["regionHourlyForecast"]
        ) 
letienne commented 1 month ago

I'm no python expert but I was able to 'fix' it by casting the "ATTR_FORECAST_CONDITION" to a string (yes I know, very ugly).

I think that for some reason your CODE_TO_CONDITION_MAP.get() is returning more than it should (the whole dictionary item): 'condition': <Condition.pouring: 'pouring'> instead of simply 'pouring' and that is why HA is having issues converting this into a proper forecast.

The answer was found here: https://community.home-assistant.io/t/new-weather-forecast-template/622642/57 and it links to https://www.home-assistant.io/integrations/weather/#service-weatherget_forecasts

This page lists all "conditions" that HA accepts.

I hope that you now have sufficient information to fix this issue :) thanks !

raducotescu commented 1 month ago

Thanks for putting the time to properly report this, @letienne! It was still on my //TODO list.

That conversion map for conditions seems to have broken the integration.

badewanne1234 commented 1 month ago

@Rudd-O would you mind to open this issue again, thank you

Rudd-O commented 1 month ago

Ohhhh. I reproduced the issue successfully. Will issue a fix very soon.

Rudd-O commented 1 month ago

It should be working now — I tested it locally. Please message if it failed so I can reopen the issue.

badewanne1234 commented 1 month ago

I can confirm its working, excellent, thank you!