How to change the optype of "Device twin change events" frome client.

Watson 0 Reputation points
2025-11-04T07:46:34.6+00:00

Hi,

The "Azure IoT Hub non-telemetry event schemas" webpage(https://docs.azure.cn/en-us/iot-hub/iot-hub-non-telemetry-event-schema)

mentions that the opType has two possible values: "replaceTwin" and "updateTwin" at the "Device twin change events".

But when I send a Device Twin message from the client, I need to know how to set the opType and which API to use.(Just to clarify, I am using the C language SDK.)"

Thanks.

Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
{count} votes

1 answer

Sort by: Most helpful
  1. Manas Mohanty 13,340 Reputation points Moderator
    2025-11-05T16:02:57.96+00:00

    Hi Watson

    1. You cann’t set opType from the device. opType is set by IoT Hub in the “device twin change event” payload to reflect what kind of operation changed the twin:
      • updateTwin ⇒ a partial update (PATCH) — e.g., when a device sends reported properties, or when a service does a partial twin update.
      • replaceTwin ⇒ a full replace (PUT) — e.g., when a service replaces the entire twin document.
    2. From the C device SDK, you can only PATCH reported properties (which results in updateTwin). You cannot trigger replaceTwin from the device SDK. replaceTwin can be done by the service (via REST or a service SDK).

    Update Twin

    
    string deviceId = "myDevice";
    
    // Retrieve current twin to get ETag
    Twin twin = await registry.GetTwinAsync(deviceId);
    
    // Build a JSON patch
    string patchJson = @"{
      properties: {
        desired: {
          threshold: 25,
          telemetryIntervalSeconds: 30
        }
      },
      tags: {
        location: { region: 'US', site: 'Redmond' }
      }
    }";
    
    // Apply patch (ETag for optimistic concurrency)
    Twin updated = await registry.UpdateTwinAsync(deviceId, patchJson, twin.ETag);
    // => Emits opType = "updateTwin"
    

    Replace twin

    string replaceDesiredJson = @"{
      properties: {
        desired: {
          threshold: 10,
          telemetryIntervalSeconds: 5,
          mode: 'eco'
        }
      }
    }";
    Twin current = await registry.GetTwinAsync(deviceId);
    Twin replacedDesired = await registry.UpdateTwinAsync(deviceId, replaceDesiredJson, current.ETag);
    
    // Replace all tags
    string replaceTagsJson = @"{
      tags: {
        location: { region: 'EU', site: 'Dublin' },
        owner: 'QuadrantResource'
      }
    }";
    Twin replacedTags = await registry.UpdateTwinAsync(deviceId, replaceTagsJson, replacedDesired.ETag);
    
    // => Each of these PUT‑style replaces causes opType = "replaceTwin".
    
    

    Hope it clarifies the situation.

    Thank you.

    1 person found this answer helpful.
    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.