Logic Machine Forum
Openweather api - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Openweather api (/showthread.php?tid=2731)

Pages: 1 2


RE: Openweather api - admin - 19.04.2021

Post full script code


RE: Openweather api - JRP - 19.04.2021

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)



RE: Openweather api - admin - 19.04.2021

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



RE: Openweather api - JRP - 19.04.2021

Thanks for your quick response.
Isn't the return on line 29 enough?


RE: Openweather api - admin - 19.04.2021

No because it belongs to a different if branch. return should be placed after each alert in your code.


RE: Openweather api - JRP - 19.04.2021

The point is that this part of the code works, after removing a letter from the token and the corresponding text appears in alerts.


RE: Openweather api - admin - 19.04.2021

It works up to this line then stops with an error if Tabla_tiempo is not a table:
Code:
Tiempo_actual = Tabla_tiempo.current



RE: Openweather api - JRP - 19.04.2021

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.


RE: Openweather api - davidchispas - 21.07.2021

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.


RE: Openweather api - Daniel - 21.07.2021

This script works fine in latest fw. You probably exceeded the free limit which is 250 calls a month.


RE: Openweather api - JRP - 30.08.2022

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


RE: Openweather api - admin - 30.08.2022

Use a scheduled script instead or add os.sleep(60) to the resident script.


RE: Openweather api - JRP - 08.09.2022

Thanks, such a simple solution that never crossed my mind.

Greeting