Skip to main content

Device Twins

Device twins maintain a synchronized view of device state between the cloud and the device. The twin has two halves — desired (what the cloud wants) and reported (what the device says it is) — plus tags for cloud-side metadata.

The two halves are written differently:

HalfWritten byEach write
reportedThe device, over MQTTReplaces the whole reported state
desiredThe platform APIIs merged into the desired state as a JSON Merge Patch (RFC 7396)

Twin Structure​

{
"deviceId": "temp-sensor-001",
"desired": {
"reporting_interval": 30,
"alert_threshold": 85.0
},
"desiredVersion": 3,
"reported": {
"firmware_version": "2.1.0",
"reporting_interval": 30,
"battery_level": 87
},
"reportedVersion": 12,
"tags": {
"location": "building-a",
"floor": 3
},
"lastActivity": "2026-02-25T10:30:00Z"
}
FieldUpdated byDescription
desiredCloud (operators, automation)Configuration the device should apply
desiredVersionAuto-incrementedMonotonic counter, incremented on each desired update
reportedDevice (via MQTT)Current device state as reported by firmware
reportedVersionAuto-incrementedMonotonic counter, incremented on each reported update
tagsCloud onlyMetadata labels, not visible to the device. There is no public API to set them today (see Tags).
lastActivityAuto-updatedTimestamp of the last reported property update

Reported Properties (Device → Cloud)​

The device publishes its state to the reported properties topic:

device/{deviceId}/twin/reported    (QoS 1)

Payload: A JSON object with the properties to report. This is a full replacement — the entire reported state is overwritten, not merged.

{
"firmware_version": "2.1.0",
"reporting_interval": 30,
"battery_level": 87
}

What Happens on Publish​

  1. The broker delivers the message to the platform
  2. The reported properties are stored in the database (atomic update)
  3. reportedVersion is incremented
  4. lastActivity is set to the current time
  5. The properties are forwarded to the message queue as a ProviderClientPropertiesChanged message for downstream processing (ingress, time-series storage, flows)

The twin and the asset view​

The twin and the asset's reported properties are not the same view:

  • The twin (GET /providers/{providerId}/clients/{clientId}/reportedProperties) holds exactly the last report. A key the device leaves out of a report is gone from the twin.
  • The asset (GET /assets/{assetId}/properties/reported) holds, for each property on the asset's model, the last value the device reported for it. A key the device leaves out keeps its previous value there, with no indication of its age. Keys that are not on the model do not appear.

A device that reports only the keys that changed therefore empties its twin of the other keys, while the asset keeps showing their last values.

When to Report​

Devices should publish reported properties when:

  • Startup — Report initial state (firmware version, hardware capabilities)
  • After applying desired changes — Confirm the new configuration is active
  • State changes — Battery level, connectivity status, error codes
  • Periodic heartbeat — Some deployments use a reporting interval

Because a report replaces the reported state, every report should carry the device's full reported state, not only the keys that changed.

Desired Properties (Cloud → Device)​

Operators or automation set desired properties through the platform API. The platform then publishes them to the device through the broker.

Setting Desired Properties​

Desired properties are set via the Beacon Tower API (not via MQTT):

curl -X PUT https://api.beacontower.ai/assets/temp-sensor-001/properties/desired \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"reporting_interval": 30,
"alert_threshold": 85.0
}'

Each write is applied to the twin as a JSON Merge Patch (RFC 7396):

  • A key with a value sets that key.
  • A key you leave out keeps its current value.
  • desiredVersion increases by one per write.

The asset API sends each property as its own write, so the example above produces two patches and two version increments. The provider route POST /providers/{providerId}/clients/{clientId}/desiredProperties, with a body of {"componentId": null, "desiredProperties": { ... }}, sends the whole object as one patch.

Neither route reports a failed write. PUT /assets/{assetId}/properties/desired stores the value on the asset and returns 200 even when the provider could not apply it; the provider route also returns 200 in that case. To confirm a write, read the twin's desired state, or wait for the device to report the new value.

On an asset property that is not bound to a device, a desired write is copied directly into the asset's reported value.

Receiving Desired Properties​

The device subscribes to:

device/{deviceId}/twin/desired    (QoS 1)

When desired properties are updated, the device receives the full desired state, published with retain=true. If the device is not connected at the time, the platform holds the update and publishes it when the device connects. Because the message is retained, a device also receives the latest desired state each time it subscribes.

{
"reporting_interval": 30,
"alert_threshold": 85.0
}

Desired-Reported Reconciliation Pattern​

A well-behaved device follows this pattern:

1. Cloud sets desired: {"reporting_interval": 30}
2. Device receives desired on twin/desired topic
3. Device applies the change (updates its interval timer)
4. Device publishes to twin/reported: {"reporting_interval": 30, ...}
5. Operator sees reported matches desired → change confirmed

If the device cannot apply a desired change, it should report its actual state so operators can see the discrepancy.

Tags (Cloud Only)​

Tags are cloud-side metadata attached to the device twin. They are not visible to the device over MQTT.

The platform API has no endpoint for twin tags today. A request to PATCH /providers/{providerId}/clients/{clientId}/tags returns 404. The only tag endpoints are /resources/tags and /resources/{resourceId}/tags, which tag resources, not device twins (see Resources).

Versioning​

Both desiredVersion and reportedVersion are monotonically increasing integers. They enable:

  • Conflict detection — Compare versions to detect concurrent updates
  • Staleness checks — Verify the device has seen the latest desired state
  • Audit trail — Track how many times state has changed

Versions are managed server-side and cannot be set by the device.

Querying Twin State​

Via Platform API​

# Get reported properties
GET /assets/{assetId}/properties/reported

# Get desired properties
GET /assets/{assetId}/properties/desired
# The twin itself
GET /providers/{providerId}/clients/{clientId}/reportedProperties
GET /providers/{providerId}/clients/{clientId}/desiredProperties

The asset routes and the twin routes return different views of reported state; see The twin and the asset view.

Via Internal Messaging​

Internal services query twin state via NATS request-reply. The client id travels in the request payload, not in the subject:

QuerySubject
Get desiredprovider.{providerId}.client.desired-properties.get
Get reportedprovider.{providerId}.client.reported-properties.get
Set desired (merge patch)provider.{providerId}.client.desired-properties.set

Next Steps​