Skip to content

Part 6. Add Historical Weather Data

Rachel edited this page Jan 30, 2018 · 6 revisions

Do you have a personal weather station running already that you'd really love to compare to what Wunderground said the weather was that day? Did you just start streaming from Wunderground, but wish you could add last month's weather to your dashboard? We're here to help!

Wunderground's API is so great that they let you access historical data, too. It does differ slightly from regular streaming, so we'll walk you through it. You can also read about the history part of their API here: https://www.wunderground.com/weather/api/d/docs?d=data/history

Basically, you give Wunderground the date you want to look at, and Wunderground will return data that looks like this:

  "tempm": "9.4",
  "tempi": "48.9",
  "dewptm": "7.8",
  "dewpti": "46.0",
  "hum": "90",
  "wspdm": "5.6",
  "wspdi": "3.5",
  "wgustm": "-9999.0",
  "wgusti": "-9999.0",
  "wdird": "240",
  "wdire": "WSW",
  "vism": "12.9",
  "visi": "8.0",
  "pressurem": "1004.8",
  "pressurei": "29.68",
  "windchillm": "-999",
  "windchilli": "-999",
  "heatindexm": "-9999",
  "heatindexi": "-9999",
  "precipm": "0.3",
  "precipi": "0.01",
  "conds": "Light Rain",
  "icon": "rain",
  "fog": "0",
  "rain": "1",
  "snow": "0",
  "hail": "0",
  "thunder": "0",
  "tornado": "0",
  "metar": "METAR KSFO 050856Z 24003KT 8SM -RA SCT032 BKN050 OVC060 09/08 A2967 RMK AO2 RAB49 SLP048 P0001 60001 T00940078 55000"

"m" at the end means metric and an "i" means imperial

There will be data for each hour of the day approximately.

So all you need to do is:

  1. Copy the script wunderground_historical.py (already on your system if you cloned the repo)
  2. Edit it like before - $ nano wunderground_historical.py
  3. Set the desired state and city on lines 10 and 11
  4. Set your Wunderground API key on line 12 and your Initial State access key on line 15
  5. Change your bucket key on line 14 to reflect the key of the dashboard you want to send to
  6. Set the Start and End dates on lines 16 and 17. They should look like date(2017,11,16)

And that's it! You can run the script with $ python wunderground_historical.py.

historicalwundergrounddashboard

A couple important things to note:

  • Because this is historical data, it has all already been collected. Meaning that we are streaming a batch of data points every 5 seconds for however as long as our specified time span. You will want to run this on a device that won't be interrupted, like a Pi! If your stream is interrupted, just look for the last date streamed and make that your new start date.
  • In this script, we are only collecting values for temperature, humidity, pressure, precipitation, and wind speed. If you'd like to collect more, just uncomment the reading you want to add from both lines 65-77 and lines 89-114.

<< Part 5: (Bonus) Configure Your Own Weather Alerts