Back to All

staticAssignedDriver - When does this get updated?

I am writing a python script which runs 7pm each day.

The script calls https://api.samsara.com/fleet/vehicles and returns the vehicle name and the staticAssignedDriver WHERE staticAssignedDriver is blank / null.

The purpose of this is to detect which drivers may have potentially forgot to remove their tacho card out the lorry at the end of their shift.

The data being returned sometimes is not true.

I.E a vehicle is showing a staticAssignedDriver, i'll go into the lorry and there is no card in the tacho machine.

When does staticAssignedDriver get updated?

Thanks

import requests
import json

url = "https://api.samsara.com/fleet/vehicles"
headers = {
    "Authorization": "Bearer samsara_api_REDACTED"
}

mailgun_api_key = "REDACTED"
mailgun_url = f"https://api.eu.mailgun.net/v3/REDACTED/messages"

response = requests.get(url, headers=headers)

if response.status_code == 200:
    data = response.json().get('data', [])
    filtered_data = [
        {
            "name": entry['name'],
            "driver_name": entry['staticAssignedDriver'].get('name', '')
        }
        for entry in data
        if 'staticAssignedDriver' in entry and entry['staticAssignedDriver'].get('name', '') != ''
    ]

    email_body = "\n".join([f"Vehicle: {entry['name']} | Driver: {entry['driver_name']}" for entry in filtered_data])

    mailgun_params = {
        "from": "[email protected]",
        "to": "[email protected]",
        "subject": "Potentially have cards left in",
        "text": f"The following vehicles may have cards in\n\n{email_body}"
    }

    mailgun_response = requests.post(
        mailgun_url,
        auth=("api", mailgun_api_key),
        data=mailgun_params
    )

    if mailgun_response.status_code == 200:
        print("Email sent successfully.")
    else:
        print(f"Error sending email: {mailgun_response.status_code}, {mailgun_response.text}")

else:
    print(f"Error: {response.status_code}, {response.text}")