Schedule Custom Function

Schedule Custom Function

When a Schedule is configured in Zoho IoT, you define the action that should run automatically at a specific time or on a recurring basis. As part of this configuration, you can specify a custom function to execute whenever the schedule is triggered. Each time the schedule runs, the associated schedule custom function is executed.

Schedule custom functions must be written using the Deluge scripting language.

This document describes the steps to create one, how to associate it with a schedule, and an example script demonstrating its use.

Creating an Schedule Custom Function

To create an Schedule custom function,
  1. Click Add Custom Function.



  2. Select Schedules in the Category field.



  3. Provide the required values in the appropriate fields.
    Example -
    Language : Deluge
    Display Name: Fuel Aggregate External (Provide a name of your choice)
    Function Name: fuel_aggregate_external (Provide a name of your choice)
    Description:  A custom function to aggregate the fuel consumed and send it to an external CRM Application.



  4. Click Save. This will open the Code Editor.



  5. Provide the required code.



  6. Click Save and Close.


The schedule custom function is now created. You can now associate it with the required schedule. 

Schedule Custom Function Code Example

Below is an example of a scheduler custom function that aggregates the total fuel consumed by diesel generators and sends the consolidated value to an external application. The schedule is configured to run at the end of every month, ensuring that the custom function automatically performs the monthly fuel-consumption calculation and updates the external application with the finalized data operational tracking.

url1 = "https://app12345akyrw.zohoiot.com/iot/v1/datapoints/data?datapoint_name=Fuel%20Consumed &source=DG001&aggregation=sum&time_grouping=month&period=thismonth";

response1 = invokeurl

[

url :url1

type :GET

connection:"datapointcirrus"

];

url2 = "https://app12345akyrw.zohoiot.com/iot/v1/datapoints/data?datapoint_name=Fuel%20Consumed &source=DG002&aggregation=sum&time_grouping=month&period=thismonth";

response2 = invokeurl

[

url :url2

type :GET

connection:"datapoint-iot"

];

info "RESPONSE:: " + response1 + response2;

fuel_consumed_dg1 = response1.get("Fuel Consumed").get(0).get(1);

fuel_consumed_dg2 = response2.get("Fuel Consumed").get(0).get(1);

fuel_consumed_dg_all = fuel_consumed_dg1 + fuel_consumed_dg2;

url3 = "https://www.zohoapis.com/creator/v2.1/data/user.zuseredu/dg-monitiring/form/Fuel_Data";

payload = Map();

dataList = List();

recordMap = Map();

recordMap.put("Fuel_Consumed",fuel_consumed_dg_all);

dataList.add(recordMap);

payload.put("data",dataList);

response3 = invokeurl

[

url :url3

type :POST

parameters:payload.toString()

connection:"creator-iot"

];

info response3;
Notes
Note: In this custom function a connection is used for authentication. Refer to this document to learn more.

Associating with a schedule

Once the custom function is created, it becomes available for selection when configuring a schedule. You can associate the custom function in the Configure Actions field while creating or editing a schedule.

While adding/editing a schedule,
  1. Click Add Action.



  2. Select Custom Function in the context menu.



  3. Select the required custom function and click Associate.



  4. Click Create/Save.


 
The custom function will now be executed each time the schedule runs.