Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
19.04.2021, 10:31
(This post was last modified: 19.04.2021, 10:32 by JRP.)
Excuse me, here it is.
Code: require('json')
require('ssl.https')
appid = 'xxxxxxxxxxxxxxxxxxx'
lat = 'xxxxx'
lon ='xxxxx'
lang = 'es'
url = 'https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&units=%s&lang=%s&appid=%s'
url = string.format(url, lat, lon, units, lang, appid)
JSON_Tiempo, error = ssl.https.request(url)
log(url, JSON_Tiempo, error)
Tabla_tiempo = json.pdecode(JSON_Tiempo)
if type(Tabla_tiempo) ~= 'table' then
alert('Fallo al cargar la información del tiempo de Openweather')
elseif Tabla_tiempo.cod == 401 then
alert('Fallo con la llave de la API de Openweather')
elseif Tabla_tiempo.cod == 404 then
alert('Fallo en la solicitud de la API de Openweather')
elseif Tabla_tiempo.cod == 429 then
alert('Fallo por exceder el límite de llamadas a la API de Openweather')
return
end
Tiempo_actual = Tabla_tiempo.current
log(Tiempo_actual)
grp.write('32/2/10', Tiempo_actual.temp)
grp.write('32/2/11', Tiempo_actual.feels_like)
grp.write('32/2/12', Tiempo_actual.dew_point)
grp.write('32/2/13', Tiempo_actual.wind_deg)
grp.write('32/2/14', Tiempo_actual.wind_speed)
grp.write('32/2/15', Tiempo_actual.humidity)
grp.write('32/2/16', Tiempo_actual.pressure)
grp.write('32/2/17', Tiempo_actual.uvi)
grp.write('32/2/18', Tiempo_actual.weather[1].description)
amanecer = os.date("%H:%M", Tiempo_actual.sunrise)
grp.write('32/2/19', amanecer)
ocaso = os.date("%H:%M", Tiempo_actual.sunset)
grp.write('32/2/20', ocaso)
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Add return after alert on line 21:
Code: if type(Tabla_tiempo) ~= 'table' then
alert('Fallo al cargar la información del tiempo de Openweather')
return
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
19.04.2021, 10:38
(This post was last modified: 19.04.2021, 10:38 by JRP.)
Thanks for your quick response.
Isn't the return on line 29 enough?
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
No because it belongs to a different if branch. return should be placed after each alert in your code.
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
The point is that this part of the code works, after removing a letter from the token and the corresponding text appears in alerts.
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
It works up to this line then stops with an error if Tabla_tiempo is not a table:
Code: Tiempo_actual = Tabla_tiempo.current
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
I have modified the code so it works shown in the alert log the text Failed to load the Openweather weather information, if you put the return.
Code: -- Comprobación de errores
if type(Tabla_tiempo) == 'table' then -- Si el contenido de la variable es distinto a una tabla entonces
alert('Fallo al cargar la información del tiempo de Openweather')
-- Si la llamada devuelve el codigo de error 401, 404 0 429
elseif Tabla_tiempo.cod == 401 then
alert('Fallo con la llave de la API de Openweather') -- Mensajes de error
elseif Tabla_tiempo.cod == 404 then
alert('Fallo en la solicitud de la API de Openweather')
elseif Tabla_tiempo.cod == 429 then
alert('Fallo por exceder el límite de llamadas a la API de Openweather')
return
end
The issue is that the error does not appear after successfully executing the script several times. I think only the first time.
Posts: 265
Threads: 37
Joined: Apr 2019
Reputation:
4
Code: require('json')
http = require('socket.http')
escape = require('socket.url').escape
key = 'xxxx'
location = 'xxxx'
url = 'http://api.weatherstack.com/current?access_key=%s&query=%s'
url = string.format(url, key, escape(location))
res = http.request(url)
data = json.pdecode(res)
if type(data) ~= 'table' then
alert('no se pudieron cargar los datos del clima')
return
end
if data.error then
log('error', data.error)
return
end
current = data.current
-- temperature
grp.write('32/1/1', current.temperature)
-- "feels like" temperature
grp.write('32/1/2', current.feelslike)
-- humidity as percentage
grp.write('32/1/3', current.humidity)
-- wind speed in kilometers per hour
grp.write('32/1/4', current.wind_speed)
-- uv index
grp.write('32/1/5', current.uv_index)
-- weather condition text
grp.write('32/1/6', current.weather_descriptions[1])
-- pressure
grp.write('32/1/7', current.pressure)
-- precipitation amount
grp.write('32/1/8', current.precip)
Hello, I don't know if this script is affected by the latest updates in LM.
Posts: 4643
Threads: 24
Joined: Aug 2017
Reputation:
207
This script works fine in latest fw. You probably exceeded the free limit which is 250 calls a month.
------------------------------
Ctrl+F5
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
Hello
Openweather one call, now only allows 1000 calls per day, until now I had it with a resident script that runs every minute, which gives a result of 1440 calls per day and therefore I exceed the current maximum.
The resident script does not support more than 60 seconds, how can I make the script run, for example every 2 minutes?
greeting
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Use a scheduled script instead or add os.sleep(60) to the resident script.
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
Thanks, such a simple solution that never crossed my mind.
Greeting
|