Skip to main content

OpenWeather

Weather information can be used for analysis and predication purposes or to control devices. OpenWeather is a free weather service which provides current weather and forecast for all locations. This integration used the standard importer in combination with a JavaScript class for retrieving and parsing the weather information. The weather information also includes the sunrise and sunset for the date and location. This can be used to control lights.

Script

Create a new script with a get function. The get function loads the secret defined in the importer, which includes the APPID. The script makes an HTTP request to the OpenWeather API and then returns the results as an object. The parameter object is the name of the location, such as Paris or Rome. Note that the object also contains a field object with the value of the parameter object, in this case the geographical location for which the weather was requested.

const get = async (object, context) => {
const url = 'https://api.openweathermap.org/data/2.5/weather?q=' + object + "&APPID=" + context.APPID;
let response = await http.get(url)
let data = response
let weather = {}
if (data) {
weather.temperature = Math.round(data.main.temp - 272.15)
weather.windchill = Math.round(data.main.feels_like - 272.15)
weather.pressure = data.main.pressure
weather.humidity = data.main.humidity
weather.wind = data.wind
weather.wind_speed = data.wind.speed
weather.wind_direction = data.wind.deg
weather.clouds = data.clouds.all
weather.sunrise = new Date(data.sys.sunrise * 1000)
weather.sunset = new Date(data.sys.sunset * 1000)
weather.visibility = data.visibility
}
return weather
}

Datasource

OpenWeather is a free service but they require you to register and obtain a key. Open a browser to https://openweathermap.org/api and subscibe to the free service and request an API key. Create a new secret with fields for type, script and APPID. This key needs to be included in every request to the API and is stored in a secret.

Connector

Create a new connector and enter the name of the secret in the data source field. Optionally specify a schedule for running the importer. Click on Object in the menu to create one or more objects that represent the locations for which you need the weather info. If no key mapping is defined all fields are automatically included with their original name.

Form

Create a new form with the following definition. Note that the wind control is displayed as a combination of wind speed and wind direction. The sunrise and sunset values are imported as date objects and displayed as time using a JavaScript function toLocaleTimeString.

Weather

Temperature
<temperature
[-40|-5:purple,-5|5:blue,5|18:magenta,18|25:green,25|30:orange,30|40:red C]
|4

Wind chill
<windchill
[-40|-5:purple,-5|5:blue,5|18:magenta,18|25:green,25|25:orange,30|40:red C]
|4

Clouds
<clouds
[0|20:blue,20|40:yellow,40|80:green,80|100:grey %]
|4

Pressure
<pressure
[0000 hPa]
|4

Humidity
<humidity*20
[00]
|4

Visibility
<visibility
[0000 m]
|4

Wind
<"" + Math.round(wind_speed*3.6) + '/' + wind_direction
[000]
|4

Sun rise
<new Date(sunrise).toLocaleTimeString()
|4

Sun set
<new Date(sunset).toLocaleTimeString()
|4