> For the complete documentation index, see [llms.txt](https://docs.datacake.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datacake.de/lorawan/payload-decoders/global-variables.md).

# Global Variables

## Concept

Sometimes you need Data to persist between the call of a payload decoder. This is normally done by using global environment variables that store the data between function calls.

### Access Database Fields

On Datacake this can be achieved by creating a dedicated field in the Database Section of your Datacake Device and by using this as the storage for this data.

```javascript
function decoder() {
    
    // get current value from field in Database
    var numberOfCalls = measurements.NUMBER_OF_CALLS.value;
    
    // do some processing
    numberOfCalls = numberOfCalls + 1;
    
    // return that field in order to write new value into database
    return [
        { "field": "NUMBER_OF_CALLS", "value": numberOfCalls }
    ]
}
```

## Time between changes

Storing data between payload decoder function calls can also be used to measure the time elapsed between a change of a value. This can be done by accessing not only the current value of a Database-Field but also the timestamp.

```javascript
 function decoder() {
 
     // pseudo code here - removed the parts before
 
     // get number of changes happened before from Database
     var lastNumberOfChanges = measurements.NUMBER_OF_CHANGES.value;
     
     // get state stored in Database
     var lastState = measurements.RO1_STATUS.value;
    
     // when new state is not equal state in database -> change happened
     if (lastState != current_RO1_STATUS) {
        
        // increase number of changes
        lastNumberOfChanges += 1;
        
        // store number of changes in database
        decoded.push({ "field":"NUMBER_OF_CHANGES","value":lastNumberOfChanges });
        
        // calculate the time between changes
        var currentTimestamp = Math.floor(Date.now() / 1000);
        var lastTimestamp = measurements.RO1_STATUS.timestamp;
        var timeDelta = (currentTimestamp - lastTimestamp) / 60;
        var timeBetweenChanges = Math.abs(Math.round(timeDelta));
        
        // store new time between last changes in database
        decoded.push({ "field":"TIME_BETWEEN_CHANGES","value":timeBetweenChanges });          
     }

    return decoded;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.datacake.de/lorawan/payload-decoders/global-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
