Rediger

Add context to readings

Look up an asset's calibration offset and add it to each reading. This example adds an offset of 2 to a value of 20, producing correctedValue: 22.

Prerequisites

Complete the shared example prerequisites, including the default profile and factory-mqtt endpoint. Use YAML for debugging and Azure CLI or Bicep for production management.

Use the existing built-in registry endpoint named default. For the environment variables, set AIO_INSTANCE_NAME and RESOURCE_GROUP to your existing instance and resource group names. For Bicep, also set CUSTOM_LOCATION_NAME to your existing custom location name.

Synchronize the state-store writer's clock with the broker. Confirm a successful +OK response when populating the calibration record before publishing readings.

Configure a calibration lookup

Diagram of a reading with value 20 entering a map transform. The transform looks up offset 2 for the same asset in the state store and emits correctedValue 22.

Use enrichment to read calibration records that an application maintains in the state store. The lookup uses the context available during processing, not necessarily the context valid when the reading was sampled. For that case, match context by timestamp.

Deploy the calibration graph:

  1. In the operations experience, open your instance and select Data flow graph > Create data flow graph.

  2. Name the graph add-calibration and select the default profile.

  3. Add a source named source using endpoint factory-mqtt and topic factory/readings.

  4. Add a map transform named calibrate, using azureiotoperations/graph-dataflow-map:1.0.0 from registry endpoint default.

  5. In the map transform configuration, add a dataset with State store key factory-calibration as calibration, Match inputs $source.assetId and $context.assetId in that order, and Match expression $1 == $2.

  6. Add these map rules:

    Inputs (in order) Output Expression
    * * Leave empty
    value, $context(calibration).offset correctedValue $1 + $2
  7. Add a destination named destination using endpoint factory-mqtt and topic factory/calibrated.

  8. Connect source → calibrate → destination, then select Save to deploy the enabled graph.

Before sending readings, write this synthetic NDJSON record under state-store key factory-calibration. Use the state-store SET procedure in Populate the state store key.

{"assetId":"line-1","offset":2}

Publish this reading to factory/readings:

{"assetId":"line-1","value":20}

Expect this payload on factory/calibrated. The map preserves the original fields and adds 20 + 2:

{"assetId":"line-1","value":20,"correctedValue":22}

This graph assumes a matching calibration record with a numeric offset. Before using it in production, define how to handle missing context and monitor lookup errors; the example doesn't include an invalid-context route.