Logic Machine Forum
Weather Apixu - 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: Weather Apixu (/showthread.php?tid=2240)



Weather Apixu - arivera - 13.09.2019

Hello, I created this scrip in several Logic Machine to know meteorological data and they worked correctly until a while ago they no longer work, they could help me find out what the error is or why they don't work.

Thank you


*****************************************************************************************************
--  https://www.apixu.com/

require('json')
https = require('ssl.https')
escape = require('socket.url').escape

key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  --API KEY  
location = 'Alcobendas'  -- lugar de clima


url = 'https://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
url = string.format(url, key, escape(location))


res = https.request(url)

data = json.pdecode(res)
if type(data) ~= 'table' then
  alert('failed to load weather data')
  log('failed to load weather data')
  return
end

if data.error then
  log('error', data.error)
  return
end

current = data.current
today = data.forecast.forecastday[ 1 ].day
tomorrow = data.forecast.forecastday[ 2 ].day


--INFO UTILIZADA

-- temperature in C
grp.write('15/1/2', current.temp_c)
-- weather condition text
grp.write('15/1/1', current.condition.text)
-- minimum temperature in celsius for the day
grp.write('15/1/4', today.mintemp_c)
-- maximum temperature in celsius for the day
grp.write('15/1/5', today.maxtemp_c)
-- weather condition text
grp.write('15/1/3', today.condition.text)
-- maximum temperature in celsius for the day
grp.write('15/1/8', tomorrow.maxtemp_c)
-- weather condition text
grp.write('15/1/6', tomorrow.condition.text)
-- minimum temperature in celsius for the day
grp.write('15/1/7', tomorrow.mintemp_c)
-- "feels like" temperature in C
grp.write('15/1/9', current.feelslike_c)
-- humidity as percentage
grp.write('15/1/12', current.humidity)
-- wind speed in kilometers per hour
grp.write('15/1/11', current.wind_kph)
-- uv index
grp.write('15/1/15', current.uv)
-- pressure in millibars
grp.write('15/1/14', current.pressure_mb)
-- "wind_dir"
grp.write('15/1/10', current.wind_dir)
-- "sunrise"


RE: Weather Apixu - admin - 13.09.2019

See here: https://forum.logicmachine.net/showthread.php?tid=253&pid=13858#pid13858


RE: Weather Apixu - arivera - 13.09.2019

Hello
I have changed the scrip as it has told me and now in registers I have no error but in registration error I have this message:

User script:15: attempt to index global 'http' (a nil value)
stack traceback:
User script:15: in main chunk


Could you help me since it is a scrip that I need in several machines.

Thank you

*********************************************************************************************************
-- https://www.apixu.com/

require('json')
https = require('ssl.https')
escape = require('socket.url').escape

key = 'My_Api_Key' --API KEY despues de realizar el registro
location = 'Alcobendas' -- lugar de clima


url = 'http://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
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')
log('failed to load weather data')
return
end

if data.error then
log('error', data.error)
return
end

current = data.current
today = data.forecast.forecastday[ 1 ].day
tomorrow = data.forecast.forecastday[ 2 ].day


--INFO UTILIZADA

-- temperature in C
grp.write('15/1/2', current.temp_c)
-- weather condition text
grp.write('15/1/1', current.condition.text)
-- minimum temperature in celsius for the day
grp.write('15/1/4', today.mintemp_c)
-- maximum temperature in celsius for the day
grp.write('15/1/5', today.maxtemp_c)
-- weather condition text
grp.write('15/1/3', today.condition.text)
-- maximum temperature in celsius for the day
grp.write('15/1/8', tomorrow.maxtemp_c)
-- weather condition text
grp.write('15/1/6', tomorrow.condition.text)
-- minimum temperature in celsius for the day
grp.write('15/1/7', tomorrow.mintemp_c)
-- "feels like" temperature in C
grp.write('15/1/9', current.feelslike_c)
-- humidity as percentage
grp.write('15/1/12', current.humidity)
-- wind speed in kilometers per hour
grp.write('15/1/11', current.wind_kph)
-- uv index
grp.write('15/1/15', current.uv)
-- pressure in millibars
grp.write('15/1/14', current.pressure_mb)
-- "wind_dir"
grp.write('15/1/10', current.wind_dir)
-- "sunrise"


RE: Weather Apixu - admin - 13.09.2019

You are missing this line:
Code:
http = require('socket.http')



RE: Weather Apixu - arivera - 15.09.2019

(13.09.2019, 17:18)admin Wrote: You are missing this line:
Code:
http = require('socket.http')
Thank you very much that was what was missing to change the code


RE: Weather Apixu - Domoticatorino - 02.10.2019

Hi Admin,
I use the same API weather service and web response is the following:

{"request":{"type":"City","query":"Turin, Italy","language":"en","unit":"m"},"location":{"name":"Turin","country":"Italy","region":"Piemonte","lat":"45.050","lon":"7.667","timezone_id":"Europe\/Rome","localtime":"2019-10-02 09:58","localtime_epoch":1570010280,"utc_offset":"2.0"},"current":{"observation_time":"07:58 AM","temperature":16,"weather_code":116,"weather_icons":["https:\/\/assets.weatherstack.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"],"weather_descriptions":["Partly cloudy"],"wind_speed":0,"wind_degree":288,"wind_dir":"WNW","pressure":1005,"precip":2.8,"humidity":94,"cloudcover":25,"feelslike":16,"uv_index":5,"visibility":10,"is_day":"yes"}}

to show localtime I use

location = data.location
grp.write('7/1/4', location.localtime)

But result is not "2019-10-02 09:58" but only "2019-10-02 09". Response is not complete. How can read evething? 

In order to read "weather_descriptions" between [...] what is the correct instruction?

Thanks.


RE: Weather Apixu - admin - 02.10.2019

Check 7/1/4 data type, it's probably 14 byte string. That's why 2019-10-02 09:58 does not fit there.


RE: Weather Apixu - Domoticatorino - 02.10.2019

(02.10.2019, 08:20)admin Wrote: Check 7/1/4 data type, it's probably 14 byte string. That's why 2019-10-02 09:58 does not fit there.

Yes, you're right. What do you suggest? 

Thanks.


RE: Weather Apixu - admin - 02.10.2019

You change year to 2 digits by using string.sub() or remove year completely.


RE: Weather Apixu - Domoticatorino - 02.10.2019

(02.10.2019, 08:42)admin Wrote: You change year to 2 digits by using string.sub() or remove year completely.

Ok thanks.

What about "weather_descriptions":["Partly cloudy"]

How can exactr it? If I use the same command of other information message is blank.

Thanks.



RE: Weather Apixu - admin - 02.10.2019

Like this:
Code:
description = data.current.weather_descriptions[1]