Need Help with Weather Forecast Automation

Hello everyone

I would like to retrieve the forecasts data from my weather entity and save them in a template sensor for further processing.

My sensor:

template:       
  - trigger:
      - platform: event
        event_type: 
        - srf_weather_forecast
        
    sensor:
      - name: "SRF Weather Forecast"
        unique_id: srf_weather_forecast
        state: "{{ trigger.event.data.srf_weather_forecast.forecast | count() }}"
        attributes:
          srf_weather_forecast: "{{ trigger.event.data.srf_weather_forecast.forecast }}"

My Automation:

alias: Test Wettervorhersage
description: ""
triggers: []
conditions: []
actions:
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: daily
    response_variable: weather_forecast
    target:
      entity_id: weather.srf_weather_46_75477_6258
    enabled: true
  - event: srf_weather_forecast
    event_data:
      srf_weather_forecast: "{{weather_forecast}}"
mode: single

In the actions, I see that I can successfully retrieve the weather forecast data from this entity:

But when I check the entitiy, it is always empty:

Can anyone tell me what I am doing wrong?

Many thanks

Greetings
Buhli

I have never seen anyone trying to get the weather forecast with an untriggered automation then passing the data to a sensor over the events bus. Is this something that AI has dreamt up for you?

Usual method is outlined here, with a trigger-based template sensor doing all the work:

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.srf_weather_46_75477_6258
        response_variable: daily
    sensor:
      - name: SRF Weather Forecast
        unique_id: ceaee602-6483-4add-90b8-81ca8c7edaad
        state: "{{ daily['weather.srf_weather_46_75477_6258']['forecast']|count }}"
        attributes:
          forecast: "{{ daily['weather.srf_weather_46_75477_6258']['forecast'] }}"
1 Like

Hi Troon

Thank you very much for your answer.

Yes, this came about with the support of AI :frowning:

Is it possible to restrict the forecast to today only? My goal is to determine the min and max temperature.

Thanks and best regards
Buhli

Not all at surprised it didn’t work, but it’s a fascinating idea. I wonder what pieces of training data came together to result in that output? Scares me witless that we’ll never know…

Yes, you can do whatever you want providing you have the data. Is your forecast daily or hourly? The screenshot above shows a daily reading but without visible min/max. If there’s an hourly response available, that would give more options for working with.

Sat up the sensor as I’ve suggested, switching the daily to hourly if appropriate. Then go to Developer Tools / Template and type or paste in:

{{ state_attr('sensor.srf_weather_forecast', 'forecast') }}

and paste the full response here, formatted as code with the </> button.

Then we can see what information you’re working with.

Hi Troon

thank you very much for your answer

In the daily query, I have a “templow” and “temperature" attribute. I think the “temperature” is the max in this case.

weather.srf_weather_46_75477_6258:
  forecast:
    - condition: rainy
      datetime: "2025-01-23T00:00:00+01:00"
      precipitation_probability: 93
      cloud_coverage: null
      wind_bearing: 240
      uv_index: 1
      is_daytime: null
      symbol_code: 4
      symbol24_code: 47
      sunrise: "2025-01-23T08:04:00+01:00"
      sunset: "2025-01-23T17:17:00+01:00"
      sunshine_hours: 0
      **temperature: 7**
**      templow: 0**
      wind_gust_speed: 24
      wind_speed: 4
      precipitation: 3

Yes, an hourly query is also possible

weather.srf_weather_46_75477_6258:
  forecast:
    - condition: partlycloudy
      datetime: "2025-01-22T06:00:00+01:00"
      precipitation_probability: 5
      cloud_coverage: null
      wind_bearing: 220
      uv_index: 1
      is_daytime: null
      symbol_code: -3
      symbol24_code: 160
      **temphigh: -0.5**
      fresh_snow_cm: null
      sunshine_minutes: 0
      irradiance: 0
      color:
        temperature: -2
        background_color: "#0c429e"
        text_color: "#ffffff"
      **temperature: -2**
      apparent_temperature: -4
      **templow: -3.1**
      dew_point: -2
      pressure: 10.13
      wind_gust_speed: 4
      wind_speed: 4
      precipitation: 0
      humidity: 100

Here is an hour from the template sensor. For the sake of readability, I have only inserted one hour.

[{'condition': 'partlycloudy', 'datetime': '2025-01-22T06:00:00+01:00', 'precipitation_probability': 5, 'cloud_coverage': None, 'wind_bearing': 220, 'uv_index': 1, 'is_daytime': None, 'symbol_code': -3, 'symbol24_code': 160, 'temphigh': -0.5, 'fresh_snow_cm': None, 'sunshine_minutes': 0, 'irradiance': 0, 'color': {'temperature': -2, 'background_color': '#0c429e', 'text_color': '#ffffff'}, 'temperature': -2.0, 'apparent_temperature': -4.0, 'templow': -3.1, 'dew_point': -2.0, 'pressure': 10.13, 'wind_gust_speed': 4.0, 'wind_speed': 4.0, 'precipitation': 0.0, 'humidity': 100}]

Could you perhaps tell me how I can adjust my sensor so that I only have the current day (from now - midnight) to be able to determine the min and max?

Or do I have to create a new sensor (srf_weather_forecast_today) with the attributs min and max?

Thank you very much

Greetings
Buhli

That’d be the simplest: get all the forecast into a “master” sensor, then build template sensors from that.

For example, to select the highest temphigh from the hourly forecast list filtering to today’s date, you’d use a state template of:

{% set f = state_attr('sensor.srf_weather_forecast','forecast') %}
{{ f|selectattr('datetime','match',now().date()|string)|map(attribute='temphigh')|max }}

You can test these out in the template editor. I had a go, although I had to make up some data which is why I’d asked for the complete forecast: if you format it correctly, it gets a scroll bar so don’t worry about pasting long stuff. You can just use the template above once the forecast sensor is working.

Here’s what I used, faking a high value in the second list element and a higher value but tomorrow’s date in the third:

{% set f = [{'condition': 'partlycloudy', 'datetime': '2025-01-22T06:00:00+01:00', 'precipitation_probability': 5, 'cloud_coverage': None, 'wind_bearing': 220, 'uv_index': 1, 'is_daytime': None, 'symbol_code': -3, 'symbol24_code': 160, 'temphigh': -0.5, 'fresh_snow_cm': None, 'sunshine_minutes': 0, 'irradiance': 0, 'color': {'temperature': -2, 'background_color': '#0c429e', 'text_color': '#ffffff'}, 'temperature': -2.0, 'apparent_temperature': -4.0, 'templow': -3.1, 'dew_point': -2.0, 'pressure': 10.13, 'wind_gust_speed': 4.0, 'wind_speed': 4.0, 'precipitation': 0.0, 'humidity': 100},
            {'condition': 'partlycloudy', 'datetime': '2025-01-22T07:00:00+01:00', 'precipitation_probability': 5, 'cloud_coverage': None, 'wind_bearing': 220, 'uv_index': 1, 'is_daytime': None, 'symbol_code': -3, 'symbol24_code': 160, 'temphigh': 42, 'fresh_snow_cm': None, 'sunshine_minutes': 0, 'irradiance': 0, 'color': {'temperature': -2, 'background_color': '#0c429e', 'text_color': '#ffffff'}, 'temperature': -2.0, 'apparent_temperature': -4.0, 'templow': -3.1, 'dew_point': -2.0, 'pressure': 10.13, 'wind_gust_speed': 4.0, 'wind_speed': 4.0, 'precipitation': 0.0, 'humidity': 100},
            {'condition': 'partlycloudy', 'datetime': '2025-01-23T01:00:00+01:00', 'precipitation_probability': 5, 'cloud_coverage': None, 'wind_bearing': 220, 'uv_index': 1, 'is_daytime': None, 'symbol_code': -3, 'symbol24_code': 160, 'temphigh': 69, 'fresh_snow_cm': None, 'sunshine_minutes': 0, 'irradiance': 0, 'color': {'temperature': -2, 'background_color': '#0c429e', 'text_color': '#ffffff'}, 'temperature': -2.0, 'apparent_temperature': -4.0, 'templow': -3.1, 'dew_point': -2.0, 'pressure': 10.13, 'wind_gust_speed': 4.0, 'wind_speed': 4.0, 'precipitation': 0.0, 'humidity': 100}] %}
{{ f|selectattr('datetime','match',now().date()|string)|map(attribute='temphigh')|max }}

Hi Troon

Thank you very much, you are a genius :slight_smile:

I have tested the following comand in the template editor:

{% set f = state_attr('sensor.srf_weather_forecast_v2','forecast') %}
{{ f|selectattr('datetime','match',now().date()|string)|map(attribute='templow')|min }}
{{ f|selectattr('datetime','match',now().date()|string)|map(attribute='temphigh')|max }}

and it seems to work:

-3.6
5.0

I have tested another command:

{% set f = state_attr('sensor.srf_weather_forecast','forecast') %}
{{ f|selectattr('datetime','match',now().date()|string)|map(attribute='datetime') | list }}

The entries marked in yellow are either duplicates or are in the past from today’s date

Same result if I look at the unfiltered list or in HA in the actions.


{% set f = state_attr('sensor.srf_weather_forecast_v2','forecast') %}
{{f | map(attribute='datetime') | list}}

[
  "2025-01-22T12:00:00+01:00",
  "2025-01-22T13:00:00+01:00",
  "2025-01-22T14:00:00+01:00",
  "2025-01-22T15:00:00+01:00",
  "2025-01-22T16:00:00+01:00",
  "2025-01-22T17:00:00+01:00",
  "2025-01-22T18:00:00+01:00",
  "2025-01-22T19:00:00+01:00",
  "2025-01-22T20:00:00+01:00",
  "2025-01-22T21:00:00+01:00",
  "2025-01-22T22:00:00+01:00",
  "2025-01-22T23:00:00+01:00",
  "2025-01-23T00:00:00+01:00",
  "2025-01-23T01:00:00+01:00",
  "2025-01-23T02:00:00+01:00",
  "2025-01-23T03:00:00+01:00",
  "2025-01-23T04:00:00+01:00",
  "2025-01-23T05:00:00+01:00",
  "2025-01-23T06:00:00+01:00",
  "2025-01-23T07:00:00+01:00",
  "2025-01-23T08:00:00+01:00",
  "2025-01-23T09:00:00+01:00",
  "2025-01-23T10:00:00+01:00",
  "2025-01-23T11:00:00+01:00",
  "2025-01-23T12:00:00+01:00",
  "2025-01-23T13:00:00+01:00",
  "2025-01-23T14:00:00+01:00",
  "2025-01-23T15:00:00+01:00",
  "2025-01-23T16:00:00+01:00",
  "2025-01-23T17:00:00+01:00",
  "2025-01-23T18:00:00+01:00",
  "2025-01-23T19:00:00+01:00",
  "2025-01-23T20:00:00+01:00",
  "2025-01-23T21:00:00+01:00",
  "2025-01-23T22:00:00+01:00",
  "2025-01-23T23:00:00+01:00",
  "2025-01-24T00:00:00+01:00",
  "2025-01-24T01:00:00+01:00",
  "2025-01-24T02:00:00+01:00",
  "2025-01-24T03:00:00+01:00",
  "2025-01-24T04:00:00+01:00",
  "2025-01-24T05:00:00+01:00",
  "2025-01-24T06:00:00+01:00",
  "2025-01-24T07:00:00+01:00",
  "2025-01-24T08:00:00+01:00",
  "2025-01-24T09:00:00+01:00",
  "2025-01-24T10:00:00+01:00",
  "2025-01-24T11:00:00+01:00",
  "2025-01-24T12:00:00+01:00",
  "2025-01-24T13:00:00+01:00",
  "2025-01-24T14:00:00+01:00",
  "2025-01-24T15:00:00+01:00",
  "2025-01-24T16:00:00+01:00",
  "2025-01-24T17:00:00+01:00",
  "2025-01-24T18:00:00+01:00",
  "2025-01-24T19:00:00+01:00",
  "2025-01-24T20:00:00+01:00",
  "2025-01-24T21:00:00+01:00",
  "2025-01-24T22:00:00+01:00",
  "2025-01-24T23:00:00+01:00",
  "2025-01-25T00:00:00+01:00",
  "2025-01-25T01:00:00+01:00",
  "2025-01-25T02:00:00+01:00",
  "2025-01-25T03:00:00+01:00",
  "2025-01-25T04:00:00+01:00",
  "2025-01-25T05:00:00+01:00",
  "2025-01-25T06:00:00+01:00",
  "2025-01-25T07:00:00+01:00",
  "2025-01-25T08:00:00+01:00",
  "2025-01-25T09:00:00+01:00",
  "2025-01-25T10:00:00+01:00",
  "2025-01-22T01:00:00+01:00",
  "2025-01-22T04:00:00+01:00",
  "2025-01-22T07:00:00+01:00",
  "2025-01-22T10:00:00+01:00",
  "2025-01-22T13:00:00+01:00",
  "2025-01-22T16:00:00+01:00",
  "2025-01-22T19:00:00+01:00",
  "2025-01-22T22:00:00+01:00",
  "2025-01-23T01:00:00+01:00",
  "2025-01-23T04:00:00+01:00",
  "2025-01-23T07:00:00+01:00",
  "2025-01-23T10:00:00+01:00",
  "2025-01-23T13:00:00+01:00",
  "2025-01-23T16:00:00+01:00",
  "2025-01-23T19:00:00+01:00",
  "2025-01-23T22:00:00+01:00",
  "2025-01-24T01:00:00+01:00",
  "2025-01-24T04:00:00+01:00",
  "2025-01-24T07:00:00+01:00",
  "2025-01-24T10:00:00+01:00",
  "2025-01-24T13:00:00+01:00",
  "2025-01-24T16:00:00+01:00",
  "2025-01-24T19:00:00+01:00",
  "2025-01-24T22:00:00+01:00",
  "2025-01-25T01:00:00+01:00",
  "2025-01-25T04:00:00+01:00",
  "2025-01-25T07:00:00+01:00",
  "2025-01-25T10:00:00+01:00",
  "2025-01-25T13:00:00+01:00",
  "2025-01-25T16:00:00+01:00",
  "2025-01-25T19:00:00+01:00",
  "2025-01-25T22:00:00+01:00",
  "2025-01-26T01:00:00+01:00",
  "2025-01-26T04:00:00+01:00",
  "2025-01-26T07:00:00+01:00",
  "2025-01-26T10:00:00+01:00",
  "2025-01-26T13:00:00+01:00",
  "2025-01-26T16:00:00+01:00",
  "2025-01-26T19:00:00+01:00",
  "2025-01-26T22:00:00+01:00",
  "2025-01-27T01:00:00+01:00",
  "2025-01-27T04:00:00+01:00",
  "2025-01-27T07:00:00+01:00",
  "2025-01-27T10:00:00+01:00",
  "2025-01-27T13:00:00+01:00",
  "2025-01-27T16:00:00+01:00",
  "2025-01-27T19:00:00+01:00",
  "2025-01-27T22:00:00+01:00",
  "2025-01-28T01:00:00+01:00",
  "2025-01-28T04:00:00+01:00",
  "2025-01-28T07:00:00+01:00",
  "2025-01-28T10:00:00+01:00",
  "2025-01-28T13:00:00+01:00",
  "2025-01-28T16:00:00+01:00",
  "2025-01-28T19:00:00+01:00",
  "2025-01-28T22:00:00+01:00",
  "2025-01-29T01:00:00+01:00",
  "2025-01-29T04:00:00+01:00",
  "2025-01-29T07:00:00+01:00",
  "2025-01-29T10:00:00+01:00",
  "2025-01-29T13:00:00+01:00",
  "2025-01-29T16:00:00+01:00",
  "2025-01-29T19:00:00+01:00",
  "2025-01-29T22:00:00+01:00",
  "2025-01-30T01:00:00+01:00",
  "2025-01-30T04:00:00+01:00",
  "2025-01-30T07:00:00+01:00",
  "2025-01-30T10:00:00+01:00"
]

I think this problem is due to the provider from whom I get this weather data? Or can I do something else to avoid this entries?

Thank you very much

Greetings
Marc

I’d still keep all the data in the master sensor and filter as needed.

Here are a couple of filters you can use. Today’s entries only:

|selectattr('datetime','match',now().date()|string)

Future entries only:

|selectattr('datetime','>',now().isoformat())

You can chain those together to get the rest of today’s items.

I can’t see why you’re getting duplicate date entries. Are they different types of data? If they are, we should be able to select what you want. Can you paste in all of {{ f }}, unfiltered, from the template editor?

Hi Troon

I have tried, unfortunately without success. The content of {{ f }} is too big. I would have to split it into about 4 posts. Is there another way? Can I upload a text file somewhere?

Thanks and best regards
Buhli

Wow. Pastebin?

Hi Troon

Here is the link: Pastebin.com - Locked Paste
Password: 6inxywcRSJ

Thanks and best regards
Buhli

Can you double-check what you pasted please? I would have expected it to start with a [; and at line 3395, there is what appears to be a second list added to the first:

image

Hi Troon

Hmm… strange. Here again a new link

Link: Pastebin.com - Locked Paste
Password: MndKZ4khAF

Thanks and best regards
Buhli

That’s better. Very odd: this is a combination of an hourly forecast from now out to 26-Jan, followed (line 2438) by three-hourly data from today out to 30-Jan, which is why you were seeing some duplicates.

Might be good to have a look at how you’re meant to query your provider.

Hi Troon

Okay thank you very much for your support

Thanks and best regards
Buhli

Hi Troon

Sorry, I have another question. I have an attribute “forecasts” where the forecasts are stored. I would now like to determine the min and max based on these forecasts. I have tried the following, unfortunately without success.

    sensor:
      - name: SRF Weather Forecast v2
        unique_id: gtaee602-6483-4add-90b8-81ca8c7edaef
        state: "{{ hourly['weather.srf_weather_46_75477_6258']['forecast']|count }}"
        attributes:
          forecasts: "{{ hourly['weather.srf_weather_46_75477_6258']['forecast'] }}"
          min: "{{ forecasts | selectattr('datetime','match',now().date()|string)|map(attribute='templow')|min }}"
          max: "{{ forecasts | selectattr('datetime','match',now().date()|string)|map(attribute='temphigh')|max }}"

Can I use this “forecasts” attribute in any way?

Thanks and best regards
Buhli

You can’t use forecasts as a variable like that. Just pop the template that you’ve used in the forecasts attribute in place of the forecasts you’ve used in min and max, like this:

          min: "{{ hourly['weather.srf_weather_46_75477_6258']['forecast'] | selectattr('datetime','match',now().date()|string)|map(attribute='templow')|min }}"

Hi Troon

it works, thank you very much

Best regards
Buhli

1 Like