Rains in next 6 hours

Hi, thank you so much for the help! So I looked up the sensor.temp_weather_data and it seems to populate info:

You can see for 19h it shows 25 probably for rain. That seems to match what my weather integration shows:

(Just a side note the time seems off. The data shows 19h for rain but here in LA it is actually 12h. Not sure if that matters?)

But my sensors still all show zero:

And here again is my code:

There seem to be some error message in there now that didn’t show up before. Not sure if that matters.

I really don’t know what I’m doing wrong, especially since the sensor.temp_weather_data seems to work?

Really looking forward to your thoughts.

For your code, I assume if you copy and paste the 6 or 12 hr probability into the developer tools template it shows 0?

I have just checked and my 12 hr one (no rain for next 6 so using that one)… is as follows:

      - name: "Next 12hr Rain Probability"
        icon: mdi:weather-rainy
        unit_of_measurement: "%"
        state:  >-
            {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
            {% set probabilities = [
              forecast[0].precipitation_probability | int,
              forecast[1].precipitation_probability | int,
              forecast[2].precipitation_probability | int,
              forecast[3].precipitation_probability | int,
              forecast[4].precipitation_probability | int,
              forecast[5].precipitation_probability | int,
              forecast[6].precipitation_probability | int,
              forecast[7].precipitation_probability | int,
              forecast[8].precipitation_probability | int,
              forecast[9].precipitation_probability | int,
              forecast[10].precipitation_probability | int,
              forecast[11].precipitation_probability | int
            ] %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ (probabilities | sum / 12) | round(2) }}
            {% endif %}

It returns 0.42 for me now.

This is effectively taking the next 12 slots of forecast and doing the math on it.

The layout is different to perhaps what I originally posted and what you have still. Maybe a change to some HA update.

The 6 hour version is as follows:

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

And the whole thing (including some hashed out bits that I must have updated…) from my template.yaml file is:

  - sensor:

#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') %}
            {% set probabilities = [
              forecast[0].precipitation_probability | int,
              forecast[1].precipitation_probability | int,
              forecast[2].precipitation_probability | int,
              forecast[3].precipitation_probability | int,
              forecast[4].precipitation_probability | int,
              forecast[5].precipitation_probability | int
            ] %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ (probabilities | sum / 6) | round(2) }}
            {% endif %}
#        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') %}
            {% set probabilities = [
              forecast[0].precipitation_probability | int,
              forecast[1].precipitation_probability | int,
              forecast[2].precipitation_probability | int,
              forecast[3].precipitation_probability | int,
              forecast[4].precipitation_probability | int,
              forecast[5].precipitation_probability | int,
              forecast[6].precipitation_probability | int,
              forecast[7].precipitation_probability | int,
              forecast[8].precipitation_probability | int,
              forecast[9].precipitation_probability | int,
              forecast[10].precipitation_probability | int,
              forecast[11].precipitation_probability | int
            ] %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ (probabilities | sum / 12) | round(2) }}
            {% endif %}
#        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:
      - trigger: time_pattern
        minutes: /30
    action:
      - action: 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 }}"

FYI you can greatly simplify your templates.

            {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
            {% set probabilities = forecast[:12] | map(attribute='precipitation_probability') | list %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ probabilities | average | round(2) }}
            {% endif %}

and whenever you want to change the number, just change the [:12] to whatever number you want. I…e 6 [:6]

1 Like

Brilliant, even simpler. I must have got it working and hadn’t dared touch it again. Just copied that over to the dev tools and it works perfect.

@VeniceNerd
So the 2 forecast now look like this:

#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') %}
            {% set probabilities = forecast[:6] | map(attribute='precipitation_probability') | list %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ probabilities | average | round(2) }}
            {% endif %}

      - name: "Next 12hr Rain Probability"
        icon: mdi:weather-rainy
        unit_of_measurement: "%"
        state:  >-
            {% set forecast = state_attr('sensor.temp_weather_data', 'forecast') %}
            {% set probabilities = forecast[:12] | map(attribute='precipitation_probability') | list %}
            {% if 100 in probabilities %}
              100
            {% else %}
              {{ probabilities | average | round(2) }}
            {% endif %}

I’m not a math major - but isn’t this over simplifying things?

This code:

{% set forecast = state_attr('sensor.weatherflow_forecast_hourly','forecast') %}
{% set probabilities = forecast[:6] | map(attribute='precipitation_probability') | list %}
{{ probabilities }}
{% if 100 in probabilities %}
  100
{% else %}
  {{ probabilities | average | round(0) }}%
{% endif %}

gives this result:

[30, 65, 55, 55, 60, 40]
51%

Which is effectively just an average. Asking ChatGPT (don’t judge me) this:

this dataset represents the chance of rain 0-1 hours from now, 1-2 hours from now, etc.
[30, 65, 55, 55, 60, 40]
Given this dataset, what's the chance of rain within the next 6 hours

gives this answer:

I’m NOT a statistician - but it does seem the average far underestimates the liklihood of rain in the next x hours

How can you trust what it says when it already just gets the multiplication portion wrong?

0.7 * .35 *.45 * .45 * .4 * .6 = 0.01190

what else is it pulling out of thin air?

My point is more around the statistical probability vs. just using an average. I’ll let someone smarter than me / ChatGPT figure out the exact math, lol.

OK, update, I think this gives the right probabilities:

{% set forecast = state_attr('sensor.weatherflow_forecast_hourly','forecast') %}
{% set probabilities = forecast[:6] | map(attribute='precipitation_probability') | list %}
{% set ns = namespace(combined_no_rain_probability=1.0) %}

{% for prob in probabilities %}
  {% set no_rain_prob = 1 - (prob / 100) %}
  {% set ns.combined_no_rain_probability = ns.combined_no_rain_probability * no_rain_prob %}
{% endfor %}

{% set rain_probability = (1 - ns.combined_no_rain_probability) * 100 %}
{{ rain_probability | round(1) }}%

My {{ probabilities }} currently is: [10, 65, 55, 55, 60, 40]
My {{ rain_probability | round(1) }}% is then 98.5%

EDIT: [Removed CHAT output, apologies, didn’t realize against forum rules]

FYI it’s against community rules to post AI responses as answers to questions.

As mentioned above, in your 2 posts, one of them it completely makes up math.

image

It did get your second post correct, surprised TBH.