cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

How to create and configure Timers and Schedulers via JavaScript Services

Highlighted
Level 10

How to create and configure Timers and Schedulers via JavaScript Services

  • Timers and Schedulers can also be created and configured programmatically via custom services.
  • The following service, which can be created on any Thing, will create a new Timer using the following Inputs:

 

CreateTimerServiceInputs.png

 

 

 

// create new Thing

var params = {
	name: ThingName /* STRING */,
	description: undefined /* STRING */,
	thingTemplateName: "Timer" /* THINGTEMPLATENAME */,
	tags: undefined /* TAGS */
};

Resources["EntityServices"].CreateThing(params);

// read initial configuration
// result: INFOTABLE

var configtable = Things[ThingName].GetConfigurationTable({tableName: "Settings"});

// update configuration with service parameters

configtable.updateRate = updateRate
configtable.runAsUser = user

// set new configuration table

var params = {
	configurationTable: configtable /* INFOTABLE */,
	persistent: true /* BOOLEAN */,
	tableName: "Settings" /* STRING */
};

Things[ThingName].SetConfigurationTable(params);

 

  • This code is an example which could also be used to create a new Scheduler.
  • The configuration table for a Timer has the following attributes:
    • updateRate
    • enabled
    • runAsUser
  • The configuration table for a Scheduler has the following attributes:
    • schedule
    • enabled
    • runAsUser

 

2 REPLIES

Re: How to create and configure Timers and Schedulers via JavaScript Services

I have currently a taks where this code is helpful but my quesiton here.

Do we need to set the thingname in the param json? like:

// create new Thing

var params = {
	name: ThingName /* STRING */,
	description: undefined /* STRING */,
	thingTemplateName: "Timer" /* THINGTEMPLATENAME */,
	tags: undefined /* TAGS */
};
params.name=ThingName; Resources["EntityServices"].CreateThing(params); ....

or is already done in the param definition? Means the input parameter is used . No problem I will test it

Re: How to create and configure Timers and Schedulers via JavaScript Services

Hi Roland,

 

the "ThingName" is a service parameter and reused to substitute the name aspect of the params JSON.

You can either set it explicitly or use variables. As long as the parameter is passed correctly to the CreateThing service, it doesn't matter if it's set directly in the params or added later on - but it has to be part of the parameters once the CreateThing service is called.

 

I have used the ThingName as service parameter and not hardcoded, as I can create dynamic named Things with it - and the name will be used multiple times during the service call, e.g. to call the GetConfigurationTable service.