Setting Alerts For NightScout/MongoDB Using Zapier and Microsoft Flow

Standard

An Introduction to NightScout

As a Type 1 Diabetic, I need to monitor my blood sugar pretty much 24/7. These days I do it with a Continuous Glucose Monitor (CGM) which sits on my arm and transmits my sugar levels, every five minutes, to my phone.

Here is the sensor and transmitter on my arm; a modified Dexcom G5.

IMG_20181002_104340[1]

and here is the output using a program called xDrip+ (an open source app for Android phones).

Screenshot_20181006-011002[1]

As mentioned in my article on looping, there is a thriving online community building better ways to manage diabetes both for people like me with the disease but also, as Type 1 Diabetes often affects children, for carers to manage the disease. One of these innovations, and a key piece of technology within the looping community is NightScout: an open source web page you can build to pull your data up onto the internet. Here is the same data stream on my NightScout web page.

image

The database technology behind NightScout is called MongoDB. MongoDB is big in the open source community but not so much in the Microsoft world. In this article I will walk through how to connect to this underlying MongoDB database using Zapier and Microsoft Flow so you can set up things like PowerBI reports, alerts when your blood glucose is out of range or even have a stream of data being emailed or tweeted to someone who wants it.

image

While NightScout can be set up on Azure, I had some real problems getting it to work so I went to the other option: Heroku. The irony that I am using a SalesForce subsidiary to house my data is not lost on me. As most people set up their NightScout on Heroku, this will form the basis for my set up instructions but the principles I am showing will work just as well on an Azure-hosted NightScout site as well.

The Easy but Expensive Way: Zapier

Zapier is by far the easiest way to connect to the MongoDB data. It literally does all the work for you. Firstly, we need to sign up for a free account with Zapier. Once this is done we will want to “Make a Zap!”.

image

For the trigger, we want MongoDB and we want it to trigger when a new Document is added.

image

Basically this means the Zap! will fire every time a new entry is transmitted from my CGM, received by xDrip+ and uploaded to NightScout’s MongoDB. Next we will need to set up the connection to our MongoDB database.

image

Fortunately, the NightScout settings have everything we need. If we go to our Heroku account, select the NightScout app, select the Settings tab and ‘Reveal Config Vars’, the one we want is ‘MONGODB_URI’. This will be in the format: mongodb://<Username>:<Password>@<Host>:<Port>/<Database>.

Transfer these values across and it should just work. Next we set up the options for the MongoDB database. The collection we want is ‘entries’

image

Next it will want a sample to understand the format of the stored data. Go ahead and let it pull a record. Once this is completed, our trigger is finished. Next we specify what happens when the trigger occurs i.e. what do we want to happen when a new reading hits MongoDB.

There are a wealth of Actions to choose from but, for simplicity, I will choose “GMail – Send Email”. Again, the process is pretty simple and mirrors the setup of the trigger. The only trick to mention is clicking the icon to the right of the field if you want to reference data from the trigger. In the case of the MongoDB data, the blood glucose level is called ‘sgv’ and is stored in the US mg/dl and not mmol/l.

image

Our final steps will be to name our Zap! and activate it.

Once done, the Zap! will query MongoDB every 15 minutes and bring back the new values and send an email for each one.

image

So far everything I have described is free. xDrip+ and NightScout are free and MongoDB and Zapier also have free accounts. So why is this the expensive option? The reason is, as soon as we want to make our Zap! a little more sophisticated, we need to upgrade our Zapier account. The free account allows you to create two-step Zap!s but if you want a condition e.g. only send an email if the sgv value is greater than or less than a specific value, you need to upgrade to the US$25/month account. Fortunately, there is an alternative.

The Trickier but Cheaper Way: Microsoft Flow

Sadly, Microsoft Flow does not have a Connector for MongoDB but it does have the ‘http’ step which serves the same purpose, using the MongoDB REST API.

For Flow, the first step is to set a trigger. While the LogicApps version of the ‘http’ step has the ability to set a recurrence when the REST API is called, the Flow version does not have this so we need to set a Schedule-Recurrence trigger.

image

Just like the Zap!, we will poll every 15 minutes.

Next we set up our ‘http-http’ step. This is the tricky bit.

We are getting data so our method is GET. The URL is what we use to call the MongoDB REST API. In our case we use it to bring back our data. The format of the URI I am using for this example is:

https://api.mlab.com/api/1/databases/<database>/collections/entries?l=1&f={”sgv”:1}&s={date:-1}&apiKey=<api-key>

Thank you to Ravi Mukkelli and Olena Grischenko for figuring this part out. Full documentation for the REST API can be found here. To translate the URI, I want it to return the first record (l=1), showing just the ‘sgv’ field where the collection is sorted in descending date order.

The API KEY is available from your MongoDB account. Simply to go your Heroku account, select your NightScout app and click through to mLab MongoDB.

image

Click on the User link.

image

and your API KEY will be shown on the next page. Also remember to enable Data API Access which can be done just below where you see the API Key.

image

Your Flow should now look something like this.

image

This will get data every 15 minutes in the form of JSON. JSON is a way to represent data in a text format. Think of it like a generic and adaptable alternative to XML which is a generic and adaptable form of HTML (the thing that web pages are made of).

To make use of the data, we need to parse it (translate it into something useful). To do this we add a new step. Searching for JSON shows the Data Operations – Parse JSON step. The content is the Body from the http step. For Flow to understand the fields, it needs to get a sample of the data. To feed it this data we click the “Use sample payload to generate schema” link.

image

To get this sample, all we need to do is paste the URI from the http step into a browser. You should get something like this:

[ { “_id” : { “$oid” : “5bb8c4cd44df60074eef234e”} , “sgv” : 77} ]

Your end result should look like this.

image

Finally, we send out email via Gmail. At some point of setting up your Gmail – Send Email step, Flow will likely insert a loop. This is because, in principle, our query could have returned more than one record. As my  query forces the return of only one record, the loop will iterate once and is not really needed. It is inelegant but it will work.

Also, by default the only field that gets shown is the oid one. Therefore, you may need to click on ‘See more’ for the sgv field to show.

image

All up, this is what our basic Flow looks like.

image

The result is similar to what we got with the Zap! one.

image

I say similar because the Zap! was slightly smarter in that it returned all records created in the 15 minute interval since the last run whereas this one only retrieves the latest record.

The one big advantage Flow has is we can add more stuff. So, for example, if we want to only send an email if the sgv value is over 180, we can add that in no problem.

image

Also, it is the cheap option because while there are different plans available for Flow, the base plan is completely free Please note that on the base plan you will only be able to run the check every 30 minutes due to the monthly limit of 2,000 runs. The next plan up is US$5/per user per month which may be a better option.

Conclusions

Tools like Microsoft Flow and Zapier offer non-coders a way to address problems in ways previously out of reach. Putting these tools in the hands of people managing diabetes means the tools could literally save lives. If you are using NightScout have a play and see how you can use the technology to make your life easier.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s