Help with weather (precipitation) forecast script

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

end

Leaving aside the fact that you are using regular expressions to parse XML (ask the Internet why that’s a bad idea), are you sure about the double-mention of DailyPrecip in the quoted code?

Your code is quite long. You could probably entice more forum members to help you pinpoint the bug if you reduced it to the minimal length that demonstrates the problem. For instance, if you know your XML is being fetched properly and that you are extracting DailyPrecip properly, replace all that with some code to assign a literal string to DailyPrecip.

I am sure there is a better way to parse XML in lua - not sure how. The code works upto getting the DailyPrecip. I remove the tags and replace the tag with a space. For today, I end up with:
0.00 0.03 0.52 0.10 0.01 0.02 0.00 0.00 0.00 0.00 0.00 0.00
In the for…do loop, I am trying to split this up and add up the precipitation. In the code above, the string splitting and addition do not work.

[quote=“smadhu, post:3, topic:175089”]For today, I end up with:
0.00 0.03 0.52 0.10 0.01 0.02 0.00 0.00 0.00 0.00 0.00 0.00
In the for…do loop, I am trying to split this up and add up the precipitation. In the code above, the string splitting and addition do not work.[/quote]

Thank you, that’s a more manageable problem than reading 30 lines of code.

The common idiom for breaking up a string into words in Lua broken by whitespace is:

str="0.00 0.03 0.52 0.10 0.01 0.02 0.00 0.00 0.00 0.00 0.00 0.00"
for value in str:gmatch("([^ ]+)") do print(value) end

You already know about tonumber() so I’m sure you’ll be able to piece it together from here.

Thanks-works like a charm. Using with a variable container for average prob of precipitation and quantity.