This is a bash script for getting weight data from the Fitbit Aria scales and loading it into Domoticz.
1) Register a new Fitbit app here: https://dev.fitbit.com/apps/new, the redirect domain also known as the "Callback URL" does not need to be real, the OAuth 2.0 Application Type is: personal, select read only access.
2) Copy your fitbit apps "OAuth 2.0 Client ID" and put it into the URL below along with your "Callback URL", then visit it using Google Chrome
Code: Select all
https://www.fitbit.com/oauth2/authorize?client_id=ENTERYOURCLIENTIDHERE&redirect_uri=ENTERYOURREDIRECTURLHERE&response_type=code&scope=weight+activity+heartrate+location+nutrition+profile+settings+sleep+social
EXAMPLE of how it should look with the added "OAuth" and "Callback URL":
https://www.fitbit.com/oauth2/authorize?client_id=abc123&redirect_uri=https://domoticz.com&response_type=code&scope=weight+activity+heartrate+location+nutrition+profile+settings+sleep+social
3) Go back to your app https://dev.fitbit.com/apps and copy the client id, client secret and callback URL into the script Fitbit configuration section.
4) Create a new Virtual Sensor in Domoticz type "Scale (Weight)" and get its IDX code from the Domoticz devices page.
5) Enter the IDX number in the bash script, fill out the Domoticz configuration section and set the weight units in the script
To make this as simple as possible, you do not need to edit anything outside of the two lines that look like this:
# ----------------------------------------------------------------------------------
6) Install system requirements at the Linux terminal: sudo apt-get install jq bc screen
7) Run the script ./aria.sh
aria.sh
Code: Select all
#!/bin/bash
# ----------------------------------------------------------------------------------
# Fitbit Configuration obtained from your app at: https://dev.fitbit.com/apps
clientid="ENTERHERE" #OAuth 2.0 Client ID
clientsecret="ENTERHERE" #Client Secret
callbackurl="ENTERHERE" #Callback URL, eg: https://domoticz.com
# Fitbit Configuration obtained from the browser redirect
code="ENTERHERE"
# Fitbit Configuration weight units
units="Metric" #Options are: Metric, en_US, en_GB
# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="443"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"
domoticzweightsensoridx="ENTERHERE" # Virtual Sensor Type: Scale (Weight)
# Path to folder containing this script
cd /root/scripts/fitbit
# ----------------------------------------------------------------------------------
# Create fitbit basic auth token
basicauthtoken=$(echo -n "${clientid}:${clientsecret}" | openssl base64)
while true; do
# Get a Fitbit oauth2 token
refreshtoken=$(cat refreshtoken.txt 2> /dev/null)
# If we already have a stored token, get a new one.
if [ ! -f refreshtoken.txt ]; then
echo "Getting access token for the first time..."
oauth=$(curl --max-time 60 -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Basic ${basicauthtoken}" "https://api.fitbit.com/oauth2/token?client_id=${clientid}&grant_type=authorization_code&redirect_uri=${callbackurl}&code=${code}")
else
echo "Detected existing refresh token, getting a new access token..."
oauth=$(curl --max-time 60 -s -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Authorization: Basic ${basicauthtoken}" "https://api.fitbit.com/oauth2/token?client_id=${clientid}&grant_type=refresh_token&refresh_token=${refreshtoken}")
fi
# Put oauth2 token into easy to use variables and store the refresh token
accesstoken=$(echo "$oauth" | jq ".access_token" | sed 's/["]*//g')
newrefreshtoken=$(echo "$oauth" | jq ".refresh_token" | sed 's/["]*//g')
echo "$newrefreshtoken" > refreshtoken.txt
# Get weight data
date=$(date +"%Y-%m-%d")
getweight=$(curl --max-time 60 -s -X GET -H "Authorization: Bearer ${accesstoken}" -H "Accept-Language: ${units}" "https://api.fitbit.com/1/user/-/body/log/weight/date/${date}/1d.json")
# Extract data from the received json array
latestkey="-1"
for row in $(echo "${getweight}" | jq -c '.weight[]'); do
latestkey=$(echo $(("$latestkey" + 1))) # Since they are all in a single line, we simply count the number of results to work out the latest index key
done
weight=$(echo "$getweight" | jq ".weight[${latestkey}].weight")
# Display data in terminal
echo "Weight: $weight"
# Check to see if a new weight has been recorded since the previous check
logid=$(echo "$getweight" | jq ".weight[${latestkey}].logId")
storedlogid=$(cat storedlogid.txt 2> /dev/null)
if [ "$logid" == "$storedlogid" ]; then
echo "No new weight data"
else
echo "New weight detected"
echo "$logid" > storedlogid.txt
# Final integrity check, does weight equal more than 5?
if (( $(echo "$weight > 5" |bc -l) )); then
# Send data to Domoticz
echo "Sending data to Domoticz..."
# Domoticz Virtual Sensor Type: Scale (Weight)
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command¶m=udevice&idx={$domoticzweightsensoridx}&nvalue=0&svalue={$weight}"
else
echo "Problem occured during the final integrity check"
fi
fi
sleep 1800
done
An alternate version is under development which is able to detect when the Fitbit Aria joins the WiFi network after standing on the scales and get the latest data within seconds. This is useful if for example you want an audio announcement in your home after a new weight is recorded: eg "Congratulations you have lost x kilograms". It is expected to be made available in the next few days.
I use this bash script to start aria.sh and run it in the background using screen at startup:
startup.sh
Code: Select all
#!/bin/bash
/usr/bin/screen -S ariamonitor -d -m nice -17 ionice -c2 -n6 /path/to/aria.sh
1) If you delete refreshtoken.txt you must repeat step 2 and enter the new code into the bash script.
2) The send to Domoticz curl command is set to accept unsigned SSL certificates which may be a security risk.
3) You need to understand and edit as needed the above script to work in your own environment.