Weather Data using Node-Red and APIs

yr.no

With the forthcoming descent of Dark Skies into an Apple-Only universe (a sad loss for the rest of us) I’ve been looking into alternative APIs to use for grabbing weather forecasts and related data. While I struggle with the MET Office behemoth, here’s a little information on yr.no

I’d not heard of them until this week so if this is new to you, don’t worry. The yr.no API is free to use, getting results is easy using Node-Red and useful for obtaining weather information, now and in the future and as a bonus, sunrise and sunset times for any place. Unlike more comprehensive and involved systems, yr.no lets you access this information by place name only, not longitude and latitude – after all do you really need weather, sunrise, sunset, wind, air pressure etc down to your exact house or office? By PLACE I mean town. Of course you could always use the Node-Red Geonames nodes if you want to put in long/lat or something else.

Ok, so when you first look at this, depending on your experience with APIs and also Node-Red, this might look horrific or simple – I can’t narrow it down more than that. I was terrified for all of 30 minutes until I twigged.

No passwords or accounts are needed to access this information. Let’s take an example, my (summer) home town in the Spain. Punch this exact URL (Universal Resource Locator) into your web browser.

https://www.yr.no/place/Spain/Andalusia/Galera/forecast.xml

So what you will get out of that URL is a huge block of data in XML format not pretty).

I was interested in temperature NOW (you can have hourly forecasts into the future) along with wind speed, precipitation and for the sake of it, sunrise and sunset times. I also wanted to be able to easily separate the location from the URL, get the output in JSON format and for demo, put bits of it into a DEBUG node. Here’s my FLOW – for in this case my local town in the UK (note the embedded space in the location). When you see this flow in Node-Red, it’s a lot easier than it looks here.

[
{
"id": "eb15898.cc42378",
"type": "debug",
"z": "2cc9e9b.cf76b16",
"name": "Json Data",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "jsonata",
"x": 760,
"y": 260,
"wires": []
},
{
"id": "a8fe5bd9.9cbb68",
"type": "inject",
"z": "2cc9e9b.cf76b16",
"name": "Wark",
"topic": "",
"payload": "United%20Kingdom/England/Wark",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 180,
"wires": [
[
"d058e98b.288fc8"
]
]
},
{
"id": "336a67bc.abb528",
"type": "http request",
"z": "2cc9e9b.cf76b16",
"name": "req1",
"method": "GET",
"ret": "txt",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 550,
"y": 240,
"wires": [
[
"2b054761.f76838"
]
]
},
{
"id": "2b054761.f76838",
"type": "xml",
"z": "2cc9e9b.cf76b16",
"name": "xml1",
"property": "payload",
"attr": "",
"chr": "",
"x": 430,
"y": 300,
"wires": [
[
"6cf9c4e9.c0d64c"
]
]
},
{
"id": "6cf9c4e9.c0d64c",
"type": "function",
"z": "2cc9e9b.cf76b16",
"name": "BIts I need",
"func": "var bits=msg.payload.weatherdata.forecast[0].tabular[0].time[0];\nvar place=msg.payload.weatherdata.location[0].name[0];\nvar sun=msg.payload.weatherdata.sun[0].$;\nmsg.payload=place + \": \" + bits.temperature[0].$.value + \"c \" + \" Precip: \" + bits.precipitation[0].$.value + \" Wind: \" + bits.windSpeed[0].$.name + \" (Rise:\" + sun.rise + \" Set:\" + sun.set + \")\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 570,
"y": 300,
"wires": [
[
"eb15898.cc42378"
]
]
},
{
"id": "f60f5d63.f1eab",
"type": "change",
"z": "2cc9e9b.cf76b16",
"name": "",
"rules": [
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "payload",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 390,
"y": 240,
"wires": [
[
"336a67bc.abb528"
]
]
},
{
"id": "d058e98b.288fc8",
"type": "function",
"z": "2cc9e9b.cf76b16",
"name": "Merge URL and PLACE",
"func": "msg.payload= \"https://www.yr.no/place/\" + msg.payload + \"/forecast.xml\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 370,
"y": 180,
"wires": [
[
"f60f5d63.f1eab"
]
]
},
{
"id": "1178dba3.b15684",
"type": "inject",
"z": "2cc9e9b.cf76b16",
"name": "Galera",
"topic": "",
"payload": "Spain/Andalusia/Galera",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 220,
"wires": [
[
"d058e98b.288fc8"
]
]
},
{
"id": "443e8b8.5b6fe74",
"type": "inject",
"z": "2cc9e9b.cf76b16",
"name": "Baza",
"topic": "",
"payload": "Spain/Andalusia/Baza",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 260,
"wires": [
[
"d058e98b.288fc8"
]
]
},
{
"id": "55efd882.53d238",
"type": "inject",
"z": "2cc9e9b.cf76b16",
"name": "Vera",
"topic": "",
"payload": "Spain/Andalusia/Vera",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 300,
"wires": [
[
"d058e98b.288fc8"
]
]
}
]

For a test, click on the top left INJECT node… The one that says WARK… and view the result in the DEBUG node. I’ve used standard Node-Red nodes here including function nodes with a little coding to break up the (now Json) data into what I’m interested in).

Another way to get info out of yr.no

https://flows.nodered.org/flow/affa4bd5194a25019081d2ec8191fa48

and for pretty pictures.. a URL like this perhaps… EASY

https://www.yr.no/place/spain/Andalusia/Galera/meteogram.png

Facebooktwitterpinterestlinkedin

20 thoughts on “Weather Data using Node-Red and APIs

  1. Hmm. That looks interesting. I currently use Dark Skies, extracting cloud cover to modify the onand off times of my BigTimer controlled lights.

    1. Hi Tony – apparently you soon won’t be using Dark Sky – they are going over to “the Daak Side” ie selling out to Apple at the expense of everyone else so that’s about to be a dead end – hence the change.

  2. Morning Pete, thanks for sharing your flow and the blog on yr.no. I found it very interesting and the flow work a treat. Thus far looks like a good replacement for Dark Sky. Interestingly the other flow example you provided looks as if it uses Latitude / Longitude so the best of both worlds. Like you I think the local town, Bakewell, in my case is near enough.

    1. Bob, my only caviat on towns is foreign towns – Huescar in Southern Spain has a little symbol above the E which some might have trouble with… I did – never occurred to me – and it is the next town along to our Spanish town of Galera.

  3. Been using yr.no for forecasts for about 8 or 9 years now – really good and mostly accurate – will look at the api now too

    1. Thanks for the confirmation Phil, it was something of a shot in the dark which seems to work well. For my next trick I need to get my head around how to get the peak result for the day.. I suppose if I have it take a reading at 8am every morning for something like “6 hours from now” that’ll be near enough….

  4. The Dark Sky API will be available until the end of 2021, so if you are lucky enough to have a subscription there is plenty of time to compare API results with other sites. I have been using the API for about 3 years for local forecasts.

    Yr.no looks very promising and they also have a very nice Android app too which I’ve been using for a week, time will tell. I had a small problem with the app and their tech people got back to me within 4 hours, which is brilliant.

    Thanks for the flow Pete, it will speed things up enormously!

    For anyone without a Dark Sky API subscription the is (was) a Dark Sky Plugin for Grafana. [edit] The Plugin needs an API Key!!!

    1. Thinking about it Gary – I DO have a Dark Sky key – got it ages ago as I did some work on my Spanish NR pages for my hall display to show expected rain or sun. I don’t use it in the Northeast of England as it is almost always going to rain, but as subscribers from warmer parts of the world will know, having info in advance is handy if you’re planning the day out.. is it to be shopping and culture – or a day at the pool/lake 🙂

      I’ll fish out the Dark sky API for comparison.

      1. Hi Pete
        I just use the functions split and switch to get the various headings and then display either by text or gauges.
        Seem to work near enough for my location.
        Cliff

  5. Note that yr.no asks that you pull data no more than once each hour.
    OpenWeatherMap permits pulling data every 10 minutes for free accounts.

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.