OpenWeatherMap precipitation probability


After a lot of trial and error, I finally figured out how to pull precipitation_probability from OpenWeatherMap, so I thought I’d share. This is the sensor I added to my configuration.yaml to get it to work after adding the OpenWeatherMap. It checks every 15 minutes, but you can adjust that.

template:
  - trigger:
      # Trigger every 15 minutes
      - platform: time_pattern
        minutes: "/15"
      # Trigger on Home Assistant start for an immediate value
      - platform: homeassistant
        event: start
    action:
      # Call the weather service and store the response in a variable
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly_forecast_data
    sensor:
      - name: "OpenWeatherMap Rain Probability Next Hour"
        unique_id: openweathermap_rain_probability_next_hour
        unit_of_measurement: "%"
        state_class: measurement
        icon: mdi:weather-pouring
        # This state template calculates the rain probability
        state: >
          {% set forecast = hourly_forecast_data['weather.openweathermap'].forecast %}
          {% if forecast is not none and forecast | length > 1 %}
            {{ (forecast[1].precipitation_probability) | round(0) }}
          {% else %}
            unavailable
          {% endif %}
        # These attributes will be added to your sensor
        attributes:
          # This timestamp will update every 15 minutes, proving the sensor is working.
          last_fetch: "{{ now().isoformat() }}"
5 Likes

I was thinking the same. Creating a sensor that will tell me if it will rain soon. So this sensor fetch precipitation_probability 1 hour from now and update every 15 minutes?

After posting this, I read your sensor name and it did say next hour. oops.

@imadethem do you also get the error message on the “time_pattern” line?

(it does work, however. just that configuration.yaml is in red)

No error in my configuration.yaml file using that time_pattern line, odd. I am using home assistant green, and I’m fairly new to HA in general, so I’m not sure if the system could be causing differences

If you copy and paste the code, delete it and type it manually. I find that HA somethings throws error even it the code looks correct.