#!/bin/bash P1_STATUS=`mosquitto_sub -t domoctopus/p1/status -C 1` if [ "$P1_STATUS" != "online" ]; then # no info from P1 # disable setpoint control echo "disable setpoint and exit" mbpoll 192.168.1.134 -a 3 -B -0 -r 40151 -t 4:int 803 exit 1 fi P1_INFO=`mosquitto_sub -t home/meterkast/P1HA -C 1` P1_POWER_IN=`echo $P1_INFO | jq -r '.elec_power_in'` P1_POWER_OUT=`echo $P1_INFO | jq -r '.elec_power_out'` echo "P1 power in" $P1_POWER_IN echo "P1 power out" $P1_POWER_OUT SI_POWER_IN=`mbpoll 192.168.1.134 -a 3 -B -0 -r 30865 -q -1 -c 1 -t 3:int | grep -e 30865 | cut -f 2` SI_POWER_OUT=`mbpoll 192.168.1.134 -a 3 -B -0 -r 30867 -q -1 -c 1 -t 3:int | grep -e 30867 | cut -f 2` echo "SI power in" $SI_POWER_IN echo "SI power out" $SI_POWER_OUT BAT_PERC=`mbpoll 192.168.1.134 -a 3 -B -0 -r 30845 -q -1 -c 1 -t 3:int | grep -e 30845 | cut -f 2` echo "BAT_PERC" $BAT_PERC # negative means we are exporting power to the grid # positive means we are taking power from the grid P1_POWER=$(($P1_POWER_IN - $P1_POWER_OUT)) # positive means we are discharging the batteries # negative means we are charging the batteries SI_POWER=$(($SI_POWER_OUT - $SI_POWER_IN)) echo "P1 power" $P1_POWER echo "SI power" $SI_POWER SI_POWER_REQ=$(($SI_POWER + $P1_POWER)) echo "SI power req" $SI_POWER_REQ LOWER_LIMIT=-2200 if [ "$SI_POWER_REQ" -lt "$LOWER_LIMIT" ]; then SI_POWER_REQ=$LOWER_LIMIT fi echo "SI power req (must be at least $LOWER_LIMIT)" $SI_POWER_REQ if [ "$SI_POWER_REQ" -gt "0" ]; then SI_POWER_REQ=0 fi echo "SI power req (must not be more than 0)" $SI_POWER_REQ mbpoll 192.168.1.134 -a 3 -B -0 -r 40149 -q -t 4:int -- $SI_POWER_REQ mbpoll 192.168.1.134 -a 3 -B -0 -r 40151 -q -t 4:int 802