How To Build Custom Email Reports

AUTHOR:  David Thai

This tutorial shows you how to create beautiful customized email reports with charts and analytics using Medium One workflows.  We are going to show you how to build an email report like this one:


Contact us @ if you’d like us to help build a custom report for your application.

This tutorial assumes you already have a Medium One account (you can sign up for a free account on our website) with some data.  In this example, we have a sensor located in a remote location gathering temperature readings.  We generate daily temperature reports with connectivity status.  This workflow can be triggered in numerous ways using Medium One (scheduled, mobile app or even SMS). Check out one of our other tutorials on how to trigger workflows.

Here is the Workflow code that generated the above email report. Medium One workflows are developed using python.  In this example the device data is stored in Medium One’s memory data store and Analytics database.

Let’s get started!

This section imports the python libraries and Medium One services that you will need. TIME_ZONE_OFFSET configures the time scale in the chart relative to the timezone specified.

Next, you will need these functions. read_json_from_store will retrieve a device profile from the data store and return it on a dict format. get_current_iso_time returns the current date and time in iso format. fill_zero_count_bin is used to create a bucket for the bar chart if none exists. This is to force Google charts to show bar of Zero when there’s no data in a given bucket.

Next, we initialize a series of date time variables and load the sensor data from Store. Note, the charts and data is for the recent 24 hour period.

Next, we generate the data required to create the temperature bar chart. First we query the last 24 hrs data from Medium One’s Analytics library. In this case the temperature is stored as raw.temperature. Then we build the data required for the labels on the chart. GoogleCharts.get_chart_url is used to generate the chart url images using Google charts. The end result is sensor_chart_html which contains the html tags with the chart url.

Using a similar concept as above, we generate a pie chart showing the percentage the device was connected. In this example the percentage is calculated by the number of ‘mac’ data points received in the past 24 hour. 144 count was expected since the device is expected to transmit once per 10 minutes.

Here is a simple example of how to include the maps image. Note: Medium One has the ability to detect a device’s GPS coordinate based on it’s ip address. That feature is not explained in this tutorial – contact us to learn more or refer to our online documentation.

Lastly, we generate the html and send to the recipient! The .replace in the bottom section inserts the charts and data needed in the email report.

Thanks for visiting this tutorial.

Author: David Thai