Openweather api
Hello
I am implementing the Openweather API in a wiser for KNX.
For now the current time I get it perfectly and I can save the data in virtual objects.
This is the script.
This is the result on the current variable
The problem is with weather_descriptions, I see that it is a table and I assign it to an object like the others, if I log it, the contents of the table appear to me, the object is updated but it does not show anything. It is defined as the 14 byte DPT ASCII string. How do I get that data from that table inside another table and assign it to the object?
Also I would like to know if it is possible to use in a conditional loop if a range of numbers, for example that it only be executed if it is between 8 and 10. Since I don't know how to do it I have evaluated each value independently at the end of the script. I do this so that the UV index is displayed in text instead of a number.
a greeting
Thank you very much
Hello
I am implementing the Openweather API in a wiser for KNX.
For now the current time I get it perfectly and I can save the data in virtual objects.
This is the script.
Code:
require('json')
http = require('socket.http')
escape = require('socket.url').escape
key = 'xxxxxxxxxxxxxxxxxxx'
location = 'city'
url = 'http://api.weatherstack.com/current?access_key=%s&query=%s&lang=es'
url = string.format(url, key, escape(location))
res = http.request(url)
data = json.pdecode(res)
if type(data) ~= 'table' then
alert('failed to load weather data')
return
end
if data.error then
log('error', data.error)
return
end
current = data.current
log(current)
-- temperature
grp.write('32/2/10', current.temperature)
-- Dirección del viento
grp.write('32/2/11', current.wind_dir)
-- Velocidad del viento
grp.write('32/2/12', current.wind_speed)
-- Humedad
grp.write('32/2/13', current.humidity)
-- Presión
grp.write('32/2/14', current.pressure)
-- Descripción
grp.write('32/2/15', current.weather_descriptions)
-- ¿Día/Noche?
grp.write('32/2/16',current.is_day)
-- ¿Índice UV?
if current.uv_index <2 then
grp.write('32/2/17','Bajo')
elseif current.uv_index == 3 then
grp.write('32/2/17','Normal')
elseif current.uv_index == 4 then
grp.write('32/2/17','Normal')
elseif current.uv_index == 5 then
grp.write('32/2/17','Normal')
elseif current.uv_index == 6 then
grp.write('32/2/17','Alto')
elseif current.uv_index == 7 then
grp.write('32/2/17','Alto')
elseif current.uv_index == 8 then
grp.write('32/2/17','Muy Alto')
elseif current.uv_index == 9 then
grp.write('32/2/17','Muy Alto')
elseif current.uv_index == 10 then
grp.write('32/2/17','Muy Alto')
elseif current.uv_index >11 then
grp.write('32/2/17','Extremadamente Alto')
end
Code:
weather 20.07.2020 13:00:38
* table:
["cloudcover"]
* number: 0
["visibility"]
* number: 10
["observation_time"]
* string: 10:53 AM
["uv_index"]
* number: 7
["wind_dir"]
* string: SE
["humidity"]
* number: 59
["weather_descriptions"]
* table:
[1]
* string: Sunny
["wind_speed"]
* number: 17
["weather_code"]
* number: 113
["is_day"]
* string: yes
["pressure"]
* number: 1014
["precip"]
* number: 0
["weather_icons"]
* table:
[1]
* string: https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png
["feelslike"]
* number: 32
["temperature"]
* number: 30
["wind_degree"]
* number: 130
The problem is with weather_descriptions, I see that it is a table and I assign it to an object like the others, if I log it, the contents of the table appear to me, the object is updated but it does not show anything. It is defined as the 14 byte DPT ASCII string. How do I get that data from that table inside another table and assign it to the object?
Also I would like to know if it is possible to use in a conditional loop if a range of numbers, for example that it only be executed if it is between 8 and 10. Since I don't know how to do it I have evaluated each value independently at the end of the script. I do this so that the UV index is displayed in text instead of a number.
a greeting
Thank you very much