Page 1 of 1

Weather underground

Posted: Wednesday 23 August 2017 8:19
by DewGew
Hi All,

I can get this code to work in dzVents 2.2.0 any ideas?

I got this error : "Error: dzVents: Error: /home/pi/domoticz/scripts/dzVents/scripts/weather.lua:26: attempt to call a nil value"

Code: Select all

fetchWeatherForecast = function()
         json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")()            --<<Line 26>>--
         local city = "ECDF"
    	 local wuAPIkey = "xxxxxxxxxxx" -- Your Weather Underground API Key
         local file=assert(io.popen('curl http://api.wunderground.com/api/'..wuAPIkey..'/forecast/q/'..city..'.json'))   
         local raw = file:read('*all')
         file:close()   
         local jsonForecast = json:decode(raw)   
         domoticz.data.recentForecast=jsonForecast.forecast.txt_forecast.forecastday[1].fcttext_metric  -- complete forecast
         --domoticz.data.recentForecast=jsonForecast.forecast.simpleforecast.forecastday[1].conditions  -- small forecast
         
         print("fetchWeatherForecast: " .. domoticz.data.recentForecast)
    
    end]
// Pierre

Re: Weather underground

Posted: Wednesday 23 August 2017 8:39
by Doler
I think the error is: json.lua should be JSON.lua. At my system the name of the library is in capitals.

Re: Weather underground

Posted: Wednesday 23 August 2017 11:46
by DewGew
SO simple..no it works. Thanks #Doler

// Pierre

Re: Weather underground

Posted: Thursday 24 August 2017 8:01
by dannybloe
Be careful though with doing http requests in your script as it may never complete and will cause your script to be killed after 10 seconds.

Re: Weather underground

Posted: Saturday 26 August 2017 9:53
by BakSeeDaa
dannybloe wrote: Thursday 24 August 2017 8:01 Be careful though with doing http requests in your script as it may never complete and will cause your script to be killed after 10 seconds.
Yeah true that. Doing like that is devastating, the event system freezes. The whole Domoticz system will appear to be unstable. Some events may even get lost. So take my advice and avoid that disastrous code at all costs.

Instead do something like this:

Code: Select all

os.execute('curl -s "'..url..'" > /tmp/resultfile&')
With the ampersand sign at the end of the command the process starts in the background, so the event system can continue and do whatever eventsystems do and won't have to wait until the script is finished.

After a minute let your script look if there is a result file.

EDIT:
Have a look at this topic which gives an example how to do it.

Re: Weather underground

Posted: Saturday 26 August 2017 22:24
by DewGew
Maybe some should mention this in the wiki also.
http://www.domoticz.com/wiki/Lua_-_json.lua