Near the Luas – PushOver

Since we moved to Ireland and have been utilizing the LUAS as our core transportation thing, I’ve wanted an alert to pop-up on my watch telling me the train times when I’m near a stop. Near being like 2-5 minutes from the stop walking speed. I’ve toyed around with the Idea, but never put it all together. Many months ago I wrote a PowerShell function to obtain LUAS train times. We generally have this running on the kitchen computer to give us an idea of when to leave the house so we don’t have to wait too long at the Gallops.  Around the same time I wrote my LUAS Time function, I wrote another few functions to send pushover notifications, and obtain DarkSkys weather data.

I was on a mission to script and automate a few data lookups I do regularly. The thinking For DarkSkys and Pushover was, I wanted to know how to dress for runs into work at 4am before getting dressed and looking outside. The script would ping DarkSkys at 3:40am grab the next hour of weather data and if it was going to be lower than 9c or rain in the next hour it would send me a notification. I used that for a while, then switched the same functionality to Microsoft Flow. Flow had a similar feature set without the need to run anything locally. It checks weather and emails me if the weather is cold or raining.

IFTTT – Setup

IFTTT_Screen_Shot.JPG

Things are slow at work today, and it’s a designated training day for me. Figured working on a scripting/coding project would be valuable as training, right? Wax on Wax off train the muscle Memory and all fo that stuff. Armed with some time and a vision I started researching how to trigger things on an iPhone via location. The winning tool in the search results appears to be the If This Then That (IFTTT) application from https://ifttt.com/. IFTTT has a built-in Trigger based on phone location; perfect.

The First step of the work was, of course, installing the IFTTT application on my phone and configuring it to allow real-time access to my location information. Always a bit spooking letting anything track your location in real time. The positive benefits of this click seems worth it to me. One of my visions had the application drop my location every few seconds somewhere into a file where it could be processed against the GPS cords of the lUAS stops and dynamically update me. – Sad face, the only option in the app is; into and or out of a GeoFence. Which is not too sad, as geo fence will work for my mission. It will, however, take more manual work setting the fence around each stop manually.

Maybe someday I’ll write a full-blown phone app with the ability to dynamically look at a location near a stop and trigger, but that day is not today. Today we take the simpe hack route.  With the trigger picked I next hunted around IFTTT applets looking for something the tool could update that was easy to track via a PowerShell script. What I chose to use was the Dropbox applet. The IFTTT Dropbox applet has an option to append a line to the end of a file stored in Dropbox. Adding a line to the end of a file I already sync between a few computers seems like a simple enough method to use as a trigger for a PowerShell script.

How it is going to work in my head in a few steps:

  1. IFTTT will trigger once my phone hits a geofence around a LUAS stop (manually configured per stop I care about).
  2. The IFTTT Dropbox Applet will open a CSV file in my Dropbox and add my current location to the end of the file which includes a manual configured StopID.
  3. After the line is added IFTTT will close the file.
  4. The file change will trigger Dropbox to sync the file to my local computers.
  5. My local machine will be running a PowerShell script checking the file every 30 seconds. When it sees a new line it will read the new line, check the LUAS times based on the data, and forward that data to Pushover.
  6. Pushover will then forward an alert to my Phone. My phone will then push the alert to my Apple watch.

Like five steps, super simple right? Having the marker file on a few machines means the script to read it can be run from any machine with access to my Dropbox; I consider this a Plus. The file will need to sync from Dropbox before Powershell can see the change and act on it; Slight negative, but we can plan for it.

 

Powershell

Most of the big bits of Powershell,  PushOver and LUAS times,  I’d written months ago. Today’s PowerShell is simpler, it needs to read a CSV file every, say, 30 seconds, and check for a new line in the file. If a new line is detected the script will lookup the LUAS times based on the StopID and send out a Pushover notification to my phone. I added in a few other bits to the script because I like tracking and clean and notes. The other bits:

  1. The script write lines to the screen to provide a clue something is happening.
  2. Based on the screenshot above saying IFTTT will create a new file at 2MB I figured I’d toss in a few lines to rename the file and create a new one when the tracking file is larger than 1.5mb.
CodeBlock.JPG
screen shot of the code and text file before final tweaking

Results

I put is all together and tested if the script would fire from work without IFTTT writing to the file, or triggering based on GeoFence. IFTTT did not appear to have a button to trigger a geo-fence event I could find. In testing with a manual text file everything worked. In testing the watch looks like the below image with all of the times for both inbound and outbound trains. You can tell the difference by destination.  On the run home from work, I ran past 2 LUAS stations I’d added in as triggers in IFTTT. Happy to say, both of the stops triggered PushOver notifications to show up on my watch. Even better, they triggered very close to where I set the edge of the fence. Telling me there is not too much delay with all of the steps involved.

WatchPush.JPG

The above image is using the full Stop name returned from the PowerShell output. The “LUAS ” words took a bunch of space on the watch and annoyed me.  I trimmed them out after getting home adding a .trim(“LUAS “) to the text builder loop. The output is much cleaner on the watch without the 5 extra characters.

foreach($luastimein$luastimes)
{
$pushovermessage=$pushovermessage+”$(($luastime.Destination).trim(“LUAS “)) in $($luastime.duetime)`n”
}
I was going to stick the script on Github, but I accidentally reformatted my phone this week and forgot to install the 2fa key generator, and I am all out of one-time use keys because those seem to be missing from the folder they should be in. Sad face. – never mind I figured something out. Which is good because github’s response was, find the files or we can delete your account and let you make a new one. I respect their stance on authentication. My recovery keys are now stored in Keepass where they should have been.
Code for the PowerShell can be found here.

Leave a Reply