Service-action in template, getting forecast data, undefined result

Hi all - this template thing, with embedded actions and the need to use this to extract weather forecast data, is beyond my limits it appears. I have looked through threads I found on this, tried many things, even involved ChatGPT, but I need more help.

What I want to do is make this template definition work (configuration.yaml), so that I get a numer in dwd_echt_templow:

template:
  - trigger:
      - platform: time
        at: "00:00:00"
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    action:
      - service: weather.get_forecast
        target:
          entity_id: weather.stuttgart_echt_stuttgart_echt_2
        data:
          type: daily
        response_variable: daily_forecast
    sensor:
      - name: dwd_echt_datetime
        state: "{{ daily_forecast['weather.stuttgart_echt_stuttgart_echt_2'].forecast[0].datetime }}"
      - name: dwd_echt_temperature
        state: "{{ daily_forecast['weather.stuttgart_echt_stuttgart_echt_2'].forecast[0].temperature }}"
      - name: dwd_echt_templow
        state: "{{ daily_forecast['weather.stuttgart_echt_stuttgart_echt_2'].forecast[0].templow }}"
    binary_sensor:
      - name: dwd_echt_needheating
        state: "{{ daily_forecast['weather.stuttgart_echt_stuttgart_echt_2'].forecast[0].templow < 10 }}"

Running the get_forecast() action manually gives me this output:

weather.stuttgart_echt_stuttgart_echt_2:
  forecast:
    - datetime: "2024-09-16T00:00:00Z"
      condition: rainy
      wind_bearing: NW
      wind_gust_speed: 25.9
      uv_index: 1
      precipitation_probability: 73
      temperature: 13
      templow: 9
      wind_speed: 14.8
      precipitation: 4.9
    - datetime: "2024-09-17T00:00:00Z"
      condition: rainy
      wind_bearing: SO
      wind_gust_speed: 27.8
      uv_index: 2
      precipitation_probability: 62
      temperature: 17
      templow: 12
      wind_speed: 14.8
      precipitation: 3.5

(and so on)

I managed to make this work in the Dev-tools template editor:

{% set daily_forecast =
{
    "weather.stuttgart_echt_stuttgart_echt_2": {
        "forecast": [
            {
                "datetime": "2024-09-16T00:00:00Z",
                "condition": "rainy",
                "wind_bearing": "NW",
                "wind_gust_speed": 25.9,
                "uv_index": 1,
                "precipitation_probability": 73,
                "temperature": 13,
                "templow": 9,
                "wind_speed": 14.8,
                "precipitation": 4.9
            },
            {
                "datetime": "2024-09-17T00:00:00Z",
                "condition": "rainy",
                "wind_bearing": "SO",
                "wind_gust_speed": 27.8,
                "uv_index": 2,
                "precipitation_probability": 62,
                "temperature": 17,
                "templow": 12,
                "wind_speed": 14.8,
                "precipitation": 3.5
            },
        ]
    }
}
%}

{{ daily_forecast['weather.stuttgart_echt_stuttgart_echt_2'].forecast[0].templow }}

But when I try to make this work in configuration.yaml (see above), I don’t get any data in the sensors I’m defining there.

What am I missing? Any help would be really appreciated!

The action is weather.get_forecasts… with an “s” at the end… Make sure you’re using the correct one in your actual sensor configuration.

1 Like

Oh my…

Ok, this was exactly the kind of thing that I wouldn’t have found for days and days.

Thank you so much! Working as expected now!

1 Like

Alright, I’ve got another question re the above template configuration:

What I want is that these forecast sensors are updated once per day (night), so that they always contain current/latest forecasts. To this end I’m using that time trigger, I also tried with time_pattern, and attribute variations for both.

Problem: these time-driven updates never happen. yaml-template reloading results in an update, home assistant start results in an update, but no automated time-driven updates are triggered.

What am I missing there?

How are you testing that? Have you tried different times? Check your weather integration to see what time they update, some start their “day” at hours other than 0. IIRC, there is at least one that rolls over at 6 and one that does it at 7, in those cases you would need to set your trigger to any point after they roll over.

Alright, I think you gave me the correct pointer again. I’ve changed my trigger to run hourly, and I see that the forecast data gets updated at 4am - so that was the issue, with me pulling data at midnight and wondering why it’s yesterday’s.

Thank you once more!

1 Like