Track Device Twin Changes with Azure Function

Drew Stevens 40 Reputation points
2025-08-29T18:03:31.7+00:00

I want to be able to track when a device updates its twin. I cant find a way to do that. I was going to make an event hub that would trigger my azure function, but Device Twin update isn't an event type. SO my question is, without having that as an event type, how can my azure function know when a device twin is updated?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
{count} votes

2 answers

Sort by: Most helpful
  1. VSawhney 1,290 Reputation points Microsoft External Staff Moderator
    2025-08-29T20:32:18.0766667+00:00

    Hello Drew Stevens,

    It sounds like you're trying to track Device Twin updates in Azure but are running into difficulty with event types. While you're correct that Device Twin updates don't directly trigger events in Event Hubs, you can still receive notifications about these changes by setting up some functionality with Azure Digital Twins.

    Here's a solution you can try:

    1. Implement Twin-to-Twin Event Handling: Azure Digital Twins can emit events when a twin is updated, allowing you to trigger your Azure Function. Set up an Azure Function that connects to Event Grid, which can handle twin lifecycle events. You can subscribe your Azure Function to an Event Grid topic that receives these dual updates.
    2. Create Azure Function:
      • Follow the documentation to create your Azure Function to listen for these events.
      • Use packages like Azure.DigitalTwins.Core and Microsoft.Azure.WebJobs.Extensions.EventGrid to help with integration.
    3. Event Subscription: Use an Azure CLI command to create an Event Grid subscription that will send the twin events to your Azure Function. This ensures your function knows when a device twin is updated.
    4. Testing: Test it by making updates to the twin and checking if your Azure Function triggers as expected.

    Here’s a high-level step for your Azure Function to update twins using the Event Grid connection:

    
    az eventgrid event-subscription create --name <your-subscription-name> --source-resource-id <event-grid-topic-resource-id> --endpoint-type azurefunction --endpoint <your-function-endpoint>
    
    

    Hope this helps!
    Thank you!

    0 comments No comments

  2. Sander van de Velde | MVP 36,951 Reputation points MVP Volunteer Moderator
    2025-09-08T09:43:15.9033333+00:00

    Hello Drew Stevens,

    welcome to this moderated Azure community forum.

    It seems you are interested in the device lifecycle in the IoT Hub. Check out this blog post showing how to cope with various messages from the IoT Hub.

    There are messages for twin changes too:

    User's image

    The messages are also available on an EventGrid.

    Notice that both the message body and the application properties need to be checked to get a full understanding of the changes.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

Your answer

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