Monday, 16 November 2020

Automating location check-ins with Power Automate

Geofencing is the idea of a location service triggering an action whenever a device (i.e. a user's mobile device) moves in or out of an area, and there are lots of great use cases. On the personal/home automation side, you might want to automatically switch the house lights off and activate the alarm when you leave home (think IFTTT or Zapier if you're familiar with those), and on the work side your employer might provide an app which automatically checks you in (or out) of an office location dependent on your location - as just two examples. I was looking at the latter scenario for our internal desk booking app as part of our "controlled use of offices during Covid" plan. 

You can imagine lots of cases where knowing the user's location and whether it has changed can be powerful.

The Power Platform has a couple of ways to tap into this. I'd been researching the Location trigger in Power Automate, and this provides a Flow action named "When I enter or exit an area":

As is normal with geofencing, you specify a location anywhere in the world and create a radius around it. The location is approximate since GPS on the user's mobile device is used, but it works great for most purposes: 



Things were working great for a while, but then I ran into this:

The Location trigger has been removed from Power Automate!

The trigger was only ever in preview, and sadly in the few weeks since I had this article in draft (and had taken the above screenshots) it was removed. At this stage it's unclear whether it will return, and whilst there are traces of it on the internet there are no mentions of it within Microsoft documentation.

So that's a shame! To be quite honest, there was a challenge with the trigger anyway in the Power Platform - it could only ever be used for personal Flows, meaning each user who should use the capability would need to create their own Flow. Clearly this doesn't work for any kind of business solution provided by an organization, but could still be useful for personal automation.

How else can we automate based on user location?

The good news is that Power Automate is still able to understand the user's device location. A fully automated solution which is triggered simply by moving in or out of a defined region is no longer possible, but if you're happy to have users manually click a button on their device, similar automations are still possible. Indeed, in some scenarios this approach may be preferred so that there's a level of human control and opt-in - allowing the user to avoid triggering a process if the circumstances don't warrant it (e.g. leaving the area radius briefly to pick up lunch). 

So, let's take a look at how we can build Power Platform apps which account for the user's location.

Using Flow buttons to log a location visit

Flow buttons provide a great way to build a mobile app with a super-simple user interface - without having to delve into any kind of coding or the world of native iOS or Android development. In my example below, I'm using a simple button and a very simple form. But first, the prerequisites.

The first requirement for a solution like this is for users to have the Power Automate app installed on their mobile device. Your organization could push it out using a MDM or MAM solution, or it's available in both the Apple and Google app stores:

The user will need to sign-in to the app with her Microsoft 365 identity. The other important thing is that location services are enabled on the device for the Power Automate app - an obvious necessity if we are collecting and logging the location. 

Once in the app, the user would go to the "Buttons" area in app using he navigation bar along the bottom.

Any Flows which have been created using the "Manually trigger a Flow" trigger will appear in here: 



In my solution, I have a Flow to log details of a location visit - this is named "Report location status" in the image above, and you can also see some other Flows which also use the the button trigger. As you've probably gathered, these are known as "Flow buttons" and provide a really quick and easy way of manually triggering a process. No need to create and deploy a custom app - instead we can piggy back on what the Power Platform provides. 

When the button is clicked, some information can be selected to feed into the process. In my example of logging a location visit, the Flow asks for a "status" to be collected:


In the case of my solution, when the user submits this "location report" I store the details in a SharePoint list. Power Automate has done the hard work of automatically deriving the user's location at the time of pressing the button, and with a little column formatting magic I can display a small map for the location instead of just the address text:

 
 
So that's it! With fairly minimal effort in the Power Platform we have a mobile app which can collect the user's location, collect additional information and log it into a central store such as a SharePoint list in Microsoft 365.
 

How do we build that?

We've covered what the user would see, but what is needed in Power Automate to create this? We start by creating a Flow with the "Manually trigger a flow" trigger. Notice in my case I've added a single input named "Status", and supplied some help text:


You could actually stack several of these inputs and essentially create a mini-form which is presented to the user when they hit the button - which becomes quite powerful when you consider that no coding is required and we don't even need a Power App. 

The next step in the Flow is simply to log the item to SharePoint. I have a list ready to go with appropriate columns, and I just need to configure the Flow action to store the data in each:

 
The important thing is that several tokens are available from the trigger, including:
  • User name
  • User email
  • Timestamp
  • Date
  • fullAddress - this is the full address of the auto-derived location of your user
  • Many address sub-components:
    • Street
    • City
    • State
    • Postal code
    • Country
    • Latitude
    • Longitude
  • Any inputs you have added (e.g. "Status") in my case 
The final step in my Flow is to send a confirmation to the user that the report was logged successfully:


Which results in this appearing on the device:


So we've managed to capture the user's location and status report of the location, and confirmed back to them that the data was saved. 

Summary


The Power Platform has many amazing facilities for building applications, and that's especially the case for simple mobile applications. The ability to tap into device features such as location and the camera mean that you can build very capable apps quickly, without code - and certainly without all the hassles of native app development and distribution. In this post we looked at how Flow buttons can be used to quickly trigger a process from the mobile app, and how to capture the user's location at the time. 

Unfortunately the "When I enter or exit an area" Power Automate trigger that was in preview hasn't made it to release - but we hope it comes back because that would unlock some great scenarios around automation and the user's location. Come on Microsoft!