FORUM FIBARO
Forum systemu FIBARO

| FAQ |  Search |  Memberlist |  Usergroups |
| Register |  Log in |

Previous topic «» Next topic
weather forecast query and set variable
Author Message
Kuuno 


Joined: 20 Mar 2012
Posts: 390
Location: Estonia
Posted: 2013-04-11, 11:47   weather forecast query and set variable

How is it possible over LUA to query for a specific value from web. lets say temperature and then set variable value according to that.

the idea would be to query tomorrows weather forecast and then control heating accordingly.
if temp forecast outside = more than 10 degrees higher than now then stop heating the house
this would help to prevent overheating when there are big temperature drops or rises


sample link to get the info:
http://weather.yahooapis....ss?w=845743&u=c

thanks!
_________________
Get Your preconfigured Fibaro solutions here: http://shop.indome.ee/

Tell us what You need and we ship it to You configured so just "plug & play"
Last edited by Kuuno on 2013-07-15, 11:30; edited 1 time in total  
 
 
PJ 

Age: 26
Joined: 09 Jan 2013
Posts: 116
Location: Gdynia
Posted: 2013-04-12, 15:07   

Something like this was posted recently on polish forum by sztywniak.

You have to create virtual device with button like this. It has polish comments, but you should be able to find out what you have to do without them:

Code:

fibaro:log('<h3><b style="color:red;">Proszę czekać</h3></b>')
HC2 = Net.FHttp("www.traxelektronik.pl")
-- wczytuje wynik zapytania do zmiennej dane
dane, status, errorcode = HC2:POST("/pogoda/stacja.php?idst=145","")
-- szukam w zmiennej ciągu znaków po którym jest godzina
godz = string.find(dane,"godz.")
-- wczytuje godzinę publikacji danych do zmiennej godztxt
godztxt = string.sub(dane,godz+9,godz+15)
-- szukam w zmiennej ciągu znaków za którym znajduje się aktualna temperatura
gdzie = (string.find(dane,"2m"))
-- wczytuje temperaturę do zmiennej temp2m
temp2m_a = (string.gsub((string.sub(dane,gdzie+87,gdzie+95)),'"',' '))
-- poniżej dodałem linię gdzie koniec pobieranego ciągu wyznaczany jest
-- dynamicznie na podstawie występowania znaku <
temp2m = (string.sub(temp2m_a,0,string.find(temp2m_a,"<")-1))
fibaro:setGlobal("Temp_zew", tonumber(temp2m))
fibaro:log("Temperatura: <b>"..temp2m.."C</b> z godziny: "..godztxt)
-- wyświetla wynik przez 5000 ms
fibaro:sleep(10*10000)


It gets information about temperature from this website - http://www.traxelektronik...ja.php?idst=145 and write it to global variable "Temp_zew"

You need to use debugger in firefox to find out where are numbers related with temperature you want. Traxelektronik is very simple in construction, so it isn't a problem. Would be more difficult with your sample link.
 
 
Kuuno 


Joined: 20 Mar 2012
Posts: 390
Location: Estonia
Posted: 2013-07-15, 11:29   

can someone help to extract today and tomorrow temp forecast from this link
http://www.wunderground.c...t?query=tallinn

thanks!

[ Added: 2013-07-15, 12:50 ]
found little bettary way
here is a sample, i should extract celsius from period 1

"epoch":"1373911200",
"pretty":"9:00 PM EEST on July 15, 2013",
"day":15,
"month":7,
"year":2013,
"yday":195,
"hour":21,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"July",
"weekday_short":"Mon",
"weekday":"Monday",
"ampm":"PM",
"tz_short":"EEST",
"tz_long":"Europe/Tallinn"
},
"period":1,
"high": {
"fahrenheit":"72",
"celsius":"22"
_________________
Get Your preconfigured Fibaro solutions here: http://shop.indome.ee/

Tell us what You need and we ship it to You configured so just "plug & play"
 
 
Shad

Age: 31
Joined: 28 Jan 2013
Posts: 970
Location: Gabon
Posted: 2013-07-15, 12:09   

You must have an API on this site web.
So with a small script in php you can extract all and put it in global variable.
_________________
How to:
- Your own sms Gateway
- Plugin Xbmc
- Philips HUE
 
 
jompa68 

Age: 46
Joined: 14 Jan 2013
Posts: 329
Location: Sweden
Posted: 2013-07-15, 12:18   

Here is an example:
you need to create an free account to fetch this data from wunderground http://www.wunderground.com/weather/api/

Create a virtual device
Code:
-- Setting up the connection data
  FHEM = Net.FHttp("api.wunderground.com")
-- geting info from your Personal Weather Station
  response = FHEM:GET("/api/API_KEY/conditions/forecast/lang:EN/q/pws:IHARJUMA2.json")
-- decoding json string to table
      result = json.decode(response)
      humidity = result.current_observation.relative_humidity
      temperature = result.current_observation.temp_c
     pression = result.current_observation.pressure_mb
  i = 1
while i  <= 300 do
fibaro:log("Temperature: "..temperature.."°C Humidity: "..humidity.." Bar: "..
pression.." mb")
fibaro:sleep(1000);
  i = i + 1
end
_________________
Regards Jonny
---------------------------------
Global moderator on Zwaveforum.se
 
 
Shad

Age: 31
Joined: 28 Jan 2013
Posts: 970
Location: Gabon
Posted: 2013-07-15, 12:19   

Good, didn't know how to parse json with HC2
_________________
How to:
- Your own sms Gateway
- Plugin Xbmc
- Philips HUE
 
 
Kuuno 


Joined: 20 Mar 2012
Posts: 390
Location: Estonia
Posted: 2013-07-15, 13:26   

thanks!

i already had the key and could get some data but used http response
your way is much better!

thanks
_________________
Get Your preconfigured Fibaro solutions here: http://shop.indome.ee/

Tell us what You need and we ship it to You configured so just "plug & play"
 
 
jompa68 

Age: 46
Joined: 14 Jan 2013
Posts: 329
Location: Sweden
Posted: 2013-07-15, 16:35   

can someone help out with this json? want to get the forecast part...

Code:
{
"response":{
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features":{
"conditions":1,
"forecast":1
}
},
"current_observation":{
"image":{
"url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
"display_location":{},
"observation_location":{},
"estimated":{
},
"station_id":"IGVLEBOR5",
"observation_time":"Last Updated on July 15, 3:25 PM CEST",
"observation_time_rfc822":"Mon, 15 Jul 2013 15:25:10 +0200",
"observation_epoch":"1373894710",
"local_time_rfc822":"Mon, 15 Jul 2013 15:29:37 +0200",
"local_epoch":"1373894977",
"local_tz_short":"CEST",
"local_tz_long":"Europe/Stockholm",
"local_tz_offset":"+0200",
"weather":"Partly Cloudy",
"temperature_string":"71.4 F (21.9 C)",
"temp_f":71.4,
"temp_c":21.9,
"relative_humidity":"39%",
"wind_string":"From the West at 5.0 MPH Gusting to 9.0 MPH",
"wind_dir":"West",
"wind_degrees":259,
"wind_mph":5.0,
"wind_gust_mph":"9.0",
"wind_kph":8.0,
"wind_gust_kph":"14.5",
"pressure_mb":"1006",
"pressure_in":"29.71",
"pressure_trend":"0",
"dewpoint_string":"45 F (7 C)",
"dewpoint_f":45,
"dewpoint_c":7,
"heat_index_string":"NA",
"heat_index_f":"NA",
"heat_index_c":"NA",
"windchill_string":"NA",
"windchill_f":"NA",
"windchill_c":"NA",
"feelslike_string":"71.4 F (21.9 C)",
"feelslike_f":"71.4",
"feelslike_c":"21.9",
"visibility_mi":"6.2",
"visibility_km":"10.0",
"solarradiation":"",
"UV":"6",
"precip_1hr_string":"0.00 in ( 0 mm)",
"precip_1hr_in":"0.00",
"precip_1hr_metric":" 0",
"precip_today_string":"0.13 in (3 mm)",
"precip_today_in":"0.13",
"precip_today_metric":"3",
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"forecast_url":"http://www.wunderground.com/global/stations/02453.html",
"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IGVLEBOR5",
"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=60.543842,16.278696"
},
"forecast":{
"txt_forecast":{
"date":"2:00 AM CEST",
"forecastday":[
{
"period":0,
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"title":"Monday",
"fcttext":"Overcast. High of 79F. Winds from the SW at 10 to 15 mph.",
"fcttext_metric":"Overcast. High of 26C. Breezy. Winds from the SW at 15 to 20 km/h.",
"pop":"0"
},
{
"period":1,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Monday Night",
"fcttext":"Partly cloudy with a chance of rain. Low of 48F. Winds from the NW at 5 to 15 mph. Chance of rain 20%.",
"fcttext_metric":"Partly cloudy with a chance of rain. Low of 9C. Breezy. Winds from the NW at 10 to 20 km/h.",
"pop":"20"
},
{
"period":2,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Tuesday",
"fcttext":"Clear in the morning, then overcast. High of 73F. Winds from the WNW at 10 to 15 mph.",
"fcttext_metric":"Clear in the morning, then overcast. High of 23C. Breezy. Winds from the WNW at 15 to 20 km/h.",
"pop":"0"
},
{
"period":3,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Tuesday Night",
"fcttext":"Partly cloudy. Low of 50F. Winds from the NNW at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy. Low of 10C. Winds from the NNW at 5 to 15 km/h.",
"pop":"0"
},
{
"period":4,
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"title":"Wednesday",
"fcttext":"Partly cloudy in the morning, then overcast. High of 79F. Winds from the NNW at 5 to 10 mph shifting to the ENE in the afternoon.",
"fcttext_metric":"Partly cloudy in the morning, then overcast. High of 26C. Winds from the NNW at 5 to 15 km/h shifting to the ENE in the afternoon.",
"pop":"0"
},
{
"period":5,
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"title":"Wednesday Night",
"fcttext":"Overcast in the evening, then partly cloudy with a chance of rain. Low of 57F. Winds less than 5 mph.",
"fcttext_metric":"Overcast in the evening, then partly cloudy with a chance of rain. Low of 14C. Winds less than 5 km/h.",
"pop":"20"
},
{
"period":6,
"icon":"chancerain",
"icon_url":"http://icons-ak.wxug.com/i/c/k/chancerain.gif",
"title":"Thursday",
"fcttext":"Partly cloudy with a chance of rain. High of 70F. Breezy. Winds from the NE at 10 to 20 mph. Chance of rain 40%.",
"fcttext_metric":"Partly cloudy with a chance of rain. High of 21C. Breezy. Winds from the NE at 20 to 25 km/h. Chance of rain 40%.",
"pop":"40"
},
{
"period":7,
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"title":"Thursday Night",
"fcttext":"Partly cloudy in the evening, then overcast. Low of 54F. Winds less than 5 mph.",
"fcttext_metric":"Partly cloudy in the evening, then overcast. Low of 12C. Winds less than 5 km/h.",
"pop":"0"
}
]
},
"simpleforecast":{
"forecastday":[
{
"date":{
"epoch":"1373922000",
"pretty":"11:00 PM CEST on July 15, 2013",
"day":15,
"month":7,
"year":2013,
"yday":195,
"hour":23,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"July",
"weekday_short":"Mon",
"weekday":"Monday",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Stockholm"
},
"period":1,
"high":{
"fahrenheit":"79",
"celsius":"26"
},
"low":{
"fahrenheit":"48",
"celsius":"9"
},
"conditions":"Mostly Cloudy",
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"skyicon":"mostlycloudy",
"pop":0,
"qpf_allday":{
"in":0.01,
"mm":0.3
},
"qpf_day":{
"in":0.00,
"mm":0.0
},
"qpf_night":{
"in":0.01,
"mm":0.3
},
"snow_allday":{
"in":0,
"cm":0
},
"snow_day":{
"in":0,
"cm":0
},
"snow_night":{
"in":0,
"cm":0
},
"maxwind":{
"mph":12,
"kph":19,
"dir":"SW",
"degrees":228
},
"avewind":{
"mph":10,
"kph":16,
"dir":"SW",
"degrees":234
},
"avehumidity":57,
"maxhumidity":78,
"minhumidity":36
},
{
"date":{
"epoch":"1374008400",
"pretty":"11:00 PM CEST on July 16, 2013",
"day":16,
"month":7,
"year":2013,
"yday":196,
"hour":23,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"July",
"weekday_short":"Tue",
"weekday":"Tuesday",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Stockholm"
},
"period":2,
"high":{
"fahrenheit":"73",
"celsius":"23"
},
"low":{
"fahrenheit":"50",
"celsius":"10"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon":"partlycloudy",
"pop":0,
"qpf_allday":{
"in":0.00,
"mm":0.0
},
"qpf_day":{
"in":0.00,
"mm":0.0
},
"qpf_night":{
"in":0.00,
"mm":0.0
},
"snow_allday":{
"in":0,
"cm":0
},
"snow_day":{
"in":0,
"cm":0
},
"snow_night":{
"in":0,
"cm":0
},
"maxwind":{
"mph":12,
"kph":19,
"dir":"NW",
"degrees":304
},
"avewind":{
"mph":9,
"kph":14,
"dir":"WNW",
"degrees":295
},
"avehumidity":48,
"maxhumidity":73,
"minhumidity":33
},
{
"date":{
"epoch":"1374094800",
"pretty":"11:00 PM CEST on July 17, 2013",
"day":17,
"month":7,
"year":2013,
"yday":197,
"hour":23,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"July",
"weekday_short":"Wed",
"weekday":"Wednesday",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Stockholm"
},
"period":3,
"high":{
"fahrenheit":"79",
"celsius":"26"
},
"low":{
"fahrenheit":"57",
"celsius":"14"
},
"conditions":"Mostly Cloudy",
"icon":"mostlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
"skyicon":"mostlycloudy",
"pop":0,
"qpf_allday":{
"in":0.01,
"mm":0.3
},
"qpf_day":{
"in":0.00,
"mm":0.0
},
"qpf_night":{
"in":0.12,
"mm":3.0
},
"snow_allday":{
"in":0,
"cm":0
},
"snow_day":{
"in":0,
"cm":0
},
"snow_night":{
"in":0,
"cm":0
},
"maxwind":{
"mph":9,
"kph":14,
"dir":"NNW",
"degrees":331
},
"avewind":{
"mph":3,
"kph":5,
"dir":"SE",
"degrees":138
},
"avehumidity":49,
"maxhumidity":65,
"minhumidity":34
},
{
"date":{
"epoch":"1374181200",
"pretty":"11:00 PM CEST on July 18, 2013",
"day":18,
"month":7,
"year":2013,
"yday":198,
"hour":23,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"July",
"weekday_short":"Thu",
"weekday":"Thursday",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Stockholm"
},
"period":4,
"high":{
"fahrenheit":"70",
"celsius":"21"
},
"low":{
"fahrenheit":"54",
"celsius":"12"
},
"conditions":"Chance of Rain",
"icon":"chancerain",
"icon_url":"http://icons-ak.wxug.com/i/c/k/chancerain.gif",
"skyicon":"partlycloudy",
"pop":40,
"qpf_allday":{
"in":0.12,
"mm":3.0
},
"qpf_day":{
"in":0.01,
"mm":0.3
},
"qpf_night":{
"in":0.00,
"mm":0.0
},
"snow_allday":{
"in":0,
"cm":0
},
"snow_day":{
"in":0,
"cm":0
},
"snow_night":{
"in":0,
"cm":0
},
"maxwind":{
"mph":15,
"kph":24,
"dir":"NE",
"degrees":35
},
"avewind":{
"mph":9,
"kph":14,
"dir":"NE",
"degrees":34
},
"avehumidity":68,
"maxhumidity":93,
"minhumidity":48
}
]
}
}
}


i can do to date part but not any longer then that, becase next part do not have any name (i think)

Code:
-- decoding json string to table
      result = json.decode(response)
      forecast1 = result.forecast.txt_forecast.date
    fibaro:log(forecast1)


[ Added: 2013-07-15, 17:13 ]
From json parser i see this:

and i test this:
Code:
-- decoding json string to table
      result = json.decode(response)
    forecast1 = result.forecast.simpleforecast.forecastday.0.conditions
    fibaro:log(forecast1)


But log do not show anything. What am i doing wrong?

Skärmavbild 2013-07-15 kl. 17.09.33.png
70 Time(s) 58.98 KB

_________________
Regards Jonny
---------------------------------
Global moderator on Zwaveforum.se
 
 
HorseOne

Joined: 15 Nov 2012
Posts: 17
Location: France
Posted: 2013-07-15, 18:21   

forecastday is an array. You should try the statement below

Code:
 forecast1 = result.forecast.simpleforecast.forecastday[0].conditions
_________________
_________________
Fibaro HC2 v3.590
Fibaro FGS221 , FGD211 , FGR221 , FGBS3211, FGK-10x, FGWPE-101
Wi8 IM61 Camera, Synology NAS
iPad, iPhone
 
 
jompa68 

Age: 46
Joined: 14 Jan 2013
Posts: 329
Location: Sweden
Posted: 2013-07-15, 18:39   

That did not work....still nothing in log

[ Added: 2013-07-15, 18:46 ]
OH! found the problem..json error?
first in array is 1 not 0...strange, can someone explain that?

[ Added: 2013-07-15, 19:04 ]
Final solution :-D

Code:

-- Setting up the connection data
  FHEM = Net.FHttp("api.wunderground.com")
-- geting info from your Personal Weather Station
  response = FHEM:GET("/api/YOUR_API/conditions/forecast/lang:EN/q/pws:YOUR_PWS.json")
-- decoding json string to table
      result = json.decode(response)
      --forecast1 = result.forecast.simpleforecast.forecastday.0.pop
    day1 = result.forecast.simpleforecast.forecastday[1].date.weekday_short
    forecast1 = result.forecast.simpleforecast.forecastday[1].conditions
    day2 = result.forecast.simpleforecast.forecastday[2].date.weekday_short
    forecast2 = result.forecast.simpleforecast.forecastday[2].conditions
    day3 = result.forecast.simpleforecast.forecastday[3].date.weekday_short
    forecast3 = result.forecast.simpleforecast.forecastday[3].conditions
   
      i = 1
while i  <= 300 do
fibaro:log(day1..":"..forecast1.." "..day2..":"..forecast2.." "..day3..":"..forecast3)
fibaro:sleep(1000);
  i = i + 1
end
_________________
Regards Jonny
---------------------------------
Global moderator on Zwaveforum.se
 
 
william.abbo

Joined: 15 Sep 2013
Posts: 16
Location: Obdam, Netherlands
  Posted: 2013-09-30, 20:30   line 8: attempt to index global 'Net' (a nil value)

jompa68 wrote:
Here is an example:
you need to create an free account to fetch this data from wunderground http://www.wunderground.com/weather/api/

Create a virtual device
Code:
-- Setting up the connection data
  FHEM = Net.FHttp("api.wunderground.com")
-- geting info from your Personal Weather Station
  response = FHEM:GET("/api/API_KEY/conditions/forecast/lang:EN/q/pws:IHARJUMA2.json")
-- decoding json string to table
      result = json.decode(response)
      humidity = result.current_observation.relative_humidity
      temperature = result.current_observation.temp_c
     pression = result.current_observation.pressure_mb
  i = 1
while i  <= 300 do
fibaro:log("Temperature: "..temperature.."°C Humidity: "..humidity.." Bar: "..
pression.." mb")
fibaro:sleep(1000);
  i = i + 1
end



Jompa,
I copied your LUA-code in a virtual device and filled in my own API-key. When I run the scene I get a error: [ERROR] 20:20:22: line 8: attempt to index global 'Net' (a nil value)
It is pointing to this line: FHEM = Net.FHttp("api.wunderground.com")

What am I doing wrong or forgetting ??


[b]Never mind, forget it. I did not read everything, it's working right now.

http://forum.fibaro.com/i...con_redface.gif
 
 
Display posts from previous:   
Reply to topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
Add this topic to your bookmarks
Printable version

Jump to:  

Powered by phpBB modified by Przemo © 2003 phpBB Group
Theme created by kemustek from Forum PC
Page generated in 0.1 second. SQL queries: 15