IoT Hub Message routing - $body not working

TITAN 46 Reputation points
2022-10-25T14:21:46.647+00:00

Hi,
I have an IoT hub device which sends messages via message routing to a bus queue and then to a logic app (which sends email alerts).
When the message routing - routing query is set to:
true

I get email notifications.
when i set it to:
$body.sensor_data.thermocouple_temperature > 0
example message from the device:
[
{
"nodeId": 0,
"firmware": 6,
"battery": "3.29",
"battery_percent": "99.64",
"counter": 64,
"sensor_type": 50,
"sensor_data": {
"rms_x": 1.69,
"rms_y": 1.93,
"rms_z": 8.5,
"max_x": 10.98,
"max_y": 10.98,
"max_z": 12.44,
"min_x": -12.44,
"min_y": -15.37,
"min_z": -186.66,
"vibration_temperature": 27,
"thermocouple_temperature": 25.75,
"current": 0
},
"sensor_name": "EXAMPLE",
"type": "sensor_data",
"addr": "EXAMPLE",
"received": 1666689894382,
"original": {
"mac": "EXAMPLE",
"receive_options": {
"ack": 0,
"broadcast": 0,
"type": ""
},
"data": [
'EXAMPLE',
'EXAMPLE',
],
"type": "receive_packet"
},
"EventProcessedUtcTime": "2022-10-25T10:30:43.3605566Z",
"PartitionId": 1,
"EventEnqueuedUtcTime": "2022-10-25T09:24:54.5080000Z",
"IoTHub": {
"MessageId": null,
"CorrelationId": null,
"ConnectionDeviceId": "EXAMPLE",
"ConnectionDeviceGenerationId": "EXAMPLENum",
"EnqueuedTime": "2022-10-25T09:24:54.4300000Z"
}
},
{
"nodeId": 0,

for example, it doesn't send emails, and the bus queue outgoing messages are 0 (zero).
253925-1service-bus-queue.png

I've read that some properties need to be set:
message.ContentType = "application/json";
message.ContentEncoding = "utf-8";

but I don't understand where to enter that.

/If any other solution is possible - please let me know.

If any other information is needed, please let me know.

Thank you

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
623 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,189 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 32,556 Reputation points MVP
    2022-10-25T21:12:00.377+00:00

    Hello @TITAN ,

    the Azure IoT Hub supports routing messages to one or more endpoints.

    You can add a condition so based on either message properties or the message body.

    The properties (both system properties and application properties) are key/value pairs in property bags so these are easy to process for the IoT Hub.

    The body is not that simple though.

    The body arrives as a byte array and needs to be 'decoded' first.

    Microsoft has decided to support only body messages written in JSON and encoded in UTF-8.

    You have to add this to each message sent.

    If you are using one of the Azure IoT Device SDKs, the class properties are available for you:

    using (var message = new Message(messageBytes))   
    {   
        message.ContentEncoding = "utf-8";   
        message.ContentType = "application/json";   
       
        await deviceClient.SendEventAsync(message);   
    }  
    

    As far as I know, this is also supported for non-SDK device connectivity solutions.

    If you fail to add these, your route will not be triggered.

    Personally, I would limit IoT Hub routing to no-functional routing because there is a limit to the number of routes and endpoints offered. I recommend using a Stream Analytics job to actually filter/route messages based on business rules.

    ----------

    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. By doing so, all community members who have a similar issue will benefit. Your contribution is highly appreciated.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.