Closed jkfranks9 closed 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.
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.
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?
Tested, and works perfectly. Thanks!
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 fromget_forecasts
, from the script trace:But AstroWeather shows:
The first part of the returned JSON result for OpenWeatherMap shows this (I modified my script to log the response variable):
But Astroweather shows:
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 inweather.py
then all is well.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.