Filter a response from openweathermap.get_minute_forecast based on timestamps

Hello, I’m trying to automate my blinds in a script. I want them to automatically raise based on a rain forecast (precipitation amount), and lower again when no precipitation is predicted.

For this, I’m using openweathermap.get_minute_forecast, which returns suitable data. The response contains 60 entries, and I’d like to filter these by a specific time range. However, this is where I’m stuck, since it seems I can’t directly compare the datetime values.

Maybe someone can help me with this.

This is a code-snippet I am using

sequence:
  - alias: Minutenvorhersage abrufen
    target:
      entity_id: weather.openweathermap
    response_variable: minute_forecast
    action: openweathermap.get_minute_forecast
    data: {}
  - alias: Forecast extrahieren
    variables:
      forecast: "{{ minute_forecast['weather.openweathermap'].forecast }}"

minute_forecasts returns something like this:

minute_forecast:
  weather.openweathermap:
    forecast:
      - datetime: '2025-06-24T21:29:00+00:00'
        precipitation: 0
      - datetime: '2025-06-24T21:30:00+00:00'
        precipitation: 0
      - datetime: '2025-06-24T21:31:00+00:00'
        precipitation: 0

I extracted the forecast data (a snippet of variable forecast):

forecast: >-
  [{'datetime': datetime.datetime(2025, 6, 24, 21, 29,
  tzinfo=datetime.timezone.utc), 'precipitation': 0}, {'datetime':
  datetime.datetime(2025, 6, 24, 21, 30, tzinfo=datetime.timezone.utc),
  'precipitation': 0},

and tried to select just some data that is in a specific time-range. This is were I got stuck.

- alias: Regenmenge in 5 Minuten berechnen
    variables:
      regen_5min_summe: >
        {% set jetzt = now().utcnow() %}
         {% set werte = forecast
          | selectattr('datetime', 'defined')
          | selectattr('datetime', '>=', jetzt)
          | selectattr('datetime', '<=', (jetzt + timedelta(minutes=5))
          | map(attribute='precipitation')
          | list %}
        {{ werte | sum }}

It seems that I can’t compare the datetime-objects in that way, but I was not able to find out how to compare them.
Maybe someone could help me here.
Thanks so far!

I found that Website with some explanations for the action get_minute_forecast

OpenWeather

It looks to me, that the data in variable „forecast“ is interpreted as String. I tried to iterate through the elements with [0],[1],… and the results were in List elements. Result was the first, second,… character.

Would it be possible to just get the first 5 precipitation values without comparing timestamps?

You can, and it will work if the attribute is a datetime object.

         {% set t = now() %}
         {% set werte = forecast
          | selectattr('datetime', 'defined')
          | selectattr('datetime', '>=', jetzt)
          | selectattr('datetime', '<=', jetzt + timedelta(minutes=5))
          | map(attribute='precipitation')
          | list %}
         {{ werte | sum }}

Your issue was an extra ( and you don’t need to use utcnow.

if you want the first 5, you can also just do this

{{ forecast[:5] | map(attribute='precipitation') | sum }}

Hi. Thanks for your response. Currently it looks to me that forecast
is completely handled as string.
Why, I don’t know. I think it happens when I set

forecast: "{{ minute_forecast['weather.openweathermap'].forecast }}"

since

minute_forecast:
  weather.openweathermap:
    forecast:
      - datetime: '2025-06-24T21:29:00+00:00'
        precipitation: 0
      - datetime: '2025-06-24T21:30:00+00:00'
        precipitation: 0
      - datetime: '2025-06-24T21:31:00+00:00'
        precipitation: 0

looks good to me.

If it’s a string, then you just compare UTC strings.

         {% set t = utcnow().replace(microsecond=0).isoformat() %}
         {% set werte = forecast
          | selectattr('datetime', 'defined')
          | selectattr('datetime', '>=', jetzt)
          | selectattr('datetime', '<=', jetzt + timedelta(minutes=5))
          | map(attribute='precipitation')
          | list %}
         {{ werte | sum }}

Misunderstanding.

Not the datetime attribute is a string. The complete variable “forecast” seems to be a string.
Tried to just access element 0/1 with

variables:
  firstentry: “{{ forecast[0] }}”
  secondentry: “{{ forecast[1] }}”

expecting to get the first and second list-element but instead I get

firstentry: '['
secondentry: '{'

and these are the first and second characters of forecast, but maybe I am messing something up in me head, I am still new to this :wink:

Yes, that could happen when it can’t serialize data inside your object (forecast). Which is likely happening because of the datetime objects.

Understand, any possibilities to change that?
Since I just get the response from the openweather action

Just use the object in your template instead of setting it in a variable.

         {% set t = now() %}
         {% set werte = minute_forecast['weather.openweathermap'].forecast
          | selectattr('datetime', 'defined')
          | selectattr('datetime', '>=', jetzt)
          | selectattr('datetime', '<=', jetzt + timedelta(minutes=5))
          | map(attribute='precipitation')
          | list %}
         {{ werte | sum }}

Thanks, that was the missing link.
Works like a charm. Can work on from here!

My script is running well, just one further question regarding script variables.

Is it possible to define “t” only once in the script instead of setting it in both variables?

Tried it with as separate variable but then I have the type-mismatch problem (str ↔ datetime).

sequence:
  - alias: Minutenvorhersage abrufen
    target:
      entity_id: weather.openweathermap
    response_variable: minute_forecast
    action: openweathermap.get_minute_forecast
    data: {}
  - alias: Regenmenge in 5 Minuten berechnen
    variables:
      regen_5min_summe: >
        {% set t = utcnow() %} {% set werte =
        minute_forecast['weather.openweathermap'].forecast
         | selectattr('datetime', 'defined')
         | selectattr('datetime', '>=', t) 
         | selectattr('datetime', '<=', (t + timedelta(minutes=forecast_time|int)))
         | map(attribute='precipitation')
         | list %}
        {{ werte | sum }}
  - alias: Regenmax in den nächsten 10 Minuten berechnen
    variables:
      regen_10min_max: >
        {% set t = utcnow() %} {% set werte =
        minute_forecast['weather.openweathermap'].forecast
          | selectattr('datetime', 'defined')
          | selectattr('datetime', '>=', t) 
          | selectattr('datetime', '<=', (t + timedelta(minutes=dry_time|int)))
          | map(attribute='precipitation')
          | list %}
         {{ werte | sum }}

You’d have to put it in a previous variable as an isoformatted string, then convert it to a datetime at use.

1 Like