If you own a Tesla or know someone with a Tesla that will share their credentials :) you can test the example below to extract current range and battery level to calculate total battery range and baseline it with AIMS.


We can recommend a couple of resources to learn about the Tesla APIs:


To get started you will need to authenticate towards Tesla and get your access token, and then use this to get hold of your ID which you will use to query the APIs.


The bash code below will first wake up the car, then check if the charge state api is available before requesting the data. Then the total range will be calculated and sent to AIMS. Notice that for simplicity we use the agent defined in the tutorial in this section to send the data, which will present the value as percent (should be "quantity"). 



#!/bin/bash
totalrange=0

while true; do
echo "----------------"
echo "Waking up car"
echo "----------------"
wakeup=`curl --location --request POST 'https://owner-api.teslamotors.com/api/1/vehicles/40918054649431185/wake_up' \
--header 'Authorization: Bearer 0d8dc880592c763ed3d856347c5c422e01ee0cfa120e3fb286e6f7b904346742'`

UTCDATE=`date -u '+%Y-%m-%dT%H:%M:%SZ'`
sleep 30

if [[ $wakeup == *"online"* ]]; then
echo "----------------"
echo "Tesla is online"
echo "----------------"

status=`curl -o /dev/null -s -w "%{http_code}\n" --location --request GET 'https://owner-api.teslamotors.com/api/1/vehicles/40918054649431185/data_request/charge_state' \
--header 'Authorization: Bearer 0d8dc880592c763ed3d856347c5c422e01ee0cfa120e3fb286e6f7b904346742'`

if [[ $status == *"200"* ]]; then
echo "----------------"
echo "200 response from Tesla charge state API, fetching data"
echo "----------------"
charger=`curl --location --request GET 'https://owner-api.teslamotors.com/api/1/vehicles/40918054649431185/data_request/charge_state' \
--header 'Authorization: Bearer 0d8dc880592c763ed3d856347c5c422e01ee0cfa120e3fb286e6f7b904346742'`
level=${charger:55:2}
range_temp=${charger:74:5}
range=${range_temp%.*}

totalrange=$(($range * 100 / $level))
echo "----------------"
echo $UTCDATE" Tesla 100% range is "$totalrange
echo "----------------"

echo "----------------"
echo "Sending range data to AIMS"
echo "----------------"
curl --location --request POST 'http://test.aimsinnovation.com/api/environments/c6d56a45-a473-474e-813c-a5a6f6018667/statpoints/' \
--header 'Content-Type: application/json' \
--header 'X-System: 10' \
--header 'Authorization: Basic aW50ZXJuYWxfYWxlcnRAYWltc2lubm92YXRpb24uY29tOk1vQjIyMmls' \
--data-raw '[
    {
        "nodeRef": {
            "nodeType": "marius.int-sys.server",
            "parts": {
                "part1": "part1value"
            }
        },
        "statType": "marius.int-sys.server-cpu",
        "time": "'"$UTCDATE"'",
        "value": "'"$totalrange"'"
    }
]'
fi
fi
echo "----------------"
echo "Waiting 30 minutes"
echo "----------------"
sleep 1800
done


You can of course also expand the agent definition to cover more datatypes from the car, and extract & push these data the same way as explained above. Then add in events that gives status changes from the car directly in AIMS.


For instance add in current estimated range / current charge % / outside temperature, and you can start to look at correlations between estimated range and temps.