Met.no template to loop through forecast for next 8 hours and if rain is present return the time it is due

Hi,

I would like to create a template to check the rain forecast for the next 8 hours and return the times that rain is due.

So far I can use

{{ 'rain' in state_attr('weather.home_hourly', 'forecast')[:7] | map(attribute='condition') }}

Which checks if rain is present but I’m not sure how to return the datetime attribute if rain is due.

My aim is to output this as a notification for example:

It will rain today at:
8 am
10am
3pm

Thanks in advance

I did this exact thing yesterday, although looking at the full hourly forecast. It could be better, but here is what I currently have which I use in push notification. Hope it helps you.

{%- set ns = namespace (z=[]) %}
{%- for i in state_attr('weather.home_hourly', 'forecast') %}
  {%- if i.condition == 'rainy' %} {% set ns.z = ns.z + [ (as_timestamp(i.datetime) | timestamp_custom('%-I%p')) ] %} {% endif %}
{%- endfor%}
{%- if ns.z|length > 0 %}
It is going to rain at {% for x in ns.z %}
  {%- if ns.z|length > 1 %}
    {%- if loop.last %} and {% endif -%}
  {% endif -%}
  {{ x }}
  {%- if ns.z|length > 2 %}
    {%- if loop.index+1 < (ns.z|length) %}, {% endif -%}
  {% endif -%}
{% endfor %}
{% else -%}
There is no rain forecast.
{% endif -%}
2 Likes

Thanks so much, that was exactly what I needed :grinning:

Lovely thank you very much. But I have a question. How can we add multiple conditions to your template. Like rainy, lightning-rainy or snowy?

{%- if i.condition in ['partlycloudy','cloudy'] %}

Though the output text would be more appropriate in that case to change it to ‘Chance of precipitation at…’