Using Function Mode in Message template
The logic of your IoT endpoint could be quite complex to be fit into a simple JSON structure. While dealing with complex simulations such as Connected bus, we felt the need to provide more power and flexibility to our users.
Function mode enables a flexible and easy way of creating message contents when your template complexity is higher.
Simply choose the radio button Function while in your message template to enable function mode.
The idea of function mode is simple. In your template code, you provide the body of a function which returns a string. This string will be sent as the payload of the generated message contents.
You could call any helper in function mode without the need for double brackets.
The signature of the function is predefined as follows
The state can be used in any way you want to store your internal representation of device. E.g.
It is not necesary to return values only in state variable. You could choose any return values you deem suitable. The return value must be a string.
You could use any chancejs or momentjs function within your function as well. Take a look at the template guides to see what helper functions can be used.
Here is a simple example of showcasing the function mode
Let’s create a simple MQTT template which publishes on the topic /iotify/demo with following contents
You could now even have your own helper function inside.
Our connected truck simulator could now be written in function mode as follows