UbiFunctions, Ubidots FaaS (Functions as a Service) module allows users to create their own HTTP API endpoints to perform actions or add functionalities that might fall outside Ubidots’ built-in features and need to be tailored to each user application.
As custom, user-built endpoints, UbiFunctions can be called via an HTTP webhook to execute any given logic, but with the native UbiFunctions action in the events module, doing so in a data-driven fashion makes this process even easier.
In this article you’ll learn how to trigger an UbiFunction using Ubidots’ events engine.
Requirements
Ubidots account (Industrial and above).
At least one UbiFunction.
NOTE: If you don’t have one yet, follow this article first
1. Create a new event
To begin, head to the “data” tab →"Events" and hover over the “+” button, located at the upper right corner of the screen, and create a conditional event, global event, or scheduled event depending on your needs.
Next, configure the event’s logic using triggers with an AND/OR logic. For additional details on how to configure these triggers, head to their corresponding guides: value based, inactivity, geofence.
2. Configure the UbiFunction action
With the "trigger" logic configured, it’s now time to set up the UbiFunction action.
1. Click on the “+ add action” button to start the process. From the available options, choose “trigger function”.
2. Select the method to trigger the UbiFunction: either GET or POST. The UbiFunction must have the same method configured.
3. Search for and select the function to be triggered in the “select function” dropdown.
4. Add the necessary headers (for POST requests, the default “Content-Type: application/json” header is required). You can add multiple headers by clicking the “+ add header” button.
5. Customize the “active trigger” and the “back to normal” (optional) messages.
NOTE: These fields always expect a JSON object. When the HTTP method is GET, the JSON is automatically converted to query parameters and appended to the UbiFunction's URL.
The following is a valid message that follows the Ubidots-compatible JSON format. Check out our API documentation to learn more.
{“value”:11.5, “timestamp“:1613665401000, ”context“:{”status“:”active“}}
By using the tag icon at the upper right corners of the “subject” and “message” fields you can easily add information such as device ID, timestamp, variable name, etc.
Note: By default, data inputs involving timestamps are sent in human-readable format (YYY-MM-DD HH:mm:ss TZ). If you'd rather attach UNIX epoch time in milliseconds, double-click on the data input and delete the text from the pipe symbol ("|") until the end, as shown below.
6. Optionally, enable the “repeat action” if you want to trigger the UbiFunction multiple times while the event is triggered. You can choose how often and up to how many times the action will be repeated. There’s a limit of 50 action repetitions.
7. Click on “save” once you finish configuring the action and on “next”, unless you want to set up multiple actions.
8. After completing the action configuration you’ll proceed to the final step, the “settings” tab. There, you’ll name the event and give it a description (optional).
9. Another optional step is to use a tag on the event. This is a great tool to control what your end users see in your app. In this case, for instance, you could decide that your end user sees one event, but not another, using tags. Learn more about tags here.
10. Finally (unless the event you created is a scheduled event), determine the activity window in which the events should be executed. An event can have multiple active windows by clicking on the “+ add window button” and configuring them correspondingly. After finishing this, click on “save” and your event will be deployed.