#AzureIoT – Processing Telemetry messages 📨 using Logic Apps
Tags: englishpost, azureiot, eventhub
Published: 2022-03-03 15:41:44

---
title: #AzureIoT – Processing Telemetry messages 📨 using Logic Apps
published: true
date: 2022-03-03 14:37:29 UTC
tags: EnglishPost,AzureIoT,EnglishPost,EventHub
canonical_url: 
---

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](https://aka.ms/FeedmySquirrels)).

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](https://brunocapuano.files.wordpress.com/2022/03/azure-iot-telemetry-to-logic-apps-diagram.jpg?w=1024)

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

- [IoT Hub data connection](https://docs.microsoft.com/en-us/azure/data-explorer/ingest-data-iot-hub-overview?wt.mc_id=twitter_brunocapuano_organicsocial_reactor)
- [Ingest data from IoT Hub into Azure Data Explorer](https://docs.microsoft.com/en-us/azure/data-explorer/ingest-data-iot-hub?wt.mc_id=twitter_brunocapuano_organicsocial_reactor)

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](https://brunocapuano.files.wordpress.com/2022/03/azure-iot-events-automated-focusing-on-power-apps.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/logic-app-designer-default-view-including-a-trigger-from-azure-event-grid.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/logic-app-designer-configure-azure-event-grid-trigger.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/logic-app-configure-azure-iot-event-resource-when-occurs.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/logic-app-adding-a-parse-json-step.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/parse-json-selecting-the-body-of-the-telemetry-message.jpg?w=1024)

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](https://gist.github.com/elbruno/90408083cf12543d5298054778ee3750).

{% gist https://gist.github.com/elbruno/90408083cf12543d5298054778ee3750 %}

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](https://brunocapuano.files.wordpress.com/2022/03/logic-app-adding-a-compose-data-operation.jpg?w=723)

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](https://brunocapuano.files.wordpress.com/2022/03/compose-operation-convert-the-telemetry-message-body-to-string.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/compose-decode-string-from-base64.jpg?w=1024)

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",
	"animal":"squirrel",
	"feedcount":1,
	"feeder_state":false,
	"range_detected":64
}

```

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](https://brunocapuano.files.wordpress.com/2022/03/parse-json-from-the-decoded-body-string.jpg?w=1024)

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](https://brunocapuano.files.wordpress.com/2022/03/body-variables-available-to-use-.jpg?w=1024)

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

Happy coding!

Greetings

El Bruno

* * *