A message handler can be created in a Zoho IoT application by selecting "Message Handler" in the Category field while creating a custom function.
Configuring the fields while creating a message handler
Language: Deluge is the supported language for writing message handler custom function.
Module/Model: The model/module to which the message handler applies is selected in this field. This associates the message handler with all device instances created based on the model. Alternatively, you can create a message handler specific to an individual device instance. Click here to learn more.
State Memory: Enabling state memory provides a dedicated space for the device to store and retrieve contextual data between successive messages. This enhances the functionality of your message handler by allowing it to retain and utilize context across multiple messages from the same device.
For example, consider an energy meter sending periodic usage data. With state memory enabled, the system can store the cumulative energy consumption for the month. When a new reading is received, the message handler adds this value to the stored total, updating the monthly consumption. This approach enables real-time tracking of total energy usage for the month.
Reset State Memory: When state memory is enabled, you also have the option to reset the stored value if the message handler function encounters a failure. Using the counter example from the State Memory example, this means you can reset and reinitialize the counter if the message handler function does not execute successfully.
Saving the Message Handler custom function
Once the field values are updated, click Save. This will open the Code Editor for the custom function.
Scripting the Message Handler custom function
Once the message handler custom function is saved, you can add the code for your message handler in the code editor.
Here is a sample script of code for a message handler custom function along with its explanation. Saving and Executing the message handler code
Once you've scripted your code, you can test it by clicking Save and Execute. This opens the Execute Function dialog box, allowing you to test your code by entering specific input values for Message, Status Type, Device ID, and State Memory are required.
Here is a sample script of code of a message handler custom function that is tested and explained. Exiting the message handler custom function
After testing your code, you can save the custom function and exit the code editor by clicking Save and Close.