Weather APIs and OpenWeatherMap

I’ve had a morbid fear of using API calls since I first heard the term years ago, just before I developed a morbid fear of JSON. Well, the latter didn’t last too long but JSON responses from websites can get awfully complicated, awfully quickly.

And that’s how tonight I ended up at the OpenWeatherMap site. It turns out I signed up for a free account a long time ago and forgot all about it – basically you go to their site – sign up with name, email address and a password and they give you an API KEY (or jumble of letters and numbers).

So – to check out their weather API today I put in their example link, added units=metric , my town and my api key.

Simple enough – and what comes back is simple enough, too. https://api.openweathermap.org/data/2.5/weather?units=metric&q=galera&appid={my_key}

Dead easy start. As for the Openweathermap icons – https://github.com/CodeExplainedRepo/Weather-App-JavaScript – it’s a short list of png files.

{
coord: {
lon: -2.5518,
lat: 37.7426
},
weather: [
{
id: 804,
main: "Clouds",
description: "overcast clouds",
icon: "04n"
}
],
base: "stations",
main: {
temp: 18.5,
feels_like: 17.68,
temp_min: 17.44,
temp_max: 21.15,
pressure: 1015,
humidity: 49,
sea_level: 1015,
grnd_level: 919
},
visibility: 10000,
wind: {
speed: 0.77,
deg: 235,
gust: 0.84
},
clouds: {
all: 90
},
dt: 1634060515,
sys: {
type: 2,
id: 2004018,
country: "ES",
sunrise: 1634019333,
sunset: 1634060247
},
timezone: 7200,
id: 2517432,
name: "Galera",
cod: 200
}

If you’re as happy (or not) with JSON as I am, this should appeal unless you want lots of info. It simply tells me today’s weather…. but what about tomorrow as it is now 9pm – that would be nice. Later you’ll see I’m looking at a 3-hourly medium-term forecast version but that gets a little more involved.

14 thoughts on “Weather APIs and OpenWeatherMap

    1. Well, it is happy with our next town HUESCAR but does not like GALERA – see the top right box.. oh dear.. (Windows 10 Chrome browser) – still – good idea 🙂

  1. Reply #2 – sorry, forgot about this nifty little (totally free) weather feed: –

    http://wttr.in/

    This is really geeky, it returns an ASCII art weather forecast with various options for choosing the format of the output.

    You can ‘curl’ or ‘wget’ it from a Linux command line and get a fully text-based weather forecast including ASCII art symbols for cloudy/sunny/snow etc.

    You could curl the URL (with your location etc added to the query string) and then parse the output to extract individual fields. The layout/output has remained pretty consistent and unchanged throughout the time I’ve been using it (on/off over the last 4 years).

    https://wttr.in/:help <— that's the URL for the documentation

  2. Hi Pete,

    For JSON parsing in Linux/Raspberry Pi etc, I use a popular downloadable command line tool called “jq”. It is brilliant – well, it is for me as I have a lot of ‘bash’ scripts on my Pi/Linux systems so piping the (JSON) output from another system to ‘jq’ with a few options added to the end makes parsing JSON so easy once you get the hang of how ‘jq’ works.

    https://stedolan.github.io/jq/

    “jq” is very popular and has a lot of users out there so it’s a decent command line tool.

    For weather data/API … I’ve used “weatherbit” in the past. I used their free trial for quite some time for customer demos at work (software dev / REST interfaces / APIs…)

    https://www.weatherbit.io/

    Hope some/all of the above is useful.

    1. I LOVE getting command line tools I’ve somehow missed- just spent the last 15 minutes playing with JQ – thanks for that…

      Got it to read a compressed JSON file and colourize the output… not yet grasped how to input a JSON string instead of a file bt give me a few more minutes:-)

      1. Here’s a simplified/hard-coded example from a Linux bash script I use to query my Philips Hue lighting using their standard API which allows LAN based communication to your Hue Hub to query and/or control Philips Hue devices.

        The Hue API uses JSON, hence it is a good example of how to use ‘curl’ from a bash script or command line and pipe the output into ‘jq’ for parsing before outputting to the screen and/or standard output

        url=”https://192.168.0.109/api/abcdefg12345HueAPIkey/lights/lamp1/ |jq .’state.bri'”
        command=”curl -X GET –insecure –silent $url”
        response=`eval $command`
        echo $response

        1. Completely hardcoded command line example without ‘bash’ script variables – just CURL: –

          url -X GET –insecure –silent “https://192.168.0.109/api/zzzzzzAPIzKEYzzzzz/lights/19/” |jq .
          {
          “state”: {
          “on”: false,
          “bri”: 13,
          “ct”: 443,
          “alert”: “select”,
          “colormode”: “ct”,
          “mode”: “homeautomation”,
          “reachable”: true
          },
          “swupdate”: {
          “state”: “noupdates”,
          “lastinstall”: “2021-08-13T07:29:47”
          },
          “type”: “Color temperature light”,
          “name”: “Landing Light”,
          “modelid”: “LTW010”,
          “manufacturername”: “Signify Netherlands B.V.”,
          “productname”: “Hue ambiance lamp”,
          “capabilities”: {
          “certified”: true,
          “control”: {
          “mindimlevel”: 1000,
          “maxlumen”: 806,
          “ct”: {
          “min”: 153,
          “max”: 454
          }
          },
          “streaming”: {
          “renderer”: false,
          “proxy”: false
          }
          },
          “config”: {
          “archetype”: “pendantround”,
          “function”: “functional”,
          “direction”: “omnidirectional”,
          “startup”: {
          “mode”: “safety”,
          “configured”: true
          }
          },
          “uniqueid”: “00:17:88:01:04:26:5b:ac-0b”,
          “swversion”: “1.88.1”,
          “swconfigid”: “5C7DF970”,
          “productid”: “Philips-LTW010-1-A19CTv2”
          }

          Let’s use ‘jq’ to just return the “state” information …

          curl -X GET –insecure –silent “https://192.168.0.109/api/zzzzzzAPIzKEYzzzzz/lights/19/” |jq .state
          {
          “on”: false,
          “bri”: 13,
          “ct”: 443,
          “alert”: “select”,
          “colormode”: “ct”,
          “mode”: “homeautomation”,
          “reachable”: true
          }

  3. Hi from Crete,

    I’ve been using APIs called from Node-red for a couple of years, but two in particular may be of interest to you. 1. The pound-euro rate, updated hourly, this one from freecurrconv: https://free.currconv.com/api/v7/convert?apiKey=xxxxxxxxxxxxx&q=GBP_EUR&compact=y , you have to request a free api key. We have Transferwise accounts which allow you to flip money from £ to € instantly, and are always looking for good rates. 2. Earthquake feed from EMSC – possibly more use at this end of the Med, we’ve had three 6+ quakes in the last two weeks, don’t know how active Spain is. EMSC-Sem.org. Keep up the good work!

    1. Hi Chris – thanks for that – when I’m done with this week’s every-expanding mail bag – I’ll take a look at these. Spain (if you exclude La Palma which is of course having a TERRIBLE time) – I’m not aware ofany significant activity on the mainland right now – but you know, having a feed like that is probably worthwhile. I’ll come back to this.

  4. I use Node-red for viewing JSON. The debug side bar shows the hierarchy and makes it easy to explore.
    I first used the OpenWeatherMap node, but it doesn’t do much for you that you can’t do with standard nodes so I ditched it.
    Documentation for the one-call API – https://openweathermap.org/api/one-call-api
    Here’s a flow that will retrieve the one-call API data. Just edit the Change node with your lat, lot, ID, units, etc.

    [{"id":"475b4b6806369bed","type":"tab","label":"OWM call","disabled":false,"info":""},{"id":"083e6ab997dee7c0","type":"comment","z":"475b4b6806369bed","name":"OpenWeatherMap One Call API for Weather and Forecast","info":"","x":270,"y":60,"wires":[]},{"id":"c582083b781b7c0e","type":"http request","z":"475b4b6806369bed","name":"Get OWM data","method":"GET","ret":"obj","paytoqs":true,"url":"https://api.openweathermap.org/data/2.5/onecall","tls":"","persist":false,"proxy":"","authType":"","x":600,"y":120,"wires":[["d434a4d4c53da03b"]]},{"id":"81c5eaf78d9582c7","type":"inject","z":"475b4b6806369bed","name":"Trigger","repeat":"600","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":120,"y":120,"wires":[["34c9f3eb94779981"]]},{"id":"34c9f3eb94779981","type":"change","z":"475b4b6806369bed","name":"Set location, appid, units","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.lat","pt":"msg","to":"40.30","tot":"str"},{"t":"set","p":"payload.lon","pt":"msg","to":"-111.69","tot":"str"},{"t":"set","p":"payload.appid","pt":"msg","to":"","tot":"str"},{"t":"set","p":"payload.units","pt":"msg","to":"imperial","tot":"str"},{"t":"set","p":"units","pt":"flow","to":"imperial","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["c582083b781b7c0e","0a16b901039d8bb0"]]},{"id":"d434a4d4c53da03b","type":"debug","z":"475b4b6806369bed","name":"OWM dump","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":120,"wires":[]},{"id":"0a16b901039d8bb0","type":"debug","z":"475b4b6806369bed","name":"query","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":590,"y":60,"wires":[]}]
    
    1. To be honest I’ve started to get used to json – once it is pretyy-printed it’s rarely as bad as it first looks. Going back to C, I hate it’s inability to handle json directly. I’ve kind of gotten used to programming NR functions 🙂 Inability of C to handle string SWITCHES is another pain.

      I’ll check out that 5-day forecast – missed that somehow.

  5. Pete,
    There is a separate api call for forecast.
    I think it is included in the free version, a bit more data to parse though.

    api.openweathermap.org/data/2.5/forecast?q={city name}&appid={API key}

    -micko

    BTW I like apis…

Comments are closed.