Datacake Docs
  • Welcome
  • Get Started
  • Changelog
  • Best practices
    • Best practices: Getting started
    • Best practices: Resolution (Timeframe)
    • Best practices: Dashboards
    • Best practices: Reports
    • Best practices: Grouping Devices
  • Datacake LNS
    • Getting Started
    • Gateways
      • Milesight LoRaWAN Gateway - UG6x
      • RAKwireless WisGate Edge Light 2
    • Devices
    • Add Devices without Gateway
  • Device
    • Product
    • Configuration
    • Claiming
    • Historical Data
    • Database
      • Configuration Fields
      • Fields
        • Manual input
        • Field Roles
        • Formulas
        • Mapping Fields
      • Data retention & Datapoints
      • Examples
        • Mapping 4-20mA Signals
        • Converting Units
  • Dashboards
    • Global Dashboards
      • Setting Homepage
    • Device Dashboards
    • Multi-Device Dashboards
    • Widgets
      • Value Widget
      • Chart Widget
      • Image Map
      • Map Widget
      • Text Widget
      • SOS Widget
      • Boolean Widget
      • iFrame Widget
      • Downlink Widget
      • Set Value Widget
      • Measurement List Widget
      • Heatmap Widget
      • Table Widget
      • Image Widget
  • Portal
    • Multi-Tenancy (Workspaces)
    • Reports
      • Energy Report
    • Administrators
    • Members
      • API Users
    • Security & Privacy
    • Billing
      • Support Packages
      • VAT Exemption / Tax ID
      • SMS Credits
      • Access Invoices
      • Unused time and Remaining time in Invoices (Prorations)
      • Codes
    • White Label
    • Rules
      • Legacy Rule Engine
        • Sending notifications via Telegram
      • New Rule Engine
        • Rule Engine Table Overview
        • Copy/Paste and Template Functionality
        • Advanced Rule Engine E-Mail and Webhook Templates
        • Time Restrictions
    • Zones
  • Cake Red
    • Get Started
    • Overview
  • LoRaWAN
    • Get Started
      • CSV Import
      • Custom LoRaWAN Device
    • Configuring LNS
      • The Things Stack (TTN / TTI) Automated Setup
      • The Things Stack (TTN / TTI) Manual Setup
      • Loriot
      • Kerlink Wanesy
      • Helium
      • ChirpStack
      • Tektelic
      • Actility ThingPark
      • Senet
      • Milesight Gateway
      • KPN
    • Downlinks (LoRaWAN)
      • Set Time Frame
    • Securing Webhooks
    • Payload Decoders
      • Location and GPS
      • Access Gateway Metadata
      • Access Measurements
      • Global Variables
    • Using Cayenne LPP
    • Converting Payload
  • Generic API Devices
    • HTTP Downlinks
  • Integrations
    • MQTT
    • Particle
      • Get Started
      • Adding Integrations
      • Decoding Payloads
      • Calling Functions
      • Templates
        • Particle Tracker One
    • Incoming Webhooks
    • Outgoing Webhooks
      • Securing Outgoing Webhooks in Datacake
    • Golioth
    • Blues Wireless Notecard
    • Sigfox
    • Swarm Asset Tracker
    • Grandcentrix Modbus Cloud Connect
    • YuDash LYNX IoT Gateway
    • Dragino NB-IoT
      • Changing NB-IoT Bands
    • Hardwario CHESTER
    • 1NCE OS
  • API
    • Exporting Data
    • Record Measurements via API
    • Node RED to Datacake
    • Generate Access Token
    • Internal MQTT
      • Get Started
      • MQTT Documentation
      • MQTT over WebSocket
      • Example Code
    • GraphQL API
      • Using GraphQL
      • Device Creation
      • Tutorials
        • Read Group via Tags
  • Guides
    • Python
    • Send Slack Notifications
    • Forward Data to Cake Red
    • Multiple Data Feeds
    • Automated Dynamic Downlinks
    • Ingesting JSON Data into Datacake API Devices
    • Working with Default HTTP Decoder on API Devices and Code Examples
    • Accessing Measurements in Decoders
    • Connecting Datacake to Notion Using Zapier
    • How to set up and use location on non-GPS devices
    • How to integrate with AWS IoT Core
    • How to Schedule Mass-Downlinks (Multicast) using Datacake Rule Engine
    • How to Map Sensor Values to Ranges or Booleans, Strings using Mapping Fields
    • Understanding Internal vs. External MQTT Brokers on Datacake
    • Sending UDP Messages between 1NCE OS and Datacake
    • Concepts of LoRaWAN Payload Decoders on Datacake
    • How to check if a value stays above or below limits for given time
Powered by GitBook
On this page
  • Guide
  • Downlink Editor
  • The Code
  • Timezone Adjustment
  • Weekend Pause

Was this helpful?

  1. LoRaWAN
  2. Downlinks (LoRaWAN)

Set Time Frame

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

Last updated 3 years ago

Was this helpful?

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.

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

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:

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.

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
}

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.

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.

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];
    }
}
Downlink Overview on Datacake Device
Downlink Encoder Editor