Understanding Templates

Templates are at the heart of IoTIFY's Network & Database simulator. Let's have a closer look at them.


A template is a javascript document which describes how your virtual device will behave during simulation. Every aspect of device behavior which needs to be modelled should be described in the template.

When a network simulation is started, the simulator will process template and create virtual devices. Each virtual device will then independently behave and connect to the IoT cloud platform, until your simulation is running.

If you are familiar with C++ or any other object oriented design, think of templates as Classes and virtual devices as Objects.

The template contains information about messages to be sent to the cloud platform and connectivity. When you are writing a template, you will need to define the body of a javascript function. The function must return a string or a Javascript buffer object, which is to be sent to your IoT cloud platform upon each iteration. The template function is invoked with a special variable called state. Let’s discuss this in further detail.

State variable

Every IoT device has several important parameters to be reported to the cloud platform. An example could be a temperature value read through a sensor or the current CPU load. The device has also some internal parameters such as avaiable SSD and memory usage, which are required to be tracked. While modelling the device, you will need a local storage to save these values and any other intermediate variable which needs to be preserved across the simulation. state is a JSON object which can be used for this purpose. E.g. consider following code snippet of a simple template:

// if temperature has not been defined previously
if (state['temperature'] === undefined ) {
state['temperature'] = 50;
return JSON.stringify(state);

here, in the very first iteration, the state object will be empty. So we will initialize the temperature value with 50. Since state is preserved across simulation, upon each subsequent call of the template, the temperature variable will increment by 1.

Returning contents

It is important that at the end of processing, function body must return a value to be sent to the cloud platform. The return value could either be a string or a binary Buffer, depending upon what values are accepted by your cloud platform provider. If you would like to skip sending anything for that particular iteration, simply return ‘undefined’ as a string.

if ( index() % 2 == 0 ) {
return 'undefined';
else {
return 'hello world';

In the above example, every alternate invocation will be skipped and every other iteration will return ‘hello world’ to the cloud platform.

Using Helper functions

You could write and use most of the native Javascript functions in the template. There are many additional helper functions available in the scope of the template body. Let’s have a short look at them.


Chancejs is a random content generation library which is quite useful to generate data. E.g. to generate a random street addresss

state['street'] = chance.address();
state['city'] = chance.city();
state['zip'] = chance.zip();
return JSON.stringify(state);

And voila, you have a perfectly looking fake street address.


Momentjs is another super useful library, especially to manipulate time. In below example, we will calculate the elapsed time since the simulation started (iteration 0)

if ( state['start'] === undefined ) {
state['start'] = moment.now();
state['elapsed'] = moment(state['start']).fromNow();
return JSON.stringify(state);


UnderscoreJS is another popular library which is a must have for serious javascript programmer. Here is an example of filtering even values in an array

var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){
return num % 2 == 0; }
return JSON.stringify(evens);

Native builtin object

There are several built in module available in the function body. Math is one of them which is quite useful for arithmatic.

We have written several helper libraries which can be used in the template. Check them out in the next section.