Hello, i am fighting almost two weeks with a way to receive notifications, based on weather forecast. In my cast incoming rain i next 24h.
currently i have integration met.no and weather.com.
The code i have is the following, but not sending me anything.
No error in the logs, the notification part is correct as the rest of the automation work smoothly.
Thank you.
alias: Rain notification next 24h (robust test)
triggers:
trigger: time_pattern
hours: /2
conditions:
condition: template
value_template: >
{% set forecast = state_attr(‘weather.home’, ‘forecast’) %} {% if not
forecast %}
false
{% else %}
{% set now_ts = now().timestamp() %}
{% set next_24h = now_ts + 86400 %}
{% set rainy = [] %}
{% for f in forecast %}
{% if f.datetime is defined and f.precipitation is defined %}
{% set ts = as_timestamp(f.datetime) %}
{% if ts >= now_ts and ts <= next_24h and f.precipitation > 0 %}
{% set rainy = rainy + [f] %}
{% endif %}
{% endif %}
{% endfor %}
{{ rainy | count > 0 }}
{% endif %}
actions:
data:
message: “ Rain detected in next 24h!”
action: notify.mobile_app_miro
mode: single
Please follow Community Questions Guideline #11 by using the “Preformatted text” option on any code or YAML configuration so that it is properly formatted. It can be difficult to determine the source of an issue if we cannot tell whether the proper indentation and syntax has been followed.
The forecast attribute was removed from most weather service integrations nearly 3 years ago… making the function call state_attr('weather.home', 'forecast') very unlikely to produce anything.
You will need to use the weather.get_forecasts action. In addition to the examples found on the linked Weather integration’s page, if you search this forum for that action you will find dozens of examples similar to what you are trying to do.
Do you mean you are receiving that error when testing the template in the Template Editor?
The variable hourly does not have any value in the Template Editor unless you define it. The editor does not parse YAML and cannot run actions… it only renders Jinja templates.
You can run the action in the Actions tool to generate a response. Scroll down to the bottom and click “Copy to clipboard as template”. Paste that into the Template Editor above the template you are testing.
If you’re using my version, please note that I fixed something in my template (post updated). It’s not related to your issue (it’s ve ry likely what Didgeridrew said), but I had to mention it: The bug was that it reported the rain’s start time incorrectly.