I try to implement a warning about possible frost via weather forecast. My plan is to use openweathermap, check if there is any temperature value below zero and send myself a notification. I could use a template sensor to check for a temperature below zero and use an automation to trigger the notification.
The forecast comes as an array with 40 objects for each time in the future.
My problem is now how to manage to iterate over that array and stop on the first finding or save the (first) finding in a variable. So far I tried namespaces but canât read the value from the namespace:
{% set ns = namespace(temp=100) %}
{% for state in states.weather.openweathermap.attributes.forecast %}
{% if state.temperature <= 0 %}
{% set ns.temp = state.temperature %}
{% endif %}
{% endfor %}
Works but not what I need: {{ns}}
Doesn't work: {{ns.temp}}
Doesn't work: {{ns['temp']}}
Also using âbreakâ to stop the iteration does not work for me (I guess it does not work with HA):
{% for state in states.weather.openweathermap.attributes.forecast %}
{% if state.temperature <= 0 %}
{{ state.temperature }}
{% break %}
{% endif %}
{% endfor %}
Does someone have an idea how I could get the first temperature below zero with it`s datetime?
Or is there a better approach to solve this without writing a custom component?
Thanks for sharing your config! Thatâs nice way to estimate the bad weather conditions It wonât do that for the next (3) day(s), right?
I would need it for a) taking plants inside that are not frost save and b) salt the pedestrian path. The earlier I know that I should do that the better
No, it doesnât predict it a few days in advance, I use that sensor to make my alarm clock go off 15 minutes early if Iâm going to have to scrape the car and take longer to get to work, so itâs more of a âthis is happening nowâ deal.
I guess though if you could get those values for 3 days time from the forecast somehow and be a little looser with the figures it could work, although itâll be less accurate so youâd have to decide whether you want it to be overly cautious or whatever.
I setup a frost alert to notify me the night before so I can take any appropriate actions. Itâs setup with DarkSky currently. You can add additional days to the forecast for more advanced notice.
Meanwhile, I got it working by following this post: Getting warned when it's going to freeze
It utilizes OpenWeatherMap, which I had as a sensor already, so appreciated that.
But I see that yours is much easier to setup with no need of Python! If I wouldn´t have it up and running already I would definitely now have gone for this one even though I would have to get another weather service up and running for it. It is also able to be used with metric / European values (i.e. Celsius instead of Fahrenheit)?
Should you be interested in a way to get tomorrowâs forecasted maximum or minimum temperatures from OpenWeatherMap, Petro posted a three-line template here:
It would be trivial to make it into a frost warning sensor.
NOTE
Any solutions based on the DarkSky API are now subject to the following limitations:
If you already have a DarkSky API key, the API service will terminate at the end of 2021.
If you do not currently have a DarkSky API key, you are no longer able to get one.
Thought to resurrect this thread as the DarkSky API is now inaccessible, so here is my solution using openWeather:
Sensor created in config.yaml
sensor:
######## Frost Sensor:
- platform: template
sensors:
24hrlowtemp:
value_template: >
{% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
{% set end = (start + timedelta(days=1)) %}
{% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
{% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
{{ state_attr('weather.openweathermap', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='templow') | list | max }}
Automation:
alias: Frost Forecast
description: >-
Checks the low temps for the next 24 hours and notifies to bring in plants and
open outdoor tap
mode: single
trigger:
- platform: time
at: '18:30:00'
condition:
- condition: numeric_state
entity_id: sensor.24hrlowtemp
below: '2'
action:
- service: notify.mobile_app
data:
title: Frost is forecast!
message: >-
Bring in the outdoor plants and consider opening the outdoor tap fully
(after turning it off, under the kitchen sink)