mawinkler / astroweather

Asynchronous Astro Weather Forecast for Home Assistant
GNU General Public License v3.0
78 stars 9 forks source link

Need to return ISO format datetime from get_forecasts service. #60

Closed jkfranks9 closed 2 months ago

jkfranks9 commented 3 months ago

I have a script that parses the result from the weather integration get_forecasts (hourly) service into a list of items for a given set of hours. This works fine when the weather service is OpenWeatherMap, but fails for AstroWeather, because the parsed result is a string instead of a list. For example OpenweatherMap shows the following (I'm only including the first item for brevity). This is the response variable returned from get_forecasts, from the script trace:

  weather.openweathermap:
    forecast:
      - datetime: '2024-08-29T17:00:00+00:00'
        condition: sunny
        temperature: 93
        pressure: 30.09
        cloud_coverage: 3
        wind_speed: 4.14
        wind_bearing: 228
        uv_index: 8.34
        precipitation_probability: 0
        precipitation: 0
        apparent_temperature: 106
        dew_point: 76
        wind_gust_speed: 4.43
        humidity: 58

But AstroWeather shows:

  {'weather.astroweather_<name>': {'forecast': [{'datetime':
  datetime.datetime(2024, 8, 29, 17, 0, tzinfo=datetime.timezone.utc),
  'calm_percentage': 93, 'cloud_area_fraction_high': 0,
  'cloud_area_fraction_low': 0, 'cloud_area_fraction_medium': 13,
  'cloud_area_fraction': 14, 'cloudcover_percentage': 14,
  'cloudless_percentage': 86, 'condition': 76, 'fog_area_fraction': 0,
  'lifted_index': -4, 'precipitation_amount': 0.0, 'precipitation_probability':
  None, 'precipitation': None, 'seeing_percentage': 75, 'seeing': 0.62,
  'temperature': 95, 'transparency_percentage': 55, 'transparency': 0.45,
  'wind_bearing': 'SW', 'wind_speed': 1.1, 'humidity': 39}

The first part of the returned JSON result for OpenWeatherMap shows this (I modified my script to log the response variable):

{'forecast': [{'datetime': '2024-08-29T17:00:00+00:00', 'condition': 'sunny', ...

But Astroweather shows:

{'forecast': [{'datetime': datetime.datetime(2024, 8, 29, 17, 0, tzinfo=datetime.timezone.utc), 'calm_percentage': 93, ...

In checking the OpenWeatherMap code, I see that they use isoformat() to return the datetime as a string, but AstroWeather returns a datetime object.

When I add the isoformat() call to the _forecast method in weather.py then all is well.

        for forecast in self.fcst_coordinator.data:
            forecasts.append(
                {
                    ATTR_FORECAST_TIME: forecast.forecast_time.isoformat(),

Note that I tried several different methods to deal with the AstroWeather result in my script but I could never manage to parse the result as a list. So I think using isoformat() here is the correct solution.

mawinkler commented 2 months ago

You're correct; I'm returning a datetime, but it should still work within your script.

This is what I quickly tested:

sequence:
  - action: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.astroweather_backyard
    response_variable: conditions
  - action: input_text.set_value
    metadata: {}
    data:
      value: "{{ conditions['weather.astroweather_backyard'].forecast[0].datetime }}"
    target:
      entity_id: input_text.temp

The input_text.temp is set correctly to 2024-09-03 13:00:00+00:00.

Am I missing something?

Sorry for late reply, I was on vacation.

jkfranks9 commented 2 months ago

Yes, picking up the datetime as an individual field does work (I assume HA just translates it). But for some reason if it's a datetime object instead of a string, the response data are not in a list form, so I can't walk through the data using a for_each in my script.

I used your test, but just logged the results, using 3 weather services (I also have Pirate Weather). If I log both the first returned result and the datetime field you can see the difference. The other 2 weather services return a string.

2024-09-04 07:52:53.075 WARNING (MainThread) [homeassistant.components.system_log.external] {'datetime': datetime.datetime(2024, 9, 4, 11, 0, tzinfo=datetime.timezone.utc), 'calm_percentage': 80, 'cloud_area_fraction_high': 80, 'cloud_area_fraction_low': 0, 'cloud_area_fraction_medium': 0, 'cloud_area_fraction': 80, 'cloudcover_percentage': 80, 'cloudless_percentage': 20, 'condition': 44, 'fog_area_fraction': 0, 'lifted_index': 15, 'precipitation_amount': 0.0, 'precipitation_probability': None, 'precipitation': None, 'seeing_percentage': 45, 'seeing': 1.38, 'temperature': 59, 'transparency_percentage': 44, 'transparency': 0.55, 'wind_bearing': 'N', 'wind_speed': 3.3, 'humidity': 79}
2024-09-04 07:52:53.077 WARNING (MainThread) [homeassistant.components.system_log.external] 2024-09-04 11:00:00+00:00
2024-09-04 07:52:53.083 WARNING (MainThread) [homeassistant.components.system_log.external] {'datetime': '2024-09-04T11:00:00+00:00', 'condition': 'cloudy', 'temperature': 59, 'pressure': 30.33, 'cloud_coverage': 90, 'wind_speed': 7.16, 'wind_bearing': 14, 'uv_index': 0.0, 'precipitation_probability': 0, 'precipitation': 0.0, 'apparent_temperature': 58, 'dew_point': 55, 'wind_gust_speed': 22.12, 'humidity': 86}
2024-09-04 07:52:53.085 WARNING (MainThread) [homeassistant.components.system_log.external] 2024-09-04T11:00:00+00:00
2024-09-04 07:52:53.119 WARNING (MainThread) [homeassistant.components.system_log.external] {'datetime': '2024-09-04T11:00:00+00:00', 'condition': 'partlycloudy', 'wind_bearing': 20.0, 'precipitation_probability': 0.0, 'cloud_coverage': 51.0, 'uv_index': 0.0, 'temperature': 57, 'apparent_temperature': 54, 'dew_point': 52, 'pressure': 30.28, 'wind_gust_speed': 11.63, 'wind_speed': 6.26, 'precipitation': 0.0, 'humidity': 85}
2024-09-04 07:52:53.122 WARNING (MainThread) [homeassistant.components.system_log.external] 2024-09-04T11:00:00+00:00

I did originally try many ways to walk through the response from AstroWeather but found no way to do it since the data are not in list form.

Also, the other weather services both use isoformat().

If you don't feel AstroWeather should return a string, I can just modify my copy to make it work for me. Perhaps no one else is trying to do what I'm doing. Thanks for listening.

mawinkler commented 2 months ago

I made the change in the weather entity and pushed it to main. No new release yet. Can you possibly test if it works for you now?

jkfranks9 commented 2 months ago

Tested, and works perfectly. Thanks!

mawinkler commented 2 months ago

Solved with release v0.60.0.