Set Time Frame
Programmatically send downlinks only within individual timeframe, not on weekends, within the week only.

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.
Please open up your downlink editor or create a new downlink.
Downlink Overview on Datacake Device
Now you can see the downlink editor and here you can paste your custom downlink encoder. This looks like the following:
Downlink Encoder Editor
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:
1
function Encoder(measurements, port) {
2
3
// Send the following payload
4
return [0x00,0xFF,0x00,0xFF];
5
}
Copied!

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.
1
function Encoder(measurements, port) {
2
3
// WARNING!!! Please note that this is UTC Time!!!
4
// Please convert to your local timezone!!!
5
6
var startTime = '07:00:00'; // 9am CET
7
var endTime = '16:00:00'; // 6pm CET
8
9
var currentDate = new Date()
10
11
startDate = new Date(currentDate.getTime());
12
startDate.setHours(startTime.split(":")[0]);
13
startDate.setMinutes(startTime.split(":")[1]);
14
startDate.setSeconds(startTime.split(":")[2]);
15
16
endDate = new Date(currentDate.getTime());
17
endDate.setHours(endTime.split(":")[0]);
18
endDate.setMinutes(endTime.split(":")[1]);
19
endDate.setSeconds(endTime.split(":")[2]);
20
21
// Check if is valid
22
var valid = startDate < currentDate && endDate > currentDate
23
24
// if the current time lies within the desired timeframe
25
if (valid) {
26
27
// send the actual downlink
28
return [0x00,0xFF,0x00,0xFF]; // TODO: Replace with your Downlink!
29
}
30
31
// in case it is not valid, do not return anything here
32
// in order to abort sending a downlink
33
}
Copied!
If you now replace the downlink payload from the example with your own downlink payload, you have successfully set up a timeframe downlink.

Timezone Adjustment

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)
For example, if you live in Germany you would likely add additional 2 hours.
1
var startTime = '07:00:00'; // 9am CET
2
var endTime = '16:00:00'; // 6pm CET
Copied!

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.
1
function Encoder(measurements, port) {
2
3
var startTime = '07:00:00'; // 9am CET
4
var endTime = '16:00:00'; // 6pm CET
5
6
var currentDate = new Date()
7
8
startDate = new Date(currentDate.getTime());
9
startDate.setHours(startTime.split(":")[0]);
10
startDate.setMinutes(startTime.split(":")[1]);
11
startDate.setSeconds(startTime.split(":")[2]);
12
13
endDate = new Date(currentDate.getTime());
14
endDate.setHours(endTime.split(":")[0]);
15
endDate.setMinutes(endTime.split(":")[1]);
16
endDate.setSeconds(endTime.split(":")[2]);
17
18
// is currentDate on weekend?
19
var weekend = (currentDate.getDay() == 6 || currentDate.getDay() == 0)
20
21
var valid = startDate < currentDate &&
22
endDate > currentDate && !weekend
23
24
console.log("Is Valid?: "+valid);
25
26
if (valid) {
27
return [0x00,0xFF,0x00,0xFF];
28
}
29
}
Copied!