Hands-on Azure Digital Twins Read Online Official
The Problem: You have a building, a factory, or a logistics center. Sensors are streaming data, but the data is "dumb"—it’s just a timestamp and a number. A temperature of 22°C in isolation means nothing.
You can do this via Azure CLI , REST API , or Azure Digital Twins Explorer . Let's use the CLI for automation.
"@id": "dtmi:handsOn:Zone;1", "@type": "Interface", "displayName": "Zone", "contents": [ "@type": "Property", "name": "temperature", "schema": "double", "writable": true , "@type": "Relationship", "name": "contains", "target": "dtmi:handsOn:Shelf;1" , "@type": "Relationship", "name": "hasSensor", "target": "dtmi:handsOn:Sensor;1" ] hands-on azure digital twins read online
# Connect to ADT credential = DefaultAzureCredential() service_client = DigitalTwinsClient(credential, "https://adt-warehouse-<unique>.api.eus.digitaltwins.azure.net")
az dt twin create --adt-name adt-warehouse-<unique> --dtid "ZoneShipping" --dtmi "dtmi:handsOn:Zone;1" az dt twin create --adt-name adt-warehouse-<unique> --dtid "ShelfA" --dtmi "dtmi:handsOn:Shelf;1" Create sensors az dt twin create --adt-name adt-warehouse-<unique> --dtid "TempSensor-Rcv" --dtmi "dtmi:handsOn:Sensor;1" --properties '"sensorType":"temp"' Step 4: Build the Graph (Relationships) This is where the magic happens. Connect the twins. The Problem: You have a building, a factory,
We’ll simulate a temperature spike and compute a "high temperature" alert using a . Architecture Pattern: IoT Device → IoT Hub → Azure Function → ADT (patch property) The Function Logic (Python example): import json import logging import azure.functions as func from azure.digitaltwins.core import DigitalTwinsClient from azure.identity import DefaultAzureCredential def main(event: func.EventHubEvent): # Parse telemetry from IoT device telemetry = json.loads(event.get_body().decode('utf-8')) sensor_id = telemetry['sensorId'] new_temp = telemetry['temperature']
# --- Compute Logic --- # If temp > 30C, find the parent Zone and mark it as "overheated" if new_temp > 30.0: # Query to find parent Zone query = f"SELECT zone FROM digitaltwins zone JOIN sensor RELATED zone.hasSensor WHERE sensor.$dtId = 'sensor_id'" zones = service_client.query_twins(query) for zone in zones: # Add a computed property zone_patch = ["op": "add", "path": "/alert", "value": "Overheating"] service_client.update_digital_twin(zone['$dtId'], zone_patch) logging.info(f"Alert set on zone zone['$dtId']") You can do this via Azure CLI ,
# Warehouse contains Zones az dt twin relationship create --adt-name adt-warehouse-<unique> \ --twin-id "WarehouseMain" \ --relationship-id "rel-wh-to-rcv" \ --relationship "contains" \ --target "ZoneReceiving" az dt twin relationship create --adt-name adt-warehouse-<unique> --twin-id "ZoneReceiving" --relationship-id "rel-zone-to-shelf" --relationship "contains" --target "ShelfA" Zone has Sensor az dt twin relationship create --adt-name adt-warehouse-<unique> --twin-id "ZoneReceiving" --relationship-id "rel-zone-to-temp" --relationship "hasSensor" --target "TempSensor-Rcv"