Repository navigation
Tutorial: Open Weather API using HTTP Agent
This tutorial explains how to connect to the Open Weather Map API using the Http Agent and is based on the basic environment described in the quick start.
- A running Open Remote instance (this tutorial assumes
https://localhost) - A free API key for the Open Weather Map API
- Login to the manager UI (
https://localhost/admin/secret) - Navigate to the Assets page and click the
+at the top of the Asset list on the left to add an Agent or Asset. - In the dialog do the following:
- Name:
HTTP API Agent - Select the agent from the list:
HTTP Agent - Confirm with
Add
- Name:
- The agent is now created with pre-configured attributes. We will set some of those to establish the connection:
- Base URI:
https://api.openweathermap.org/data/2.5/(Don't forget to send the value by clicking the send button on the right or pressing Enter) - Request query parameters:
{"appid": ["YOUR_API_KEY"]}(Input the API key from you openweathermap account)
- Base URI:
You now have a basic HTTP API protocol ready to be linked to by asset attributes.
In the next step we will need the Asset ID of this agent, so copy that from the URL now. e.g. 501p87wK1bhf6Dh2M5ZQZj if URL is: https://localhost/main/#!assets/false/501p87wK1bhf6Dh2M5ZQZj
Now we will create a weather asset where we show the temperature and humidity in Rotterdam that we collect using the OpenWeatherMap API.
- Click the
+to add an asset:- Name:
Weather Rotterdam - Select the asset type from the list:
Weather Asset - Confirm with
AddThe weather asset will now appear in the list as a child of theHTTP API Agent. You can change its parent if you wish.
- Name:
- Go to the edit mode by clicking the toggle at the top of the asset page. In the edit mode you can modify the attributes of an asset and set configuration items.
- First we will set up the humidity value:
- Expand the
humidityattribute - Click
Add configuration itemand select and addAgent link. - Paste the following in the text area and replace the
idwith the agentidfrom above:
- Expand the
{
"type": "HTTPAgent.HTTPAgentLink",
"id": "501p87wK1bhf6Dh2M5ZQZj",
"queryParameters": {
"q": [
"Rotterdam,nl"
],
"units": [
"metric"
]
},
"pollingMillis": 60000,
"path": "weather",
"valueFilters": [
{
"type": "jsonPath",
"path": "$.main.humidity",
"returnFirst": true,
"returnLast": false
}
]
}
What this means is that, we are looking for the data of the city of Rotterdam, in metric units. We want this information every minute. The information can be found at the base URI + /weather. Then the returned data (JSON payload) is filtered to find the humidity value.
- We do the same for the temperature value:
- Expand the
temperatureattribute - Click
Add configuration itemand select and addAgent link. - Paste the following in the text area and replace the
idwith the agentidfrom above
- Expand the
{
"type": "HTTPAgent.HTTPAgentLink",
"id": "501p87wK1bhf6Dh2M5ZQZj",
"queryParameters": {
"q": [
"Rotterdam,nl"
],
"units": [
"metric"
]
},
"pollingMillis": 60000,
"path": "weather",
"valueFilters": [
{
"type": "jsonPath",
"path": "$.main.temp",
"returnFirst": true,
"returnLast": false
}
]
}
- Save the asset (top right)
Switch to view mode (by clicking the edit mode toggle at the top again) to view the live values. You now have the live weather data of Rotterdam linked.
As you may have noticed we do two calls in the above example, one to get the humidity and one to get the temperature. You can imagine that when collecting all the attributes this way, and for several assets, will greatly increase the number of calls you make. This can be optimized by doing one call and extracting all the values you need and pushing them to the attributes of your assets. We will recreate the example above using this method.
- First lets remove the agent links from the
Weatherasset attributestemperatureandhumidity.- Go to the edit mode in the
Weatherasset - Expand
humidity - Click the 'X' on the right of the
Agent linkconfiguration item
- Go to the edit mode in the
- Do the same for the temperature asset.
- Now we create a custom attribute that will hold the weather data we collect so that we can push the values from there. You could also do this from the
HTTP API Agentasset or any other asset if that makes more sense to you.- Click
Add attributeat the bottom of the list of attributes. - Select custom, select the attribute type
JSON objectand give it a name:weatherData. ClickAdd - Add the configuration item
Agent link,Attribute linkandRead onlyby selecting them in the add configuration item dialog and clickingAdd.
- Click
- In the
Agent linkconfiguration item add the following, again using the ID from your weather agent:
{
"type": "HTTPAgent.HTTPAgentLink",
"id": "501p87wK1bhf6Dh2M5ZQZj",
"queryParameters": {
"q": [
"Rotterdam,nl"
],
"units": [
"metric"
]
},
"pollingMillis": 60000,
"path": "weather"
}
- In the
attribute linkconfiguration item add the following, using the ID copied from this assets URL (the asset that has the attributes you want to push the data to):
[
{
"ref": {
"id": "THE_ASSET_ID_YOU_COPY_FROM_ITS_URL",
"name": "temperature"
},
"filters": [
{
"type": "jsonPath",
"path": "$.main.temp",
"returnFirst": true,
"returnLast": false
}
]
},
{
"ref": {
"id": "THE_ASSET_ID_YOU_COPY_FROM_ITS_URL",
"name": "humidity"
},
"filters": [
{
"type": "jsonPath",
"path": "$.main.humidity",
"returnFirst": true,
"returnLast": false
}
]
}
]
- Click read only to set it to true.
- Save the asset (top right)
With the attribute link configuration item you filter the values collected with the agent link and push them to the attributes in your specified asset. When you go to view mode you will see the weather data, temperature, and humidity value in their respective attributes.
Try and create additional attributes that link to the OpenWeatherMap API, some ideas:
- Get temperature in Fahrenheit
- Get windspeed (a place to get started with this: https://openweathermap.org/current)
Source · Documentation · Forum · Issues · Docker Images · OpenRemote Inc.