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.
I THINK I’ve seen wttr.in before but forgot all about it – WELL DONE. Yes – just got the weather – had it and lost it ages ago… lovely.
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 🙂
wttr.in can also return the ASCII text weather data as a ‘png’ image file instead of actual text.
Examples for Hunstanton, Norfolk, UK
Current day + 2 days – text format
https://wttr.in/hunstanton
Current day + 2 days – png image
https://wttr.in/hunstanton.png
Current weather only – png image
https://wttr.in/hunstanton_0pq.png
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
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.
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:-)
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
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
}
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!
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.
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.
The OpneWeatherMap API will also give you a 5 day forecast if you want…
https://openweathermap.org/forecast5
the responses can get quite big though, so depending on how you’re calling the API you may need to specify a large enough buffer to hold the response.
There’s also a Node-Red contrib that eliminates the need for messing about with all that nasty JSON stuff!
https://flows.nodered.org/node/node-red-node-openweathermap
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.
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…