I have a helper that I use to determine if I need to shut the shades on hot days.
I’ve read that get_forecast has been replaced by get_forecasts, but all the examples of using get_forecasts I can find are service calls…
{{
state_attr('weather.openweathermap', 'forecast') |
selectattr('datetime', '>=', utcnow().isoformat()) |
selectattr('datetime', '<=', (utcnow() + timedelta(days=1)).isoformat()) |
map(attribute='temperature') | list |max
}}
How can I write a helper or template to call get_forecasts using openweathermap?
Am I correct in assuming that this mechanism to scan the forecast is being deprecated?
My attempts to RTFM have not enlightened me.
Thanks in advance
Yes, the forecast
attribute has been deprecated and will be removed in 2024.3 . So you have more than 3 months left to think about your needs and figure out how to use weather.get_forecasts
and the response data.
At least for now, you can’t do it directly in a template helper, since they only support state-based sensors. It must be configured in YAML. Trigger-based template sensors now allow an action block that can be used to call the service and return the value.
Trigger-based handling of service response data
Forum Search Results: get_forecasts
My solution was to put the following into my configuration.yaml so I should always have a forecast available
template:
# My Weather Entities Daily
- trigger:
- platform: time_pattern
hours: /1
- platform: homeassistant
event: start
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.openweathermap
response_variable: hourly
sensor:
- name: Weather Forecast Hourly
unique_id: weather_forecast_hourly
icon: mdi:weather-cloudy
state: "{{ hourly['weather.openweathermap'].forecast[0].condition }}"
attributes:
forecast: "{{ hourly['weather.openweathermap'].forecast }}"
Once this was in place then I could write template helpers like
Is it going to snow this evening:
{{
state_attr('sensor.weather_forecast_hourly', 'forecast')[1].condition in ("snowy")
}}
or what’s the max temp for the day:
{{
state_attr('sensor.weather_forecast_hourly', 'forecast') |
selectattr('datetime', '>=', utcnow().isoformat()) |
selectattr('datetime', '<=', (utcnow() + timedelta(days=1)).isoformat()) |
map(attribute='temperature') | list |max
}}
3 Likes
ddppddpp
(Ddppddpp)
August 5, 2024, 1:07pm
4
thanks for your post, it helped me re-create my openweathermap based automation.
Technically speaking, (utcnow() + timedelta(days=1)).isoformat() limits the list to the next 24h, not to the current date.
This is what works for me:
selectattr('datetime','<=',(utcnow()+ timedelta(days=1)).replace(hour=0,minute=0, second=0))
also, openweathermap returns forecasts starting with date starting with now(), so I don’t think you need the first selectattr check.