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.

Facebooktwitterpinterestlinkedin

4 thoughts on “Weather APIs and OpenWeatherMap

  1. 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…

    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.

  2. 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":[]}]
    

Leave a Reply

Your email address will not be published. Required fields are marked *

Leave the field below empty!

This site uses Akismet to reduce spam. Learn how your comment data is processed.