I am trying to set up a script that can update the predicted total precipitation for the next few days. The intended use is to control irrigation of my lawn.
I am using the National Digital Forecast SOAP service. An example of the feed can be found here:
http://graphical.weather.gov/xml/SOAP_server/ndfdXMLclient.php?whichClient=NDFDgen&lat=41.4783&lon=-81.4639&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2017-02-27T00%3A00%3A00&Unit=e&qpf=qpf&Submit=Submit
and some documentation and a graphical interface is here:
http://graphical.weather.gov/xml/SOAP_server/ndfdXML.htm
This is the code that I have come up with. So far, I have hard coded my Latitude and Longitude as well as the number of days I want to get precipitation forecast (currently 3). I am able to submit a correctly formed URL and retrieve the XML. As you can see in the example (fist link), the XML returns the predicted precipitation(in inches) for each 6 hour period enclosed in tags. I am trying to split them and add up the precipitation amounts without success - any help is greatly appreciated.
local http = require(“socket.http”)
local m_now= os.time()
–calculate 3 days forward
local m_endTime=m_now+3600243
local now = os.date(“%Y-%m-%d”, m_now)
local endTime =os.date(“%Y-%m-%d”, m_endTime)
–create the URL
local PrecipitationURL = “http://graphical.weather.gov/xml/SOAP_server/ndfdXMLclient.php?whichClient=NDFDgen&lat=41.4783&lon=-81.4639&product=time-series&begin=“..now..“T00%3A00%3A00&end=”..“T00%3A00%3A00”..endTime..”&Unit=e&qpf=qpf&Submit=Submit”
–This code extracts elements
local TAG1 = “<%s>(.)</%s>"
local TAG2 = "<%s .->(.)</%s>”
local function extractElement(tag, xml, default)
local result = xml:match(TAG1:format(tag, tag))
if (result ~= nil) then
return result
else
return (xml:match(TAG2:format(tag, tag)) or default)
end
end
local status, rss=luup.inet.wget(PrecipitationURL, 10)
if (status == 0) then
local totalPrecip
totalPrecip = 0
local DailyPrecip = extractElement(“value”, rss, “N/A”)
–there is perhaps a more elegant way to parse this XML
–I am replacing the opening and closing XML tags
DailyPrecip =string.gsub(DailyPrecip , “%s*”, “”)
DailyPrecip =string.gsub(DailyPrecip , “”, " ")
–this is what I end up with…
–a space delimited string of 6 hourly precipitation amounts in inches
for const in DailyPrecip :gmatch(DailyPrecip ,“%s”) do
–cannot seem to split and add
totalPrecip = tonumber(const) + totalPrecip
end