Frost warning via weather forecast

Hej there,

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.

[{'condition': 'sunny', 'temperature': 6.9, 'datetime': 1540922400000, 'precipitation': None},
{'condition': 'sunny', 'temperature': 4.9, 'datetime': 1540933200000, 'precipitation': None},
{'condition': 'sunny', 'temperature': 1.4, 'datetime': 1540944000000, 'precipitation': None}, ...]

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?

Looking forward to read your ideas! Cheers
Mario

1 Like

I have a ‘bad weather sensor’ that uses darksky to establish frost, snow and heavy rain…

sensor:
  - platform: template 
    sensors:
      bad_weather:
        value_template: >
          {% if is_state('sensor.dark_sky_precip' , 'snow') %} snow
          {% elif ((states('sensor.dark_sky_daily_low_temperature')|int < 4) and (states('sensor.dark_sky_dew_point')|int < 6)
            and (states('sensor.dark_sky_wind_speed')|int < 12) and (states('sensor.dark_sky_cloud_coverage')|int < 20)
            and (states('sensor.dark_sky_humidity')|int > 50)) %} frost
          {% elif (states('sensor.dark_sky_precip_intensity')|int > 7) %} heavy_rain
          {% else %} clear {% endif %}
1 Like

Thanks for sharing your config! That’s nice way to estimate the bad weather conditions :slight_smile: 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 :wink:

1 Like

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.

Hi Mario,

did you manage to get this work somehow?

I just was on my search for a solution to this also exactly with the same intention: save the plants etc.

BR, Jochen

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.

sensor:
  - platform: darksky
    api_key: !secret dark_sky_api
    forecast:
      - 0
    monitored_conditions:
      - temperature_low

automation:
  - alias: 'Freeze Alert'
    initial_state: true
    trigger:
      - platform: time
        at: "21:30:00"
    condition:
      condition: numeric_state
      entity_id: sensor.dark_sky_overnight_low_temperature_0d
      below: 33
    action:
      service: notify.sms_alert
      data:
        title: 'Low Temp is {{ states.sensor.dark_sky_overnight_low_temperature_0d.state }}'
        message: 'Low outside temperature is {{ states.sensor.dark_sky_overnight_low_temperature_0d.state }}.'

Thanks Weok!

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)?

BR, Jochen

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)

3 Likes

Hmm this hasn’t been triggering the last 2 days, when it’s been -2.
Is it working for you ok?