# Set Time Frame

## Guide

In this tutorial, we are going to show you how you can use the downlink editor function to programmatically set a time frame in which a downlink is being queued up.

### Downlink Editor

Please open up your downlink editor or create a new downlink.

![Downlink Overview on Datacake Device](/files/LLzqTEVNDL7lHbwKrvbu)

Now you can see the downlink editor and here you can paste your custom downlink encoder. This looks like the following:

![Downlink Encoder Editor](/files/KkeUFFPnKJ2sGQnIOa6T)

Now you need to make changes to your existing Downlink Encoder or create a new one. We show you what you need to do in the next chapter.

### The Code

#### Original Encoder

Let's assume you would already have a downlink encoder set up and this would look like the following:

```javascript
function Encoder(measurements, port) {

    // Send the following payload
    return [0x00,0xFF,0x00,0xFF];
}
```

#### Timeframe Option

Now we will add a timeframe to the above downlink. This will look like the following. You can copy and paste this and only adapt the payload that you want to send.

```javascript
function Encoder(measurements, port) {
    
    // WARNING!!! Please note that this is UTC Time!!!
    // Please convert to your local timezone!!!
    
    var startTime = '07:00:00'; // 9am CET
    var endTime = '16:00:00'; // 6pm CET
    
    var currentDate = new Date()   
    
    startDate = new Date(currentDate.getTime());
    startDate.setHours(startTime.split(":")[0]);
    startDate.setMinutes(startTime.split(":")[1]);
    startDate.setSeconds(startTime.split(":")[2]);
    
    endDate = new Date(currentDate.getTime());
    endDate.setHours(endTime.split(":")[0]);
    endDate.setMinutes(endTime.split(":")[1]);
    endDate.setSeconds(endTime.split(":")[2]);
    
    // Check if is valid
    var valid = startDate < currentDate && endDate > currentDate
    
    // if the current time lies within the desired timeframe
    if (valid) {
    
        // send the actual downlink
        return [0x00,0xFF,0x00,0xFF]; // TODO: Replace with your Downlink!
    }
    
    // in case it is not valid, do not return anything here 
    // in order to abort sending a downlink
}
```

{% hint style="info" %}
If you now replace the downlink payload from the example with your own downlink payload, you have successfully set up a timeframe downlink.
{% endhint %}

### Timezone Adjustment

{% hint style="danger" %}
Downlink Encoders are being executed on the Datacake Backend so please make sure that the time you provide here is converted to your actual timezone (and in respect of stupid daylight savings)
{% endhint %}

For example, if you live in Germany you would likely add additional 2 hours.

```javascript
var startTime = '07:00:00'; // 9am CET
var endTime = '16:00:00'; // 6pm CET
```

### Weekend Pause

If you want your automated downlinks to be valid on workdays only (and avoid the weekend) you can adapt the above code by adding a "is it weekend already?" check.

```javascript
function Encoder(measurements, port) {
    
    var startTime = '07:00:00'; // 9am CET
    var endTime = '16:00:00'; // 6pm CET
    
    var currentDate = new Date()   
    
    startDate = new Date(currentDate.getTime());
    startDate.setHours(startTime.split(":")[0]);
    startDate.setMinutes(startTime.split(":")[1]);
    startDate.setSeconds(startTime.split(":")[2]);
    
    endDate = new Date(currentDate.getTime());
    endDate.setHours(endTime.split(":")[0]);
    endDate.setMinutes(endTime.split(":")[1]);
    endDate.setSeconds(endTime.split(":")[2]);
    
    // is currentDate on weekend?
    var weekend = (currentDate.getDay() == 6 || currentDate.getDay() == 0)
    
    var valid = startDate < currentDate && 
    endDate > currentDate && !weekend
    
    console.log("Is Valid?: "+valid);
    
    if (valid) {
        return [0x00,0xFF,0x00,0xFF];
    }
}
```


---

# 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/lorawan/downlinks/set-time-frame.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.
