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 assigning training courses to drivers based on their safety events. It interacts with Samsara APIs to fetch drivers based on tags, check existing assignments, and create new assignments for drivers based on their safety events.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.
Example use cases
- A Safety manager wants to assign the “Electronic Distractions” course to a driver when the driver has 2 or more Mobile Usage events in the past 30 days.
- A Fleet manager wants to assign the “Speeding” course to a driver when the driver has 3 Severe Speeding events that are marked as “Needs Coaching” status in the past 45 days AND has a safety score below 80.
Trigger conditions
- Specify safety event count threshold (e.g., 2 events in 45 days)
- Specify safety behavior (e.g., “Mobile Usage”)
- Specify safety event status (e.g., “Needs Coaching”)
- Specify conditions under which assignments will skip drivers
- Specify driver tags for assignment
Action
- Assign training course with specific due dates
Execution flow
Fetch safety events
Convert
SAFETY_EVENTS_LOOKBACK_START and SAFETY_EVENTS_LOOKBACK_END to RFC3339 format. Retrieve safety events for the given period and tag IDs.Filter drivers based on event behavior and status
Count the number of times each driver has exhibited the specified safety behavior, considering only the events for which the status matches
EVENT_STATUS. Filter only drivers who meet or exceed BEHAVIOUR_THRESHOLD.Check driver safety scores
Convert
SAFETY_EVENTS_LOOKBACK_START and SAFETY_EVENTS_LOOKBACK_END to milliseconds. Retrieve safety scores for each driver and filter drivers whose score is below SAFETY_SCORE_THRESHOLD.Identify drivers who require training
Loop through the drivers that meet the above thresholds. If a driver has already been assigned the course, omit those drivers from
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. These code snippets can be copied but will need to be customized to match your organization’s specific use case.
Appendix
Constant definitions
| Constant | Type | Description |
|---|---|---|
SAFETY_EVENTS_LOOKBACK_START | Integer | How many days back to look for safety events. |
SAFETY_EVENTS_LOOKBACK_END | Integer | End date for looking up safety events. |
BEHAVIOR_TO_COACH | String | Specific safety event behavior to track. |
EVENT_STATUS | String | The status for a safety event. |
TARGET_DRIVER_TAG_IDS | List of Strings | Tag IDs used to filter specific drivers. |
SAFETY_VIOLATION_THRESHOLD | Integer | Threshold for number of behaviors before training is assigned. |
SAFETY_SCORE_THRESHOLD | Integer | Drivers with a safety score below this threshold will be considered. |
COURSE_ID | String | ID of the training course that will be assigned. |
TRAINING_DUE_DAYS | Integer | Number of days from today to set as the due date. |
TRAINING_LOOKBACK_DAYS | Integer | Number of days to look back for training assignments. |
Function definitions
| Function | Description |
|---|---|
get_safety_events | Fetches safety events for a given time range and filters by driver tag IDs. |
get_driver_safety_score | Retrieves a driver’s safety score for a given time range. |
assign_training | Creates training assignments for drivers. |
get_training_assignment_details | Retrieves training assignments filtered by assignment_interval_timestamp and course_id. |
generate_rfc3339_timestamp | Generates a UTC midnight RFC3339 timestamp. See Timestamps. |
generate_milliseconds_timestamp | Generates a millisecond timestamp by adding or subtracting delta_days from now. |