Announcement

Collapse
No announcement yet.

Parse XML into Virtual Device

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Parse XML into Virtual Device

    I am trying to take data from an XML file and just put two of the elements into virtual devices. I have been searching up and down and can't seem to find the info I need to do this. I suspect it will take scripting but i really am limited (can read and modify slightly, but not build).

    What I have done is setup a PHP script (there may be a better way of doing this, but its working) that outputs the data from said website into an XML formatted file that I have saved locally to my machine when invoked.

    I need to be able to also refresh the data probably every 5 minutes for a few hours in the morning to keep the data fresh, but don't want to do it all the time all day to avoid doing too many API calls in a day.

    The end results I want is to display this text in HSTouch (where 23 and Mild is the data I am getting from the XML file):

    Current Drive Time is Roughly 23 Minutes, and Traffic Congestion is Mild.

    #2
    Rather than convert to XML, why not use something like my Datascraper script which can extract the data and format the output string as you like.
    Jon

    Comment


      #3
      I am looking at your plugin now to see if I can figure it out.

      I am not converting to XML so much as taking the XML that comes from the API call and storing it into a file.

      Here is the URL I am using (Stripped of sensitive data) just to show you what I am using:

      http://dev.virtualearth.net/REST/v1/...ut=xml&key=API Key

      I have attached the file it creates (stripped of sensitive data). I only care to pull the data from TrafficCongestion and TravelDuration. Also travel duration is in seconds so / 60 to get minutes
      Attached Files

      Comment


        #4
        There are honestly plenty of scripts around for parsing XML - you will need a namespace manager as well and there are scripts there for that as I have posted one before. I would suggest that you look at using XPathBuilder (not that it is a complex path) to get the data you after.

        Comment


          #5
          Jon, you will be having some money coming your way from me. I am still trying to figure out this plugin, but you got me a lot further than i was. Now if i could just figure out how to change the time from seconds into minutes before displayed (results / 60), I would be all set.

          Again THANK YOU!

          Edit: For those that are looking as well, I had to change the API call from XML to JSON for it to read properly
          Attached Files

          Comment


            #6
            At the moment, there is no method to divide by sixty but I can add this as a feature request if you want. It's easy to add to the replacement tag list.

            You do not need to have 2 virtual devices either.

            I assume you have done this as 2 patterns i.e. under Pattern1 and Pattern2?

            Whatever tags are created (I'll assume [0] for status and [100] for seconds), you just create your string as follows:

            DeviceText1=Current Drive Time is Roughly [100] seconds and Traffic Congestion is [0].

            If you want to remove the icon so it just displays text on the device, just add:

            DeviceImage1=-

            i.e. you just add a -
            Jon

            Comment


              #7
              Jon,
              Yea if its not too much to do, I would like it, but I can figure out another way to make it work too. Good thing about this stuff is there is different ways to skin the cat as they say.

              You are correct I built it the way you stated, and I would love to have it in one statement except for the travel time is in seconds and that's not useful for the wife when displayed.

              Also since this is stored as a string (except the # I was able to store as a value), how do i keep the data updated in HSTouch panels I have around the house without a refresh of the page?

              Again thanks for your PI, I see many more uses for it in the future now.

              Comment


                #8
                If you can wait until the weekend then I'll take a look.

                You can still set the device value to the time and construct the string differently i.e

                DeviceText1=Current Drive Time is Roughly [100] seconds and Traffic Congestion is [0].
                DeviceValue1=[100]
                DeviceImage1=-
                Jon

                Comment


                  #9
                  Try V1.0.6 on my site.

                  To divide by 60 use:

                  DeviceText1=Current Drive Time is Roughly [100/60] minutes and Traffic Congestion is [0].
                  DeviceValue1=[100/60]
                  Jon

                  Comment


                    #10
                    Perfect. Thanks Again.
                    Attached Files

                    Comment


                      #11
                      Originally posted by waynehead99 View Post
                      Perfect. Thanks Again.
                      Could you post details on how you pull this info out of Bing? I'd love to do the same thing for my commute.

                      Thanks
                      Al
                      HS 3.0.0.548: 1990 Devices 1172 Events
                      Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                      Comment


                        #12
                        First you will need an API key from Bing:
                        https://msdn.microsoft.com/en-us/library/ff428642.aspx

                        Then I followed this API for route info:
                        https://msdn.microsoft.com/en-us/library/ff701717.aspx

                        It has all the details on what you need to add to the URL. The main thing to make sure you have is optimize=timeWithTraffic to give you the data.

                        For my needs I also just used lat/long points for the waypoints and said avoid highways and tolls since I live next to a toll road and don't use it. The link shows you have a lot of flexibility for way points.

                        You can output XML or JSON.

                        I did the output and read it over to verify that the directions followed the route that my wife takes for work to validate that it was right info.

                        Here is the URL i add that gives me the data (minus the sensitive data)

                        dev.virtualearth"DOT"net/REST/v1/Routes/Driving?wp.0=LAT/LONG&wp.1=LAT/LONG&optimize=timeWithTraffic&routePathOutput=Points&distanc eUnit=mi&output=xml&key=BING API KEY

                        I then just added it in pretty much exactly as Jon listed above with a couple tweaks and got the proper output. The travel time comes in seconds is the reason i had to /60 on the data.

                        Comment


                          #13
                          Also to add on how I am using it. I have an event set to run the script to update every 5 minutes between the times of 6 AM and 9 AM Mon-Fri so that I can display the data real time on HSTouch panel that is in the Master Bathroom. I didn't want to blast Bing all day for no reason.

                          Comment


                            #14
                            Great, thanks!

                            Cheers
                            Al
                            HS 3.0.0.548: 1990 Devices 1172 Events
                            Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                            Comment


                              #15
                              Thanks again, I was able to get it working. I have 3 options for my commute, so my plan is to send the best route out of the three based on traffic to my phone/Pebble as I leave the house. I have the three routes defined using 3 different intermediate waypoints (vwp.1) and now just need to set up the parsing and then create the script to tell me the best option.

                              Code:
                              http://dev.virtualearth.net/REST/v1/Routes/Driving?wp.0=00.00000,-00.000000&vwp.1=00.00000,-00.000006&wp.2=00.00000,-00.0000&optimize=timeWithTraffic&routePathOutput=Points&distanc%20eUnit=km&output=xml&key=BINGKEY
                              Cheers
                              Al
                              HS 3.0.0.548: 1990 Devices 1172 Events
                              Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                              Comment

                              Working...
                              X