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 }}"