Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Last active December 18, 2020 14:48
Show Gist options
  • Save mschmitt/ae37f81a38e447e931a04d6e99c00ac8 to your computer and use it in GitHub Desktop.
Save mschmitt/ae37f81a38e447e931a04d6e99c00ac8 to your computer and use it in GitHub Desktop.
Zählerstand der go-e Wallbox auslesen
#!/bin/bash
url="http://go-echarger/status"
#url="https://api.go-e.co/api_status?token=abcdef0123"
if [[ "${url}" =~ api.go-e.co ]]
then
dataobject='.data'
else
dataobject='.'
fi
if ! json="$(curl -s "${url}" | jq "${dataobject}")"
then
printf "Unable to connect to: %s\n" "${url}"
fi
time="$(jq --raw-output .tme <<<"${json}")"
printf "Datum/Uhrzeit: %s.%s.%s %s:%s\n" \
"${time:0:2}" "${time:2:2}" "${time:4:2}" "${time:6:2}" "${time:8:2}"
serial="$(jq --raw-output .sse <<<"${json}")"
printf "Seriennummer: %s\n\n" "${serial}"
key_card_id=('rca' 'rcr' 'rcd' 'rc4' 'rc5' 'rc6' 'rc7' 'rc8' 'rc9' 'rc1')
key_card_name=('rna' 'rnm' 'rne' 'rn4' 'rn5' 'rn6' 'rn7' 'rn8' 'rn9' 'rn1')
key_card_energy=('eca' 'ecr' 'ecd' 'ec4' 'ec5' 'ec6' 'ec7' 'ec8' 'ec9' 'ec1')
for (( card=0; card<=9; card++))
do
# Is a card registered to this slot?
this_id="$(jq --raw-output ."${key_card_id["${card}"]}" <<<"${json}")"
if [[ -z "${this_id}" ]]
then
continue
fi
this_energy_raw="$(jq --raw-output ."${key_card_energy["${card}"]}" <<<"${json}")"
this_energy_kwh="$(( ${this_energy_raw} / 10 ))"
this_name="$(jq --raw-output ."${key_card_name["${card}"]}" <<<"${json}")"
printf "Zählerstand für Karte %s, ID %s (%s): %s kWh\n" "$(( ${card} + 1 ))" "${this_id}" "${this_name}" "${this_energy_kwh}"
done
energy_total_raw="$(jq --raw-output .eto <<<"${json}")"
energy_total_kwh="$(( ${energy_total_raw} / 10 ))"
printf "\nZählerstand insgesamt: %s kWh\n" "${energy_total_kwh}"
@lix-src
Copy link

lix-src commented Dec 18, 2020

Hier die exakte Fehlermeldung:

./go-e-zaehlerstand: line 37: "0" / 10 : syntax error: operand expected (error token is ""0" / 10 ")
./go-e-zaehlerstand: line 40: "0" / 10 : syntax error: operand expected (error token is ""0" / 10 ")

@mschmitt
Copy link
Author

mschmitt commented Dec 18, 2020

Die 0 steht als "0" in der Variable. Das sieht wie ein Problem mit jq --raw-output aus und da wird es auch nicht helfen, die überflüssigen Quotes aus dem Script zu nehmen oder den Zähler über die 0 zu heben. Der Murks fängt strenggenommen schon da an, wo der Zählerstand als String aus dem API kommt.

Ich bin jetzt auch nicht der große jq-Guru. Was für eine Version von jq hast du da? Bekommst du hier die 0 mit Quotes oder ohne?

echo '{"foo": "0"}' | jq --raw-output .foo

P.S.: Hab noch jemanden mit dem selben Verhalten. MacOS+brew?

@mschmitt
Copy link
Author

mschmitt commented Dec 18, 2020

af919ccb6253f355afe07848cf65a71e4748e7cf Removed quotes in arithmetic expansion for MacOS bash 3 compatibility

Natürlich war das sinnlose Quoting schuld, was sonst.

@lix-src
Copy link

lix-src commented Dec 18, 2020

Knuts-MacBook-Pro:~ knut$ echo '{"foo": "0"}' | jq --raw-output .foo
0
Knuts-MacBook-Pro:~ knut$

Nun ist nur noch der Fehler in Zeile 39.

./go-e-zaehlerstand: line 39: "0" + 1 : syntax error: operand expected (error token is ""0" + 1 ")

@mschmitt
Copy link
Author

Fixed. Was ein Murks mit Apple hey. 🤐

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