1
votes

I'm routing telemetry messages via IoT Events and event Grid to Logic Apps using a webhook. The logic app lets you input a sample JSON message and then use dynamic content to add information to an email alert I'm sending(O365: Send an Email V2)

I can include System Properties like "iothub-connection-device-id" But when I try to pick temeletry data I get the following error:

InvalidTemplate. Unable to process template language expressions in action 'Send_an_email_(V2)' inputs at line '1' and column '1680': 'The template language expression 'items('For_each')?['data']?['body']?['windingTemp1']' cannot be evaluated because property 'windingTemp1' cannot be selected. Property selection is not supported on values of type 'String'. Please see https://aka.ms/logicexpressions for usage details.'.

When I look at the raw output of the webhook connector it shows the following message but the telemetry points are cleary not there. I'd expect to see them in the "body" property but instead there is just the string: "eyJ3aW5kaW5nVGVtcDEiOjg2LjYzOTYxNzk4MjYxODMzLCJ3aW5kaW5nVGVtcDIiOjc4LjQ1MDc4NTgwMjQyMTUyLCJ3aW5kaW5nVGVtcDMiOjg1LjUzMDYxMDY5OTQ1MzY1LCJMb2FkQSI6MjAyOS44NDgyMTg4ODYxMTEsIkxvYWRCIjoyMDQwLjgxMDk4OTg0MDMzMzgsIkxvYWRWIjoyMDA0LjYxMTkzMjMyNTQ2MTgsIk9pbFRlbXAiOjk5LjA2MjMyNjU2MTY4ODU4fQ=="

Looking for help to determine what could be causing this and how to get the telemetry data passed through correctly so that I can inculde it dynamically in the email alert.

Thanks!

{
"headers": {
    "Connection": "Keep-Alive",
    "Accept-Encoding": "gzip,deflate",
    "Host": "prod-24.northeurope.logic.azure.com",
    "aeg-subscription-name": "TEMPALERT",
    "aeg-delivery-count": "1",
    "aeg-data-version": "",
    "aeg-metadata-version": "1",
    "aeg-event-type": "Notification",
    "Content-Length": "1017",
    "Content-Type": "application/json; charset=utf-8"
},
"body": [
    {
        "id": "c767fb91-3806-324c-ec3c-XXXXXXXXXX",
        "topic": "/SUBSCRIPTIONS/XXXXXXXXXXXX",
        "subject": "devices/Device-001",
        "eventType": "Microsoft.Devices.DeviceTelemetry",
        "data": {
            "properties": {
                "TempAlarm": "true"
            },
            "systemProperties": {
                "iothub-connection-device-id": "Device-001",
                "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
                "iothub-connection-auth-generation-id": "637264713410XXXX",
                "iothub-enqueuedtime": "2020-06-01T23:05:58.3130000Z",
                "iothub-message-source": "Telemetry"
            },
            "body": "eyJ3aW5kaW5nVGVtcDEiOjg2LjYzOTYxNzk4MjYxODMzLCJ3aW5kaW5nVGVtcDIiOjc4LjQ1MDc4NTgwMjQyMTUyLCJ3aW5kaW5nVGVtcDMiOjg1LjUzMDYxMDY5OTQ1MzY1LCJMb2FkQSI6MjAyOS44NDgyMTg4ODYxMTEsIkxvYWRCIjoyMDQwLjgxMDk4OTg0MDMzMzgsIkxvYWRWIjoyMDA0LjYxMTkzMjMyNTQ2MTgsIk9pbFRlbXAiOjk5LjA2MjMyNjU2MTY4ODU4fQ=="
        },
        "dataVersion": "",
        "metadataVersion": "1",
        "eventTime": "2020-06-01T23:05:58.313Z"
    }
]

}

Here is the sample input I am using with the trigger:

[{
  "id": "9af86784-8d40-fe2g-8b2a-bab65e106785",
  "topic": "/SUBSCRIPTIONS/<subscription ID>/RESOURCEGROUPS/<resource group name>/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/<hub name>", 
  "subject": "devices/LogicAppTestDevice", 
  "eventType": "Microsoft.Devices.DeviceTelemetry",
  "eventTime": "2019-01-07T20:58:30.48Z",
  "data": {        
      "body": {            
          "windingTemp1": 95.62818310718433       
      },
        "properties": {            
          "Status": "Active"        
        },
        "systemProperties": {            
            "iothub-content-type": "application/json",
            "iothub-content-encoding": "utf-8",
            "iothub-connection-device-id": "d1",
            "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
            "iothub-connection-auth-generation-id": "123455432199234570",
            "iothub-enqueuedtime": "2019-01-07T20:58:30.48Z",
            "iothub-message-source": "Telemetry"        
        }    
    },
  "dataVersion": "",
  "metadataVersion": "1"
}]
1
Check out this question: stackoverflow.com/questions/58247124/… In your sample input I can see the content type is set correctly, but make sure the device is also setting the properties correctly. Right now it's coming in as Base64.Matthijs van der Veer
Thanks @MatthijsvanderVeer - this is what was missing. Manged to add it and telemetry is able to be picked up by the logic apprareg3636
Can you convert the comment into the answer to this to help others? Thanks!asergaz

1 Answers

2
votes

Summary comment to answer to help others who have same problem.

The body you provided is Base64 encoded, you can decode it with Convert.FromBase64String(String) Method.

byte[] newBytes = Convert.FromBase64String(body);

For more details, you could refer to this issue.

Update:

Add the following code in my application will solve the problem.

message.ContentEncoding = "utf-8"; 
message.ContentType = "application/json";