SOLVED
Home

IoT Hub Device twin returning connectionState = Disconnected, when it's actually connected

%3CLINGO-SUB%20id%3D%22lingo-sub-306925%22%20slang%3D%22en-US%22%3EIoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-306925%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20Monitoring%20web%20job%20that%20querys%20my%20iot%20devices%20for%20any%20that%20have%20a%20connection%20state%20%3D%20disconnected%2C%20and%20then%20sends%20an%20email%20out%20to%20the%20user%20of%20that%20device%20to%20let%20them%20know%20it%20has%20a%20connection%20state%20issue.%26nbsp%3B%20I'm%20seeing%20a%20problem%20where%20one%20device%20will%20look%20like%20it%20is%20down%20about%20once%20every%20hour%20(or%201%20hour%20and%205%20minutes).%26nbsp%3B%20But%20the%20device%20is%20not%20down%20and%20there%20is%20no%20intermittent%20connection%20issue.%26nbsp%3B%20I%20currently%20have%203%20devices%2C%20all%20connected%20through%20different%20internet%20ISPs.%26nbsp%3B%20One%20system%20started%20having%20this%20issue%20every%20so%20often%2C%20then%20another%20did%2C%20and%20now%20the%203rd%20one%20is%20having%20this%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20web%20job%20code%20is%20pretty%20simple%20and%20runs%20every%205%20minutes%20to%20see%20if%20there%20are%20any%20devices%20that%20are%20down.%26nbsp%3B%20Here's%20my%20code%3C%2FP%3E%3CP%3E%26nbsp%3Bpublic%20static%20async%20Task%20CheckDeviceAvailability(%5BTimerTrigger(%2200%3A05%3A00%22%2C%20RunOnStartup%20%3D%20true)%5D%20TimerInfo%20timer)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20string%20connectionString%20%3D%20CloudConfigurationManager.GetSetting(%22IotHubConnectionString%22)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20try%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20RegistryManager%20manager%20%3D%20RegistryManager.CreateFromConnectionString(connectionString)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20IQuery%20query%20%3D%20manager.CreateQuery(%22SELECT%20*%20FROM%20devices%20where%20ConnectionState%20%3D%20'Disconnected'%22%2C%2010)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20while%20(query.HasMoreResults)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20var%20page%20%3D%20await%20query.GetNextAsTwinAsync()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20foreach%20(Twin%20twin%20in%20page)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20await%20SendNotification(twin.DeviceId)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20catch%20(Exception%20exc)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Console.WriteLine(exc.Message)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20just%20added%20a%20check%20(if%20(twin.ConnectionState%20%3D%3D%20DeviceConnectionState.Disconnected))%20above%20the%20Await%20SendNotification%20line%20in%20the%20above%20code%20just%20to%20see%20if%20this%20helps%20out%2C%20but%20it%20seems%20like%20the%20query%20shouldn't%20be%20returning%20any%20devices%20that%20are%20connected.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-332525%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-332525%22%20slang%3D%22en-US%22%3E%3CP%3Eahh....yes%2C%20was%20thinking%20I%20would%20have%20to%20ping%20all%20devices%20every%205%20minutes%2C%20that%20makes%20a%20lot%20more%20sense.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20the%20suggestion...it%20seems%20to%20be%20the%20easiest%20and%20cleanest%20way%20to%20do%20that!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-332032%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-332032%22%20slang%3D%22en-US%22%3E%3CP%3EActually%20it%20won't.%20The%20function%20is%20only%20triggered%20once%20when%20IoT%20Hub%20gets%20the%20Device%20disconnected%20event%2C%20tries%20the%20method%20call%20with%205%20minutes%20timeout%20and%20if%20noone%20answers%20it%20sends%20a%20notification%20out%20that%20the%20device%20is%20disconnected.%20The%20Function%20doesn't%20loop.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-332028%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-332028%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20problem%20with%20that%20is%20it%20will%20send%20many%20messages%20(ie%20running%20every%205%20minutes%2C%2024x7)%20and%20would%20not%20be%20very%20cost%20effective%20from%20a%20hosting%20standpoint.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309868%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309868%22%20slang%3D%22en-US%22%3E%3CP%3EA%20way%20you%20could%20implement%20a%20check%20on%20device%20connection%20would%20be%20to%20trigger%20a%20Function%20on%20the%20Device%20disconnected%20event%20that%20would%26nbsp%3Binvoke%20a%20direct%20method%20on%20the%20device%20with%20a%20timeout%20of%205%20minutes.%20If%20the%20device%20comes%20back%20online%20in%20the%205%20minutes%2C%20then%20the%20method%20will%20get%20answered.%20If%20not%2C%20you'll%20know%20the%20device%20has%20been%20disconnected%20for%205%20minutes%20and%20can%20trigger%20the%20alert.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309189%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309189%22%20slang%3D%22en-US%22%3E%3CP%3EI%20don't%20care%20much%20about%20disconnects%20of%20less%20than%20a%20few%20minutes%2C%20I'm%20more%20concerted%20about%20disconnects%20that%20don't%20come%20back%20and%20get%20connected%20(ie%205%20minutes%20or%20longer).%26nbsp%3B%20Then%20I%20know%20there%20is%20some%20kind%20of%20issue.%26nbsp%3B%20I%20guess%20I%20will%20just%20subscribe%20to%20a%20disconnect%20event%20and%20notify%20on%20that%20and%20not%20worry%20about%20connection%20events%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309142%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309142%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20not%20a%20bug%20and%20the%20two%20options%20I%20shared%20with%20you%20are%20the%20supported%20ones.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20connect%5Cdisconnect%20events%20will%20sometimes%20show%20in%20a%20reverse%20order%20(mainly%20when%20the%20connect%5Cdisconnect%20of%20a%20device%20takes%20less%20than%203%20seconds)%20and%201%3A1%26nbsp%3Bratios%20are%20not%20guaranteed%20when%20using%26nbsp%3B%3CSTRONG%3EconnectionState%3C%2FSTRONG%3E%20field.%20T%3CFONT%3Ehere%20is%20not%20much%20we%20can%20do%20about%20this%20because%20of%20the%20mechanics%20here%20(device%20may%20reconnect%20to%20a%20different%20gateway%20in%20which%20case%20the%20disconnect%20and%20connect%20events%20may%20race).%20%3C%2FFONT%3EOne%20option%20is%20to%20check%20the%20timestamp%20of%20the%20connectionState%20messages%20coming%20in%20the%20IoTHub%26nbsp%3B%3CFONT%3Ebut%20unfortunately%20even%20that%20is%20not%20super%20deterministic%20because%20of%20potential%20clock%20skew%20between%20the%20gateway%20VMs.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%3EThanks.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309137%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309137%22%20slang%3D%22en-US%22%3E%3CP%3EYes%2C%20you%20are%20just%20restating%20what%20is%20in%20the%20documentation.%26nbsp%3B%20Doing%20a%20heartbeat%20is%20worthless%20as%20it%20would%20require%20way%20to%20many%20messages%20just%20for%20a%20heart%20beat%20(ie%20every%205%20minutes).%26nbsp%3B%20I%20don't%20want%20to%20know%20if%20a%20device%20is%20connected%2C%20I%20want%20to%20know%20when%20a%20device%20is%20disconnected%2C%20if%20devices%20are%20connected%2C%20everything%20is%20good%2C%20when%20a%20device%20disconnects%2C%20then%20there%20is%20either%20a%20network%20issue%20where%20the%20device%20is%20located%2C%20or%20something%20with%20the%20code%20on%20the%20device%20crashed%20or%20something%20else%20causing%20it%20not%20to%20connect%20to%20the%20cloud.%26nbsp%3B%20That%20is%20the%20critical%20issue%20and%20I%20want%20to%20notify%20the%20user%20of%20the%20device%20when%20this%20happens%20so%20they%20can%20validate%20the%20device%20is%20off%20line%20and%20remedy%20the%20situation.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20again%20disconnecting%20my%20test%20device%20from%20the%20network%2C%20and%20did%20eventually%20receive%20a%20disconnected%20event.%26nbsp%3B%20I%20guess%20I%20will%20modify%20my%20webjob%20and%20event%20to%20only%20look%20at%2Fget%20device%20disconnected%20events%20and%20send%20out%20an%20alert%20when%20that%20happens.%26nbsp%3B%20I%20don't%20want%20to%20be%20constantly%20sending%20out%20device%20is%20online%20messages%20when%20the%20device%20never%20actually%20goes%20offline.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAgain%2C%20if%20the%20iot%20hub%20is%20sending%20a%20device%20connected%20event%2C%20shouldn't%20have%20a%20devicedisconnected%20event%20being%20sent%20first%20(or%20at%20least%20being%20sent)%E2%80%A6.this%20doesn't%20happen%2C%20I%20only%20see%20many%20device%20connected%20events%20come%20through%20the%20queue.%26nbsp%3B%20I%20would%20expect%20a%201%3A1%20ratio%20of%20connected%2Fdisconnected%20events.%26nbsp%3B%20I%20also%20wouldn't%20expect%20multiple%20connected%20events%20to%20come%20through%20on%20devices%20that%20are%20connected....seems%20like%20an%20IOT%20hub%20bug%20to%20me%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309111%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309111%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20I%20mentioned%20above%20you%20can't%20rely%20on%26nbsp%3B%3CSTRONG%3EconnectionState%3C%2FSTRONG%3E%20field%20(which%20looks%20like%20is%20what%20you%20are%20using%20right%3F).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20don't%20want%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-how-to-order-connection-state-events%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EOrder%20device%20connection%20events%20from%20Azure%20IoT%20Hub%20using%20Azure%20Cosmos%20DB%3C%2FA%3E%20the%20other%20supported%20alternative%20is%20to%20implement%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-devguide-identity-registry%23device-heartbeat%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Edevice%20heartbeat%20pattern%3C%2FA%3E%3CFONT%3E%3A%3C%2FFONT%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%3E%22%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIf%20your%20IoT%20solution%20needs%20to%20know%20if%20a%20device%20is%20connected%2C%20you%20can%20implement%20the%20%3CEM%3Eheartbeat%20pattern%3C%2FEM%3E.%20In%20the%20heartbeat%20pattern%2C%20the%20device%20sends%20device-to-cloud%20messages%20at%20least%20once%20every%20fixed%20amount%20of%20time%20(for%20example%2C%20at%20least%20once%20every%20hour).%20Therefore%2C%20even%20if%20a%20device%20does%20not%20have%20any%20data%20to%20send%2C%20it%20still%20sends%20an%20empty%20device-to-cloud%20message%20(usually%20with%20a%20property%20that%20identifies%20it%20as%20a%20heartbeat).%20%3CFONT%20color%3D%22%23ff0000%22%3EOn%20the%20service%20side%2C%20the%20solution%20maintains%20a%20map%20with%20the%20last%20heartbeat%20received%20for%20each%20device.%3C%2FFONT%3E%20If%20the%20solution%20does%20not%20receive%20a%20heartbeat%20message%20within%20the%20expected%20time%20from%20the%20device%2C%20it%20assumes%20that%20there%20is%20a%20problem%20with%20the%20device.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20more%20complex%20implementation%20could%20include%20the%20information%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Findex%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Monitor%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fservice-health%2Fresource-health-overview%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Resource%20Health%3C%2FA%3E%20to%20identify%20devices%20that%20are%20trying%20to%20connect%20or%20communicate%20but%20failing%2C%20check%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-monitor-resource-health%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EMonitor%20with%20diagnostics%3C%2FA%3E%20guide.%20%3CFONT%20color%3D%22%23ff0000%22%3EWhen%20you%20implement%20the%20heartbeat%20pattern%2C%20make%20sure%20to%20check%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-devguide-quotas-throttling%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EIoT%20Hub%20Quotas%20and%20Throttles%3C%2FA%3E.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309088%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309088%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20goal%20of%20this%20is%20to%20monitor%20for%20disconnected%20devices%20and%20notify%20the%20user%20of%20that%20device%20that%20it%20is%20offline%20(or%20back%20online).%26nbsp%3B%20I%20cleared%20out%20the%20queue%20this%20morning%20and%20already%20have%2015%20device%20connected%20events%20for%203%20different%20iot%20devices.%26nbsp%3B%20This%20is%20problematic%20as%20it%20will%20send%20false%20notifications%20to%20something%20that%20has%20not%20disconnected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20just%20want%20to%20know%20when%20a%20device%20goes%20offline%2C%20and%20when%20it%20comes%20back%20up%20so%20that%20I%20can%20notify%20users%20of%20the%20device%20in%20a%20timely%20manner%20and%20at%20the%20same%20time%2C%20not%20spam%20them%20with%20messages%20that%20don't%20offer%20any%20benefit.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20this%20make%20sense%20at%20what%20I'm%20trying%20to%20accomplish%3F%26nbsp%3B%20the%20device%20twin%20query%20that%20ran%20every%205%20minutes%20seem%20to%20work%20better%20than%20this%2C%20only%20that%20once%20in%20a%20while%20a%20device%20would%20report%20offline%20once%20every%201%20hour%20%26amp%3B%205%20minutes%20for%20what%20ever%20reason%2C%20giving%20false%20issues.%26nbsp%3B%20the%20connected%20events%20are%20showing%20up%20way%20more%20frequently%2C%20and%20I%20don't%20even%20seem%20to%20get%20a%20disconnected%20event%20even%20when%20I%20pull%20the%20network%20cable%20for%205%20minutes%20from%20a%20test%20device.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309079%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309079%22%20slang%3D%22en-US%22%3E%3CP%3EI%20don't%20send%20any%20messages%20from%20my%20device%20to%20the%20iot%20hub%2C%20I%20want%20to%20monitor%20my%20devices%20from%20the%20cloud%20so%20I%20can%20send%20an%20alert%20email%20when%20one%20of%20them%20is%20no%20longer%20connected%2C%20hence%20the%20original%20webjob%20that%20queried%20the%20IOT%20hub%20for%20disconnected%20devices.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESince%20that%20is%20supposedly%20not%20a%20proper%20way%2C%20I%20created%20event%20subscriptions%20in%20Iot%20hub%20to%20subscribe%20to%20the%20device%20disconnected%20and%20device%20connected%20events%20that%20are%20available%2C%20and%20send%20those%20messages%20to%20a%20queue.%26nbsp%3B%20I%20created%20a%20new%20webjob%20that%20listens%20on%20that%20queue%20for%20those%20messages%20and%20then%20intern%2C%20send%20out%20an%20email%20to%20the%20appropriate%20email%20address%20based%20on%20the%20device%20and%20the%20connection%20state%20in%20the%20event.%26nbsp%3B%20The%20problem%20with%20this%20is%20that%20I%20don't%20ever%20seem%20to%20get%20any%20disconnected%20events%20and%20only%20get%20connected%20events.%26nbsp%3B%20Also%2C%20I%20ran%20this%20overnight%20at%20got%20many%20connected%20events%20and%20only%20one%20disconnected%20event%2C%20I%20would%20expect%20to%20get%20a%20one%20to%20one%20ratio....and%20I%20really%20should%20have%20not%20gotten%20any%20connect%2Fdisconnect%20events.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309059%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309059%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3ECan%20you%20share%20the%20snippet%20of%20the%20code%3F%20I%20am%20not%20sure%20how%20do%20you%20manage%20to%20send%20a%20%22disconnected%22%20message%20from%20your%20device%20to%20IoTHub%20if%20the%20device%20is%20disconnected%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308762%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308762%22%20slang%3D%22en-US%22%3E%3CP%3EI%20was%20able%20to%20create%20events%20on%20my%20Iot%20hub%20for%20device%20connected%20and%20device%20disconnected%2C%20and%20it%20sends%20them%20to%20a%20queue.%26nbsp%3B%20I%20created%20a%20webjob%20that%20gets%20triggered%20by%20the%20queue%20and%20reads%20the%20id%20and%20the%20state%20from%20the%20message%2C%20but%20I%20only%20seem%20to%20get%20connected%20messages%2C%20not%20disconnected%20messages.%26nbsp%3B%20Ie%20if%20I%20unplug%20my%20iot%20device%2C%20nothing%20happens%2C%20even%20if%20I%20wait%205%2B%20minutes.%26nbsp%3B%20When%20I%20plug%20it%20back%20in%2C%20I%20get%20a%20device%20connected%20message%20after%20a%20minute%20or%20so.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%20what%20I%20might%20be%20doing%20wrong%3F%26nbsp%3B%20Is%20it%20something%20on%20my%20device%20client%20code%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308722%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308722%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20tutorial%20seems%20overly%20complicated.%26nbsp%3B%20I%20don't%20want%20to%20have%20a%20cosmos%20db%20to%20store%20connection%20state%20info%20just%20to%20send%20an%20email.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESeems%20like%20I%20could%20create%20a%20subscription%20on%20the%20IOT%20hub%20and%20have%20it%20put%20a%20disconnection%2Fconnection%20message%20into%20a%20queue%2C%20and%20have%20my%20webjob%20listen%20on%20this%20queue%20for%20connection%20state%20changes%20and%20then%20send%20out%20emails.%26nbsp%3B%20I%20can't%20find%20much%20info%20on%20this%2C%20and%20only%20the%20connection%20message%20is%20listed.%26nbsp%3B%20I%20tried%20this%20but%20my%20webjob%20(running%20locally%20never%20got%20triggered).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20on%20this%20would%20be%20greatly%20appreciated!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308216%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308216%22%20slang%3D%22en-US%22%3EThanks%2C%20I%20will%20take%20a%20look%20and%20make%20changes%20accordingly.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307667%22%20slang%3D%22en-US%22%3ERe%3A%20IoT%20Hub%20Device%20twin%20returning%20connectionState%20%3D%20Disconnected%2C%20when%20it's%20actually%20connected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307667%22%20slang%3D%22en-US%22%3E%3CP%3EHi!%3C%2FP%3E%0A%3CP%3EYou%20should%20have%20a%20look%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-devguide-identity-registry%23device-heartbeat%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%3EDevice%20heartbeat%3C%2FFONT%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%22%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20IoT%20Hub%20identity%20registry%20contains%20a%20field%20called%20%3C%2FSPAN%3E%3CSTRONG%3EconnectionState%3C%2FSTRONG%3E%3CSPAN%3E.%20Only%20use%20the%20%3C%2FSPAN%3E%3CSTRONG%3EconnectionState%3C%2FSTRONG%3E%3CSPAN%3E%20field%20during%20development%20and%20debugging.%20IoT%20solutions%20should%20not%20query%20the%20field%20at%20run%20time.%20For%20example%2C%20do%20not%20query%20the%20%3C%2FSPAN%3E%3CSTRONG%3EconnectionState%3C%2FSTRONG%3E%3CSPAN%3E%20field%20to%20check%20if%20a%20device%20is%20connected%20before%20you%20send%20a%20cloud-to-device%20message%20or%20an%20SMS.%20We%20recommend%20subscribing%20to%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-event-grid%23event-types%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3Edevice%20disconnected%3C%2FSTRONG%3E%20event%3C%2FA%3E%3CSPAN%3E%20on%20Event%20Grid%20to%20get%20alerts%20and%20monitor%20the%20device%20connection%20state.%20Use%20this%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-how-to-order-connection-state-events%22%20data-linktype%3D%22relative-path%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Etutorial%3C%2FA%3E%3CSPAN%3E%20to%20learn%20how%20to%20integrate%20Device%20Connected%20and%20Device%20Disconnected%20events%20from%20IoT%20Hub%20in%20your%20IoT%20solution.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%22%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EHope%20that%20helps!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThanks%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
smart_door
Occasional Contributor

I have a Monitoring web job that querys my iot devices for any that have a connection state = disconnected, and then sends an email out to the user of that device to let them know it has a connection state issue.  I'm seeing a problem where one device will look like it is down about once every hour (or 1 hour and 5 minutes).  But the device is not down and there is no intermittent connection issue.  I currently have 3 devices, all connected through different internet ISPs.  One system started having this issue every so often, then another did, and now the 3rd one is having this issue.

 

My web job code is pretty simple and runs every 5 minutes to see if there are any devices that are down.  Here's my code

 public static async Task CheckDeviceAvailability([TimerTrigger("00:05:00", RunOnStartup = true)] TimerInfo timer)
        {
            string connectionString = CloudConfigurationManager.GetSetting("IotHubConnectionString");
            try
            {
                RegistryManager manager = RegistryManager.CreateFromConnectionString(connectionString);
                IQuery query = manager.CreateQuery("SELECT * FROM devices where ConnectionState = 'Disconnected'", 10);
                while (query.HasMoreResults)
                {
                    var page = await query.GetNextAsTwinAsync();
                    foreach (Twin twin in page)
                    {
                            await SendNotification(twin.DeviceId);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
        }

 

I've just added a check (if (twin.ConnectionState == DeviceConnectionState.Disconnected)) above the Await SendNotification line in the above code just to see if this helps out, but it seems like the query shouldn't be returning any devices that are connected.

15 Replies
Highlighted
Solution

Hi!

You should have a look here: Device heartbeat

 

"

The IoT Hub identity registry contains a field called connectionState. Only use the connectionState field during development and debugging. IoT solutions should not query the field at run time. For example, do not query the connectionState field to check if a device is connected before you send a cloud-to-device message or an SMS. We recommend subscribing to the device disconnected event on Event Grid to get alerts and monitor the device connection state. Use this tutorial to learn how to integrate Device Connected and Device Disconnected events from IoT Hub in your IoT solution.

"

 

Hope that helps!

Thanks

Thanks, I will take a look and make changes accordingly.

The tutorial seems overly complicated.  I don't want to have a cosmos db to store connection state info just to send an email.  

 

Seems like I could create a subscription on the IOT hub and have it put a disconnection/connection message into a queue, and have my webjob listen on this queue for connection state changes and then send out emails.  I can't find much info on this, and only the connection message is listed.  I tried this but my webjob (running locally never got triggered).

 

Any help on this would be greatly appreciated!

 

Thanks

I was able to create events on my Iot hub for device connected and device disconnected, and it sends them to a queue.  I created a webjob that gets triggered by the queue and reads the id and the state from the message, but I only seem to get connected messages, not disconnected messages.  Ie if I unplug my iot device, nothing happens, even if I wait 5+ minutes.  When I plug it back in, I get a device connected message after a minute or so.

 

Any ideas what I might be doing wrong?  Is it something on my device client code?

Hi,

Can you share the snippet of the code? I am not sure how do you manage to send a "disconnected" message from your device to IoTHub if the device is disconnected?

 

Thanks.

I don't send any messages from my device to the iot hub, I want to monitor my devices from the cloud so I can send an alert email when one of them is no longer connected, hence the original webjob that queried the IOT hub for disconnected devices.

 

Since that is supposedly not a proper way, I created event subscriptions in Iot hub to subscribe to the device disconnected and device connected events that are available, and send those messages to a queue.  I created a new webjob that listens on that queue for those messages and then intern, send out an email to the appropriate email address based on the device and the connection state in the event.  The problem with this is that I don't ever seem to get any disconnected events and only get connected events.  Also, I ran this overnight at got many connected events and only one disconnected event, I would expect to get a one to one ratio....and I really should have not gotten any connect/disconnect events.

The goal of this is to monitor for disconnected devices and notify the user of that device that it is offline (or back online).  I cleared out the queue this morning and already have 15 device connected events for 3 different iot devices.  This is problematic as it will send false notifications to something that has not disconnected.

 

I just want to know when a device goes offline, and when it comes back up so that I can notify users of the device in a timely manner and at the same time, not spam them with messages that don't offer any benefit.

 

Does this make sense at what I'm trying to accomplish?  the device twin query that ran every 5 minutes seem to work better than this, only that once in a while a device would report offline once every 1 hour & 5 minutes for what ever reason, giving false issues.  the connected events are showing up way more frequently, and I don't even seem to get a disconnected event even when I pull the network cable for 5 minutes from a test device.

Hi,

 

Like I mentioned above you can't rely on connectionState field (which looks like is what you are using right?).

 

If you don't want to Order device connection events from Azure IoT Hub using Azure Cosmos DB the other supported alternative is to implement the device heartbeat pattern:

 

"

If your IoT solution needs to know if a device is connected, you can implement the heartbeat pattern. In the heartbeat pattern, the device sends device-to-cloud messages at least once every fixed amount of time (for example, at least once every hour). Therefore, even if a device does not have any data to send, it still sends an empty device-to-cloud message (usually with a property that identifies it as a heartbeat). On the service side, the solution maintains a map with the last heartbeat received for each device. If the solution does not receive a heartbeat message within the expected time from the device, it assumes that there is a problem with the device.

 

A more complex implementation could include the information from Azure Monitor and Azure Resource Health to identify devices that are trying to connect or communicate but failing, check Monitor with diagnostics guide. When you implement the heartbeat pattern, make sure to check IoT Hub Quotas and Throttles.

"

 

Yes, you are just restating what is in the documentation.  Doing a heartbeat is worthless as it would require way to many messages just for a heart beat (ie every 5 minutes).  I don't want to know if a device is connected, I want to know when a device is disconnected, if devices are connected, everything is good, when a device disconnects, then there is either a network issue where the device is located, or something with the code on the device crashed or something else causing it not to connect to the cloud.  That is the critical issue and I want to notify the user of the device when this happens so they can validate the device is off line and remedy the situation.

 

I tried again disconnecting my test device from the network, and did eventually receive a disconnected event.  I guess I will modify my webjob and event to only look at/get device disconnected events and send out an alert when that happens.  I don't want to be constantly sending out device is online messages when the device never actually goes offline.  

 

Again, if the iot hub is sending a device connected event, shouldn't have a devicedisconnected event being sent first (or at least being sent)….this doesn't happen, I only see many device connected events come through the queue.  I would expect a 1:1 ratio of connected/disconnected events.  I also wouldn't expect multiple connected events to come through on devices that are connected....seems like an IOT hub bug to me?

This is not a bug and the two options I shared with you are the supported ones.

 

The connect\disconnect events will sometimes show in a reverse order (mainly when the connect\disconnect of a device takes less than 3 seconds) and 1:1 ratios are not guaranteed when using connectionState field. There is not much we can do about this because of the mechanics here (device may reconnect to a different gateway in which case the disconnect and connect events may race). One option is to check the timestamp of the connectionState messages coming in the IoTHub but unfortunately even that is not super deterministic because of potential clock skew between the gateway VMs.

 

Thanks.

 

 

I don't care much about disconnects of less than a few minutes, I'm more concerted about disconnects that don't come back and get connected (ie 5 minutes or longer).  Then I know there is some kind of issue.  I guess I will just subscribe to a disconnect event and notify on that and not worry about connection events

A way you could implement a check on device connection would be to trigger a Function on the Device disconnected event that would invoke a direct method on the device with a timeout of 5 minutes. If the device comes back online in the 5 minutes, then the method will get answered. If not, you'll know the device has been disconnected for 5 minutes and can trigger the alert.

The problem with that is it will send many messages (ie running every 5 minutes, 24x7) and would not be very cost effective from a hosting standpoint.

Actually it won't. The function is only triggered once when IoT Hub gets the Device disconnected event, tries the method call with 5 minutes timeout and if noone answers it sends a notification out that the device is disconnected. The Function doesn't loop.

ahh....yes, was thinking I would have to ping all devices every 5 minutes, that makes a lot more sense.

 

Thank you for the suggestion...it seems to be the easiest and cleanest way to do that!

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies