I had a chat with my pal Jonathan today about the weather – and more specifically getting it into Node-Red. If you are building your home control with Node-Red at the centre then it is always nice to have information about the local weather to send off to your various displays (such as the one described earlier today) and forecast.io is a dead easy way to get it!
Go to http://forecast.io/ and sign up - that will give you an API number. No, really it is that easy and as long as you don’t use it 1,000 times a day, it is free. Simples.
Here’s what you do:
Using an HTTP node (the yellow one) and fill it in as below substituting XXXXXXX for your API key – and the long/lat coordinates for your own (I got mine off the Google Maps command line when pointing at my village). If you’re not in the UK then you’ll have to try something else. US is probably a good start for our friends in the USA.
In the function (orange) you need this..
var weather = JSON.parse(msg.payload);
And that is that. NOW you need to know what is available in your new json object and remember – don’t use that timestamp inject (it is irrelevant what it is injecting – it’s just a trigger) more than 999 times a day
So if you look at the output from the incoming XML it is MASSIVE – but put into a JSON online viewer like jsonviewer.stack.hu once converted it starts to look more reasonable.
For example if we expand currently – we get…
And how do we access that? Simples – having called the object “weather” we get the temperature as weather.currently.temperature
and so on. In HOURLY we see timestamps and things like chances of precipitation… a little clever loop and… remember in an earlier blog we looked at showing graphs of history in Imperihome? It would not be beyond the bounds of reason to feed this lot into said graphs!!!
Well, even without digging any further, hopefully there are a few items of interest for you. weather.currently.summary is good as are weather.hourly.summary and weather.daily.summary.
If you want to check the weather you could look at this – but with your coordinates. . http://forecast.io/#/f/51.6571,-0.0295
And on the subject of weather but otherwise unrelated – this looks like a fun site… http://www.wunderground.com/
Oh, nearly forgot – and you’ll be needing some fancy icons.. http://www.wunderground.com/weather/api/d/docs?d=resources/icon-sets
Have fun. And if you know of something BETTER – leave a link in the comments.