Hi
PLEASE READ ALSO FOSSY777’s post below. This is not working in recent versions of HA
I’m trying to save my outside water tap and some plants from freezing. So I came up with the following idea: Let’s use the weather forecast (in my case Openweathermap) and notify if there are freezing temperatures in the forecast. If that is the case, send me a mail. Openweathermap provides a 3hour/5day forecast and 5 days should be enough as a head warning. A previous solution did not keep state across HA restarts.
So this is how I went a about.
- Use a python script to determine if freezing temperatures are in the future
- Maintain a template sensor with the lowest temperature in the forecast and the moment the temperature goes below 0°C
- Have an automation to notify me when the minimum temperature goes below 0°C or back above 0°C
Here is the code.
The python script:
epoch = 0
mintemp = 100.0
sensor = 'sensor.mintemp'
weather = 'weather.openweathermap'
is_forecast = 'input_select.forecast'
forecast = hass.states.get(weather).attributes['forecast']
current = float( hass.states.get(sensor).state )
date0 = epoch
for fc3h in forecast:
temp = fc3h['temperature']
dt = int(fc3h['datetime'])
if temp < mintemp:
mintemp = temp
if temp < 0.0 and epoch == date0:
date0 = dt
date0 = date0 / 1000.0
hass.states.set(sensor, mintemp, {
"epoch0": date0,
} )
if mintemp < 0.0:
result = 'freeze'
else:
result = 'non-freeze'
hass.states.set(is_forecast, result)
The sensor configuration.yaml
definitons
sensor:
- platform: template
sensors:
mintemp:
value_template: 0.0
unit_of_measurement: "°C"
attribute_templates:
epoch0: 0
input_select:
forecast:
name: forecasting freezing temperatures
options:
- non-freeze
- freeze
weather:
- platform: openweathermap
api_key: !secret openweathermap_key
Automation to trigger the python script from
alias: calculate minimum temparature (they don't get simpler)
trigger:
platform: state
entity_id: weather.openweathermap
action:
- service: python_script.mintemp
The automations to detect the freezing forecast (not the end of freezing one as it is very similar. I’m not sure why I added this one as it is very straightforward):
alias: freezing on
trigger:
platform: state
entity_id: input_select.forecast
from: 'non-freeze'
to: 'freeze'
action:
- service: notify.mailer
data_template:
title: "HA: Vriestemperaturen in het vooruitzicht"
message: >
Er wordt een minimum temperatuur verwacht van
{{states('sensor.mintemp')}}.
De thermometer gaat onder nul vanaf
{{ state_attr( 'sensor.mintemp', 'epoch0' ) | timestamp_custom( '%e %B om %k uur' ) }}
And that’s it. As I don’t have deep knowledge on HA, there may be better solutions to this. If they exist I’m happy to learn about them.
Note that I could do more (if not everything) in the python script, I know. I prefer to do things as much as possible in YAML. But if there are good reasons for that I would like to know those.
Regards, G
EDIT: I changed two things: i no longer need the -3°, so removed that, and introduced the input select so the state survives HA restarts.
EDIT2: Added the configuration for openweathermap.