Documentation Index
Fetch the complete documentation index at: https://samsara-showcase.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This script automates the process of scheduling training course assignments to drivers. It interacts with Samsara APIs to fetch drivers based on tags, check existing assignments, and create new assignments for drivers who have not yet been trained.This guide assumes you are already familiar with the basics of the Samsara Training Product and the overall GET Training Assignments, POST Training Assignments, and GET Training Courses APIs. If you are new to Training, see the Driver Training Guide first.
Example use cases
- An HR manager wants to schedule an “Annual Privacy and Security” training course to be assigned to drivers annually every January.
- A Fleet manager wants to schedule a “Safety Refresher” training course to be assigned to drivers every quarter.
Trigger conditions
- Define the frequency of training (e.g., every 12 months, every quarter)
- Specify when the automation stops
- Specify driver tags for assignment
- Specify conditions under which assignments will skip drivers (e.g., skip assignment if the driver has completed the course within the past 30 days)
Action
- Assign training course with specific due dates
Execution flow
Check execution date
Convert
PLANNED_EXECUTION_DATE from string to a datetime object. Compare the current date with the planned execution date and stop the script if they don’t match or if the current year exceeds STOP_YEAR.Generate assignment interval timestamp
Call
generate_rfc3339_timestamp() with TRAINING_LOOKBACK_DAYS to create the assignment lookback period timestamp.Retrieve training assignments
Call
get_training_assignment_details() to fetch training assignments within the defined lookback period for the specified course_id.Identify drivers who have completed training
Loop through the retrieved
assignment_details and identify drivers who have completed the training (status: completed). Add the IDs of completed drivers to drivers_completed_training.Identify drivers who require training
Loop through the drivers fetched from
get_drivers(). If a driver’s ID is not in drivers_completed_training, add them to drivers_requiring_training.Generate training due date
Call
generate_rfc3339_timestamp() with TRAINING_DUE_DAYS to get the training due date in RFC3339 format.Script
To run certain automations, you will need to set up a development environment. Ensure your environment has access to Samsara API endpoints and is properly configured for integration. These code snippets can be copied but will need to be customized to match your organization’s specific use case, including correct API tokens and other organizational details.
Appendix
Constant definitions
| Constant | Type | Description |
|---|---|---|
COURSE_ID | String | ID of the training course that will be assigned. |
TARGET_DRIVER_TAG_IDS | List of Strings | List of tag IDs used to filter specific drivers. |
TRAINING_LOOKBACK_DAYS | Integer | Number of days to look back for training assignments. |
TRAINING_DUE_DAYS | Integer | Number of days from today to set as the due date for training assignments. |
STOP_YEAR | Integer | The year after which the automation should stop. |
PLANNED_EXECUTION_DATE | String | The date on which the script should be executed. |
Function definitions
| Function | Description |
|---|---|
get_drivers | Retrieves a list of drivers associated with the configured tag_ids, handling pagination. |
get_training_assignment_details | Retrieves a stream of training assignments filtered by assignment_interval_timestamp and course_id. |
assign_training | Creates training assignments for drivers given course_id, driver_ids, and due_date. |
generate_rfc3339_timestamp | Generates a UTC midnight RFC3339 timestamp by adding or subtracting delta_days from the current date. See Timestamps for more information. |