Skip to content

Commit

Permalink
Fix script issues at internet connection problems
Browse files Browse the repository at this point in the history
  • Loading branch information
christian1980nrw authored Jan 7, 2024
1 parent 434438e commit 38375bf
Showing 1 changed file with 58 additions and 28 deletions.
86 changes: 58 additions & 28 deletions scripts/controller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,15 @@ download_solarenergy() {
log_message "E: Could not get solarenergy data, missing file '$file3'. Solarenergy will be ignored."
fi

if grep -q "API" "$file3"; then
log_message "E: Error, there is a problem with the Solarweather-API."
if [ -f "$file3" ]; then
if grep -q "API" "$file3"; then
log_message "E: Error, there is a problem with the Solarweather-API."
cat "$file3"
return 1
fi
echo
rm "$file3"
fi
fi


if [ -n "$DEBUG" ]; then
log_message "D: File3 $file3 downloaded" >&2
Expand Down Expand Up @@ -579,34 +583,43 @@ get_prices_integer_entsoe() {
}

get_solarenergy_today() {
solarenergy_today=$(sed '2!d' $file3 | cut -d',' -f2)
if [ ! -s "$file3" ]; then return; fi
solarenergy_today=$(sed '2!d' "$file3" | cut -d',' -f2)
solarenergy_today_integer=$(euroToMillicent "${solarenergy_today}" 15)
abort_solar_yield_today_integer=$(euroToMillicent "${abort_solar_yield_today}" 15)
}

get_solarenergy_tomorrow() {
solarenergy_tomorrow=$(sed '3!d' $file3 | cut -d',' -f2)
if [ ! -s "$file3" ]; then return; fi
solarenergy_tomorrow=$(sed '3!d' "$file3" | cut -d',' -f2)
solarenergy_tomorrow_integer=$(euroToMillicent "$solarenergy_tomorrow" 15)
abort_solar_yield_tomorrow_integer=$(euroToMillicent "${abort_solar_yield_tomorrow}" 15)
}

get_cloudcover_today() {
cloudcover_today=$(sed '2!d' $file3 | cut -d',' -f1)
if [ ! -s "$file3" ]; then return; fi
cloudcover_today=$(sed '2!d' "$file3" | cut -d',' -f1)
}

get_cloudcover_tomorrow() {
cloudcover_tomorrow=$(sed '3!d' $file3 | cut -d',' -f1)
if [ ! -s "$file3" ]; then return; fi
cloudcover_tomorrow=$(sed '3!d' "$file3" | cut -d',' -f1)
}

get_sunrise_today() {
sunrise_today=$(sed '2!d' $file3 | cut -d',' -f3 | cut -d 'T' -f2 | awk -F: '{ print $1 ":" $2 }')
if [ ! -s "$file3" ]; then return; fi
sunrise_today=$(sed '2!d' "$file3" | cut -d',' -f3 | cut -d 'T' -f2 | awk -F: '{ print $1 ":" $2 }')
}

get_sunset_today() {
sunset_today=$(sed '2!d' $file3 | cut -d',' -f4 | cut -d 'T' -f2 | awk -F: '{ print $1 ":" $2 }')
if [ ! -s "$file3" ]; then return; fi
sunset_today=$(sed '2!d' "$file3" | cut -d',' -f4 | cut -d 'T' -f2 | awk -F: '{ print $1 ":" $2 }')
}

get_suntime_today() {
if [ ! -s "$file3" ]; then return; fi
get_sunrise_today
get_sunset_today
suntime_today=$((($(TZ=$TZ date -d "1970-01-01 $sunset_today" +%s) - $(TZ=$TZ date -d "1970-01-01 $sunrise_today" +%s)) / 60))
}

Expand Down Expand Up @@ -699,10 +712,10 @@ manage_charging() {

if [[ $action == "on" ]]; then
$charger_command_charge >/dev/null
log_message "I: Victron scheduled charging is ON. Battery SOC is at $SOC_percent %. $reason"
log_message "I: Victron scheduled charging is ON. Battery SOC is at $SOC_percent%. $reason"
else
$charger_command_stop_charging >/dev/null
log_message "I: Victron scheduled charging is OFF. Battery SOC is at $SOC_percent %. $reason"
log_message "I: Victron scheduled charging is OFF. Battery SOC is at $SOC_percent%. $reason"
fi
}

Expand All @@ -713,10 +726,10 @@ manage_discharging() {

if [[ $action == "on" ]]; then
$charger_enable_inverter >/dev/null
log_message "I: Victron discharging (ESS) is ON. Battery SOC is at $SOC_percent %. $reason"
log_message "I: Victron discharging (ESS) is ON. Battery SOC is at $SOC_percent%. $reason"
else
$charger_disable_inverter >/dev/null
log_message "I: Victron discharging (ESS) is OFF. Battery SOC is at $SOC_percent %. $reason"
log_message "I: Victron discharging (ESS) is OFF. Battery SOC is at $SOC_percent%. $reason"
fi
}

Expand Down Expand Up @@ -1291,21 +1304,29 @@ fi


if ((use_solarweather_api_to_abort == 1)); then
log_message "I: Sunrise today will be $sunrise_today and sunset will be $sunset_today. Suntime will be $suntime_today minutes."
log_message "I: Solarenergy today will be $solarenergy_today megajoule per sqaremeter with $cloudcover_today percent clouds."
log_message "I: Solarenergy tomorrow will be $solarenergy_tomorrow megajoule per squaremeter with $cloudcover_tomorrow percent clouds."
if ((SOC_percent != -1)); then
target_soc=$(get_target_soc "$solarenergy_today")
log_message "I: At $solarenergy_today megajoule there will be a dynamic SOC charge-target of $target_soc % calculated. The rest is reserved for solar."
eval "$charger_command_set_SOC_target $target_soc" >/dev/null
fi
if [ ! -s $file3 ]; then
log_message "E: File '$file3' is empty, please check your API Key if download is still not possible tomorrow."
if [ -f "$file3" ] && [ -s "$file3" ]; then
log_message "I: Sunrise today will be $sunrise_today and sunset will be $sunset_today. Suntime will be $suntime_today minutes."
log_message "I: Solarenergy today will be $solarenergy_today megajoule per sqaremeter with $cloudcover_today percent clouds."
log_message "I: Solarenergy tomorrow will be $solarenergy_tomorrow megajoule per squaremeter with $cloudcover_tomorrow percent clouds."

if ((SOC_percent != -1)); then
target_soc=$(get_target_soc "$solarenergy_today")
log_message "I: At $solarenergy_today megajoule there will be a dynamic SOC charge-target of $target_soc% calculated. The rest is reserved for solar."
eval "$charger_command_set_SOC_target $target_soc" >/dev/null
fi
else
log_message "E: No solar data. Please check your internet connection and API Key or wait if it is a temporary error."
if ((SOC_percent != -1)); then
target_soc=$(get_target_soc "$solarenergy_today")
log_message "E: A SOC charge-target of $target_soc% will be used without valid solarweather-data."
eval "$charger_command_set_SOC_target $target_soc" >/dev/null
fi
fi
find "$file3" -size 0 -delete # FIXME - looks wrong and complicated - simple RM included in prior if clause?
else
log_message "D: skip Solarweather. not activated"
fi


charging_condition_met=""
discharging_condition_met=""
switchablesockets_condition_met=""
Expand Down Expand Up @@ -1391,12 +1412,21 @@ log_message "D: After evaluating discharging conditions - execute_discharging: $
log_message "D: After evaluating switchable sockets conditions - execute_switchablesockets_on: $execute_switchablesockets_on "
fi

if ((use_solarweather_api_to_abort == 1)); then
check_abort_condition $((abort_suntime <= suntime_today)) "There are enough sun minutes today. No need to charge or swtich."
check_abort_conditions() {
if [ ! -s "$file3" ]; then
log_message "E: File '$file3' does not exist or is empty."
return
fi
check_abort_condition $((abort_suntime <= suntime_today)) "There are enough sun minutes today. No need to charge or switch."
check_abort_condition $((abort_solar_yield_today_integer <= solarenergy_today_integer)) "There is enough solarenergy today. No need to charge or switch."
check_abort_condition $((abort_solar_yield_tomorrow_integer <= solarenergy_tomorrow_integer)) "There is enough sun tomorrow. No need to charge or switch."
check_abort_condition $((abort_solar_yield_tomorrow_integer <= solarenergy_tomorrow_integer)) "There is enough solarenergy tomorrow. No need to charge or switch."
}

if ((use_solarweather_api_to_abort == 1)); then
check_abort_conditions
fi


check_abort_condition $((abort_price_integer <= current_price_integer)) "Current price ($(millicentToEuro "$current_price_integer")€) is too high. Abort. ($(millicentToEuro "$abort_price_integer")€)"

# If any charging condition is met, start charging
Expand Down

0 comments on commit 38375bf

Please sign in to comment.