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 code which determines how your device behave during simulation. Every aspect of device behavior needs to be represented and controlled in the template.

When the network simulation is ongoing, the network simulator will process template for every simulated client and will send the generated message out to your IoT cloud platform.

Templates could be reused across the database or network. For the better understanding, let’s focus on Database applications for now.

A database template is simply a valid JSON document with special syntax. Unlike a normal JSON object, it has certain fields which are interpreted while generating a record out of it. These fields are represented in a special syntax form ( Inside double curly brackets ) as follows:-

{{ special_field }}

Let’s look at a simple template.

This template represents the data captured through a coffee machine. A coffee machine, in this case, generates six different fields, each representing a particular property of the machine. 

You are free to choose how many properties you would need to represent your database. You are also allowed to choose any name for the key value. Keep in mind that you shouldn’t use any special characters in the keys if you would like to store it into a MongoDB database. 

Static Values

Any value which is not a special field will always be present in all of the generated records. productcode in the above example is one such value. These could be Numbers, Dates, Booleans or any other Javascript valid objects. 

Random Values

Database generator will interpret special fields and generate random values based on what is specified in the field. You could use chance.js functions to generate random data. Chance.js provides many functions like name(), age(), string(), zip(), phone(), etc. You can find all chance functions with their snippets in the quick reference search bar on the right.

Could special fields be also used inside JSON keys? Like this:-

"{{chance.word()}}": 10,
"numberString": "10"

Unfortunately, this is not supported for JSON structures.

What can you write inside an expression?

Expression syntax is based on Javascript. If you are not familiar with Javascript, don’t worry. It is super easy. Just checkout few tutorials and you will be ready in no time.

You can write any standard Javascript code within an expression as follows:

"hello": "{{ 5 + 10 % 21 + Math.floor(Math.random()*123) }}"

The expression will be interpreted at the runtime and output will be a string, replacing the original expression.

Automatic Parsing of Integer values

It is important to note that the template engine will automatically try to parse the values and convert them to integer or float or boolean if possible. If you would like to preserve the output value as string, just append an empty single quote character to the end, as follows:

"number": "{{ 5 + 5 }}",
"numberString": "{{ 5 + 5 }}''"

The above template will produce the following:

"number": 10,
"numberString": "10"

Notice that the two single quote characters at the end of the string. They will be eventually replaced by our engine, but the resulting value will remain as a string, despite being an integer or float.

Nested Objects

Inside a JSON object, the value of an attribute can be another JSON object, like in the following example:

The “phones” attribute is a JSON object with two inner attributes. The template engine will parse the attribute values not only in the main object (i.e. the values of the “company” and “duties” attributes), but also in the nested object (“mobile” and “work”).

Repeating Values

If a key has an array type, you don’t need to specify all the members. You could simply call a repeat() function to generate multiple values of the same type as follows:-

Syntax: { repeat(N) : value }  => [value, value, value, …. N times ]

N is the number of times you want to repeat the value field.

For example, in order to generate an array of 5 words, you would need:-

{ tag : { repeat(5) : {{ chance.word() }} } }

Upon generation, this will transform into

{ tags: ["dofidoh","wudviuj","kij","sowkase","notim"] }

Note that the entire object including the repeat(N) keyword will be replaced by an Array.

Custom Fields

Quite often, you would like to control what values are generated for a custom field. This could be controlled by writing a javascript IIFE function. Read more about this in our guide Generating custom values.