Rains in next 6 hours

I cannot get the solution from the various topics on this. So asking for some specific help please.

I want a sensor that will just tell me the chance of rain in the next 6 hours.
I used to have it until the change to the logic of the forecast and cannot get it to work.

I have the weather.openweathermap entity.
I can call the service in developer tools to get the response but no idea (and copying the options presented before) doesnt seem to cover it.
The forecast comes back with a “precipitation_probability” value for each hours time slot.

It sounds easy but I just cannot figure this one out and need a bit of help please.

Can someone post what I need to copy paste? I can usually work these out through trial and error but this one has me going around in circles. Resorted to chatgpt but that is quoting old solutions.

Don’t use ChatGPT for HA.

Do you mean you have successfully created this template sensor (extension of the example here)? If not, do that first:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Weather forecast
        state: "{{ hourly['weather.openweathermap'].forecast[0].temperature }}"
        unit_of_measurement: °C
        attributes:
          forecast: "{{ hourly['weather.openweathermap'].forecast }}"

Then put this in Developer Tools / Editor and paste the correctly-formatted result here:

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

Try to get an understanding of what you’re doing. Copy / paste and trial & error are both terrible methods.

I have sensors that do this same thing but it’s not really a copy-paste thing because there are several factors I incorporate. Especially since the change away from the forecast attribute on weather entities to the get_forecasts method you have to do this quite differently.

For me, I use a trigger sensor every 5 minutes, that way I can call get_forecasts in the sensor, it’s the only way I’ve found to call get_forecasts as part of my sensor. Once you have that you iterate over each value to see the precipitation_probability attribute of each day and compare it to whatever value you feel is right to know if rain is possible (here if it’s less than 20% then it’s pretty low probability).

This is not my exact setup, just snippets from several things thrown together for your example:

trigger:
  - platform: time_pattern
    minutes: /5
  - platform: homeassistant
    event: start

action:
  - service: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.openweathermap
    response_variable: owm_hourly

sensor:
  - name: Next Forecast Rain
    state: >-
        {% set ns = namespace(raintime = "", hours_count = 0) %}
        {% for s in owm_hourly['weather.forecast_home'].forecast | list %}
          {% set ns.hours_count = ns.hours_count + 1 %}
          {% if int(s['precipitation_probability'], 0) > 19 %}
            {% set ns.raintime = int(s['precipitation_probability'], 0) | string + '% Chance on ' + as_timestamp(s.datetime) | timestamp_custom('%A %b %-d at %-I:00 %p ') %}
          {% endif %}
          {% if ns.hours_count > 5 %}
            {% break %}
          {% endif %}
        {% endfor %}

        {% if ns.raintime != "" %}
          {{ ns.raintime }}
        {% else %}
          {{ "Nothing 20% or more forecast for the next 6 hours" }}
        {% endif %}

Mine is different than what you are looking for in that it checks for the next time there is a 20% or more chance of rain for however many days my system caches the data (I used OWM as an example but I actually use my WeatherFlow that does 96 hours), but hopefully you can derive what you need from this code and make your own.

Without testing I threw in what you might have in there to check for only the next 6 hours.

I have added the template for the standard temperature item as posted and also what I think ought to work for the rain in next hour. Waiting for it to populate

Ok, so they populated. With the info for the hour.

Pasting {{ state_attr(‘sensor.temperature_forecast_next_hour’,‘forecast’) }} into developer tools returns ‘null’

That’s a nebulous question, we need to see the sensor code to know.

My sensor created so far is

  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Precipitation forecast next hour
        unique_id: precipitation_forecast_next_hour
        state: "{{ hourly['weather.openweathermap'].forecast[0].precipitation_probability }}"
        unit_of_measurement: '%'

Next step to get it to the 6 hour value. where 100% in any section = 100%.
So I guess 0,0,0,0,0,0,100 = 100%
20,20,20,20,20,20 = 20%

Have another look at my template sensor above. You haven’t included the forecast attribute, which is needed to pull in the information you need.

1 Like

Oh ues, now I have added that to the bottom and I get an error immediately.

My typo: I’d missed the close brackets. This is the problem with writing code for someone else’s system: it’s “freehand” and untested. Could you not see the missing brackets?

          forecast: "{{ hourly['weather.openweathermap'].forecast }}"
------------------------------------------------------------------^^

This is what I mean about copy/paste: you’re totally reliant on others to fix any issues. With a bit of learning, you’d have been able to fix that yourself much quicker than it took to prepare the response above.

Right, thanks.

So I have this template sensor.

  - trigger:
      - platform: time_pattern
        minutes: /5
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Precipitation forecast next hour
        unique_id: precipitation_forecast_next_hour
        state: "{{ hourly['weather.openweathermap'].forecast[0].precipitation_probability }}"
        unit_of_measurement: '%'
        attributes:
          forecast: "{{ hourly['weather.openweathermap'].forecast }}"

If I place in developer tools/template

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

I get

[
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-21T18:00:00+00:00",
    "wind_bearing": 74,
    "cloud_coverage": 100,
    "temperature": 16.7,
    "pressure": 1009,
    "wind_speed": 1.04,
    "precipitation": 2.04,
    "humidity": 56
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-21T21:00:00+00:00",
    "wind_bearing": 268,
    "cloud_coverage": 100,
    "temperature": 14,
    "pressure": 1009,
    "wind_speed": 2.02,
    "precipitation": 2.82,
    "humidity": 78
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T00:00:00+00:00",
    "wind_bearing": 341,
    "cloud_coverage": 100,
    "temperature": 12.1,
    "pressure": 1008,
    "wind_speed": 8.5,
    "precipitation": 2.52,
    "humidity": 97
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T03:00:00+00:00",
    "wind_bearing": 358,
    "cloud_coverage": 100,
    "temperature": 11.8,
    "pressure": 1006,
    "wind_speed": 6.16,
    "precipitation": 1.63,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T06:00:00+00:00",
    "wind_bearing": 323,
    "cloud_coverage": 100,
    "temperature": 11.7,
    "pressure": 1006,
    "wind_speed": 10.37,
    "precipitation": 3.89,
    "humidity": 99
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T09:00:00+00:00",
    "wind_bearing": 303,
    "cloud_coverage": 100,
    "temperature": 11.3,
    "pressure": 1005,
    "wind_speed": 15.8,
    "precipitation": 3.94,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T12:00:00+00:00",
    "wind_bearing": 299,
    "cloud_coverage": 100,
    "temperature": 11.5,
    "pressure": 1004,
    "wind_speed": 25.02,
    "precipitation": 7.64,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T15:00:00+00:00",
    "wind_bearing": 291,
    "cloud_coverage": 100,
    "temperature": 11.8,
    "pressure": 1004,
    "wind_speed": 20.56,
    "precipitation": 5.17,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T18:00:00+00:00",
    "wind_bearing": 281,
    "cloud_coverage": 100,
    "temperature": 11,
    "pressure": 1004,
    "wind_speed": 25.16,
    "precipitation": 5.51,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-22T21:00:00+00:00",
    "wind_bearing": 291,
    "cloud_coverage": 100,
    "temperature": 10.8,
    "pressure": 1005,
    "wind_speed": 27.58,
    "precipitation": 5.57,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-23T00:00:00+00:00",
    "wind_bearing": 272,
    "cloud_coverage": 100,
    "temperature": 9.8,
    "pressure": 1006,
    "wind_speed": 27.97,
    "precipitation": 3.83,
    "humidity": 98
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-23T03:00:00+00:00",
    "wind_bearing": 271,
    "cloud_coverage": 100,
    "temperature": 8.9,
    "pressure": 1007,
    "wind_speed": 30.89,
    "precipitation": 1.77,
    "humidity": 97
  },
  {
    "condition": "rainy",
    "precipitation_probability": 100,
    "datetime": "2024-05-23T06:00:00+00:00",
    "wind_bearing": 271,
    "cloud_coverage": 100,
    "temperature": 8.1,
    "pressure": 1009,
    "wind_speed": 30.6,
    "precipitation": 0.14,
    "humidity": 96
  },
  {
    "condition": "rainy",
    "precipitation_probability": 38,
    "datetime": "2024-05-23T09:00:00+00:00",
    "wind_bearing": 274,
    "cloud_coverage": 100,
    "temperature": 8,
    "pressure": 1011,
    "wind_speed": 31.1,
    "precipitation": 0.23,
    "humidity": 95
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 22,
    "datetime": "2024-05-23T12:00:00+00:00",
    "wind_bearing": 276,
    "cloud_coverage": 100,
    "temperature": 8.1,
    "pressure": 1013,
    "wind_speed": 30.89,
    "precipitation": 0,
    "humidity": 93
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-23T15:00:00+00:00",
    "wind_bearing": 275,
    "cloud_coverage": 100,
    "temperature": 8.6,
    "pressure": 1014,
    "wind_speed": 31.46,
    "precipitation": 0,
    "humidity": 89
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-23T18:00:00+00:00",
    "wind_bearing": 284,
    "cloud_coverage": 100,
    "temperature": 8.5,
    "pressure": 1015,
    "wind_speed": 28.12,
    "precipitation": 0,
    "humidity": 86
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-23T21:00:00+00:00",
    "wind_bearing": 277,
    "cloud_coverage": 98,
    "temperature": 8.9,
    "pressure": 1017,
    "wind_speed": 23.26,
    "precipitation": 0,
    "humidity": 81
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T00:00:00+00:00",
    "wind_bearing": 268,
    "cloud_coverage": 99,
    "temperature": 8.4,
    "pressure": 1017,
    "wind_speed": 20.38,
    "precipitation": 0,
    "humidity": 81
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T03:00:00+00:00",
    "wind_bearing": 267,
    "cloud_coverage": 99,
    "temperature": 7.9,
    "pressure": 1017,
    "wind_speed": 19.94,
    "precipitation": 0,
    "humidity": 82
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T06:00:00+00:00",
    "wind_bearing": 252,
    "cloud_coverage": 100,
    "temperature": 7.5,
    "pressure": 1018,
    "wind_speed": 14.44,
    "precipitation": 0,
    "humidity": 86
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T09:00:00+00:00",
    "wind_bearing": 256,
    "cloud_coverage": 98,
    "temperature": 8.7,
    "pressure": 1019,
    "wind_speed": 13.21,
    "precipitation": 0,
    "humidity": 84
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T12:00:00+00:00",
    "wind_bearing": 264,
    "cloud_coverage": 99,
    "temperature": 10.9,
    "pressure": 1019,
    "wind_speed": 10.62,
    "precipitation": 0,
    "humidity": 81
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T15:00:00+00:00",
    "wind_bearing": 289,
    "cloud_coverage": 100,
    "temperature": 11.3,
    "pressure": 1019,
    "wind_speed": 6.98,
    "precipitation": 0,
    "humidity": 80
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T18:00:00+00:00",
    "wind_bearing": 318,
    "cloud_coverage": 100,
    "temperature": 11.6,
    "pressure": 1020,
    "wind_speed": 3.06,
    "precipitation": 0,
    "humidity": 83
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-24T21:00:00+00:00",
    "wind_bearing": 5,
    "cloud_coverage": 99,
    "temperature": 9.8,
    "pressure": 1020,
    "wind_speed": 5.04,
    "precipitation": 0,
    "humidity": 89
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T00:00:00+00:00",
    "wind_bearing": 102,
    "cloud_coverage": 97,
    "temperature": 8.8,
    "pressure": 1020,
    "wind_speed": 3.06,
    "precipitation": 0,
    "humidity": 91
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T03:00:00+00:00",
    "wind_bearing": 166,
    "cloud_coverage": 100,
    "temperature": 9.3,
    "pressure": 1020,
    "wind_speed": 4.61,
    "precipitation": 0,
    "humidity": 90
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T06:00:00+00:00",
    "wind_bearing": 158,
    "cloud_coverage": 66,
    "temperature": 9.3,
    "pressure": 1020,
    "wind_speed": 5.83,
    "precipitation": 0,
    "humidity": 92
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T09:00:00+00:00",
    "wind_bearing": 168,
    "cloud_coverage": 58,
    "temperature": 12.4,
    "pressure": 1020,
    "wind_speed": 14.26,
    "precipitation": 0,
    "humidity": 74
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T12:00:00+00:00",
    "wind_bearing": 192,
    "cloud_coverage": 79,
    "temperature": 14.2,
    "pressure": 1021,
    "wind_speed": 18.65,
    "precipitation": 0,
    "humidity": 64
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T15:00:00+00:00",
    "wind_bearing": 217,
    "cloud_coverage": 100,
    "temperature": 14.5,
    "pressure": 1020,
    "wind_speed": 17.53,
    "precipitation": 0,
    "humidity": 68
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-25T18:00:00+00:00",
    "wind_bearing": 200,
    "cloud_coverage": 100,
    "temperature": 13.6,
    "pressure": 1021,
    "wind_speed": 12.35,
    "precipitation": 0,
    "humidity": 82
  },
  {
    "condition": "rainy",
    "precipitation_probability": 20,
    "datetime": "2024-05-25T21:00:00+00:00",
    "wind_bearing": 191,
    "cloud_coverage": 78,
    "temperature": 11.1,
    "pressure": 1022,
    "wind_speed": 9.47,
    "precipitation": 0.12,
    "humidity": 91
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-26T00:00:00+00:00",
    "wind_bearing": 177,
    "cloud_coverage": 89,
    "temperature": 10.5,
    "pressure": 1022,
    "wind_speed": 8.57,
    "precipitation": 0,
    "humidity": 89
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-26T03:00:00+00:00",
    "wind_bearing": 173,
    "cloud_coverage": 100,
    "temperature": 9.9,
    "pressure": 1022,
    "wind_speed": 8.14,
    "precipitation": 0,
    "humidity": 91
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-26T06:00:00+00:00",
    "wind_bearing": 174,
    "cloud_coverage": 89,
    "temperature": 10.9,
    "pressure": 1023,
    "wind_speed": 8.39,
    "precipitation": 0,
    "humidity": 90
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-26T09:00:00+00:00",
    "wind_bearing": 188,
    "cloud_coverage": 81,
    "temperature": 12.9,
    "pressure": 1024,
    "wind_speed": 13.32,
    "precipitation": 0,
    "humidity": 82
  },
  {
    "condition": "cloudy",
    "precipitation_probability": 0,
    "datetime": "2024-05-26T12:00:00+00:00",
    "wind_bearing": 190,
    "cloud_coverage": 59,
    "temperature": 17.2,
    "pressure": 1024,
    "wind_speed": 15.88,
    "precipitation": 0,
    "humidity": 69
  },
  {
    "condition": "rainy",
    "precipitation_probability": 69,
    "datetime": "2024-05-26T15:00:00+00:00",
    "wind_bearing": 209,
    "cloud_coverage": 65,
    "temperature": 18,
    "pressure": 1023,
    "wind_speed": 15.19,
    "precipitation": 0.42,
    "humidity": 71
  }
]

So I seem to have data on a 3 hourly basis from that provider, perhaps I can get it elsewhere for each hour. But assume can work on these data points anyway

1 Like

Perfect, that’s working. So to answer your original question, yes, rains in the next 6 hours :wink:

If you need further help in extracting the data just ask, being precise in what you want.

As an example, the second time point’s probability is:

{{ state_attr('sensor.precipitation_forecast_next_hour','forecast')[1]['precipitation_probability'] }}
1 Like

Cheers. That’s why I got when then. :stuck_out_tongue_winking_eye:

I have switched back to my original sensor now on Climacell/Tomorrow as that does have hourly.
In the output of that entity I now see the hourly forecasts, though the new code you shared returns the right answer. I will now figure out how to add and amount together and divide it . I think the old sensor did that. Will try to figure that one out on my own. WIll post the success for an ask for more help shortly!

1 Like

Solved.

I now have a 6 and a 12 hour sensor back. This is the code in my templates yaml file.

#rain sensors 2024 version. use the new format in the template below to work.
      - name: "Next 6hr Rain Probability"
        icon: mdi:weather-rainy
        unit_of_measurement: "%"
        state:  >-
          {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
          {{ ((forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 6)  |round(2) }}

      - name: "Next 12hr Rain Probability"
        icon: mdi:weather-rainy
        unit_of_measurement: "%"
        state:  >-
          {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
          {{ ((forecast[11].precipitation_probability | int + forecast[10].precipitation_probability | int + forecast[9].precipitation_probability | int + forecast[8].precipitation_probability | int + forecast[7].precipitation_probability | int + forecast[6].precipitation_probability | int + forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 12)  |round(2) }}
#template sensor for the rain trigger to get the forecast info and save it to look up
  - trigger:
      - platform: time_pattern
        minutes: /30
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.climacell_hourly
        response_variable: hourly
    sensor:
      - name: Temp Weather Data
        unique_id: temp_weather_data
        state: "{{ hourly['weather.climacell_hourly'].forecast[0].precipitation_probability }}"
        unit_of_measurement: '%'
        attributes:
          forecast: "{{ hourly['weather.climacell_hourly'].forecast }}"

Then I have a conditional chip on my main dashboard that I can use to show me the forecast, but I can also use the sensors as part of my goodnight automation again. Useful in the British summer when you get fooled into thinking the weather is nice, and leave windows open! Only to wake up to a puddle.

type: conditional
conditions:
  - condition: numeric_state
    entity: sensor.next_12hr_rain_probability
    above: 5
chip:
  type: template
  icon: mdi:weather-rainy
  icon_color: blue
  content: >-
    6/12hr {{ states('sensor.next_6hr_rain_probability') }}/{{
    states('sensor.next_12hr_rain_probability') }}%

image

Thanks for the support on this.

2 Likes

Hey @richsimpson is there any way that you could attach your completed script as a YAML file? I am trying to use your code but copying and pasting seems to mess with the spacing and I can not get it to work.

It would be so helpful!

Hey again! I THINK I figured out the correct formatting. I installed the Climacell/Tomorrow plugin and added your code to my templates YAML file. The only change I made was renaming the Climacell entity to how it shows up in my HA.

When I check in Developer Tools/State the “sensor.temp_weather_data” sensor shows a value of “0” (since it wont be raining):

However, the 6 hour and 12 hour sensors show up as “unknown”:

Here is my code:

#rain sensors 2024 version. use the new format in the template below to work.
    - name: "Next 6hr Rain Probability"
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state:  >-
        {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
        {{ ((forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 6)  |round(2) }}

    - name: "Next 12hr Rain Probability"
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state:  >-
        {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
        {{ ((forecast[11].precipitation_probability | int + forecast[10].precipitation_probability | int + forecast[9].precipitation_probability | int + forecast[8].precipitation_probability | int + forecast[7].precipitation_probability | int + forecast[6].precipitation_probability | int + forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 12)  |round(2) }}
#template sensor for the rain trigger to get the forecast info and save it to look up
- trigger:
    - platform: time_pattern
      minutes: /30
    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.tomorrow_io_bauhaus_daily
      response_variable: hourly
  sensor:
    - name: Temp Weather Data
      unique_id: temp_weather_data
      state: "{{ hourly['weather.tomorrow_io_bauhaus_daily'].forecast[0].precipitation_probability }}"
      unit_of_measurement: '%'
      attributes:
        forecast: "{{ hourly['weather.tomorrow_io_bauhaus_daily'].forecast }}"

Here is my YAML file: https://drive.google.com/file/d/1_doZ1CBJ8NvDF1tcM_0K06TMWqMt3yDq/view?usp=sharing

Do you have any idea why the calculations for the time sensors may not be working? I don’t understand the code well enough yet to quite figure out what may have been gone wrong.

EDIT: Just after posting this the sensors are suddenly showing up with data:

I have no idea why it suddenly started working. Weirdly enough I seem to have a non function copy of each sensor, though. Any idea how to delete those?

1 Like

Thanks for this - I was able to repurpose this to get an average forcast temperature for the next X number of hours. Using that to determine if I should turn on/off the AC and open or close the windows.

My YAML looks like this for anyone looking to do something similar.

The get_forcasts template sensor with the trigger. Currently set to every 45 minutes, but might adjust that to hourly. Not sure if it matters.

  - trigger:
      - platform: time_pattern
        minutes: /45
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
    sensor:
      - name: Forecast Temperatures
        unique_id: forecast_temp
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C
        attributes:
          forecast: "{{ hourly['weather.home'].forecast }}"

And then my sensor to average out the number of forecasted hours that I want. Currently set to 3 hours.

    - name: "Average Temp Next 3 Hours"
      icon: mdi:thermometer
      unit_of_measurement: "°C"
      state:  >-
        {% set forecast = state_attr('sensor.forecast_temperatures', 'forecast') %}
        {{ ((forecast[2].temperature | int + forecast[1].temperature | int + forecast[0].temperature | int) / 3)  | round(2) }}

Have to still setup automations based on this but should be pretty straightforward using the current indoor temp, the AC set temperture, the AC mode (cool/off), and the average upcoming temperature.

Will likely just do notifications based on this for now.

1 Like

Apologies, missed this, are you sorted? If not I will look out for reply and happy to share. Been working fine since I set it up.

Hi, yes I would love your help! I had the sensor set up for three weeks now but it seems it’s always just showing as “0”. Even when it is actually raining it still shows “0” as if the sensor doesn’t actually get updated. Could you have a look at my code and see if I made a mistake?

##### Sprinkler Total Expected Precipitation ##################################
- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.openweathermap
      data:
        type: hourly
      response_variable: my_forecast
  sensor:
    - name: Total Expected Precipitation
      unique_id: 361196b7-f914-47c0-8716-498361f82730
      icon: mdi:weather-pouring
      unit_of_measurement: mm
      state: >
        {% set ns = namespace() %} 
        {% set ns.totalprecipitation = 0 %}
        {% for daypart in range(0, 7) %}
          {% set precipitation = my_forecast['weather.openweathermap'].forecast[daypart].precipitation %} 
          {% set precipitation_probability = my_forecast['weather.openweathermap'].forecast[daypart].precipitation_probability / 100 %}
          {% if precipitation_probability > 0 %}
            {% set precipitation = precipitation * precipitation_probability %}
          {% endif %}
          {% set ns.totalprecipitation = ns.totalprecipitation + precipitation %}
        {% endfor %}
        {{ ns.totalprecipitation | float(0) | round(2) }}


#rain sensors 2024 version. use the new format in the template below to work.
    - name: "Next 2hr Rain Probability"
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state:  >-
        {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
        {{ ((forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 2)  |round(2) }}

    - name: "Next 6hr Rain Probability"
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state:  >-
        {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
        {{ ((forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 6)  |round(2) }}

    - name: "Next 12hr Rain Probability"
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state:  >-
        {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
        {{ ((forecast[11].precipitation_probability | int + forecast[10].precipitation_probability | int + forecast[9].precipitation_probability | int + forecast[8].precipitation_probability | int + forecast[7].precipitation_probability | int + forecast[6].precipitation_probability | int + forecast[5].precipitation_probability | int + forecast[4].precipitation_probability | int + forecast[3].precipitation_probability | int + forecast[2].precipitation_probability | int + forecast[1].precipitation_probability | int + forecast[0].precipitation_probability | int) / 12)  |round(2) }}
#template sensor for the rain trigger to get the forecast info and save it to look up
- trigger:
    - platform: time_pattern
      minutes: /30
    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.tomorrow_io_bauhaus_daily
      response_variable: hourly
  sensor:
    - name: Temp Weather Data
      unique_id: temp_weather_data
      state: "{{ hourly['weather.tomorrow_io_bauhaus_daily'].forecast[0].precipitation_probability }}"
      unit_of_measurement: '%'
      attributes:
        forecast: "{{ hourly['weather.tomorrow_io_bauhaus_daily'].forecast }}"

Different forecast provider so it might be entirely different attributes. Not really sure.

If I paste my 6hr probability of rain sensor into the Developer Tools and Template it does tell me the value.

I suspect that the forecast it is retrieving is not right. But perhaps the sensor is ok?

To begin with I would look at your other sensor which is where the forecast is being placed temporarily, before the rain in 2/6 looks at it. In my example it was “sensor.temp_weather_data”
If I look that up in the Developer Tools States, I see that the sensor is there and holds data from the latest trigger / timer pattern run. Like this:

Your 2 and 6 hr rain probability are perhaps failing due to lack of info within that temporary sensor?

In my 6 hr probability, you see here is is taking the attributes from the sensor.temp_weather_data


For your sensor it is (I think) sensor.temp_weather_data too yes?

What is stored in that item if you look at it in Developer tools and states? Does it contain the weather forecast? If it does, thats half the battle, we can then focus on why it is not being read by the other sensor for the 2 and 6 hr probability