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/{id}/wake_up' \
--header 'Authorization: Bearer {bearer_token}'`

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/{id}/data_request/charge_state' \
--header 'Authorization: Bearer {bearer_token}'`

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/{id}/data_request/charge_state' \
--header 'Authorization: Bearer {bearer_token}'`
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/{environment_id}/statpoints/' \
--header 'Content-Type: application/json' \
--header 'X-System: 10' \
--header 'Authorization: Basic {token}' \
--data-raw '[
    {
        "nodeRef": {
            "nodeType": "aims.int-sys.server",
            "parts": {
                "part1": "part1value"
            }
        },
        "statType": "aims.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.