Skip to content

Commit

Permalink
Update controller.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
christian1980nrw authored Oct 29, 2024
1 parent d98631f commit 62a4da5
Showing 1 changed file with 35 additions and 52 deletions.
87 changes: 35 additions & 52 deletions scripts/controller.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -x
#!/bin/bash

VERSION="2.4.13-DEV"
VERSION="2.4.14-DEV"

set -e

Expand All @@ -15,10 +15,10 @@ fi
#######################################

if [[ ${BASH_VERSINFO[0]} -le 4 ]]; then
valid_config_version=6 # Please increase this value by 1 when changing the configuration variables
valid_config_version=7 # Please increase this value by 1 when changing the configuration variables
else
declare -A valid_vars=(
["config_version"]="6" # Please increase this value by 1 if variables are added or deleted in the valid_vars array
["config_version"]="7" # Please increase this value by 1 if variables are added or deleted in the valid_vars array
["use_fritz_dect_sockets"]="0|1"
["fbox"]="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
["user"]="string"
Expand Down Expand Up @@ -63,7 +63,9 @@ else
["mqtt_broker_Port_subscribe"]="^[0-9]*$"
["mqtt_broker_topic_publish"]="string"
["mqtt_broker_topic_subscribe"]="string"
)
["reenable_inverting_at_fullbatt"]="0|1"
["reenable_inverting_at_soc"]="^([1-9][0-9]?|100)$"
)

declare -A config_values
fi
Expand Down Expand Up @@ -746,13 +748,11 @@ manage_charging() {
local reason=$2

if [[ $action == "on" ]]; then
charger_command_charge >/dev/null
$charger_command_charge >/dev/null
log_message "I: Charging is ON. $reason"

else
charger_command_stop_charging >/dev/null
$charger_command_stop_charging >/dev/null
log_message "I: Charging is OFF. $reason"

fi
}

Expand All @@ -762,13 +762,11 @@ manage_discharging() {
local reason=$2

if [[ $action == "on" ]]; then
charger_enable_inverter >/dev/null
$charger_enable_inverter >/dev/null
log_message "I: Discharging is ON. Battery SOC is at $SOC_percent%."

else
charger_disable_inverter >/dev/null
$charger_disable_inverter >/dev/null
log_message "I: Discharging is OFF. Battery SOC is at $SOC_percent%."

fi
}

Expand All @@ -795,10 +793,10 @@ manage_fritz_socket() {
local action=$1
local socket=$2

if [ "$1" != "off" ] && [ "$economic" == "expensive" ] && [ "$use_charger" != "0" ]; then
log_message "I: Disabling inverter while switching."
$charger_disable_inverter >/dev/null
fi
if [ "$1" != "off" ] && [ "$economic" == "expensive" ] && { [ "$use_charger" == "1" ]; }; then
log_message "I: Disabling inverter while switching."
$charger_disable_inverter >/dev/null
fi
local url="http://$fbox/webservices/homeautoswitch.lua?sid=$sid&ain=$socket&switchcmd=setswitch$action"
curl -s "$url" >/dev/null || log_message "E: Could not call URL '$url' to switch $action said switch - ignored."
}
Expand Down Expand Up @@ -842,7 +840,7 @@ manage_shelly_sockets() {
manage_shelly_socket() {
local action=$1
local ip=$2
if [ "$1" != "off" ] && [ "$economic" == "expensive" ] && [ "$use_charger" != "0" ]; then
if [ "$1" != "off" ] && [ "$economic" == "expensive" ] && { [ "$use_charger" == "1" ]; }; then
log_message "I: Disabling inverter while switching."
$charger_disable_inverter >/dev/null
fi
Expand Down Expand Up @@ -912,10 +910,10 @@ log_message() {

exit_with_cleanup() {
log_message "I: Cleanup and exit with error $1"
if ((use_charger != 0)); then
if ((use_charger == 1)); then
manage_charging "off" "Turn off charging."
fi
if ((execute_discharging == 0 && use_charger != 0)); then
if ((execute_discharging == 0 && (use_charger == 1))); then
manage_discharging "on" "Spotmarket-Switcher is disabling itself. Maybe there is no internet connection."
fi
manage_fritz_sockets "off"
Expand Down Expand Up @@ -970,32 +968,16 @@ fi
if [ -f "$DIR/$CONFIG" ]; then
source "$DIR/$CONFIG"

# Victron Charging
num_tools_missing=0
SOC_percent=-1 # Set to negative -1 first (maybe no charger is activated).
tools="awk cat sed sort head tail"
tools="awk curl cat sed sort head tail"
if [ "$use_charger" == "1" ]; then
tools="$tools dbus"
charger_command_charge() {
"dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Day SetValue -- 7"
}
charger_command_stop_charging() {
"dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Day SetValue -- -7"
}
charger_command_set_SOC_target() {
"dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Soc SetValue --"
}
charger_disable_inverter() {
"dbus -y com.victronenergy.settings /Settings/CGwacs/MaxDischargePower SetValue -- 0"
}
charger_enable_inverter() {
"dbus -y com.victronenergy.settings /Settings/CGwacs/MaxDischargePower SetValue -- $limit_inverter_power_after_enabling"
}
charger_command_charge
charger_command_stop_charging
charger_command_set_SOC_target
charger_disable_inverter
charger_enable_inverter
charger_command_charge="dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Day SetValue -- 7"
charger_command_stop_charging="dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Day SetValue -- -7"
charger_command_set_SOC_target="dbus -y com.victronenergy.settings /Settings/CGwacs/BatteryLife/Schedule/Charge/0/Soc SetValue --"
charger_disable_inverter="dbus -y com.victronenergy.settings /Settings/CGwacs/MaxDischargePower SetValue -- 0"
charger_enable_inverter="dbus -y com.victronenergy.settings /Settings/CGwacs/MaxDischargePower SetValue -- $limit_inverter_power_after_enabling"
SOC_percent="$(dbus-send --system --print-reply --dest=com.victronenergy.system /Dc/Battery/Soc com.victronenergy.BusItem.GetValue | grep variant | awk '{print int($3)}' | tr -d '[:space:]')"
if ! [[ "$SOC_percent" =~ ^[0-9]+$ ]]; then
log_message 'E: SOC cannot be read properly. Value is not an integer.'
Expand All @@ -1007,11 +989,11 @@ if [ "$use_charger" == "1" ]; then

fi
# MQTT Charging
if [ "$use_charger" == "2" ]; then
num_tools_missing=0
SOC_percent=-1 # Set to negative -1 first (maybe no charger is activated).
tools="curl mosquitto_sub "
if [ "$use_charger" == "2" ]; then
tools="$tools mosquitto_pub"
tools="$tools mosquitto_pub"

charger_command_charge() {
mosquitto_pub -h $mqtt_broker_host_publish -p $mqtt_broker_port_publish -t "$mqtt_broker_topic_publish/charger_command" -m true
}
Expand Down Expand Up @@ -1549,10 +1531,11 @@ if ((use_solarweather_api_to_abort == 1)); then
fi

fi

if ((SOC_percent >= 90)); then
if ((reenable_inverting_at_fullbatt == 1)); then
if (( SOC_percent >= reenable_inverting_at_soc )); then
log_message "I: The battery is getting full. Re-enabling inverter. This is important on a DC-AC system to enable grid-feedin."
execute_discharging=1
fi
fi

if ((abort_price_integer <= current_price_integer)); then
Expand All @@ -1568,7 +1551,7 @@ percent_of_current_price_integer=$(awk "BEGIN {printf \"%.0f\", $current_price_i
total_cost_integer=$((current_price_integer + percent_of_current_price_integer + battery_lifecycle_costs_cent_per_kwh_integer))

# If any charging condition is met, start charging
if ((execute_charging == 1 && use_charger != 0)); then
if ((execute_charging == 1 && use_charger == 1)); then
economic=""
# Evaluate if charging is economical
percent_of_current_price_integer=$(awk "BEGIN {printf \"%.0f\", $current_price_integer*$energy_loss_percent/100}")
Expand All @@ -1585,17 +1568,17 @@ if ((execute_charging == 1 && use_charger != 0)); then
economic="expensive"
manage_charging "off" "$reason_msg Total charging costs: $(millicentToEuro "$total_cost_integer")"
fi
elif ((execute_charging != 1 && use_charger != 0)); then
elif ((execute_charging != 1 && use_charger == 1)); then
manage_charging "off" "Charging was not executed. Total charging costs: $(millicentToEuro "$total_cost_integer")"
else
log_message "D: skip Charger. not activated "
log_message "D: Skip charger. Not activated. "
fi


if ((execute_discharging == 1 && use_charger != 0)); then
if ((execute_discharging == 1 && use_charger == 1)); then
manage_discharging "on" "$reason_msg Total charging costs: $(millicentToEuro "$total_cost_integer")"
fi
if ((execute_discharging == 0 && use_charger != 0)); then
if ((execute_discharging == 0 && use_charger == 1)); then
manage_discharging "off" "$reason_msg Total charging costs: $(millicentToEuro "$total_cost_integer")"
fi

Expand Down

0 comments on commit 62a4da5

Please sign in to comment.