Announcement

Collapse
No announcement yet.

You Can Get Current Drive Time Using Python's WazeRouteCalculator

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

    You Can Get Current Drive Time Using Python's WazeRouteCalculator

    I couldn't find any posts discussing Waze. Hope I didn't duplicate someone else's efforts...

    Over the weekend, I installed the HS WeatherXML plugin (holy crap, that thing is huge and awesome!) to add some weather info to my "good morning" TTS script and thought it'd be cool to find a way to somehow add my current drive time to work like Waze provides. WazeRouteCalculator does just that!

    I'm running HS .435 on Windows 10, with Python 3.7.0 & pip 10.0.1. This should work just as well on Linux but the process of calling it and reading the output will be slightly different. If anyone wants to convert and try on Linux, that'd be great!

    Install the package via DOS CMD window:
    Code:
    pip install WazeRouteCalculator
    Create a python script that makes the call to Waze. Name it something like: DriveTimeToWork.py
    Code:
    import WazeRouteCalculator
    
    from_address = "123 S Some Home Street, Your City, State Abbreviation"
    to_address = "456 Some Work Street, Some City, State Abbreviation"
    region = "US"
    route = WazeRouteCalculator.WazeRouteCalculator(from_address, to_address, region)
    routeTime, routeDistance = route.calc_route_info()
    print( int(round(routeTime,2)) )
    Create a HS event that uses VBScript to call the Python script. Name it something like: DriveTimeToWork.vb
    Code:
    Sub Main(parm as object)
    
        Dim wsh As Object = CreateObject("WScript.shell")
        Dim oExec = wsh.Exec("Python C:\Users\Randy\scripts\DriveTimeToWork[COLOR=#FF0000].py[/COLOR]")
        Dim routeTime = oExec.StdOut.ReadLine
    
        hs.writelog("WAZE", "Current drive time to work is " & routeTime & " minutes.")
        hs.speak("Your current drive time to work is " & routeTime & " minutes.")
    
    end sub
    I believe the region is listed as optional, but it returned errors when I omitted it. Other regions are listed in the links below. I originally saved the DriveTimeToWork.py script in the HS \scripts folder but had some issues with my early attempts to call it, so moved it to a location without spaces in the path as a troubleshooting step. I'm sure it would work from within the \scripts folder, I just haven't tried moving it around now that it's working. I can call the Python script directly in a DOS window without prefixing it with 'Python ' as I have Python in my path, but it wouldn't work calling it without 'Python ' from the VBScript. Probably need to figure out how to get environment variables to load when VB drops down into a command line. Then again, it finds Python when I type it without including a direct path to it. No biggie to include it.

    I know Waze can accept different departure times to calculate what traffic should be like later. I'm hoping to be able to read my calendar to find the first meeting I have each day and then subtract the drive time that would then tell me what time I have to leave to get to work on time for that meeting.

    Updates could include passing in from/to addresses from HS to the Python script, as well as time of departure noted above. Then I could create a VR script like, "How long is the drive from home to Santa Monica pier", if I had addresses stored in HS. I also run a version of this VBScript every 10 minutes that simply stores the minutes in a HS device so I can see it at a glance via the web interface or HS3Touch. I have a room named, "Randy" for statuses such as Presence (home/away), Consciousness (awake/sleeping), Location (which room in my home I'm in), iPhone (on local network or not).Click image for larger version  Name:	Screen Shot 2018-09-10 at 9.32.18 AM.png Views:	1 Size:	15.0 KB ID:	1246634

    Hope someone finds this useful! Enjoy your drives to work!

    Here's the little 200x200 icon I made for it: Click image for larger version  Name:	waze.png Views:	1 Size:	22.7 KB ID:	1246635
    Click the "Add a range graphic" under the "Status Graphics" tab for the device with values like Start: 1 End: 120 (I hope it doesn't take 120 minutes for your drive to work!) Can also click, "Add new range value" Start: 1 End: 120 and set the Suffix: min

    Some links:
    Github repository for the Python package: https://github.com/kovacsbalu/WazeRouteCalculator
    Discussion thread on HomeAssistant forum with other options/ideas: https://community.home-assistant.io/...pdate/50955/21
    Last edited by RandyInLA; September 10, 2018, 12:26 PM.
Working...
X