Weather underground

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Weather underground

Post 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
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
Doler
Posts: 163
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sint-Oedenrode, Netherlands
Contact:

Re: Weather underground

Post by Doler »

I think the error is: json.lua should be JSON.lua. At my system the name of the library is in capitals.
Mark: Domoticz latest Beta on Virtual machine (Proxmox) running Debian Bookworm - Z-Stick 7 - RFXCom - P1 - SolarEdge - Dahua - Philips Hue - Docker - Zigbee2mqtt (latest, docker) via python plugin - Zwave-js-ui (latest, docker) - dzVents - Nodered
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: Weather underground

Post by DewGew »

SO simple..no it works. Thanks #Doler

// Pierre
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Weather underground

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Weather underground

Post 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.
Last edited by BakSeeDaa on Saturday 09 September 2017 10:54, edited 1 time in total.
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: Weather underground

Post by DewGew »

Maybe some should mention this in the wiki also.
http://www.domoticz.com/wiki/Lua_-_json.lua
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest