Skip to content

Instantly share code, notes, and snippets.

@maccyber
Last active February 4, 2020 22:12
Show Gist options
  • Save maccyber/d3d69337e3d074d5be710ef14240d657 to your computer and use it in GitHub Desktop.
Save maccyber/d3d69337e3d074d5be710ef14240d657 to your computer and use it in GitHub Desktop.
#!/bin/bash
FILE="/tmp/leaf.cache"
USERNAME=myleaf@leaf.com
PASSWORD=mypassword
update_status() {
result=`leaf-connect-cli cachedStatus -u $USERNAME -p $PASSWORD -r NE`
BATTERY_STATUS=`echo ${result} | jq '.BatteryStatusRecords.BatteryStatus.SOC.Value' | sed 's/"//g'`
CHARGING_STATUS=`echo ${result} | jq '.BatteryStatusRecords.BatteryStatus.BatteryChargingStatus' | sed 's/"//g'`
echo "$BATTERY_STATUS" > $FILE
echo "$CHARGING_STATUS" >> $FILE
}
display_status() {
if [ ! -f "$FILE" ]; then
update_status
fi
FILE_TIMESTAMP=$(stat -c %Y $FILE)
NOW_TIMESTAMP=$(date +"%s")
DIFF="$(($NOW_TIMESTAMP-$FILE_TIMESTAMP))"
if [ $DIFF -ge "600" ]; then
update_status
fi
status_text=$(head -n 1 $FILE)
charging=$(tail -n 1 $FILE)
icon=
if [ -z "$status_text" ]; then
color=#36a8d5
status_text="retrieving info"
elif [ $status_text -ge "70" ]; then
color=#88b090
status_text="${status_text}%"
elif [ $status_text -ge "30" ]; then
color=#ccdc90
status_text="${status_text}%"
elif [ $status_text -lt "30" ]; then
color=#e89393
status_text="${status_text}%"
else
color=#36a8d5
status_text="retrieving info"
fi
if [ $charging != "NOT_CHARGING" ]; then
color=#88b090
icon+=
fi
echo '[{"name": "leaf", "instance": "battery", "full_text": " '${icon}' ' ${status_text} '", "color": "'${color}'"}]'
}
while sleep 10; do
display_status
done
@maccyber
Copy link
Author

maccyber commented Feb 3, 2020

Display battery and charging status on Nissan Leaf with i3bar and i3cat
leaf

npm install -g leaf-connect-cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment