Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Log ZCell contactor state
#!/bin/bash
#
# Polls the ZCell BMS REST API once per second and prints the
# state of the Charge, Discharge and Energy Extrator (Strip)
# contactors if they've changed since the last poll. Will
# run forever, or until all three contactors are open at the
# same time, whichever comes first.
# Requires `curl` and `jq`.
#
# Note: this only looks at the first battery in the system
# (".list[0]" in the `jq` invocation).
#
if [ -z "$1" ]; then
echo "Usage: $(basename $0) BMS_IP [POLL_INTERVAL]"
exit 1
fi
STATUS_URL="http://${1}:3000/rest/1.0/status"
INTERVAL="${2:-1}"
LAST=""
echo "Polling ${STATUS_URL} at ${INTERVAL} second intervals"
while true ; do
CONTACTORS="$(curl -s ${STATUS_URL} | jq -r '[(if .list[0].charge_contactor_state then "C" else "_" end), (if .list[0].discharge_contactor_state then "D" else "_" end), (if .list[0].strip_contactor_state then "E" else "_" end)] | join(" ")')"
if [ "$CONTACTORS" != "$LAST" ] ; then
echo -e "$(date --iso-8601=seconds)\t${CONTACTORS}"
LAST="${CONTACTORS}"
fi
if [ "$CONTACTORS" = "_ _ _" ] ; then
echo "All contactors open, exiting"
break
fi
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment