Parsing XML help

artpics

New member
Hi everyone thanks for the great resources on this forum
i need a bit of help

i want to get my Apex XML into my home automation program.

so i can turn on my AC if the temp gets too high etc.

here is the code i am using.

Code:
set gatewayAddress to "http://7x.1xx.9x.xxx"
set thePort to "1001"
set output_path to "~/Desktop/ApexData.xml"


set fileURL to gatewayAddress & ":" & thePort & "/cgi-bin/status.xml"
do shell script "curl " & fileURL & " -o " & output_path

set XMLfile to "~/Desktop/ApexData.xml"


tell application "System Events"
* *set xml_data to contents of XML file XMLfile
* *tell xml_data to set live_data to XML element "LiveData"
* *tell xml_data to set power to XML element "Power" of live_data
* *tell xml_data to set total to XML element "Total" of power
* *tell xml_data to set power_now to XML element "PowerNow" of total
* *set power to value of power_now
* *tell application "IndigoServer"
* ** *set value of variable "current_power" to power as integer
* *end tell
end tell

currently the XML is saved to my desktop every 15 mins great.

the problem is matching this code to the outlet names of the apex.
Code:
tell application "System Events"
* *set xml_data to contents of XML file XMLfile
* *tell xml_data to set live_data to XML element "LiveData"
* *tell xml_data to set power to XML element "Power" of live_data
* *tell xml_data to set total to XML element "Total" of power
* *tell xml_data to set power_now to XML element "PowerNow" of total
* *set power to value of power_now
* *tell application "IndigoServer"
* ** *set value of variable "current_power" to power as integer
* *end tell
end tell

if i create a variable in my home automation software and call it current_temp instead of current_power it should put the data into my HA software. but from the code above i do not know what to delete or what names to change.

Any help truly appreciated thanks.

here is the thread where i got the code from.
http://www.perceptiveautomation.com/userforum/viewtopic.php?t=5002


here is my XML from the Apex.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<status software="4.12L_2A12" hardware="1.0">
<hostname>apex</hostname>
<serial>AC4:04376</serial>
<timezone>-8</timezone>
<date>05/07/2012 10:27:05</date>
<power>
<failed>04/26/2012 08:55:46</failed>
<restored>04/26/2012 08:56:20</restored>
</power>
<probes>
<probe>
<name>Temp</name>
<value>78.2</value>
<type>Temp</type>
</probe>
<probe>
<name>pH</name>
<value>8.20</value>
<type>pH</type>
</probe>
<probe>
<name>Amp_3</name>
<value>0.0</value>
<type>Amps</type>
</probe>
</probes>
<outlets>
<outlet>
<name>zSndAlm_I6</name>
<outputID>0</outputID>
<state>AOF</state>
<deviceID>base_Alarm</deviceID>
</outlet>
<outlet>
<name>zSndWrn_I7</name>
<outputID>1</outputID>
<state>AOF</state>
<deviceID>base_Warn</deviceID>
</outlet>
<outlet>
<name>zEmailAlm_I5</name>
<outputID>2</outputID>
<state>AOF</state>
<deviceID>base_email</deviceID>
</outlet>
<outlet>
<name>HighPump</name>
<outputID>3</outputID>
<state>AON</state>
<deviceID>3_1</deviceID>
</outlet>
<outlet>
<name>Skimmer</name>
<outputID>4</outputID>
<state>ON</state>
<deviceID>3_2</deviceID>
</outlet>
<outlet>
<name>LowPump</name>
<outputID>5</outputID>
<state>ON</state>
<deviceID>3_3</deviceID>
</outlet>
<outlet>
<name>Heater</name>
<outputID>6</outputID>
<state>AON</state>
<deviceID>3_4</deviceID>
</outlet>
<outlet>
<name>Vortech</name>
<outputID>7</outputID>
<state>AON</state>
<deviceID>3_5</deviceID>
</outlet>
<outlet>
<name>Fan</name>
<outputID>8</outputID>
<state>AOF</state>
<deviceID>3_6</deviceID>
</outlet>
<outlet>
<name>Reactor</name>
<outputID>9</outputID>
<state>AON</state>
<deviceID>3_7</deviceID>
</outlet>
<outlet>
<name>Radion</name>
<outputID>10</outputID>
<state>AOF</state>
<deviceID>3_8</deviceID>
</outlet>
<outlet>
<name>Feed</name>
<outputID>11</outputID>
<state>OFF</state>
<deviceID>Cntl:A1</deviceID>
</outlet>
<outlet>
<name>Maintain</name>
<outputID>12</outputID>
<state>OFF</state>
<deviceID>Cntl:A2</deviceID>
</outlet>
<outlet>
<name>z----</name>
<outputID>13</outputID>
<state>OFF</state>
<deviceID>Cntl:A3</deviceID>
</outlet>
<outlet>
<name>z----------</name>
<outputID>14</outputID>
<state>OFF</state>
<deviceID>Cntl:A4</deviceID>
</outlet>
</outlets>
</status>
 
Interesting... I've been seriously considering a TED5000, since the monitoring that came with my solar system is pathetic.

I can sort of follow along with that Indigo code. I searched for and found an example of TED5000 XML output so I could see the structure and schema. Comparing that with Apex XML, it seems that to get it to read temp, you'll need to do something like this:

Code:
tell application "System Events" 
* *set xml_data to contents of XML file XMLfile 
* *tell xml_data to set probes to XML element "Probes" 
* *tell xml_data to set probe to XML element "Probe" of probes 
* *tell xml_data to set temperature to XML element "Value" of probe 
* *set tanktemp to value of temperature 
* *tell application "IndigoServer" 
* ** *set value of variable "Temp" to tanktemp as integer 
* *end tell
That should get you the temperature, assuming that the temp probe is the first probe listed. In order to get other values, you'll have to figure out how to recursively parse the probes tags, and if desired, the outlet tags.
 
I'm hoping that half of the confusion is just due to XML being pasted into a forum not displaying well.
 
Thanks for some help RussM and the research.

i get an error

Result:
error "System Events got an error: Can't get XML element \"Probes\" of contents of XML file \"Macintosh HD:Users:orit:Desktop:ApexData.xml\"." number -1728 from XML element "Probes" of contents of XML file "Macintosh HD:Users:rt:Desktop:ApexData.xml"


i have tried using probes instead of Probes and a few other combinations but still get the same error.
 
I don't see any provision for authentication. Normally, the Apex requires authentication even to get to XML data. Do you have the Open XML option enabled? It's only available in v4.10 and later firmware. It allows XML data to be accessed anonymously.
 
And yep, I goofed.... I capitalized tags that are lowercase in the raw XML

* *tell xml_data to set probes to XML element "probes"
* *tell xml_data to set probe to XML element "probe" of probes
* *tell xml_data to set temperature to XML element "value" of probe
 
In all honesty, you'd probably be better off posting Apex status XML in that other forum and getting ideas of how to parse it from the people who know those HA controllers.

Or just buy a iPad or iPod Touch and use the app ;)


If I do get a TED5000, I'll be using PHP to collect, parse, and graph power production and consumption data, just like I do for Apex/AC3/RK data on http://www.reeftronics.net
 
Well thanks for trying Russ. i think the problem now is how the XML is being encoded. that is why it cannot find the probe

i will post the code on the HA forum and post my results here if i get one.
i already have the iPad and iPhone app and use reeftronics nice code and functionality.

sig.php


Ted seems like a great bit of hardware good luck.
 
Well i got it going.
now the tank is protected.

if the tank temp goes above 80 it turns on my AC and sets it to 78 and sends me an email
if the tank temp goes below 77 it turns on my Heating to 78 and sends me an email
if the humidity is 100% it turns on the fans until my threshold is reached.
:dance:

etc etc etc .....

-------------

set apex to " /usr/bin/curl 'http://myip.com:myport/cgi-bin/status.xml' > /tmp/apex.xml"
do shell script apex

tell application "System Events"
set file1 to XML file "/tmp/apex.xml"
tell file1
tell XML element "status"
tell XML element "probes"
-- there are multiple probe elements. First is default, but you can specify whichever you want
tell (first XML element whose name is "probe")
set theValue to value of XML element "value"
end tell
end tell
end tell
end tell
end tell

tell application "IndigoServer"
set value of variable "ApexTemp" to theValue as integer
end tell
 
You've probably already considered this but let me add it anyway. If a temp probe fails (or any probe for that matter) it will give crazy values. Be careful about having your heat or cooling respond and try to fix something that's not broken.

For example, a failed temp probe will routinely read 20 degrees. Would be a shame to have your furnace run all day and bake your house. Make sure there's a second independent t-stat that will prevent that.
 
Thanks i do have a fail safe installed in case the temp probe fails.
also got the PH being sent when i have time i will work on the amps
so if amps =0.0 for >2 hours send me an email this would mean a pump or heater had failed :thumbsup:

You've probably already considered this but let me add it anyway. If a temp probe fails (or any probe for that matter) it will give crazy values. Be careful about having your heat or cooling respond and try to fix something that's not broken.

For example, a failed temp probe will routinely read 20 degrees. Would be a shame to have your furnace run all day and bake your house. Make sure there's a second independent t-stat that will prevent that.
 
yes i am Russ.


Set OFF
If Temp > 80.0 Then ON
If Temp < 77.5 Then ON
If pH > 08.35 Then ON
If pH < 08.10 Then ON
 
Back
Top