Interacting with Azure Edge Devices through ThingWorx > Setting Up Azure IoT Device Lifecycle Events
Setting Up Azure IoT Device Lifecycle Events
Azure fires lifecycle events when a new device is either created or deleted from a hub. You can subscribe to these events in ThingWorx. Currently the following properties are propagated to the ThingWorx Platform:
deviceId — Azure IoT device identifier (Thing name in ThingWorx)
status — Lifecycle status, such as deleteDeviceIdentity or createDeviceIdentity
twin — Value of the device twin at the time of creation or deletion.
For more details about lifecycle notifications, refer to Device lifecycle notifications (Microsoft documentation).
To set up these notifications, you first need to configure the event on the Azure IoT Hub and then set up an event consumer (a "subscription" in ThingWorx).
To configure the events in Azure:
1. In the Azure UI, go to your hub, then select Messaging > Route.
2. Create a new route, and specify the following parameters:
Name — Enter lifecycleRoute (or any other name)
Data source — Choose Device Lifecycle Events.
Endpoint — Choose events.
To set up an event consumer (subscription) in ThingWorx:
1. In ThingWorx Composer, create a new Thing for the “subscriber” and select the GenericThing Thing Template.
2. Create the following properties:
deviceId (String)
status (String)
twin (JSON)
3. Create a new subscription, using the following settings:
a. Source: ConnectionServicesHub
b. Event: AzureThingLifecycleEvent
c. Enabled: true
d. Script:

me.twin = eventData.twin
me.deviceId = eventData.deviceId
me.status = eventData.status
4. Save the Thing. When it is created, the properties of the subscriber Thing are set to the values in the AzureThingLifecycleEvent.