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

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

  1. Hello Pete
    I feed directly from yr into ui template pdf file with meteogram + 9 day forecast
    inject timestamp when need to update
    In ui template
    see attached pics for result

    regards Brian

  2. Hi Peter,
    I have been using the XML data from YR.no for a few years, but this is soon becoming obsolete as they have just launched a whole new api which provides all their weather data directly in JSON format!

    I have already implemented a few node-red flows using the new api and it is far simpler to work with than the old XML data.

    You can read about it here:
    https://developer.yr.no/doc/

    Thanks for your work on this. Hopefully there will be some new node-red nodes coming out soon which make use of their new api.

  3. Hello Peter,
    i’m building a meteo station based on Node-red, ESP32, BM280 and an old retired wind vane that used to be on a sailboat mast.
    Everything works fine but i don’t like the wind direction chart (cf picture)when around north winds. I would like a “modulo” Y-axis where for exemple 350° and 10° would be close. Y-axis should adapt to show for example -10, +10 when wind varies between 350 and 10 °. Do you know if that kind of chart exists or can be developped ?
    sorry if this question is not in the good thread …
    regards
    Jérôme

    1. Hi,
      Awesome dashboard, I wonder to know the hardware needed to attach your wind vane.
      I build a weather forecast API in Node.js and think that the best way to display wind directions are:
      – little arrows
      – abrevs like NNE for example
      So my opinion is that the simple line chart could look more like a meteogram (see above).

      I also understand that a sailor or a sail boat needs to avoid and track no-sail zones. In this case I would design an interactive chart to track the desired direction vs actual wind directions.
      Sample formula: Y = f(x); f(x): delta directions
      ABS(d-a) – 360*(ABS(d-a)>180)

  4. 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.

      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

      2. Node red like you. I only strip out the current temperature to send to an emoncms dashboard.

        Simon

  5. 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.

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

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

  8. 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.

  9. Huh,I had never heard the term “meteogram” before but that’s a really nice way to show the forecast.

Comments are closed.