Skip to content

Commit

Permalink
python_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed Dec 9, 2021
1 parent c598662 commit 8ca1591
Show file tree
Hide file tree
Showing 45 changed files with 1,675 additions and 967 deletions.
23 changes: 2 additions & 21 deletions modules/wr2_kostalpiko/main.sh
@@ -1,24 +1,5 @@
#!/bin/bash

# Auslesen eines Kostal Piko WR über die integrierte API des WR. Rückgabewert ist die aktuelle Wattleistung.
if [[ "$speichermodul" != "none" ]]; then
pvwatttmp=$(curl --connect-timeout 5 -s $pv2ip/api/dxs.json?dxsEntries=33556736'&'dxsEntries=251658753)
else
pvwatttmp=$(curl --connect-timeout 5 -s $pv2ip/api/dxs.json?dxsEntries=67109120'&'dxsEntries=251658753)
fi
# aktuelle Ausgangsleistung am WR [W]
pvwatt=$(echo $pvwatttmp | jq '.dxsEntries[0].value' | sed 's/\..*$//')

if [ $pvwatt > 5 ]
then
pvwatt=$(echo "$pvwatt*-1" |bc)
fi
python3 /var/www/html/openWB/modules/wr_kostalpiko/kostal_piko_var1.py 2 $speichermodul $pv2ip
pvwatt=$(</var/www/html/openWB/ramdisk/pv2watt)
echo $pvwatt
# zur weiteren verwendung im webinterface
echo $pvwatt > /var/www/html/openWB/ramdisk/pv2watt
# Gesamtzählerstand am WR [kWh]
pvkwh=$(echo $pvwatttmp | jq '.dxsEntries[1].value' | sed 's/\..*$//')
echo $pvkwh > /var/www/html/openWB/ramdisk/pv2kwhk
pvkwh=$(echo "$pvkwh*1000" |bc)
# zur weiteren verwendung im webinterface
echo $pvkwh > /var/www/html/openWB/ramdisk/pv2kwh
42 changes: 3 additions & 39 deletions modules/wr2_kostalpikovar2/main.sh
@@ -1,41 +1,5 @@
#!/bin/bash
# initially created by Stefan Schefler for openWB 2019
# modified by Kevin Wieland
# based on Homematic Script v0.2 (c) 2018 by Alchy


# Daten einlesen
HTML=$(/usr/bin/curl -u $wr2_piko2_user:$wr2_piko2_pass --connect-timeout 10 -s -k $wr2_piko2_url | /usr/bin/tr -d '\r' | /usr/bin/tr -d '\n' | /usr/bin/tr -d ' ' | /usr/bin/tr '#' ' ')
# request html, concat to one line, remove spaces, add spaces before color changes (#)

if [[ -n $HTML ]] # check if valid content of request
then
counter=0
for LINE in $HTML # parse all html lines
do
if [[ $LINE =~ FFFFFF ]]; # search for white background color
then
((counter++))
PART2=${LINE##*F\">} # strip before number
VALUE=${PART2%%<*} # strip after number

if [[ $counter -eq 1 ]] # pvwatt
then
if [[ $VALUE = "xxx" ]] # off-value equals zero
then
$VALUE = "0"
fi
re='^[-+]?[0-9]+\.?[0-9]*$'
if ! [[ $VALUE =~ $re ]] # check for valid number
then
VALUE=$(</var/www/html/openWB/ramdisk/pv2watt)
fi
echo $((VALUE*-1)) > /var/www/html/openWB/ramdisk/pv2watt
echo $((VALUE*-1))
elif [[ $counter -eq 2 ]] # pvkwhk
then
echo ${VALUE} > /var/www/html/openWB/ramdisk/pv2kwhk
fi
fi
done
fi
python3 /var/www/html/openWB/modules/wr_kostalpikovar2/kostal_piko_var2.py 2 $wr2_piko2_url $wr2_piko2_user $wr2_piko2_pass
pvwatt=$(</var/www/html/openWB/ramdisk/pv2watt)
echo $pvwatt
87 changes: 87 additions & 0 deletions modules/wr2_kostalsteca/kostal_steca.py
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
from datetime import datetime, timezone
import os
import xml.etree.ElementTree as ET
import re
import requests
import subprocess
import sys

Debug = int(os.environ.get('debug'))
myPid = str(os.getpid())

pv2ip = str(sys.argv[1])
#
# RainerW 8th of April 2020
# Unfortunately Kostal has introduced the third version of interface: XML
# This script is for Kostal_Piko_MP_plus and StecaGrid coolcept (single phase inverter)
# In fact Kostal is not developing own single phase inverter anymore but is sourcing them from Steca
# If you have the chance to test this module for the latest three phase inverter from Kostal (Plenticore) or Steca (coolcept3 or coolcept XL) let us know if it works
# DetMoerk 20210323: Anpassung fuer ein- und dreiphasige WR der Serie. Anstatt eine feste Zeile aus dem Ergebnis zu schneiden wird nach der Zeile mit AC_Power gesucht.


def DebugLog(message):
local_time = datetime.now(timezone.utc).astimezone()
print(local_time.strftime(format="%Y-%m-%d %H:%M:%S") + ": PID: " + myPid + ": " + message)


if Debug >= 2:
DebugLog('PV Kostal Steca IP:' + pv2ip)

if Debug > 1:
measure = requests.get("http://"+pv2ip+"/measurements.xml", timeout=5).text
msg = "'MEASURE: "+str(measure)+"'"
subprocess.run(['bash', '-c', 'source /var/www/html/openWB/helperFunctions.sh; openwbDebugLog "PV" 2 '+msg])

# call for XML file and parse it for current PV power
response = requests.get("http://"+pv2ip+"/measurements.xml", timeout=5).text
tree = ET.fromstring(response)
root = tree.getroot()
for element in root.iter("Measurement"):
if element.get("Type") == "AC_Power":
power_kostal_piko_MP = element.get("Value")
break

# cut the comma and the digit behind the comma
power_kostal_piko_MP = int(power_kostal_piko_MP)

# allow only numbers
regex = '^-?[0-9]+$'
if re.search(regex, power_kostal_piko_MP) == None:
power_kostal_piko_MP = "0"

msg = "'PVWatt: "+str(power_kostal_piko_MP)+"'"
subprocess.run(['bash', '-c', 'source /var/www/html/openWB/helperFunctions.sh; openwbDebugLog "PV" 1 '+msg])

# call for XML file and parse it for total produced kwh
if Debug > 1:
yields = requests.get("http://"+pv2ip+"/yields.xml", timeout=5).text
msg = "'YIELD: "+yields+"'"
subprocess.run(['bash', '-c', 'source /var/www/html/openWB/helperFunctions.sh; openwbDebugLog "PV" 2 '+msg])

response = requests.get("http://"+pv2ip+"/yields.xml", timeout=5).text
tree = ET.fromstring(response)
root = tree.getroot()
for element in root.iter("YieldValue"):
pvkwh_kostal_piko_MP = element.get("Value")
break

if re.search(regex, pvkwh_kostal_piko_MP) == None:
subprocess.run(['bash', '-c', 'source /var/www/html/openWB/helperFunctions.sh; openwbDebugLog "PV" 2 "PVkWh: NaN get prev. Value"'])
with open("/var/www/html/openWB/ramdisk/pv2kwh", "r") as f:
pvkwh_kostal_piko_MP = f.read()

msg = "'PVkWh: "+str(pvkwh_kostal_piko_MP)+"'"
subprocess.run(['bash', '-c', 'source /var/www/html/openWB/helperFunctions.sh; openwbDebugLog "PV" 1 '+msg])

# Daten in Ramdisk schreiben
if Debug >= 1:
DebugLog('WR Energie: ' + str(pvkwh_kostal_piko_MP))
with open("/var/www/html/openWB/ramdisk/pv2kwh", "w") as f:
f.write(str(pvkwh_kostal_piko_MP))
if Debug >= 1:
DebugLog('WR Leistung: ' + "-"+str(power_kostal_piko_MP))
with open("/var/www/html/openWB/ramdisk/pv2watt", "w") as f:
f.write("-"+str(power_kostal_piko_MP))

exit(0)
56 changes: 18 additions & 38 deletions modules/wr2_kostalsteca/main.sh
@@ -1,47 +1,27 @@
#!/bin/bash
#
# RainerW 8th of April 2020
# Unfortunately Kostal has introduced the third version of interface: XML
# This script is for Kostal_Piko_MP_plus and StecaGrid coolcept (single phase inverter)
# In fact Kostal is not developing own single phase inverter anymore but is sourcing them from Steca
# If you have the chance to test this module for the latest three phase inverter from Kostal (Plenticore) or Steca (coolcept3 or coolcept XL) let us know if it works
# DetMoerk 20210323: Anpassung fuer ein- und dreiphasige WR der Serie. Anstatt eine feste Zeile aus dem Ergebnis zu schneiden wird nach der Zeile mit AC_Power gesucht.
DMOD="PV"
Debug=$debug

if (( $Debug > 1 )); then
measure=$(curl --connect-timeout 5 -s $pv2ip/measurements.xml)
openwbDebugLog ${DMOD} 2 "MEASURE: $measure"
fi
# call for XML file and parse it for current PV power
power_kostal_piko_MP=$(curl --connect-timeout 5 -s $pv2ip/measurements.xml |python3 -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())'|grep -e "Type=\"AC_Power\""| grep -Po "Value=\"\K[^\"]*" )
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
#MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="PV"
DMOD="MAIN"
Debug=$debug

# cut the comma and the digit behind the comma
power_kostal_piko_MP=$(echo $power_kostal_piko_MP | sed 's/\..*$//')
#For Development only
#Debug=1

# allow only numbers
re='^-?[0-9]+$'
if ! [[ $power_kostal_piko_MP =~ $re ]] ; then
power_kostal_piko_MP="0"
if [ $DMOD == "MAIN" ]; then
MYLOGFILE="${RAMDISKDIR}/openWB.log"
else
MYLOGFILE="${RAMDISKDIR}/wr2_kostalsteca.log"
fi
openwbDebugLog ${DMOD} 1 "PVWatt: $power_kostal_piko_MP"

# call for XML file and parse it for total produced kwh
if (( $Debug > 1 )); then
yield=$(curl --connect-timeout 5 -s $pv2ip/yields.xml)
openwbDebugLog ${DMOD} 2 "YIELD: $yield"
fi
#pvkwh_kostal_piko_MP=$(curl --connect-timeout 5 -s $pv2ip/yields.xml | grep -Po "Value=\"\K[^\"]*" | sed -n 1p)
pvkwh_kostal_piko_MP=$(curl --connect-timeout 5 -s $pv2ip/yields.xml |python3 -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())'|grep "YieldValue" | grep -Po "Value=\"\K[^\"]*" )
if ! [[ $pvkwh_kostal_piko_MP =~ $re ]] ; then
openwbDebugLog ${DMOD} 2 "PVkWh: NaN get prev. Value"
pvkwh_kostal_piko_MP=$(</var/www/html/openWB/ramdisk/pv2kwh)
fi
openwbDebugLog ${DMOD} 2 "PV IP: ${pv2ip}"

openwbDebugLog ${DMOD} 1 "PVkWh: $pvkwh_kostal_piko_MP"
python3 /var/www/html/openWB/modules/wr2_kostalsteca/kostal_steca.py "${pv2ip}" >>$MYLOGFILE 2>&1
ret=$?

## Daten in Ramdisk schreiben
openwbDebugLog ${DMOD} 2 "RET: ${ret}"

echo $pvkwh_kostal_piko_MP > /var/www/html/openWB/ramdisk/pv2kwh
echo '-'$power_kostal_piko_MP > /var/www/html/openWB/ramdisk/pv2watt
echo '-'$power_kostal_piko_MP
pvwatt=$(</var/www/html/openWB/ramdisk/pvwatt)
echo $pvwatt
52 changes: 52 additions & 0 deletions modules/wr_discovergy/discovergy.py
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

from datetime import datetime, timezone
import os
import requests
import sys
import traceback

Debug = int(os.environ.get('debug'))
myPid = str(os.getpid())

discovergyuser = str(sys.argv[1])
discovergypass = str(sys.argv[2])
discovergypvid = str(sys.argv[3])

def DebugLog(message):
local_time = datetime.now(timezone.utc).astimezone()
print(local_time.strftime(format = "%Y-%m-%d %H:%M:%S") + ": PID: "+ myPid +": " + message)


if Debug >= 2:
DebugLog('Wechselrichter Discovergy User: ' + discovergyuser)
DebugLog('Wechselrichter Discovergy Passwort: ' + discovergypass)
DebugLog('Wechselrichter Discovergy ID: ' + discovergypvid)

params = (
('meterId', discovergypvid),
)
output = requests.get('https://api.discovergy.com/public/v1/last_reading', params=params, auth=(discovergyuser, discovergypass), timeout=5).json()
try:
pvwh = output["values"]["energyOut"]
except:
traceback.print_exc()
exit(1)
pvwh = pvwh / 10000000
if Debug >= 1:
DebugLog('WR Energie: ' + str(pvwh))
with open("/var/www/html/openWB/ramdisk/pvkwh", "w") as f:
f.write(str(pvwh))

try:
watt = output["values"]["power"]
except:
traceback.print_exc()
exit(1)
watt = watt / 1000
if Debug >= 1:
DebugLog('WR Leistung: ' + str(watt))
with open("/var/www/html/openWB/ramdisk/pvwatt", "w") as f:
f.write(str(watt))

exit(0)
32 changes: 24 additions & 8 deletions modules/wr_discovergy/main.sh
@@ -1,12 +1,28 @@
#!/bin/bash
output=$(curl --connect-timeout 5 -s -u $discovergyuser:"$discovergypass" "https://api.discovergy.com/public/v1/last_reading?meterId=$discovergypvid")
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="EVU"
DMOD="MAIN"
Debug=$debug

pvwh=$(echo $output | jq .values.energyOut)
pvwh=$(( pvwh / 10000000 ))
echo $pvwh > /var/www/html/openWB/ramdisk/pvkwh
#For development only
#Debug=1

watt=$(echo $output | jq .values.power)
watt=$(( watt / 1000 ))
echo $watt > /var/www/html/openWB/ramdisk/pvwatt
if [ ${DMOD} == "MAIN" ]; then
MYLOGFILE="${RAMDISKDIR}/openWB.log"
else
MYLOGFILE="${RAMDISKDIR}/wr_discovergy.log"
fi

echo $watt
openwbDebugLog ${DMOD} 2 "WR User: ${discovergyuser}"
openwbDebugLog ${DMOD} 2 "WR Passwort: ${discovergypass}"
openwbDebugLog ${DMOD} 2 "WR ID: ${discovergypvid}"

python3 /var/www/html/openWB/modules/wr_discovergy/discovergy.py "${discovergyuser}" "${discovergypass}" "${discovergypvid}" >>$MYLOGFILE 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "RET: ${ret}"

pvwatt=$(</var/www/html/openWB/ramdisk/pvwatt)
echo $pvwatt
52 changes: 52 additions & 0 deletions modules/wr_fems/fems.py
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
from datetime import datetime, timezone
import os
import re
import requests
import sys
import traceback

Debug = int(os.environ.get('debug'))
myPid = str(os.getpid())

femskacopw = str(sys.argv[1])
femsip = str(sys.argv[2])


def DebugLog(message):
local_time = datetime.now(timezone.utc).astimezone()
print(local_time.strftime(format="%Y-%m-%d %H:%M:%S") + ": PID: " + myPid + ": " + message)


if Debug >= 2:
DebugLog('Wechselrichter FEMS Passwort: ' + femskacopw)
DebugLog('Wechselrichter FEMS IP: ' + femsip)

response = requests.get('http://x:'+femskacopw+'@'+femsip+':8084/rest/channel/_sum/ProductionActivePower').json()
try:
pvwatt = response["value"] * -1
except:
traceback.print_exc()
exit(1)

response = requests.get('http://x:'+femskacopw+'@'+femsip+':8084/rest/channel/_sum/ProductionActiveEnergy').json()
try:
pvwh = response["value"]
except:
traceback.print_exc()
exit(1)

regex = '^-?[0-9]+$'
if re.search(regex, pvwatt) == None:
pvwatt = "0"
if re.search(regex, pvwh) != None:
if Debug >= 1:
DebugLog('WR Energie: ' + str(pvwh))
with open("/var/www/html/openWB/ramdisk/pvkwh", "w") as f:
f.write(str(pvwh))
if Debug >= 1:
DebugLog('WR Leistung: ' + str(pvwatt))
with open("/var/www/html/openWB/ramdisk/pvwatt", "w") as f:
f.write(str(pvwatt))

exit(0)

0 comments on commit 8ca1591

Please sign in to comment.