Today we’re excited to announce smart fleets, a new Notehub feature that lets you automatically categorize your devices based on their data.
What are Smart Fleets?
Since day one, Notehub has made it possible to group your devices into fleets for easier management. However, the actual grouping process has always been manual, requiring a fleet manager to add new devices to their fleets using the Notehub UI or API, and to manually update those fleets as their project’s requirements change.
Smart fleets introduce a new way to automate device groupings using smart-fleet rules—expressions that Notehub evaluates to add or remove devices based on their data. Let’s look at how they work.
How to Use Smart Fleets
To try out smart fleets, visit any existing Notehub fleet’s settings, and find the new Smart Fleet Rules section.
When enabled, Notehub will run every incoming event through the smart-fleet rules you provide. The rules themselves must be a valid JSONata expression, and that expression must return one the following three functions to categorize your devices:
-
$addToFleet()
: Adds the current device to the fleet. -
$removeFromFleet()
: Removes the current device from the fleet. -
$leaveFleetAlone()
: Neither adds nor removes the current device from the fleet.
For example, the following JSONata expression looks for inbound data from a _temp.qo
Notefile, and automatically adds devices with a temperature over 20 to the current fleet, and automatically removes devices with a temperature under 20 from the fleet.
(
$notefileToCheck := "_temp.qo";
$threshold := 20;
$result := (file != $notefileToCheck)
? $leaveFleetAlone()
: (body.temperature > $threshold)
? $addToFleet()
: $removeFromFleet();
)
JSONata can be a bit intimidating, so let’s go through what’s happening in this example in detail. To start, using parenthesis in JSONata defines a scope, and a scope allows you to use variables.
/* This creates a scope */
(
/* This creates a variable, x, and sets its value to 5 */
$x := 5;
)
JSONata exports the last-declared variable within a scope.
/* The result of this expression is 7, as it’s the last-declared
variable within the scope */
(
$x := 2;
$y := 7;
)
JSONata has a ternary operator allowing you conditionally set variables.
/* The result of this expression is true, as the variable $temp
is > 10 */
(
$temp := 20;
$result := $temp > 10 ? true : false
)
Putting that all together let’s return to the example JSONata.
(
$notefileToCheck := "_temp.qo";
$threshold := 20;
$result := (file != $notefileToCheck)
? $leaveFleetAlone()
: (body.temperature > $threshold)
? $addToFleet()
: $removeFromFleet();
)
This expression performs two checks.
-
The first check
(file != $notefileToCheck)
is used to filter out data that’s not relevant to the current fleet. The example fleet is named “High Temp” and is only interested in temperature data, so this initial check tells Notehub to leave the fleet alone for other types of events such as platform events. -
The second check
(body.temperature > $threshold)
is a simple comparison that determines whether the device’s temperature is above the configured threshold, and adds or removes the device from the fleet accordingly.
Most smart-fleet scenarios can be implemented by making small tweaks to the example JSONata shown above. If you get stuck writing your own JSONata, feel free to reach out in our community forum for help.
Once you save your smart-fleet rules—you’re good to go. As event data comes in, Notehub will run your provided JSONata expression and categorize devices into your fleets automatically.
Smart-fleet assignments display in your project's device list, as well as on a device's Summary page.
What Can You Do With Smart Fleets?
Fleets with smart-fleet rules are still Notehub fleets, and can use existing fleet features. For example, you can apply environment variables to fleets with smart-fleet rules.
And you can use fleets with smart-fleet rules through the Notehub API.
This combination makes some interesting scenarios possible.
-
Suppose your project controls a sprinkler system for lawns, and you store how often the sprinklers run in an environment variable. You could create a “High Temp” smart fleet, and set a more-frequent interval for sprinklers that detect a higher temperature.
-
Suppose you’re tasked with building a daily report on devices with poor connectivity. Instead of looking through event data manually, you could create a smart fleet based off the metadata in
_session.qo
events, and use the Notehub API’s Get Fleet Devices request to retrieve a list of devices that match your criteria.
Overall, we’re excited about the power that smart fleets give you to organize your devices and automate business processes.
And we’re just getting started. If you have feedback about Notehub smart fleets, or if you have a scenario in mind you’d like to try out, let us know in the comments.