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:

  • Getting Started guide.
  • Rule Engine Overview.
  • External rule nodes.

    Adding the asset

    Add Asset entity in ThingsBoard. Its name is Building A and its type is building.
    Weather reading using REST API calls - 图1
    Note:If you have Professional Edition, you will need to add asset to customer using Customer Hierarchy the following way:

  • Go to Customers Hierarchy -> All -> (Current tenant) -> Customer groups -> (Your customer group) -> (Your customer) -> Asset groups -> (Your asset group) -> Add asset

Weather reading using REST API calls - 图2

Assigning asset to customer in Community edition

  • Go to Assets -> Assign to customer -> (Your Customer) -> Assign

Weather reading using REST API calls - 图3

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.
Weather reading using REST API calls - 图4

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:

  • Go to (Assigned customer) -> Attributes -> Add

Weather reading using REST API calls - 图5
Add the attribute as following:

Field Data Type Input Data
APPID String (an API key you got from OpenWeatherMap)

Asset attributes should look like this:

  • Go to Building A -> Attributes -> Add

Weather reading using REST API calls - 图6

  • Fill in the attributes with the input data shown in the following table: | Field | Data Type | Input 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:

  • Outside Temperature/Humidity - rule chain sends API calls to OpenWeatherMap every 15 seconds and sends data about humidity and temperature to a chosen asset.

The following screenshot show how the above Rule Chain should look like:
Weather reading using REST API calls - 图7
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:

  • Name : Outside Temperature/Humidity

Weather reading using REST API calls - 图8
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
  • Add the Generator node. This rule node Generates empty messages to trigger REST API calls.
  • Fill its fields the following way: | Field | Value | | :—- | :—- | | 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” }; |

Weather reading using REST API calls - 图9

Node B: Customer attributes enrichment node
  • Add the Customer attributes node and connect it to a Generator node with a relation type Success. This node will put customer attribute APPID into the metadata of message.
  • Fill its fields the following way: | Field | Value | | :—- | :—- | | Name | Get customer API key | | Latest telemetry | False | | Source attribute | APPID | | Target attribute | APPID |

Weather reading using REST API calls - 图10

Node C: Originator attributes enrichment node
  • Add the Originator attributes enrichment node and connect it to Customer attributes node node with a relation type Success. This node will fetch server attributes latitude, longitude and units of the originator set up in a Generator node into metadata
  • Fill its fields the following way: | Field | Value | | :—- | :—- | | Name | Latitude/Longitude | | Server attributes | latitude, longitude, units |

  • Weather reading using REST API calls - 图11

    Node D: External REST API call node
  • Add the External REST API call node and connect it to Originator attributes enrichment node with a relation type Success. This node will perform REST API calls to OpenWeatherMap.

  • Fill its fields the following way: | Field | Value | | :—- | :—- | | Name | Get Weather Data | | Endpoint URL pattern | http://api.openweathermap.org/data/2.5/weather?lat=${ss_latitude}&lon=${ss_longitude}&units=${ss_units}&APPID=${APPID} | | Request method | GET | | Use simple client HTTP factory | False |

  • ss_latitude, ss_longitude, ss_units, ss_APPID are server attributes fetched from metadata which were put there by Originator attributes enrichment node

Weather reading using REST API calls - 图12

Node E: Script transformation node
  • Add the Script transformation node and connect it to External REST API call node with a relation type Success. This node will put outside temperature, maximal temperature, minimal temperature and humidity into the message.
  • Fill Transform function the following way: ``` var newMsg = {
    1. "outsideTemp": msg.main.temp,
    2. "outsideMaxTemp": msg.main.temp_max,
    3. "outsideMinTemp": msg.main.temp_min,
    4. "outsideHumidity": msg.main.humidity,
    5. };
  1. return {msg: newMsg, metadata: metadata, msgType: msgType};

``` Weather reading using REST API calls - 图13

Node F: Save timeseries node
  • Add the Script transformation node and connect it to External REST API call node with a relation type Success. This node will put message into telemetry.

Weather reading using REST API calls - 图14

Setting up dashboard

Download and import attached json file with a dashboard for this tutorial.
The dashboard should look like this: Weather reading using REST API calls - 图15