Triggers

Triggers work as listeners that monitor specific events or changes to automatically invoke functions in response. They convert incoming events to HTTP requests to asynchronously invoke all the Functions that match certain criteria.

You will need to define either an MQTT trigger or a Cron trigger, depending on what you have defined in your Function.

MQTT Trigger

If you have defined an MQTT topic as the event trigger that will initiate the execution of your Function, you need to create an MQTT trigger to listen to that particular MQTT topic.

The MQTT Trigger will connect to an MQTT broker and create a subscription to the topics defined on it. You can use multiple topics per Trigger and use wildcards to keep things simple.

When a message is received, the Trigger will compare the topic of that message with the topics configured in the Functions. All the matching Functions will be invoked.

When a MQTT Trigger invokes a Function, the payload of the MQTT message is sent as the body of the HTTP invocation request. The topic of the message is sent in the X-Topic header of the request.

You have the option to specify a callback URL where the trigger will send notifications or data payloads when certain events occur. This callback URL acts as a point of contact for receiving information from the trigger. You can use the webhook website to generate your unique URL for easily inspect and test the incoming requests.

Create an MQTT Trigger

Complete the steps below to create a Trigger.

  1. Click User Functions > Triggers.
  2. Click New Trigger.


  3. In the New Event Trigger panel, enter the Trigger description and type.
    Figure 1.


  4. If you are using the MQTT broker provided by IoT Studio, enter the following values:
    1. Under Config, enter the host as mqtt.swx.altairone.com and port 1883.
    2. The username and password should allow access to the specified MQTT topic. By providing these credentials, the communication between the trigger and the MQTT broker is secure and controlled. If the topic is listening to a specific thing, you can use the MQTT Thing's credentials. If you are using a topic based on a label, you can use the MQTT label's credentials
    3. Under Add Topic, enter the MQTT topic.
      Figure 2.


      This endpoint must be the same as the Event Trigger in Functions.

  5. You have the option to specify a callback URL where the trigger will send notifications or data payloads when certain events occur. This callback URL acts as a point of contact for receiving information from the trigger. You can use the webhook website to generate your unique URL for easily inspect and test the incoming requests.
  6. Click Save.
    The Trigger status will change to Running.
    Note: If a User changes the credentials, the Trigger's status can change to Failed.

Cron Trigger

A Cron Trigger allows you to schedule when to invoke a Function, using the following Cron expressions.
Note: A Cron Trigger must be created to enable to edit the Cron expression. More information about this can be found clicking the help button . Below some examples are shown:
Schedule minute, hour, day of moth, month, day of week.
  • */10 * * * * to fire the Function every 10 minutes.
  • 0 0 1 * * to fire the Function once at 00:00 on the first of each month.
  • 0 0 * * 3 to fire the Function at 00:00 every Wednesday.
Use one of the following descriptors:
  • @yearly or @annually
  • @monthly
  • @weekly
  • @daily or @midnight
  • @hourly

Create a Cron Trigger

Cron Triggers require no configuration, so just one Cron Trigger can be created for space. Once you have created a Cron Trigger, the option to add a new Cron Trigger will disappear from the type drop-down menu.

  1. Click User Functions > Triggers.
  2. Click Add.
    Figure 3.


  3. In the New Event Trigger panel, enter the Trigger description.
  4. From the Type drop down menu, select Cron.
    Figure 4.


  5. Click Save.