Moving from Weather Underground to OpenWeatherMap

Since Weather Underground has announced the End of Service for their API by the end of 2018, I stopped uploading data from my weather station to Weather Underground and started to look for an alternative API in order to still being able to display the current weather conditions on my Pimoroni Scroll pHAT HD display and the current air pressure on my GUI for rtl_433, along with the received temperature and humidity.

With OpenWeatherMap, I found a good substitute for that. They also provide an API that can be used for free for a limited amount of calls. For the average hobbyist, the limitations of the API does not matter. Like for Weather Underground in the past, you also have to get an API-Key, before you will be able to use the API.

In the following picture, you can see a small Windows program (similar to my rtl_433-GUI) that I wrote for learning about the OpenWeatherMap-API. It displays the name of the city where the data is coming from, an icon that represents the current weather conditions and the values for temperature, humidity and air pressure. A complete description of the weather conditions and the weather icons used by OpenweatherMap can be found here.

Wetterstation_OWM

I will leave all the Weather Underground related articles here unchanged for reference, but additionally, I will provide a working example on how to use the Openweathermap-API in this article, so that you will be able to adapt the code that uses the Weather Underground-API, to now use the OpenWeatherMap-API.

Using the OpenWeatherMap-API is straightforward and very similar to using the one from Weather Underground. Anyway, there is one thing that I’ve noticed when testing the API. It sometimes delivers the air pressure using one of two different ways. I pay attention to this in my example code so that it should work, either if the API delivers the normalized pressure at sea level via ‘pressure’, or via ‘sea_level’. Below, you can see the code that I use within the program that I’ve shown above. Only thing you have to do before you can use the code, is to replace YOURCITYID with the id of the city you want to get the data for and YOURAPPID with your personal API-Key. How to determine the City-Id is described here.

import requests
import json

try:
    OWM_str = 'http://api.openweathermap.org/data/2.5/weather?id=YOURCITYID&appid=YOURAPPID'
    response = requests.get(OWM_str)
    x = response.json() 

    if x['cod'] != '404':
        y = x['main']
        temp = y['temp']
        temp_float = float(temp)
        temp_float = temp_float-273.15
        temp_float = round(temp_float, 1)
        temp = str(temp_float)
        humi = y['humidity']
        temp_humi = float(humi)
        temp_humi = round(temp_humi, 1)
        humi = str(temp_humi)
        if 'sea_level' not in y:
            press = y['pressure']
            press_float = float(press)
            press_float = round(press_float, 1)
            press = str(press_float)
        else:
            press = y['sea_level']
            press_float = float(press)
            press_float = round(press_float, 1)
            press = str(press_float)
        z = x['weather']
        icon = z[0]['icon']
        icon = icon + '.png'
        city = x['name']
    else:
        temp = "--.-"
        humi = "--.-"
        press = '---.-'
        icon = '-'
        city = '-'
except:
    temp = "--.-"
    humi = "--.-"
    press = "---.-"
    icon = '-'
    city = '-'