Need help extract specific data in xml file please

Hi everyone

here’s what i want to extract from:

<current_value at=“2012-09-08T00:56:13.042288Z”>25.687500</current_value>,

i want the 25.687500 value (current temperature)

here what i try

local TempSalon = extractElement(“current_value at=”, rss, “N/A”) but N/A is showing up in the virtual sensor device.

The thing is that i feed from a arduino some temperature to the site pachube and i want to import it to the vera from a xml file.

Im able to extract the minimum and maximum value from the xml but it’s like this form, there no time and date variable:

<max_value>27.5625</max_value>

thanks
Michael

What does your extract element function look like?

Here’s the code

[code]local function extractElement(tag, xml, default)

local pattern = “<”…tag…“>(.*)</”…tag…“>”

local result = (xml:match(pattern) or default)

return result

end

local url = “http://api.cosm.com/v2/feeds/73762.xml?key=xxxxxxxxxxxxxxxxxxxx

local status, rss = luup.inet.wget(url, 10)

if (status == 0) then

local piscine = extractElement(“current_value at=”, rss, “N/A piscine”)

luup.variable_set(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, piscine, 35)
end[/code]

here the xml

[code]

Temperature maison https://api.cosm.com/v2/feeds/73762.xml live false Piscine 23.250000 28.0625 22.0625 c Dehors c [/code]

thanks

It’s a little rough, but this method uses a fallback pattern to handle 2 of the cases… the two that you have:

[code]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
[/code]

There are more generic XML Parsers for Lua, but this will cover your current use-cases.

Sorry but i’m not an expert in luup, are you able to give me more hint, i try what you say but not sure how to do it right. What the code should look like.

thanks again

My extractElement function replaces your one, and adds the missing functionality to be able to parse both XML element styles.

You need to replace your fn defn with the one I provided… including the 2 constants.

Your a genius it’s working, thanks,

I don’t want to push my luck further but do you know by any chance how to convert this data in fahrenheit with the luup code.

Thanks again

You can adapt the bits you need, but here’s the part where the weather plugin does that conversion:
http://code.mios.com/trac/mios_weather/browser/tags/1.47/I_GoogleWeather.xml#L185

ok,
it working. here the code that work for me:

[code]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 url = "https://api.cosm.com/v2/feeds/73762.xml?key=Api key here "

local status, rss = luup.inet.wget(url, 10)

if (status == 0) then

local TempSalon = math.floor(extractElement(“current_value”, rss, “N/A”) * 1.8 + 32)extractElement(“current_value”, rss, “N/A”)

luup.variable_set(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, TempSalon, 37)
end[/code]

Thanks for your help cause without you…

Hi again,
When i use the command to change some value to ferinheight, there no concideration of the decimal,
like if the value in celcius is 23.8, in ferinheight it will be 74. I want a little bit more precision with decimal. here the code

local TempSalon = math.floor(extractElement(“current_value”, rss, “N/A”) * 1.8 + 32)extractElement(“current_value”, rss, “N/A”)

Thanks again

The [tt]math.floor(…)[/tt] function is rounding (down) the value after the calculation. Not sure what the bit at the end is doing.

Thanks

hey guessed,
do you have any idea to take feed from 2 sensors here the xml

Temperature maison https://api.cosm.com/v2/feeds/73762.xml live false Piscine 24.000000 37.625 -127.0 c Dehors 29.125000 29.125 24.0 c

i will need the current value for id0 and id1 and eventually more sensor.

Thanks again i really appreciate your help

The small convenience function you’ve been using isn’t really setup to handle “repeating” blocks of XML like you have here.

It’s implemented using a Lua function called [tt]string.match()[/tt] that you could use to process the more complex pattern that you have here.

Using the man pages that I’ve linked, along with the existing pattern you’ve got inside your existing extractElement function, you should be able to write a new pattern (and a new function) that does the required extraction.

I strongly recommend you experiment with this using a free-standing Lua interpreter, not the one in Vera, so you can can try tests quickly, and repeat until you get the pattern match correct.

With pachube, i also have the possibility to have the value in cvs here’s the result:

0,2012-09-13T17:26:22.361482Z,23.812500
1,2012-09-13T17:26:22.361482Z,29.687500

Do you think it’s possible and less complicate to import these value to vera?

Nevermind, problem solve, if i use this :

http://api.cosm.com/v2/feeds/73762/datastreams/1.xml?key=xxxxxxxxxxxxxx

i only have the specific data that i want so i use a scene for every sensor.

Thanks alot again

Well, if you ever decide you want to do it with the XML, there’s a sample pattern in the attached Lua file. It works in the command line Lua interpreter.

To use it in Vera, you need to replace the “[tt]<[/tt]” characters in the pattern with “[tt]<[/tt]” but the rest is operational.

[quote=“guessed, post:17, topic:172624”]Well, if you ever decide you want to do it with the XML, there’s a sample pattern in the attached Lua file. It works in the command line Lua interpreter.

To use it in Vera, you need to replace the “[tt]<[/tt]” characters in the pattern with “[tt]<[/tt]” but the rest is operational.[/quote]

If I use “Test Luup code (Lua)” - how can I see in Chrome (in popup for example) print command? Or where I can see results in logs? UI?