Xml

Any tools available to parse XML in the agent. I have an agent the goes out to earth tools to get sunrise and sunset, the return data is in XML.

Thoughts?

I don’t have an answer for XML encoding, but if you don’t mind using another service, I can give you code to get sunrise and sunset from the Weather Channel API. That comes back as JSON, which is somewhat easier to use.

Great, sounds perfect.

I think I found it, I have key and it now returning a JSON string with sunset. Thanks.

Here it is, thanks again for the tip. You’ll need a key, {YourKeyHere} in the URL. The for loops just show you what is in the table.

http.get(“http://api.wunderground.com/api/{YourKeyHere}/astronomy/q/Colorado/denver.json”).sendasync(function(resp) {
// log the response, should be 200: OK is things worked
server.log(“There are : " + resp.len() + " in the table”);
//foreach(idx,val in resp)
//server.log(“index=”+idx+" value="+val+"
");
server.log(format("%i: %s", resp.statuscode, resp.body));
local astroTable = http.jsondecode(resp.body);
server.log(“There are : “+ astroTable.len() +” in the table”);
foreach (idx, val in astroTable)
{
server.log(“index=”+idx+" value="+val);
}
foreach (idx, val in astroTable.sun_phase)
{
server.log(“Sun index=”+idx+" value="+val);
}
foreach (idx, val in astroTable.sun_phase.sunset)
{
server.log(“SunSet index=”+idx+" value="+val);
}
server.log(“Sunrise will be at " + astroTable.sun_phase.sunrise.hour +”:" + astroTable.sun_phase.sunrise.minute);
server.log(“Sunset will be at " + astroTable.sun_phase.sunset.hour +”:" + astroTable.sun_phase.sunset.minute);
});

You got it… Here is some slightly different code that I modified from one of Tom’s projects: I use it to turn lights on and off at sunrise and sunsets…

`//Weather Channel***********
// Weather Channel Trigger Agent
// Uses Wunderground API to obtain current time and sunrise/sunset information
// for use in triggering events.
// Modified from code available at the Electric Imp GitHub page.

server.log(“Weather Agent Running”);

// Add your own wunderground API Key here.
// Register for free at http://api.wunderground.com/weather/api/
local myAPIKey = “”;
local wunderBaseURL = “http://api.wunderground.com/api/"+myAPIKey+"/”;

// Add the zip code you want to get information for here.
local zip = “17601”;

// The wunderground API has a lot of different features (tides, sailing, etc)
// We use “astronomy” to indicate we want the information available for solar
// and lunar events.
local reportType = “astronomy”;

function getConditions() {
server.log(format(“Agent getting current conditions for %s”, zip));

// cat some strings together to build our request URL
local reqURL = wunderBaseURL+reportType+"/q/"+zip+".json";

// call http.get on our new URL to get an HttpRequest object. Note: we're not using any headers
server.log(format("Sending request to %s", reqURL));
local req = http.get(reqURL);

// send the request synchronously (blocking). Returns an HttpMessage object.
local res = req.sendsync();

// check the status code on the response to verify that it's what we actually wanted.
server.log(format("Response returned with status %d", res.statuscode));
if (res.statuscode != 200) {
    server.log("Request for Weather Channel API data failed.");
    imp.wakeup(600, getConditions);
    return;
}
// hand off data to be parsed
local response = http.jsondecode(res.body); 
// load table "astronomy" with the moon_phase JSON data
local astronomy = response.moon_phase;
// get and format Sunrise time string
local sunrise = "";
sunrise += (astronomy.sunrise.hour + ":" + astronomy.sunrise.minute);
server.log("Sunrise: " + sunrise);
// get and format Sunset time string
local sunset = "";
sunset += (astronomy.sunset.hour + ":" + astronomy.sunset.minute);
server.log("Sunset: " + sunset);
// Get and convert the current time to minutes since zero hour.
server.log("Current time: " + astronomy.current_time.hour + ":" + astronomy.current_time.minute);
local mszh = (astronomy.current_time.hour.tointeger() * 60) + (astronomy.current_time.minute.tointeger());
server.log("Minutes since zero hour: " + mszh);
// convert sunset to minutes
local sunsetmin = (astronomy.sunset.hour.tointeger() * 60) + (astronomy.sunset.minute.tointeger());
server.log("Sunset in Minutes: " + sunsetmin);
// convert sunrise to minutes
local sunrisemin = (astronomy.sunrise.hour.tointeger() * 60) + (astronomy.sunrise.minute.tointeger());
server.log("Sunrise in Minutes: " + sunrisemin);

}
getConditions();
`