Star

Weather reading using REST API calls

This tutorial will show how to get weather data using REST API.

Use case

Let’s assume your need to know the current weather in the asset location. You may use the weather info for certain data processing logic or just to track history and enable visualization of this info on the dashboard.

In this tutorial we will configure ThingsBoard Rule Engine to automatically get weather information using REST API. You can use this tutorial as a basis for more complex tasks.

Prerequisites

We assume you have completed the following guides and reviewed the articles listed below:

Adding the asset

Add Asset entity in ThingsBoard. Its name is Building A and its type is building.

image

Note:If you have Professional Edition, you will need to add asset to customer using Customer Hierarchy the following way:

image

Assigning asset to customer in Community edition

image

Registering on data-providing website

In order to get weather data you should register on a website which will provide it. In this case OpenWeatherMap will be used.

After signing up there go to this page to get your api key.

image

Creating attributes

To perform REST API call we need the following URL parameters: API key, longitude, latitude, and units of measurement.

We suggest adding an API key parameter to the customer server-side attribute and other parameters to the asset server-side attributes.

Customer attribute should look like this:

image

Add the attribute as following:

FieldData TypeInput Data
APPID String (an API key you got from OpenWeatherMap)

Asset attributes should look like this:

image

FieldData TypeInput Data
latitude Double latitude of an asset
longitude Double longitude of an asset
units String "metric" for meters per second wind speed and Celsius temperature, "imperial" for miles per hour wind speed and Fahrenheit temperature, empty for meters per second wind speed and Kelvin temperature

In this example the coordinates of New York City and metric units will be used.

Message flow

In this section, we explain the purpose of each node in this tutorial. There will be one rule chain involved:

The following screenshot show how the above Rule Chain should look like:

image

Download and import attached json file with a rule chain for this tutorial. Be aware that you need to set the asset you created in the beginning as an originator in the leftmost generator node.

The following section shows you how to create this rule chain from scratch.

Create new Rule Chain (Outside Temperature/Humidity)

Go to Rule Chains -> Add new Rule Chain

Configuration:

image

New Rule Chain is created. Press Edit button and configure Chain.

Adding the required nodes

In this rule chain, you will create 5 nodes as it will be explained in the following sections:

Node A: Generator node
FieldValue
Name Generate requests
Message count 0
Period in seconds 15
Originator type Asset
Asset Building A
Generate function return { msg: {}, metadata: {}, msgType: "POST_TELEMETRY_REQUEST" };

image

Node B: Customer attributes enrichment node
FieldValue
Name Get customer API key
Latest telemetry False
Source attribute APPID
Target attribute APPID

image

Node C: Originator attributes enrichment node
Node D: External REST API call node

image

Node E: Script transformation node
      var newMsg = {
          "outsideTemp": msg.main.temp,
          "outsideMaxTemp": msg.main.temp_max,
          "outsideMinTemp": msg.main.temp_min,
          "outsideHumidity": msg.main.humidity,
      };
      
      
      return {msg: newMsg, metadata: metadata, msgType: msgType};

image

Node F: Save timeseries node

image

Setting up dashboard

Download and import attached json file with a dashboard for this tutorial.

The dashboard should look like this: image

Next steps