'IoT Hub Event Grid integration is creating two "Disconnected" events instead of a "Connected" and "Disconnected" event

I am using IoT Hub's Event Grid integration to monitor device connections and disconnections and am finding that I receive 2 "Disconnected" events and no "Connected" events. I am currently routing the Events to an Azure Function. Please let me know if I have configured it correctly.

IoT Hub Configuration: enter image description here

Azure Function Code:

    public static class IoTConnectionTrigger
    {
        //public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
        [FunctionName("IoTConnectionTrigger")]
        public static void Run([EventGridTrigger]JObject eventGridEvent, ILogger log)
        {
            log.LogInformation(eventGridEvent.ToString(Formatting.Indented));
        }
    }
}

Code used to create the IoT Hub message:

        static void Main(string[] args)
        {
            for (int i=0; i<10; i++)
            {
                DeviceClient client = DeviceClient.CreateFromConnectionString("HostName=<IoT Hub Name>.azure-devices.net;DeviceId=SampleDevice;SharedAccessKey=<key>");
                
                // Create JSON message
                var telemetryDataPoint = new
                {
                    Temperature = 50,
                    Location = "Calgary",
                    TimeStamp = DateTime.Now
                };   
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);   
                Message message = new Message(Encoding.UTF8.GetBytes(messageString));
                message.ContentType = "application/json";
                message.ContentEncoding = "UTF-8";            

                try
                {
                    client.SendEventAsync(message).Wait();
                    Console.WriteLine("Sent!");
                } catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.WriteLine("Complete");
        }

Output: (Note the two "Microsoft.Devices.DeviceDisconnected" event types, and that no "Connected" events were raised)

Executing 'IoTConnectionTrigger' (Reason='EventGrid trigger fired at 2022-03-18T16:15:16.3717212+00:00', Id=84001f7e-6743-4847-ba73-48b597af33fa)
2022-03-18T16:15:16.372 [Information] {
  "id": "de252911-9e70-b66f-954a-641d2c508040",
  "topic": "/SUBSCRIPTIONS/7965FC25-694A-478C-B4FA-911F94239D30/RESOURCEGROUPS/TMRESOURCEGROUP/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/TMIOTHUB",
  "subject": "devices/SampleDevice",
  "eventType": "Microsoft.Devices.DeviceDisconnected",
  "data": {
    "deviceConnectionStateEventInfo": {
      "sequenceNumber": "000000000000000001D80D890755FFF30000004500000000000000000000000B"
    },
    "hubName": "<IoT Hub Name>",
    "deviceId": "SampleDevice"
  },
  "dataVersion": "",
  "metadataVersion": "1",
  "eventTime": "2022-03-18T16:14:36.8975903Z"
}
2022-03-18T16:15:16.372 [Information] Executed 'IoTConnectionTrigger' (Succeeded, Id=84001f7e-6743-4847-ba73-48b597af33fa, Duration=1ms)
2022-03-18T16:15:16.371 [Information] Executing 'IoTConnectionTrigger' (Reason='EventGrid trigger fired at 2022-03-18T16:15:16.3717212+00:00', Id=84001f7e-6743-4847-ba73-48b597af33fa)
2022-03-18T16:15:16.372 [Information] {
  "id": "de252911-9e70-b66f-954a-641d2c508040",
  "topic": "/SUBSCRIPTIONS/7965FC25-694A-478C-B4FA-911F94239D30/RESOURCEGROUPS/TMRESOURCEGROUP/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/TMIOTHUB",
  "subject": "devices/SampleDevice",
  "eventType": "Microsoft.Devices.DeviceDisconnected",
  "data": {
    "deviceConnectionStateEventInfo": {
      "sequenceNumber": "000000000000000001D80D890755FFF30000004500000000000000000000000B"
    },
    "hubName": "<IoT Hub Name>",
    "deviceId": "SampleDevice"
  },
  "dataVersion": "",
  "metadataVersion": "1",
  "eventTime": "2022-03-18T16:14:36.8975903Z"


Solution 1:[1]

Once a device is registered, the Hub start polling device status at a rate of about once per minute. So if a device is connected for 5 minutes, you should expect 5 reported connection events. If the device then disconnects for the next 3 minutes, you should expect 3 disconnection events. Because the sample is only taken once per minute, if a device manages to connect and then disconnect within a minute, the reported connection status may never actually report that it was connected. The way it works is documented here: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-event-grid#limitations-for-device-connected-and-device-disconnected-events

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1