Saturday, January 21, 2023

Setting a frost alarm on Home assistant

One of the issues with winter is the possibility of ice covering your windscreen which needs to be cleared before you can drive. Clearing out the wind screen in such situations requires additional time in the morning. Or you can put a simple cover over the windscreen which prevents condensation and therefore the formation of ice. However this is not needed everyday - only on nights when the temperature goes to freezing. I wrote an automation in Home Assistant which checks the forecast and warns me if there is a chance of frost at night.

I use the MET office integration in Home Assistant for my forecasts. One of the entities available is a forecast in 3 hourly chunks for the next 4 days. To determine if there is a chance of frost at night, at 2100, I check the next 3 - 3hourly forecast temperatures and determine if the temperature goes below 3C. If yes, I send an announcement on my google home devices to warn occupants of a chance of frost. This is a good reminder to cover the windscreen to avoid ice on the wind screen the next morning.

To implement this in Homeassistant, I first create a template sensor with the following.

# Minimum temperature over the next 3 3hour forecasts
  - name: "Minimum Forecast Temperature"
    unique_id: "minimum_forecast_temperature"
    unit_of_measurement: '°C'
    state: >-
      {% set mylist = namespace(temps=[]) %}
      {% for s in state_attr('weather.met_office_home_3_hourly', 'forecast')[0:3] -%}
      {% set mylist.temps = mylist.temps + [s.temperature] %}
      {%- endfor %}
      {{ mylist.temps|min }}
Here, weather.met_office_home_3_hourly is the sensor provided by the MET office integration. We go through the list of forecasts for the next 3 - 3 hour chunks and report the minimum temperature.

The template sensor, sensor.minimum_forecast_temperature now returns the minimum temperature for the next 9 hours. 

You can now set an automation to check if the temperature dips below a pre-set temperature and make the announcement.

alias: "Door: Check for frost"
description: ""
trigger:
  - platform: time
    at: "21:00:00"
condition:
  - condition: numeric_state
    entity_id: sensor.minimum_forecast_temperature
    below: 3
action:
  - service: tts.google_say
    data:
      entity_id: >-
        media_player.dining_speaker
      message: >-
        Minimum temperature falling below freezing. Expect frost tomorrow
        morning.
mode: single

Setting a frost alarm on Home assistant

One of the issues with winter is the possibility of ice covering your windscreen which needs to be cleared before you can drive. Clearing ou...