#AzureIoT – Processing Telemetry messages πŸ“¨ using Logic Apps

Hi !

Yesterday, I hosted a session with my good friend Ashraf and we talk about how we can use Power BI to create dashboards from telemetry data from Azure IoT (recording here).

The basic steps for this scenario is this one

  • We have 1 or more devices connected to Azure IoT Hub.
  • The devices are sending telemetry messages.
  • The telemetry messages are processed by Event Hub.
  • A Logic App is connected to the Event Hub to process the telemetry messages.
  • The Logic App will process and send the information to a DB.
azure iot telemetry to logic apps diagram

Before going on, I will suggest to read these articles to get more insights about how to get the environment up and running

We are ready to go !

Create the Logic App connected to the Event Hub

Let’s start in our Azure Portal in the main view for our Azure IoT Hub. We have several event handlers supported natively. In example:

  • Logic Apps
  • Azure Functions
  • Storage Queues
  • And more …
azure iot events automated focusing on power apps

When we select [Logic App], a new Logic App will be created including a trigger from [Azure Event Grid].

Logic App Designer default view including a trigger from Azure Event Grid

If this is the 1st time you are performing this, you need to configure your Azure Event Grid trigger.

Logic App Designer configure Azure Event Grid Trigger

Now we have to configure what happens when an resource event occurs. There are several options to configure here:

  • In [Resource type] change it to [Microsoft.Devices.IoTHubs]
  • Select the [Resource Name]. You may have several depending on your current configuration.
  • Select the [Event Type], for this sample, we will work with [Device Telemetry]
Logic App configure Azure IoT event resource when occurs

And we are ready to go ! Out Logic App will listen to any telemetry message routed to this event handler.

Processing Telemetry Messages in the Logic App

It’s time to parse several JSON string. So let’s start.

Let’s add a new step: [Parse JSON].

logic app adding a parse json step

Once we have the [Parse JSON] step, let’s use the [Body] of the message as the content.

Parse Json selecting the body of the telemetry message

In order to create the JSON schema, I’ll use a set of sample messages. The [Parse JSON] step will automatically generate the schema from this JSON. You can download a sample message from here.

"id": "<Telemetry ID>",
"subject": "devices/wioSquirrelFeeder",
"eventType": "Microsoft.Devices.DeviceTelemetry",
"data": {
"properties": {},
"systemProperties": {
"iothub-connection-device-id": "wioSquirrelFeeder",
"iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"iothub-connection-auth-generation-id": "637769331872294921",
"iothub-enqueuedtime": "2022-03-02T15:27:08.4210000Z",
"iothub-message-source": "Telemetry"
"body": "eyJhY3Rpb24iOiJhbmltYWwgZGV0ZWN0ZWQiLCJhbmltYWwiOiJzcXVpcnJlbCIsImZlZWRjb3VudCI6OCwiZmVlZGVyX3N0YXRlIjpmYWxzZSwicmFuZ2VfZGV0ZWN0ZWQiOjM3fQ=="
"dataVersion": "",
"metadataVersion": "1",
"eventTime": "2022-03-02T15:27:08.421Z"

As you can see in the sample message, the body of the message is encoded to base64. We need some work to do here.

Let’s add a new step of type [Compose].

logic app adding a compose data operation

Once we have the compose step, I renamed the step to [convert body to string].

Then we will use a string expression to convert the body from the telemetry message to a string variable.

Compose Operation convert the telemetry message body to string

Let’s add a new Compose step. I named this one [decode string body from base64 to string].

And I’ll use the decodeBase64() expression to process the previous Compose output.

Compose decode string from base64

We have our body decoded !

Using a [Parse JSON] step, I can also generate the necessary fields from the telemetry message body. This is a sample body message.

	"action":"animal detected",

In this step is important to note, that we must select the correct Output from the previous Compose steps. That’s why I named them.

Parse Json from the decoded body string

And we are ready to process our body telemetry messages. In the next image, you can see how I have access to the following fields from the telemetry body to be used in an send email step.

Body variables available to use

In next posts, I’ll show how this works with a Database !

Happy coding!


El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Leave a comment

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: