I am trying to create an automation such that when the current hour forecast calls for precipitation probability > 65%, the automation will read the future forecast hours [1]-[23] and let me know how many hours the rain is forecast to continue. For example if rain starts at 1500 and the forecast is >65% probability for hours 1600 [1] - 1900 [3], the automation would indicate that rain is starting soon and will continue for 4 more hours. The automation is not an issue but I’m struggling with how to iterate and return the values where consecutive hours of probability are >65%. Has anyone done anything similar that they can share for me to see the concept?
trigger:
- trigger: state
entity_id: weather.example
to:
- rainy
conditions: []
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.example
response_variable: hourly
- variables:
rain_hours: |
{% set ns = namespace(predictions=[]) %}
{% for f in hourly["weather.example"]["forecast"] %}
{% if f.precipitation_probability|int >= 65 %}
{% set ns.predictions = ns.predictions + [f] %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{{ ns.predictions | count }}
- condition: template
value_template: "{{ rain_hours > 0 }}"
- action: notify.example
data:
message: The rain has started, it will last approximately {{ rain_hours }} hours
Great. Works perfect! I’ll modify it for some other conditions like high wind as well. Thank you.