Example: Featch weather forecast from Apixu

Getting API key

Create free account on https://www.apixu.com. Once this is done you will see API key which will be used in the script below.

Scheduled script

Make sure that your script does not fetch data too quickly (not less than once in 5 minutes).
In this example, we will fetch the data every 10 minutes.

In the below script change key variable to your API key, location variable to required location in textual form. This example fetches current weather condition and forecast for today and tomorrow. Fields can be found in apixu documentation: https://www.apixu.com/doc/forecast.aspx (day Element).

Source code    
  1. require('json')
  2. https = require('ssl.https')
  3. escape = require('socket.url').escape
  4.  
  5. key = 'my_api_key'
  6. location = 'my location name'
  7.  
  8. url = 'https://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
  9. url = string.format(url, key, escape(location))
  10.  
  11. res = https.request(url)
  12.  
  13. data = json.pdecode(res)
  14. if type(data) ~= 'table' then
  15.   alert('failed to load weather data')
  16.   return
  17. end
  18.  
  19. if data.error then
  20.   log('error', data.error)
  21.   return
  22. end
  23.  
  24. current = data.current
  25. today = data.forecast.forecastday[ 1 ].day
  26. tomorrow = data.forecast.forecastday[ 2 ].day
  27.  
  28. -- log(current, today, tomorrow)
  29.  
  30. -- temperature in C
  31. grp.write('32/1/1', current.temp_c)
  32. -- "feels like" temperature in C
  33. grp.write('32/1/2', current.feelslike_c)
  34. -- humidity as percentage
  35. grp.write('32/1/3', current.humidity)
  36. -- wind speed in kilometers per hour
  37. grp.write('32/1/4', current.wind_kph)
  38. -- uv index
  39. grp.write('32/1/5', current.uv)
  40. -- weather condition text
  41. grp.write('32/1/6', current.condition.text)
  42. -- pressure in millibars
  43. grp.write('32/1/7', current.pressure_mb)
  44. -- precipitation amount in millimeters
  45. grp.write('32/1/8', current.precip_mm)
  46.  
  47. -- minimum temperature in celsius for the day
  48. grp.write('32/2/1', today.mintemp_c)
  49. -- maximum temperature in celsius for the day
  50. grp.write('32/2/2', today.maxtemp_c)
  51. -- average temperature in celsius for the day
  52. grp.write('32/2/3', today.avgtemp_c)
  53. -- average humidity as percentage
  54. grp.write('32/2/4', today.avghumidity)
  55. -- maximum wind speed in kilometers per hour
  56. grp.write('32/2/5', today.maxwind_kph)
  57. -- uv index
  58. grp.write('32/2/6', today.uv)
  59. -- weather condition text
  60. grp.write('32/2/7', today.condition.text)
  61. -- total precipitation in millimeters
  62. grp.write('32/2/8', today.totalprecip_mm)
  63.  
  64. -- minimum temperature in celsius for the day
  65. grp.write('32/3/1', tomorrow.mintemp_c)
  66. -- maximum temperature in celsius for the day
  67. grp.write('32/3/2', tomorrow.maxtemp_c)
  68. -- average temperature in celsius for the day
  69. grp.write('32/3/3', tomorrow.avgtemp_c)
  70. -- average humidity as percentage
  71. grp.write('32/3/4', tomorrow.avghumidity)
  72. -- maximum wind speed in kilometers per hour
  73. grp.write('32/3/5', tomorrow.maxwind_kph)
  74. -- uv index
  75. grp.write('32/3/6', tomorrow.uv)
  76. -- weather condition text
  77. grp.write('32/3/7', tomorrow.condition.text)
  78. -- total precipitation in millimeters
  79. grp.write('32/3/8', tomorrow.totalprecip_mm)