LUA parse JSON from WU
Posted: Saturday 17 June 2017 11:58
Hi all
I am getting JSON info from WU to get the weather condition. It works well.
I now want to access the Weather underground forecast info to decide or not to close my shutter in the morning to keep the house fresh.
I found a solution with WU to have the info in JSON but impossible to see it in LUA. Don't know why.
here is the code in LUA :
Any idea of what is wrong ?
I tried to print the JSON read by LUA, but is shows in the log only the text part of the JSON.
The JSON code :
I am getting JSON info from WU to get the weather condition. It works well.
I now want to access the Weather underground forecast info to decide or not to close my shutter in the morning to keep the house fresh.
I found a solution with WU to have the info in JSON but impossible to see it in LUA. Don't know why.
here is the code in LUA :
Code: Select all
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
-- settings WU
local wuAPIkey='xxxxxxxxxx'
local city='pws:xxxxxxxxx' -- WU info removed for the publication in the forum
-- API Wunderground
local config=assert(io.popen('curl http://api.wunderground.com/api/'..wuAPIkey..'/forecast/q/'..city..'.json'))
local location = config:read('*all')
config:close()
local jsonLocation = json:decode(location)
TempForecast = jsonLocation.forecast.simpleforecast.forecastday[0].high.celsius
I tried to print the JSON read by LUA, but is shows in the log only the text part of the JSON.
The JSON code :
Code: Select all
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"forecast": 1
}
}
,
"forecast":{
"txt_forecast": {
"date":"11:12 CEST",
"forecastday": [
{
"period":0,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"samedi",
"fcttext":"Ciel plutôt dégagé. Maximales : 81 ºF. Vents N soufflant de 10 à 15 mi/h.",
"fcttext_metric":"Ciel plutôt dégagé. Maximales : 28 ºC. Vents N soufflant de 10 à 15 km/h.",
"pop":"0"
}
,
{
"period":1,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"samedi soir",
"fcttext":"Ciel plutôt dégagé. Minimales : 58 ºF. Vents NO soufflant de 5 à 10 mi/h.",
"fcttext_metric":"Ciel plutôt dégagé. Minimales : 15 ºC. Vents NO soufflant de 10 à 15 km/h.",
"pop":"0"
}
,
{
"period":2,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"dimanche",
"fcttext":"Ciel plutôt dégagé. Maximales : 87 ºF. Vents NNO soufflant de 5 à 10 mi/h.",
"fcttext_metric":"Ciel plutôt dégagé. Maximales : 31 ºC. Vents NO soufflant de 10 à 15 km/h.",
"pop":"10"
}
,
{
"period":3,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"dimanche soir",
"fcttext":"Ciel plutôt dégagé. Minimales : 62 ºF. Vents ESE et variables.",
"fcttext_metric":"Ciel plutôt dégagé. Minimales : 17 ºC. Vents ESE et variables.",
"pop":"10"
}
,
{
"period":4,
"icon":"chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
"title":"lundi",
"fcttext":"Averses au cours de l'après-midi. Maximales : 87 ºF. Vents NO soufflant de 5 à 10 mi/h. Risque de pluie : 50 %.",
"fcttext_metric":"Averses. Maximales : 31 ºC. Vents O soufflant de 10 à 15 km/h. Risque de pluie : 50 %.",
"pop":"50"
}
,
{
"period":5,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"lundi soir",
"fcttext":"Ciel plutôt dégagé. Minimales : 62 ºF. Vents SE et variables.",
"fcttext_metric":"Averses en début de journée. Minimales : 17 ºC. Vents SE et variables. Risque de pluie : 40 %.",
"pop":"10"
}
,
{
"period":6,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"mardi",
"fcttext":"Ciel plutôt dégagé. Maximales : 90 ºF. Vents NNO soufflant de 5 à 10 mi/h.",
"fcttext_metric":"Ciel plutôt dégagé. Maximales : 33 ºC. Vents NO et variables.",
"pop":"10"
}
,
{
"period":7,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"mardi soir",
"fcttext":"Ciel plutôt dégagé. Minimales : 62 ºF. Vents E et variables.",
"fcttext_metric":"Ciel plutôt dégagé. Minimales : 17 ºC. Vents E et variables.",
"pop":"0"
}
]
},
"simpleforecast": {
"forecastday": [
{"date":{
"epoch":"1497718800",
"pretty":"19:00 CEST le 17 juin 2017",
"day":17,
"month":6,
"year":2017,
"yday":167,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"juin",
"monthname_short":"juin",
"weekday_short":"sam",
"weekday":"samedi",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Paris"
},
"period":1,
"high": {
"fahrenheit":"81",
"celsius":"27"
},
"low": {
"fahrenheit":"58",
"celsius":"14"
},
"conditions":"Ciel dégagé",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":0,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "N",
"degrees": 356
},
"avewind": {
"mph": 10,
"kph": 16,
"dir": "N",
"degrees": 356
},
"avehumidity": 44,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1497805200",
"pretty":"19:00 CEST le 18 juin 2017",
"day":18,
"month":6,
"year":2017,
"yday":168,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"juin",
"monthname_short":"juin",
"weekday_short":"dim",
"weekday":"dimanche",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Paris"
},
"period":2,
"high": {
"fahrenheit":"87",
"celsius":"31"
},
"low": {
"fahrenheit":"62",
"celsius":"17"
},
"conditions":"Ciel dégagé",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "NNO",
"degrees": 327
},
"avewind": {
"mph": 6,
"kph": 10,
"dir": "NNO",
"degrees": 327
},
"avehumidity": 56,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1497891600",
"pretty":"19:00 CEST le 19 juin 2017",
"day":19,
"month":6,
"year":2017,
"yday":169,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"juin",
"monthname_short":"juin",
"weekday_short":"lun",
"weekday":"lundi",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Paris"
},
"period":3,
"high": {
"fahrenheit":"87",
"celsius":"31"
},
"low": {
"fahrenheit":"62",
"celsius":"17"
},
"conditions":"Risque de pluie",
"icon":"chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
"skyicon":"",
"pop":50,
"qpf_allday": {
"in": 0.06,
"mm": 2
},
"qpf_day": {
"in": 0.06,
"mm": 2
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "NO",
"degrees": 309
},
"avewind": {
"mph": 6,
"kph": 10,
"dir": "NO",
"degrees": 309
},
"avehumidity": 55,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1497978000",
"pretty":"19:00 CEST le 20 juin 2017",
"day":20,
"month":6,
"year":2017,
"yday":170,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"juin",
"monthname_short":"juin",
"weekday_short":"mar",
"weekday":"mardi",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Paris"
},
"period":4,
"high": {
"fahrenheit":"90",
"celsius":"32"
},
"low": {
"fahrenheit":"62",
"celsius":"17"
},
"conditions":"Ciel dégagé",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "NNO",
"degrees": 331
},
"avewind": {
"mph": 6,
"kph": 10,
"dir": "NNO",
"degrees": 331
},
"avehumidity": 48,
"maxhumidity": 0,
"minhumidity": 0
}
]
}
}
}