Hi @Juanhe Shen Thank you for posting the question on this forum. Have you looked into the code sample from Python SDK dt_digitaltwins_get.py The sample provides a reference on how to get the Digital Twin data using Python. I have tried the same code and could extract a single property of the Digital Twin. Please refer the below code sample change I have added to the GitHub code to extract a single property from the returned Digital Twin dictionary.
import os
import sys
import logging
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
from azure.digitaltwins.core import DigitalTwinsClient
try:
url = os.getenv("AZURE_URL")
# DefaultAzureCredential expects the following three environment variables:
# - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
# - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
# - AZURE_CLIENT_SECRET: The client secret for the registered application
credential = DefaultAzureCredential()
# Create logger
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
# Create service client and enable logging for all operations
service_client = DigitalTwinsClient("https://<ADTInstanceName>.api.eus.digitaltwins.azure.net", credential)
digital_twint_id = "ReadingTank"
# Get twin
digital_twin = service_client.get_digital_twin(digital_twint_id)
print("Starting to print digital twin")
print(digital_twin)
print('Extracting level of material')
for key, value in digital_twin.items():
if key == 'Level_of_Material':
print(value)
except HttpResponseError as e:
print("\nThis sample has caught an error. {0}".format(e.message))
For testing purposes I have directly provided the ADT instance URL in the line of the code where the service_client
is initialized. I have also removed the function overload logging_enable=True
. I could get the twin data as expected and could extract the value. Please refer the below image for reference.
Hope this helps. Please let us know if you have any additional questions on this.
If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.