Quickstart -> Actions

Actions are programmable units of compute you can write, and they'll be triggered on incoming messages from your devices. You can use them to execute effects such as triggering webhooks, or sending/broadcasting messages to devices. The idea is you write logic on Fostrom, and keep your devices managing input/output. This way, you can evolve your IoT projects without having to deal with messy over-the-air updates and complex testing of independent systems.

We'll write one Action today, but you can go ahead and play around to build more advanced behaviours.

This action will get triggered when the message detected-incorrect-zone is sent from a drone. It will then figure out which gate to open or close, and queue a message to that gate controller's device mailbox. The gate controller will then be pushed the message (if it is online) or fetch the message whenever it connects next.

Writing Actions

To create the action, head over the Actions tab in your fleet, and click on the New Action button. We'll put the name manage-animal-flow for the action. We want it to trigger on an incoming message so we'll leave the Trigger Type as Packet (the default), and choose our message packet schema detected-incorrect-zone from the list below. Go ahead and create the action. You'll see a code editor wherein you can write JavaScript to build your logic and execute effects.

The action has access to the incoming message's payload, so we'll know which zone is the gate supposed to be open in, and how many animals are in that area. We'll use these to build our logic and queue a message to the right gate controller to open its gates.

// if the message does not specify to open the gate,
// let's end the execution here.
if (!pl.open_gate) { return }

if (pl.zone == 'a') {

  fostrom.sendMsg("zone-a-gate", "set-gate-state", {
    zone: pl.zone, open_gate: true, priority: 0
  })

} else if (pl.zone == 'b') {

    if (pl.n_animals_in_fov > 5) {

      fostrom.sendMsg("zone-b-large-gate", "set-gate-state", {
        zone: pl.zone, open_gate: true, priority: 1
      })

      console.warn("Opening Large Gate...")

    } else {

      fostrom.sendMsg("zone-b-small-gate", "set-gate-state", {
        zone: pl.zone, open_gate: true, priority: 0
      })

    }

}

There is a small caveat in the code above. We're writing the names of the devices and packet schemas in a string, but that is not correct. When you write the code yourself, you'll see an autocomplete (or press Ctrl+Space to open the autocomplete) with a list of devices or packet schemas. You need to choose from that list for this code to work. Once you do that, you'll see a blue box around your device name or packet schema name, and that's when you know its the correct way.

Once you've entered the code you want, click on the Deploy Action button. This will ensure that your new or updated code will execute the next time this action is triggered.

Actions are quite powerful, but we're also actively developing and improving them. Head over the Actions documentation for more details.

Now that we've setup the action, let's go ahead and send a message from the drone, and have the message delivered to a gate controller.