LUA, tonumber give me nil. Why ?
Posted: Saturday 01 October 2016 10:15
Hello everyone,
Could someone give me a quick hint on this issue ?
I have my own weatherstation connected on the Web. I can pull an XML file providing all the values.
However, when I extract the values from the XML, they are strings and not value. When I convert the string "17.3" with tonumber, it gives a nil reply.
Here is the code:
___________________________________
commandArray = {}
function XML_Capture(cmd,flatten)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if flatten then
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
end
return s
end
debug = true
Device_IP = "192.168.1.45"
XML_string=XML_Capture("curl -s 'http://"..Device_IP.."/weather/wxrss.xml'",1)
if debug == true then
print("Reading values from: 'http://"..Device_IP.."/status.xml'")
end
temp1 = string.find(XML_string,"<temp>")
temp2 = string.find(XML_string,"</temp>") -1
uv1 = string.find(XML_string,"<UV>")
uv2 = string.find(XML_string,"</UV>") -1
wind1 = string.find(XML_string,"<wind>")
wind2 = string.find(XML_string,"</wind>") -1
rain1 = string.find(XML_string,"<rainrate>")
rain2 = string.find(XML_string,"</rainrate>") -1
temp = string.sub(XML_string,temp1,temp2)
UV = string.sub(XML_string,uv1,uv2)
wind = string.sub(XML_string,wind1,wind2)
rain = string.sub(XML_string,rain1,rain2)
print ('Temp: ' .. temp .. ' UV: ' .. UV .. ' Wind: ' .. wind .. ' Rain: ' .. rain)
temp3 = tonumber(temp)
commandArray[1] = {['UpdateDevice'] = '366'.."|0|"..temp3..";"..temp3} -- send updated values to Domoticz
return commandArray
______________________________________________
Could someone tell me why the temp3 variable has a nil value ?
Thanks in advance
Regards
Sam
Could someone give me a quick hint on this issue ?
I have my own weatherstation connected on the Web. I can pull an XML file providing all the values.
However, when I extract the values from the XML, they are strings and not value. When I convert the string "17.3" with tonumber, it gives a nil reply.
Here is the code:
___________________________________
commandArray = {}
function XML_Capture(cmd,flatten)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if flatten then
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
end
return s
end
debug = true
Device_IP = "192.168.1.45"
XML_string=XML_Capture("curl -s 'http://"..Device_IP.."/weather/wxrss.xml'",1)
if debug == true then
print("Reading values from: 'http://"..Device_IP.."/status.xml'")
end
temp1 = string.find(XML_string,"<temp>")
temp2 = string.find(XML_string,"</temp>") -1
uv1 = string.find(XML_string,"<UV>")
uv2 = string.find(XML_string,"</UV>") -1
wind1 = string.find(XML_string,"<wind>")
wind2 = string.find(XML_string,"</wind>") -1
rain1 = string.find(XML_string,"<rainrate>")
rain2 = string.find(XML_string,"</rainrate>") -1
temp = string.sub(XML_string,temp1,temp2)
UV = string.sub(XML_string,uv1,uv2)
wind = string.sub(XML_string,wind1,wind2)
rain = string.sub(XML_string,rain1,rain2)
print ('Temp: ' .. temp .. ' UV: ' .. UV .. ' Wind: ' .. wind .. ' Rain: ' .. rain)
temp3 = tonumber(temp)
commandArray[1] = {['UpdateDevice'] = '366'.."|0|"..temp3..";"..temp3} -- send updated values to Domoticz
return commandArray
______________________________________________
Could someone tell me why the temp3 variable has a nil value ?
Thanks in advance
Regards
Sam