jcallaghan / home-assistant-config

My Home Assistant configuration & documentation.
https://www.jcallaghan.com/
MIT License
175 stars 8 forks source link

Tomorrows weather forecast 📅☔ #87

Closed jcallaghan closed 4 years ago

jcallaghan commented 4 years ago

Objective.

It rained and I forgot to cover up the garden furniture yesterday. I want to leverage my weather station data to forecast deteriorating weather conditions and rain and provide notification when it is worsening.

Ideas

Related: #13

jcallaghan commented 4 years ago

Norwegian Meteorological Institute

The weather integration from NMI provides local weather data based on the location in the configuration. This is available through an entity like weather.the_retreat.

API - Seeking out the weather conditions schema

The integration uses the locationforecast API. After reviewing the code for the Home Assistant integration I can see the weather is updated 55-65 minutes which allows updates to be spread out. After exploring the locationforecast schema I can see they use a weather symbol to denote the weather condition. This is a separate API which is where I found their definitions of the weather conditions but these still don't stack up to the conditions I see. I'm getting rainy back from the template but via the API it is Drizzle. I can't see where the translation is happening.

1 Sun
2 LightCloud
3 PartlyCloud
4 Cloud
5 LightRainSun
6 LightRainThunderSun
7 SleetSun
8 SnowSun
9 LightRain
10 Rain
11 RainThunder
12 Sleet
13 Snow
14 SnowThunder
15 Fog
20 SleetSunThunder
21 SnowSunThunder
22 LightRainThunder
23 SleetThunder
24 DrizzleThunderSun
25 RainThunderSun
26 LightSleetThunderSun
27 HeavySleetThunderSun
28 LightSnowThunderSun
29 HeavySnowThunderSun
30 DrizzleThunder
31 LightSleetThunder
32 HeavySleetThunder
33 LightSnowThunder
34 HeavySnowThunder
40 DrizzleSun
41 RainSun
42 LightSleetSun
43 HeavySleetSun
44 LightSnowSun
45 HeavysnowSun
46 Drizzle
47 LightSleet
48 HeavySleet
49 LightSnow
50 HeavySnow

To standardise the UI Home Assistant standardises these labels through from homeassistant.components.weather import PLATFORM_SCHEMA, WeatherEntity. This results in the following mapping.

{
  "state": {
    "_": {
      "clear-night": "Clear, night",
      "cloudy": "Cloudy",
      "exceptional": "Exceptional",
      "fog": "Fog",
      "hail": "Hail",
      "lightning": "Lightning",
      "lightning-rainy": "Lightning, rainy",
      "partlycloudy": "Partly cloudy",
      "pouring": "Pouring",
      "rainy": "Rainy",
      "snowy": "Snowy",
      "snowy-rainy": "Snowy, rainy",
      "sunny": "Sunny",
      "windy": "Windy",
      "windy-variant": "Windy"
    }
  }
}

Developer tools

This entity has a number of attributes that provide a summary of today's weather along with a five-day forecast.

Using Developer Tools > Template we can explore the attributes this entity contains using {{ states.weather.the_retreat }}. This provides the following output:

<template state weather.the_retreat=partlycloudy; 
  temperature=13.2, 
  humidity=59, 
  pressure=1011.2, 
  wind_bearing=27.7, 
  wind_speed=18.7, 
  attribution=Weather forecast from met.no, delivered by the Norwegian Meteorological 
 Institute., 
  forecast=[
   {'datetime': datetime.datetime(2020, 6, 4, 12, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>), 
     'temperature': 12.0, 
     'condition': 'rainy', 
     'pressure': 1004.8, 
     'humidity': 64.5, 
     'wind_speed': 13.0, 
     'wind_bearing': 294.3}, 
   {'datetime': datetime.datetime(2020, 6, 5, 12, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>),
     'temperature': 11.0, 
     'condition': 'rainy', 
     'pressure': 997.1, 
     'humidity': 80.8, 
     'wind_speed': 17.3, 
     'wind_bearing': 300.7},
   {'datetime': datetime.datetime(2020, 6, 6, 12, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>), 
     'temperature': 7.6, 
     'condition': 'rainy', 
     'pressure': 996.8, 
     'humidity': 87.5, 
     'wind_speed': 29.2, 
     'wind_bearing': 267.2}, 
   {'datetime': datetime.datetime(2020, 6, 7, 12, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>), 
     'temperature': 14.5, 
     'condition': 'partlycloudy', 
     'pressure': 1004.8, 
     'humidity': 57.8, 
     'wind_speed': 21.6, 
     'wind_bearing': 21.2},
   {'datetime': datetime.datetime(2020, 6, 8, 12, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>), 
     'temperature': 15.8, 
     'condition': 'cloudy', 
     'pressure': 1016.5, 
     'humidity': 50.3, 
     'wind_speed': 14.8, 
     'wind_bearing': 30.1}
   ], 
  friendly_name=The Retreat @ 2020-06-03T18:31:35.601035+01:00>

Weather forecast

Using {{ states.weather.the_retreat.attributes.forecast[0] }} we can see the weather forecasted for tomorrow. Replacing [0] with 1, 2,3 and 4 will push out the forecast accordingly.

image

Today and tomorrows forecast

Dialling this in a little more we can fetch specific attributes in the forecast such as the condition {{ states.weather.the_retreat.attributes.forecast[0].condition }} and temperatures {{ states.weather.the_retreat.attributes.forecast[0].temperature }}.

Now
Condition: {{ states('weather.the_retreat') }}
Temperature: {{ state_attr('weather.the_retreat','temperature') }}
Humidity: {{ state_attr('weather.the_retreat','humidity') }}
Pressure: {{ state_attr('weather.the_retreat','pressure') }}
Wind speed: {{ state_attr('weather.the_retreat','wind_speed') }} 
Wind bearing: {{ state_attr('weather.the_retreat','wind_bearing') }}

Now
Condition: cloudy
Temperature: 12.6
Humidity: 62
Pressure: 1005.3
Wind speed: 11.2 
Wind bearing: 274.8

Tomorrow:
Condition: {{ states.weather.the_retreat.attributes.forecast[0].condition }}
Temperature: {{ states.weather.the_retreat.attributes.forecast[0].temperature }}
Humidity: {{ states.weather.the_retreat.attributes.forecast[0].humidity }}
Pressure: {{ states.weather.the_retreat.attributes.forecast[0].pressure }}
Wind speed: {{ states.weather.the_retreat.attributes.forecast[0].wind_speed }}
Wind bearing: {{ states.weather.the_retreat.attributes.forecast[0].wind_bearing }}

Tomorrow:
Condition: rainy
Temperature: 10.5
Humidity: 69.0
Pressure: 996.7
Wind speed: 25.9
Wind bearing: 300.2

image