inverter_tasmota_arbeitszimmer.sh #!/bin/bash #set Inverter/Tasmota IPs IPINVERTER="192.168.178.55" IPTASMOTA="192.168.178.61" #get timestamp YYYY-MM-DD HH24:MI TIMESTAMP=`date +'%Y-%m-%d %H:%M'` #set start/stop values for month XY MONTH=`date +'%m'` if [[ ${MONTH} == "09" ]] then START_VALUE="-1800"; STOP_VALUE="0"; elif [[ ${MONTH} == "10" ]] then START_VALUE="-1600"; STOP_VALUE="50"; elif [[ ${MONTH} == "11" ]] then START_VALUE="-1400"; STOP_VALUE="100"; elif [[ ${MONTH} == "12" ]] then START_VALUE="-1200"; STOP_VALUE="150"; else START_VALUE="-2500"; STOP_VALUE="-500"; fi #set & create log file with header if not exists LOG_FILE=/home/pi/inverter_tasmota_arbeitszimmer.log if [[ ! -f ${LOG_FILE} ]] then echo "Timestamp;IP Inverter;PowerReal_P_Sum;IP Tasmota;Tasmota Power State;Start Value;Stop Value;Message;Loop T;Loop I" > ${LOG_FILE}; fi #get PowerValue from Inverter API & format output, loop if tasmota is unreachable LOOP_I=0 PowerReal_P_Sum=`curl -s http://${IPINVERTER}/solar_api/v1/GetMeterRealtimeData.cgi |grep -i "PowerReal_P_Sum" |tr -s " " |cut -d ":" -f2 |sed 's/,//g' |sed 's/^ //g'| cut -d $(locale decimal_point) -f1` while [[ -z ${PowerReal_P_Sum} ]] do if [[ ${LOOP_I} -eq 3 ]] then MESSAGE="No Connection to Inverter when getting PowerReal_P_Sum"; echo "${TIMESTAMP};${IPINVERTER};${PowerReal_P_Sum};${IPTASMOTA};${TASMOTA_POWER_STATE};${START_VALUE};${STOP_VALUE};${MESSAGE};${LOOP_T};${LOOP_I}" >> ${LOG_FILE}; exit 1; fi let LOOP_I++; sleep 10; PowerReal_P_Sum=`curl -s http://${IPINVERTER}/solar_api/v1/GetMeterRealtimeData.cgi |grep -i "PowerReal_P_Sum" |tr -s " " |cut -d ":" -f2 |sed 's/,//g' |sed 's/^ //g'| cut -d $(locale decimal_point) -f1`; done #get tasmota state & format output, loop if tasmota is unreachable LOOP_T=0 TASMOTA_POWER_STATE=`curl -s http://${IPTASMOTA}/cm?cmnd=Power | sed 's/{//g' | sed 's/}//g' |sed 's/"//g' |cut -d ":" -f2` while [[ -z ${TASMOTA_POWER_STATE} ]] do if [[ ${LOOP_T} -eq 3 ]] then MESSAGE="No Connection to Tasmota when getting Power State"; echo "${TIMESTAMP};${IPINVERTER};${PowerReal_P_Sum};${IPTASMOTA};${TASMOTA_POWER_STATE};${START_VALUE};${STOP_VALUE};${MESSAGE};${LOOP_T};${LOOP_I}" >> ${LOG_FILE}; exit 1; fi let LOOP_T++; sleep 10; TASMOTA_POWER_STATE=`curl -s http://${IPTASMOTA}/cm?cmnd=Power | sed 's/{//g' | sed 's/}//g' |sed 's/"//g' |cut -d ":" -f2`; done #set new tasmota state if [[ ${TASMOTA_POWER_STATE} = "OFF" ]] && [[ ${PowerReal_P_Sum} -lt ${START_VALUE} ]] then STATE="ON"; elif [[ ${TASMOTA_POWER_STATE} = "ON" ]] && [[ ${PowerReal_P_Sum} -gt ${STOP_VALUE} ]] then STATE="OFF"; else STATE="LEAVE"; fi #turn tasmota on/off or let it in actual state, loop if tasmota is unreachable if [[ ${STATE} == "ON" ]] || [[ ${STATE} == "OFF" ]] then MESSAGE="Tasmota, turn it ${STATE}"; LOOP_T=0; TASMOTA_SWITCH=`curl -s "http://${IPTASMOTA}/cm?cmnd=Power%20${STATE}"`; while [[ -z ${TASMOTA_SWITCH} ]] do if [[ ${LOOP_T} -eq 3 ]] then MESSAGE="No Connection to Tasmota when setting Power State ${STATE}"; echo "${TIMESTAMP};${IPINVERTER};${PowerReal_P_Sum};${IPTASMOTA};${TASMOTA_POWER_STATE};${START_VALUE};${STOP_VALUE};${MESSAGE};${LOOP_T};${LOOP_I}" >> ${LOG_FILE}; exit 1; fi let LOOP_T++; sleep 10; TASMOTA_SWITCH=`curl -s "http://${IPTASMOTA}/cm?cmnd=Power%20${STATE}"`; done elif [[ $STATE == "LEAVE" ]] then MESSAGE="Tasmota, don't change Power State (${STATE})"; else MESSAGE="unknown state: ${STATE}"; fi #write log echo "${TIMESTAMP};${IPINVERTER};${PowerReal_P_Sum};${IPTASMOTA};${TASMOTA_POWER_STATE};${START_VALUE};${STOP_VALUE};${MESSAGE};${LOOP_T};${LOOP_I}" >> ${LOG_FILE} exit 0