# Access Measurements in API Decoders

## Reference

### Convert Serial to UUID

The measurements object, which holds the measurement data of all database fields, contains an additional level in the dictionary for API and MQTT Decoder, namely that of the device ID. The dictionary looks like this here.

`measurements["26ab17ac-5ce4-41a1-b873-b2dff76a8f38"].TEMPERATURE.value`

However, if you do not have the Datacake UUID available within these decoders, then you can use the following helper function in the decoder to convert a user serial to the respective Datacake UUID of your device in order to be able to identify the device in the measurements object.

```javascript
var data = JSON.parse(payload.body)
var serial = data.serial // as an example

// Now request the Datacake UUID for given user-serial
var datacakeUUID = deviceSerialToId[serial]

var temperatureInDB = measurements[datacakeUUID]["TEMPERATURE"].value;
```

## Examples

### Message Counter

#### API Device Decoder

```javascript
function Decoder(payload) {
    
  var data = JSON.parse(payload.body);

  var serial = data.serial

  // lets assume you have a database field called MESSAGE_COUNT
  // and you want to increment this +1 everytime this decoder runs

  // first we need to load previous state (actually current state before new data will be ingested) 
  // of the database field into the decoder

  var message_count = measurements[deviceSerialToId[serial]]["MESSAGE_COUNT"].value;

  /*
  Info
  Datacake API decoder do have a helper function to get the ID from custom 
  serial numbers provided by the user.
  -> deviceSerialToId["ABC123123"]
   */

  // Now increment
  message_count += 1

  // Return back to Datacake
  return [
    {
      field: "MESSAGE_COUNT",
      value: message_count,
      device: serial
    }
  ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datacake.de/generic-api-devices/access-measurements-in-api-decoders.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
