How to use Weather Company Data and Netatmo with Ruby
Tags: ruby, ibm, netatmo, weather
Published: 2019-03-15 17:34:27

---
title: How to use Weather Company Data and Netatmo with Ruby
published: true
tags: Ruby,IBM,Netatmo,weather
canonical_url: https://tmarkiewicz.com/how-to-use-weather-company-data-and-netatmo-with-ruby/
---

While weather insights are critical to many businesses, local weather data on a more personal and practical level can be important as well. Since I recently moved to an area with some pretty extreme weather, I find myself monitoring and measuring the local conditions much more than I used to. And with wind gusts that regularly exceed 50mph in the winter, this isn’t just an academic interest. Safety (both personal and property) is a significant concern.

In addition to the wind, I’m a skier, so I’m always interested in the amount of snow in the forecast. For some reason, this isn’t often displayed in most popular weather apps at a glance, so I was interested in building something simple to track my local weather as well as the forecast tailored to my interests.

To monitor the weather, a recent addition to my home was the [Netatmo Smart Home Weather Station](https://www.netatmo.com/en-us/weather) with a [wind gauge module](https://www.netatmo.com/en-us/weather/weatherstation/accessories#windgauge). While Netatmo provides an app and web dashboard to view all your data, it’s a bit much at times, especially when there are only a couple measurements I’m interested in monitoring. You can see a demo app of their dashboard [here](https://my.netatmo.com/app/station?demomode=true). Additionally, they offer excellent integration with [Alexa](https://www.netatmo.com/en-us/helpcenter/weather/1/how-do-i-set-up-amazon-alexa-to-control-netatmo-products/331) and [IFTTT](https://ifttt.com/services/netatmo) so that you can incorporate your weather monitoring into a home automation system.

![Netatmo Dashboard](https://tmarkiewicz.com/wp-content/uploads/2019/03/netatmo-dashboard-1024x653.png)

As my neighbor regularly texts me to find out what the station is measuring for the wind gusts on particularly bad evenings (hey, get your own!), I thought it would be interesting to explore my recorded data using the [Netatmo API](https://dev.netatmo.com/en-US/resources/technical/reference/weatherapi) as well as examining at how it corresponds to an accurate weather API with a forecast.

There are numerous popular weather data APIs available for free or low cost, but since IBM owns the [Weather Company](https://www.ibm.com/weather), I figured this would be a logical place to start and be an interesting way to experiment with their weather data.

IBM bought the Weather Company in 2016 and offers its weather data as an API available through the IBM Cloud. From IBM:

> The Weather Company, an IBM Business, delivers personalized, actionable insights to consumers and businesses across the globe by combining the world’s most accurate weather data with industry-leading AI, Internet of Things (IoT) and analytics technologies.
> 
> Our solutions provide newscasters, pilots, energy traders, insurance agents, state employees, retail managers and more with insight into weather’s impact on their businesses, helping them make smarter decisions to improve safety, reduce costs and drive revenue.

The [Weather Company API](https://twcservice.mybluemix.net/rest-api/) provides a ton of data including an hourly forecast, daily forecast, intraday forecast, current conditions, historical data, weather alerts, location services, and almanac services.

![IBM Weather Company Data](https://tmarkiewicz.com/wp-content/uploads/2019/03/IBM-Weather-Company.jpg)

Here, I’ll only be using the 3-day forecast, but the API offers a daily forecast for each of the next 3, 5, 7, or 10 days starting from the current day, including forecasts for the daytime and nighttime segments.

As far as [pricing](https://cloud.ibm.com/catalog/services/weather-company-data), on a free plan you get 10 API calls per minute and then 10,000 calls total per account. So the free plan isn’t free forever, and once you hit the 10,000 call limit, you’ll have to start paying. But this is more than enough for testing, prototypes, and hobby projects.

For this article, I’ll be illustrating how to use [Ruby](https://www.ruby-lang.org/en/) to make access and parse the data from both APIs. In a future article, I’ll examine building a basic dashboard to monitor the data you’re interested in on the web (and in my case stop my neighbor from asking me!).

To follow along with the entire demo though, you’ll need an actual Netatmo device and account as they don’t offer any [demo keys, products, or a sandbox](https://dev.netatmo.com/resources/technical/support/helpcenter).

The full tutorial walkthrough and repo can be found on Github here: [https://github.com/tmarkiewicz/weather-ruby](https://github.com/tmarkiewicz/weather-ruby) and I’ll highlight some of the key steps below.

### Initial setup

As you can see from the tutorial repo, I’m using Ruby on a Mac. OS X comes installed with Ruby, but you can check your version using this command:

```
ruby -v
```

If you want to upgrade (or install multiple versions of Ruby), I recommend using the [Ruby Version Manager (RVM)](https://rvm.io/).

The next step is to clone the repo from Github and then run the bundler:

```
$ git clone https://github.com/tmarkiewicz/weather-ruby
$ cd weather-ruby

```

Then you’ll need to install the required Ruby gems using the [Bundler](https://bundler.io/) Ruby gem:

```
$ gem install bundler
$ bundle install

```

The tutorial code samples in Ruby can be run from the command line via this syntax:

```
$ ruby filename.rb
```

The next two sections outline how to use the tutorial for both Weather Company Data and then Netatmo.

### Weather Company

To access the Weather Company Data API, you’ll need an account on the IBM Cloud. My [tutorial](https://github.com/tmarkiewicz/weather-ruby) outlines the process in detail, but the necessary steps are:

- sign up (or sign in) for the [IBM Cloud](https://cloud.ibm.com)
- add the [Weather Company Data](https://cloud.ibm.com/catalog/services/weather-company-data) as a new service
- select the free plan
- select add credentials, then view the credentials’ JSON
- add these credentials to a .env file in the app directory

As a side note, there’s a great guide to using environment variables in Ruby [here](https://blog.honeybadger.io/ruby-guide-environment-variables/). In the tutorial, I also use this Ruby gem: [https://github.com/bkeepers/dotenv](https://github.com/bkeepers/dotenv)

The next step is to make a test API call. I like to always start with curl or the [Postman](https://www.getpostman.com/) app to test any API credentials, the construction of the API endpoint, and of course the results:

![Postman with Weather Company Data API](https://tmarkiewicz.com/wp-content/uploads/2019/03/postman-weather-company-data-api-1024x653.png)From the [Weather Company Data API docs](https://twcservice.mybluemix.net/rest-api/), we can build the URL to make a request to retrieve the 3-day forecast for a specific latitude and longitude like this:

`https://twcservice.mybluemix.net/api/weather/v1/geocode/40.014984/-105.270546/forecast/daily/3day.json`

In the URL above, replace the latitude/longitude with those of the location you’d like to retrieve weather data. An excellent site to find your latitude and longitude can be found at [LatLong.net](https://www.latlong.net/).

Now that we have all the requirements, we can run the main file from the command line:

```
$ ruby weather-company.rb
```

And the output will look like this:

```
Forecast for Friday, February 22, 2019:
Periods of snow. Lows overnight in the upper teens.
1 to 3 inches of snow expected.
Snow this evening will give way to lingering snow showers late. Low 19F. Winds light and variable. Chance of snow 80%. Snow accumulating 1 to 3 inches.

```

### Netatmo

As I mentioned above, this part of the tutorial is only available if you already have one or more Netatmo devices. If you’re looking at a full featured weather station with API access, I highly recommend their system as it’s pretty affordable for all the features provided.

![Netatmo Weather Station and iOS app](https://tmarkiewicz.com/wp-content/uploads/2019/03/Weather_Station_iPhone_6-FR_WEB-1-1024x691.jpg)<figcaption>Photo via Netatmo: Masaki Okumura, Marion Leflour, and Fred Raynaud</figcaption>

That said, you can still follow along with the remainder of this demo and if/when you get a Netatmo device you’ll know where to go for some step-by-step instructions.

One caveat on the data from Netatmo devices: they unfortunately only send measurements to the servers [every ten minutes](https://dev.netatmo.com/resources/technical/guides/ratelimits). From the Netatmo API docs:

> Do not try to pull data every minute. Netatmo Weather Station sends its measures to the server every ten minutes. Get the last measures from all the devices of one user with the Getstationsdata method.

So while I’d much rather have some data much more frequently (wind speed and gusts come to mind), we have to work with what the company gives us in this case. Inconvenient for building a weather app, but not a dealbreaker.

As with the Weather Company section, the full steps for setting up the Netatmo API are in the Github repo, but the basic steps are as follows:

- if you already haven’t, create a user account with Netatmo
- sign up for a developer account
- create a new application
- copy the credentials for the new app

![Netatmo Connect developer dashboard](https://tmarkiewicz.com/wp-content/uploads/2019/03/netatmo-connect-dashboard-3-1024x653.png)

Once you’ve added the client id and client secret credentials to the .env file, you can then run the Netatmo file from the command line:

```
$ ruby netatmo.rb

```

The output will look like this:

```
Time: 2019-03-12 14:19:50 -0600
Temperature: 50 deg F
Humidity: 45%

```

And that’s it! A quick and easy to way get access to local weather data and forecasts.

### Summary

While this is an introductory tutorial on to how to access weather data via the Weather Company and Netatmo, it’s not accessible outside running locally on your machine. In a future article, I’ll show how to quickly take this data and deploy it to an actual application. Once that’s done, my neighbor will be happy and possibly stop texting me for wind updates!

And if you're interested in using weather data with a mobile app, take a look at this tutorial on how to [create a mobile app with weather forecasting](https://ibm.biz/BdzFna). Additionally, take a look at using a [serverless approach to Weather Underground](https://ibm.biz/BdzFnL) data.