Skip to content

Commit

Permalink
python modules for counters (snaptec#1519)
Browse files Browse the repository at this point in the history
* python module for powerwall

* python modules for fronius_sm, kostal_piko and _plenticore, go-e

* python scripts for http, json, lg, powerfox, smartfox
  • Loading branch information
LKuemmel authored Sep 27, 2021
1 parent 6eb1a40 commit 849b845
Show file tree
Hide file tree
Showing 20 changed files with 925 additions and 397 deletions.
2 changes: 1 addition & 1 deletion modules/bezug_fronius_sm/fronius_sm.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,4 @@ def get_int_value(response, key):
DebugLog(1, 'Strom L3: ' + str(bezuga3))
DebugLog(1, 'Frequenz: ' + str(evuhz))

exit(0)
exit(0)
2 changes: 0 additions & 2 deletions modules/bezug_fronius_sm/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ openwbDebugLog ${DMOD} 2 "RET: ${ret}"

wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug


45 changes: 19 additions & 26 deletions modules/bezug_http/main.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
#!/bin/bash
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="EVU"
DMOD="MAIN"
Debug=$debug

re='^-?[0-9]+$'
#For development only
#Debug=1

wattbezug=$(curl --connect-timeout 10 -s $bezug_http_w_url)
if ! [[ $wattbezug =~ $re ]] ; then
wattbezug="0"
if [ $DMOD == "MAIN" ]; then
MYLOGFILE="$RAMDISKDIR/openWB.log"
else
MYLOGFILE="$RAMDISKDIR/evu_json.log"
fi
echo $wattbezug
echo $wattbezug > /var/www/html/openWB/ramdisk/wattbezug

if [[ $bezug_http_ikwh_url != "none" ]]; then
ikwh=$(curl --connect-timeout 5 -s $bezug_http_ikwh_url)
echo $ikwh > /var/www/html/openWB/ramdisk/bezugkwh
fi
if [[ $bezug_http_ekwh_url != "none" ]]; then
ekwh=$(curl --connect-timeout 5 -s $bezug_http_ekwh_url)
echo $ekwh > /var/www/html/openWB/ramdisk/einspeisungkwh
fi
if [[ $bezug_http_l1_url != "none" ]]; then
l1a=$(curl --connect-timeout 5 -s $bezug_http_l1_url)
echo $l1a > /var/www/html/openWB/ramdisk/bezuga1
fi
if [[ $bezug_http_l2_url != "none" ]]; then
l2a=$(curl --connect-timeout 5 -s $bezug_http_l2_url)
echo $l2a > /var/www/html/openWB/ramdisk/bezuga2
fi
if [[ $bezug_http_l3_url != "none" ]]; then
l3a=$(curl --connect-timeout 5 -s $bezug_http_l3_url)
echo $l3a > /var/www/html/openWB/ramdisk/bezuga3
fi
python3 /var/www/html/openWB/modules/bezug_http/read_http.py "${bezug_http_w_url}" "${bezug_http_ikwh_url}" "${bezug_http_ekwh_url}" "${bezug_http_l1_url}" "${bezug_http_l2_url}" "${bezug_http_l3_url}" >>$MYLOGFILE 2>&1
ret=$?

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

wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
67 changes: 67 additions & 0 deletions modules/bezug_http/read_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/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())

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

def get_value(url, file):
try:
if url != "none":
response = requests.get(url, timeout=5)
response.encoding = 'utf-8'
value = response.text.replace("\n", "")
with open("/var/www/html/openWB/ramdisk/"+file, "w") as f:
f.write(str(value))
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog(file+': ' + str(wattbezug))


bezug_http_w_url = str(sys.argv[1])
bezug_http_ikwh_url = str(sys.argv[2])
bezug_http_ekwh_url = str(sys.argv[3])
bezug_http_l1_url = str(sys.argv[4])
bezug_http_l2_url = str(sys.argv[5])
bezug_http_l3_url = str(sys.argv[6])

if Debug >= 2:
DebugLog('http Watt: ' + bezug_http_w_url)
DebugLog('http Bezug: ' + bezug_http_ikwh_url)
DebugLog('http Einsp: ' + bezug_http_ekwh_url)
DebugLog('http Strom L1: ' + bezug_http_l1_url)
DebugLog('http Strom L2: ' + bezug_http_l2_url)
DebugLog('http Strom L3: ' + bezug_http_l3_url)

try:
response = requests.get(bezug_http_w_url, timeout=10)
response.encoding = 'utf-8'
wattbezug = response.text.replace("\n", "")
regex = '^-?[0-9]+$'
if re.search(regex, wattbezug) == None:
wattbezug = "0"
with open("/var/www/html/openWB/ramdisk/wattbezug", "w") as f:
f.write(str(wattbezug))
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Watt: ' + str(wattbezug))

get_value(bezug_http_ikwh_url, "bezugkwh")
get_value(bezug_http_ekwh_url, "einspeisungkwh")
get_value(bezug_http_l1_url, "bezuga1")
get_value(bezug_http_l2_url, "bezuga2")
get_value(bezug_http_l3_url, "bezuga3")

exit(0)
Empty file modified modules/bezug_json/read_json.py
100644 → 100755
Empty file.
108 changes: 108 additions & 0 deletions modules/bezug_kostalpiko/kostal_piko.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python3
from datetime import datetime, timezone
import os
import requests
import sys
import traceback

wrkostalpikoip = str(sys.argv[1])
speichermodul = str(sys.argv[2])

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


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('Kostal Piko IP: ' + wrkostalpikoip)
DebugLog('Kostal Piko Speicher: ' + speichermodul)

# Auslesen eines Kostal Piko WR über die integrierte API des WR mit angeschlossenem Eigenverbrauchssensor.

params = (
('dxsEntries', ['33556736', '251658753', '83887106', '83887362', '83887618)']),
)
pvwatttmp = requests.get('http://'+wrkostalpikoip+'/api/dxs.json', params=params, timeout=3).json()
# aktuelle Ausgangsleistung am WR [W]
try:
pvwatt = int(pvwatttmp["dxsEntries"][0]["value"])
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Leistung WR: ' + str(pvwatt))

if pvwatt > 5:
pvwatt = pvwatt*-1

# zur weiteren verwendung im webinterface
with open("/var/www/html/openWB/ramdisk/pvwatt", "w") as f:
f.write(pvwatt)
# Gesamtzählerstand am WR [kWh]
try:
pvkwh = int(pvwatttmp["dxsEntries"][1]["value"])
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Zaehlerstand WR: ' + str(pvkwh))

pvkwh = pvkwh*1000
# zur weiteren verwendung im webinterface
with open("/var/www/html/openWB/ramdisk/pvkwh", "w") as f:
f.write(str(pvkwh))

try:
bezugw1 = int(pvwatttmp["dxsEntries"][2]["value"])
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Leistung L1: ' + str(bezugw1))

try:
bezugw2 = int(pvwatttmp["dxsEntries"][3]["value"])
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Leistung L2: ' + str(bezugw2))

try:
bezugw3 = int(pvwatttmp["dxsEntries"][4]["value"])
except:
traceback.print_exc()
exit(1)
if Debug >= 1:
DebugLog('Leistung L3: ' + str(bezugw3))

if speichermodul == "speicher_bydhv":
with open("/var/www/html/openWB/ramdisk/speicherleistung", "r") as f:
speicherleistung = f.read()
wattbezug = bezugw1+bezugw2+bezugw3+pvwatt+speicherleistung
else:
wattbezug = bezugw1+bezugw2+bezugw3+pvwatt

with open("/var/www/html/openWB/ramdisk/wattbezug", "w") as f:
f.write(str(wattbezug))
if Debug >= 1:
DebugLog('Watt: ' + str(wattbezug))
bezuga1 = round((bezugw1 / 225), 2)
bezuga2 = round((bezugw2 / 225), 2)
bezuga3 = round((bezugw3 / 225), 2)
with open("/var/www/html/openWB/ramdisk/bezuga1", "w") as f:
f.write(str(bezuga1))
with open("/var/www/html/openWB/ramdisk/bezuga2", "w") as f:
f.write(str(bezuga2))
with open("/var/www/html/openWB/ramdisk/bezuga3", "w") as f:
f.write(str(bezuga3))
if Debug >= 1:
DebugLog('Strom L1: ' + str(bezuga1))
DebugLog('Strom L2: ' + str(bezuga2))
DebugLog('Strom L3: ' + str(bezuga3))

exit(0)
49 changes: 17 additions & 32 deletions modules/bezug_kostalpiko/main.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
#!/bin/bash
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="EVU"
DMOD="MAIN"
Debug=$debug

#Auslesen eines Kostal Piko WR über die integrierte API des WR mit angeschlossenem Eigenverbrauchssensor.
#For development only
#Debug=1

pvwatttmp=$(curl --connect-timeout 3 -s $wrkostalpikoip/api/dxs.json?dxsEntries=33556736'&'dxsEntries=251658753'&'dxsEntries=83887106'&'dxsEntries=83887362'&'dxsEntries=83887618)

#aktuelle Ausgangsleistung am WR [W]
pvwatt=$(echo $pvwatttmp | jq '.dxsEntries[0].value' | sed 's/\..*$//')

if [ $pvwatt > 5 ] ; then
pvwatt=$(echo "$pvwatt*-1" |bc)
if [ $DMOD == "MAIN" ]; then
MYLOGFILE="$RAMDISKDIR/openWB.log"
else
MYLOGFILE="$RAMDISKDIR/evu_json.log"
fi

#zur weiteren verwendung im webinterface
echo $pvwatt > /var/www/html/openWB/ramdisk/pvwatt
#Gesamtzählerstand am WR [kWh]
pvkwh=$(echo $pvwatttmp | jq '.dxsEntries[1].value' | sed 's/\..*$//')
pvkwh=$(echo "$pvkwh*1000" |bc)
#zur weiteren verwendung im webinterface
echo $pvkwh > /var/www/html/openWB/ramdisk/pvkwh
python3 /var/www/html/openWB/modules/bezug_kostalpiko/kostal_piko.py "${wrkostalpikoip}" "${speichermodul}" >>$MYLOGFILE 2>&1
ret=$?

bezugw1=$(echo $pvwatttmp | jq '.dxsEntries[2].value' | sed 's/\..*$//')
bezugw2=$(echo $pvwatttmp | jq '.dxsEntries[3].value' | sed 's/\..*$//')
bezugw3=$(echo $pvwatttmp | jq '.dxsEntries[4].value' | sed 's/\..*$//')
if [[ "$speichermodul" == "speicher_bydhv" ]]; then
speicherleistung=$(</var/www/html/openWB/ramdisk/speicherleistung)
wattbezug=$(echo "$bezugw1+$bezugw2+$bezugw3+$pvwatt+$speicherleistung" | bc)
else
wattbezug=$(echo "$bezugw1+$bezugw2+$bezugw3+$pvwatt" |bc)
fi
openwbDebugLog ${DMOD} 2 "RET: ${ret}"

echo $wattbezug
echo $wattbezug > /var/www/html/openWB/ramdisk/wattbezug
bezuga1=$(echo "scale=2 ; $bezugw1 / 225" | bc)
bezuga2=$(echo "scale=2 ; $bezugw2 / 225" | bc)
bezuga3=$(echo "scale=2 ; $bezugw3 / 225" | bc)
echo $bezuga1 > /var/www/html/openWB/ramdisk/bezuga1
echo $bezuga2 > /var/www/html/openWB/ramdisk/bezuga2
echo $bezuga3 > /var/www/html/openWB/ramdisk/bezuga3
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
91 changes: 91 additions & 0 deletions modules/bezug_kostalplenticoreem300haus/kostal_plenticore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python3

# Konfigurationsdatei einbinden

#########################################################
#
# ermittelt Werte Kostal Plenticore mit EM300
# fuer alle 3 Phasen Leistung, Strom, Spannung
# dann Netzfrequenz und Bezug/Einspeisung
#
# Werte werden im Wechselrichter-Modul ausgelesen, hier nur
# in die passende ramdisk geschrieben
#
# 2019 Michael Ortenstein
# This file is part of openWB
#
#########################################################
from datetime import datetime, timezone
import os
import shutil
import sys

kostalplenticorehaus = int(sys.argv[1])
Debug = int(os.environ.get('debug'))
myPid = str(os.getpid())

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('Kostal Plenticore Haus: ' + str(kostalplenticorehaus))

# Unterscheidung EM300 Sensorposition zur Bestimmung Bezug EVU
if kostalplenticorehaus == 1:
# EM300 Sensorposition 2 (am EVU-Uebergabepunkt = grid connection)
# Bezug EVU wurde bereits im wr_plenticore Modul aus den Modbus-Registern gelesen
shutil.copy("/var/www/html/openWB/ramdisk/temp_wattbezug", "/var/www/html/openWB/ramdisk/wattbezug")
else:
# EM300 Sensorposition 1 (im Hausverbrauchszweig = home consumption)
# Werte aus (temporaerer) ramdisk lesen
# aktueller Hausverbrauch
with open("/var/www/html/openWB/ramdisk/temp_wattbezug", "r") as f:
home_consumption = int(f.read())
# aktuelle PV-Leistung
with open("/var/www/html/openWB/ramdisk/pvwatt", "r") as f:
pv_power_ac = int(f.read())
# aktuelle Speicherleistung
with open("/var/www/html/openWB/ramdisk/temp_speicherleistung", "r") as f:
actual_batt_ch_disch_power = int(f.read())
# Bezug berechnen
bezug = pv_power_ac + actual_batt_ch_disch_power + home_consumption
# und in die ramdisk
with open("/var/www/html/openWB/ramdisk/wattbezug", "w") as f:
f.write(str(bezug))
if Debug >= 1:
DebugLog('Watt: ' + str(bezug))


# Daten aus temporaerer ramdisk zur globalen Weiterverarbeitung in die
# entsprechenden ramdisks kopieren. Die temporaeren Werte stammen aus dem
# wr_plenticore Modul, werden dort zentral aus den Modbus-Registern gelesen

# Bezug Strom Phase 1
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezuga1", "/var/www/html/openWB/ramdisk/bezuga1")
# Bezug Strom Phase 2
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezuga2", "/var/www/html/openWB/ramdisk/bezuga2")
# Bezug Strom Phase 3
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezuga3", "/var/www/html/openWB/ramdisk/bezuga3")
# Netzfrequenz
shutil.copy("/var/www/html/openWB/ramdisk/temp_evuhz", "/var/www/html/openWB/ramdisk/evuhz")
# Bezug Leistung Phase 1
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezugw1", "/var/www/html/openWB/ramdisk/bezugw1")
# Bezug Leistung Phase 2
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezugw2", "/var/www/html/openWB/ramdisk/bezugw2")
# Bezug Leistung Phase 3
shutil.copy("/var/www/html/openWB/ramdisk/temp_bezugw3", "/var/www/html/openWB/ramdisk/bezugw3")
# Spannung Phase 1
shutil.copy("/var/www/html/openWB/ramdisk/temp_evuv1", "/var/www/html/openWB/ramdisk/evuv1")
# Spannung Phase 2
shutil.copy("/var/www/html/openWB/ramdisk/temp_evuv2", "/var/www/html/openWB/ramdisk/evuv2")
# Spannung Phase 3
shutil.copy("/var/www/html/openWB/ramdisk/temp_evuv3", "/var/www/html/openWB/ramdisk/evuv3")
# Power Faktor Phase 1
shutil.copy("/var/www/html/openWB/ramdisk/temp_evupf1", "/var/www/html/openWB/ramdisk/evupf1")
# Power Faktor Phase 2
shutil.copy("/var/www/html/openWB/ramdisk/temp_evupf2", "/var/www/html/openWB/ramdisk/evupf2")
# Power Faktor Phase 3
shutil.copy("/var/www/html/openWB/ramdisk/temp_evupf3", "/var/www/html/openWB/ramdisk/evupf3")

exit(0)
Loading

0 comments on commit 849b845

Please sign in to comment.