Weather forecast help

Hello, I am looking for some help getting an automation based on future weather forecast working.

At sunrise I want to check if the majority of the next 12 hours will be cloudy or rainy and preform an action based if it will be cloudy or rainy.

I believe that I need to call the weather.get_forecasts service but I am lost on how to use that information properly.

I attempted quite a few versions of this with chatgpt but nothing worked correctly. I would appreciate any help!

Post what you have created.

This is as far as I was able to get with chatgpt and reading other forum post.

alias: Turn On Rainy Day Boolean Test 2
description: ""
triggers:
  - event: sunrise
    offset: "-00:30:00"
    id: Check weather
    trigger: sun
conditions: []
actions:
  - action: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.forecast_home
    response_variable: hourly

  - condition: template
    value_template: >
      {% set rainy_or_cloudy_count = 0 %}
      {% for hour in state_attr('weather.forecast_home', 'forecast')[:12] %}
        {% if hour.condition in ['rainy', 'cloudy'] %}
          {% set rainy_or_cloudy_count = rainy_or_cloudy_count + 1 %}
        {% endif %}
      {% endfor %}
      {{ rainy_or_cloudy_count > 6 }}

  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.rainy_day

mode: single

ChatGPT and other LLM bullshit engines are, for the most part, very bad at Home Assistant and produce a lot of plausible-looking, non-functional configurations. HA updates often and the majority of available examples that LLMs can pull from are old and/or from people posting non-working configs here or on Reddit. Deciphering and correcting LLM slop often requires a higher level of knowledge than it would take to just create the config yourself from scratch.

Issues:

  • Your condition references the forecast attribute of a weather entity which was removed over a year ago. You need to get that data by referencing the response variable you retrieved with the weather.get_forecasts action.
  • The way you have set up that loop will always result in the final comparison rendering false. If you want to extract the value out of the loop you would need to use a namespace. However, the loop isn’t necessary and is less efficient than using built-in filters.
  - condition: template
    value_template: >
      {{ hourly['weather.forecast_home'].forecast[:12] 
      | map(attribute='condition') 
      | select('in', ['rainy', 'cloudy']) | list | count > 6 }}
1 Like

In addition to everything Didgeridrew recommended, I suggest you consider using a Trigger-based Template Binary instead of an automation that sets an Input Boolean.

template:
  - trigger:
      - event: sunrise
        offset: "-00:30:00"
        trigger: sun
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    binary_sensor:
      - name: Rainy Day
        unique_id: rainy_day_abc123
        value_template: >
          {{ hourly["weather.forecast_home"].forecast[:12] 
            | map(attribute='condition') 
            | select('in', ['rainy', 'cloudy']) | list | count > 6 }}
1 Like

Thank you both! Y’all put me on the right track and after a few days of tinkering I got it working. Thank you again!