Skip to content

Commit

Permalink
python modules for smartme, solarlog, solarwatt, solarworld, sonneneco
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed Dec 6, 2021
1 parent bea61e5 commit ee822ba
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 230 deletions.
82 changes: 3 additions & 79 deletions modules/bezug_smartme/main.sh
Original file line number Diff line number Diff line change
@@ -1,81 +1,5 @@
#!/bin/bash

#Daten einlesen
json=$(curl -s -u $bezug_smartme_user:$bezug_smartme_pass --connect-timeout 10 -s $bezug_smartme_url)

#Aktuelle Leistung (kW --> W)
wattbezug=$(echo $json | jq .ActivePower)
wattbezug=$(echo "scale=3 ; $wattbezug * 1000" | bc)
wattbezug=$(echo "$wattbezug / 1" | bc)

wattbezug1=$(echo $json | jq .ActivePowerL1)
wattbezug1=$(echo "scale=3 ; $wattbezug1 * 1000" | bc)
wattbezug1=$(echo "$wattbezug1 / 1" | bc)

wattbezug2=$(echo $json | jq .ActivePowerL2)
wattbezug2=$(echo "scale=3 ; $wattbezug2 * 1000" | bc)
wattbezug2=$(echo "$wattbezug2 / 1" | bc)

wattbezug3=$(echo $json | jq .ActivePowerL3)
wattbezug3=$(echo "scale=3 ; $wattbezug3 * 1000" | bc)
wattbezug3=$(echo "$wattbezug3 / 1" | bc)

if [ $wattbezug1 = '0' ] ; then
wattbezug1=$wattbezug
fi

#Zählerstand Import(kWh)
ikwh=$(echo $json | jq .CounterReadingImport)
ikwh=$(echo "scale=3 ; $ikwh * 1000" | bc)
#Zur Reduzierung der Datenmenge kann die folgende Zeile eingefügt werden.
#ikwh=$(echo "$ikwh / 1" | bc)

#Zählerstand Export(kWh)
ekwh=$(echo $json | jq .CounterReadingExport)
ekwh=$(echo "scale=3 ; $ekwh * 1000" | bc)
#Zur Reduzierung der Datenmenge kann die folgende Zeile eingefügt werden.
#ekwh=$(echo "$ekwh / 1" | bc)

#Weitere Zählerdaten für die Statusseite (PowerFaktor, Spannung und Strom)
evupf1=$(echo $json | jq .PowerFactorL1)
evupf2=$(echo $json | jq .PowerFactorL2)
evupf3=$(echo $json | jq .PowerFactorL3)
evuv1=$(echo $json | jq .VoltageL1)
evuv2=$(echo $json | jq .VoltageL2)
evuv3=$(echo $json | jq .VoltageL3)
bezuga1=$(echo $json | jq .CurrentL1)
bezuga2=$(echo $json | jq .CurrentL2)
bezuga3=$(echo $json | jq .CurrentL3)
if [ $bezuga1 = 'null' ] ; then
bezuga1=$(echo $json | jq .Current)
fi

#Prüfen ob Werte gültig
re='^[-+]?[0-9]+\.?[0-9]*$'
if ! [[ $wattbezug =~ $re ]] ; then
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
fi
if ! [[ $ikwh =~ $re ]] ; then
ikwh=$(</var/www/html/openWB/ramdisk/bezugkwh)
fi
if ! [[ $ekwh =~ $re ]] ; then
ekwh=$(</var/www/html/openWB/ramdisk/einspeisungkwh)
fi

#Ausgabe
echo $wattbezug
echo $wattbezug > /var/www/html/openWB/ramdisk/wattbezug
echo $wattbezug1 > /var/www/html/openWB/ramdisk/bezugw1
echo $wattbezug2 > /var/www/html/openWB/ramdisk/bezugw2
echo $wattbezug3 > /var/www/html/openWB/ramdisk/bezugw3
echo $ikwh > /var/www/html/openWB/ramdisk/bezugkwh
echo $ekwh > /var/www/html/openWB/ramdisk/einspeisungkwh
echo $evupf1 > /var/www/html/openWB/ramdisk/evupf1
echo $evupf2 > /var/www/html/openWB/ramdisk/evupf2
echo $evupf3 > /var/www/html/openWB/ramdisk/evupf3
echo $evuv1 > /var/www/html/openWB/ramdisk/evuv1
echo $evuv2 > /var/www/html/openWB/ramdisk/evuv2
echo $evuv3 > /var/www/html/openWB/ramdisk/evuv3
echo $bezuga1 > /var/www/html/openWB/ramdisk/bezuga1
echo $bezuga2 > /var/www/html/openWB/ramdisk/bezuga2
echo $bezuga3 > /var/www/html/openWB/ramdisk/bezuga3
python3 /var/www/html/openWB/modules/bezug_smartme/smartme.py $bezug_smartme_url $bezug_smartme_user $bezug_smartme_pass
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
99 changes: 99 additions & 0 deletions modules/bezug_smartme/smartme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3

import json
import re
import requests
import sys

bezug_smartme_url = str(sys.argv[1])
bezug_smartme_user = str(sys.argv[2])
bezug_smartme_pass = str(sys.argv[3])

# Daten einlesen
response = requests.get('http://'+bezug_smartme_url, auth=(bezug_smartme_user, bezug_smartme_pass), timeout=10)
response = json.loads(response)

# Aktuelle Leistung (kW --> W)
wattbezug = response["ActivePower"]
wattbezug = int(wattbezug * 1000)

wattbezug1 = response["ActivePowerL1"]
wattbezug1 = int(wattbezug1 * 1000)

wattbezug2 = response["ActivePowerL2"]
wattbezug2 = int(wattbezug2 * 1000)

wattbezug3 = response["ActivePowerL3"]
wattbezug3 = int(wattbezug3 * 1000)

if wattbezug1 == 0:
wattbezug1 = wattbezug

# Zählerstand Import(kWh)
ikwh = response["CounterReadingImport"]
ikwh = round(ikwh * 1000, 3)
# Zur Reduzierung der Datenmenge kann die folgende Zeile eingefügt werden.
# ikwh=$(echo "$ikwh / 1" | bc)

# Zählerstand Export(kWh)
ekwh = response["CounterReadingExport"]
ekwh = round(ekwh * 1000, 3)
# Zur Reduzierung der Datenmenge kann die folgende Zeile eingefügt werden.
# ekwh=$(echo "$ekwh / 1" | bc)

# Weitere Zählerdaten für die Statusseite (PowerFaktor, Spannung und Strom)
evupf1 = response["PowerFactorL1"]
evupf2 = response["PowerFactorL2"]
evupf3 = response["PowerFactorL3"]
evuv1 = response["VoltageL1"]
evuv2 = response["VoltageL2"]
evuv3 = response["VoltageL3"]
bezuga1 = response["CurrentL1"]
bezuga2 = response["CurrentL2"]
bezuga3 = response["CurrentL3"]
if bezuga1 == 'null':
bezuga1 = response["Current"]

# Prüfen ob Werte gültig
regex = '^[-+]?[0-9]+\.?[0-9]*$'
if re.sreach(regex, wattbezug) == None:
with open("/var/www/html/openWB/ramdisk/wattbezug", "r") as f:
wattbezug = f.read()
if re.search(regex, ikwh) == None:
with open("/var/www/html/openWB/ramdisk/bezugkwh", "r") as f:
ikwh = f.read()
if re.search(regex, ekwh) == None:
with open("/var/www/html/openWB/ramdisk/einspeisungkwh", "r") as f:
ekwh = f.read()

# Ausgabe
with open("/var/www/html/openWB/ramdisk/wattbezug", "w") as f:
f.write(str(wattbezug))
with open("/var/www/html/openWB/ramdisk/bezugw1", "w") as f:
f.write(str(wattbezug1))
with open("/var/www/html/openWB/ramdisk/bezugw2", "w") as f:
f.write(str(wattbezug2))
with open("/var/www/html/openWB/ramdisk/bezugw3", "w") as f:
f.write(str(wattbezug3))
with open("/var/www/html/openWB/ramdisk/bezugkwh", "w") as f:
f.write(str(ikwh))
with open("/var/www/html/openWB/ramdisk/einspeisungkwh", "w") as f:
f.write(str(ekwh))
with open("/var/www/html/openWB/ramdisk/evupf1", "w") as f:
f.write(str(evupf1))
with open("/var/www/html/openWB/ramdisk/evupf2", "w") as f:
f.write(str(evupf2))
with open("/var/www/html/openWB/ramdisk/evupf3", "w") as f:
f.write(str(evupf3))
with open("/var/www/html/openWB/ramdisk/evuv1", "w") as f:
f.write(str(evuv1))
with open("/var/www/html/openWB/ramdisk/evuv2", "w") as f:
f.write(str(evuv2))
with open("/var/www/html/openWB/ramdisk/evuv3", "w") as f:
f.write(str(evuv3))
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))
23 changes: 3 additions & 20 deletions modules/bezug_solarlog/main.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
#!/bin/bash

answer=$(curl -d {\"801\":{\"170\":null}} --connect-timeout 5 -s $bezug_solarlog_ip/getjp)

pvwatt=$(echo $answer | jq '."801"."170"."101"' )
hausverbrauch=$(echo $answer | jq '."801"."170"."110"' )
bezugwatt=$(echo "$hausverbrauch - $pvwatt" |bc)
pvkwh=$(echo $answer | jq '."801"."170"."109"' )

if (( bezug_solarlog_speicherv == 1 )); then
speicherleistung=$(<ramdisk/speicherleistung)
bezugwatt=$(( bezugwatt + speicherleistung ))
fi
if (( $pvwatt > 5 )); then
pvwatt=$(echo "$pvwatt*-1" |bc)
fi
echo $bezugwatt
echo $bezugwatt > /var/www/html/openWB/ramdisk/wattbezug
echo $pvwatt > /var/www/html/openWB/ramdisk/pvwatt
echo $pvkwh > /var/www/html/openWB/ramdisk/pvkwh
pvkwhk=$(echo "$pvkwh*1000" |bc)
echo $pvkwhk > /var/www/html/openWB/ramdisk/pvkwhk
python3 /var/www/html/openWB/modules/bezug_solarlog/solarlog.py $bezug_solarlog_ip $bezug_solarlog_speicherv
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
36 changes: 36 additions & 0 deletions modules/bezug_solarlog/solarlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

import json
import requests
import sys

bezug_solarlog_ip = str(sys.argv[1])
bezug_solarlog_speicherv = str(sys.argv[1])

data = {"801": {"170": None}}
data = json.dumps(data)
response = requests.post('http://'+bezug_solarlog_ip+'/getjp', data=data, timeout=5)
response = json.loads(response)


pvwatt = response["801"]["170"]["101"]
hausverbrauch = response["801"]["170"]["110"]
bezugwatt = hausverbrauch - pvwatt
pvkwh = response["801"]["170"]["109"]

if bezug_solarlog_speicherv == 1:
with open("ramdisk/speicherleistung", "r") as f:
speicherleistung = f.read()
bezugwatt = bezugwatt + speicherleistung
if pvwatt > 5:
pvwatt = pvwatt*-1

with open("/var/www/html/openWB/ramdisk/wattbezug", "w") as f:
f.write(str(bezugwatt))
with open("/var/www/html/openWB/ramdisk/pvwatt", "w") as f:
f.write(str(pvwatt))
with open("/var/www/html/openWB/ramdisk/pvkwh", "w") as f:
f.write(str(pvkwh))
pvkwhk = pvkwh*1000
with open("/var/www/html/openWB/ramdisk/pvkwhk", "w") as f:
f.write(str(pvkwhk))
39 changes: 3 additions & 36 deletions modules/bezug_solarwatt/main.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
#!/bin/bash

OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
MODULE="EVU"
LOGFILE="$RAMDISKDIR/openWB.log"
Debug=$debug
BEZUGFILE="$RAMDISKDIR/wattbezug"

DebugLog(){
if (( Debug > 0 )); then
timestamp=`date +"%Y-%m-%d %H:%M:%S"`
echo "$timestamp: ${MODULE}: $@" >> $LOGFILE
fi
}

if (( $solarwattmethod == 0 )); then #Abruf über Energy Manager
sresponse=$(curl --connect-timeout 3 -s "http://${speicher1_ip}/rest/kiwigrid/wizard/devices")

if ((${#sresponse} < 10)); then
bezugwatt=$(<$BEZUGFILE)
else
bezugw=$(echo $sresponse | jq '.result.items | .[] | select(.tagValues.PowerConsumedFromGrid.value != null) | .tagValues.PowerConsumedFromGrid.value' | sed 's/\..*$//')
einspeisungw=$(echo $sresponse | jq '.result.items | .[] | select(.tagValues.PowerOut.value != null) | .tagValues.PowerOut.value' | head -n 1 | sed 's/\..*$//')
bezugwatt=$(echo "scale=0; $bezugw - $einspeisungw /1" |bc)
fi
fi
if (( $solarwattmethod == 1 )); then #Abruf über Gateway
sresponse=$(curl --connect-timeout 3 -s "http://${speicher1_ip2}:8080/")

if ((${#sresponse} < 10)); then
bezugwatt=$(<$BEZUGFILE)
else
bezugw=$(echo $sresponse | jq '.FData.PGrid' | sed 's/\..*$//')
bezugwatt=$(echo "scale=0; $bezugw / 1" | bc)
fi
fi

DebugLog "Netzbezug: $bezugwatt W"
echo $bezugwatt > $BEZUGFILE
echo $bezugwatt
python3 /var/www/html/openWB/modules/bezug_solarwatt/solarwatt.py $OPENWBBASEDIR $Debug $solarwattmethod $speicher1_ip $speicher1_ip2
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
58 changes: 58 additions & 0 deletions modules/bezug_solarwatt/solarwatt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import datetime
import json
import requests
import sys

base_dir = str(sys.argv[1])
debug = str(sys.argv[2])
solarwattmethod = str(sys.argv[3])
speicher1_ip = str(sys.argv[4])
speicher1_ip2 = str(sys.argv[5])

ramdisk_dir = base_dir+"/ramdisk"
module = "EVU"
logfile = ramdisk_dir+"/openWB.log"
Debug = debug
bezug_file = ramdisk_dir+"/wattbezug"


def debugLog(msg):
if Debug > 0:
timestamp = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
with open(logfile, "a") as f:
f.write(str(timestamp)+": "+str(module)+": "+msg)


if solarwattmethod == 0: # Abruf über Energy Manager
sresponse = requests.get('http://'+speicher1_ip+'/rest/kiwigrid/wizard/devices', timeout=3)
sresponse = json.loads(sresponse)
if len(str(sresponse)) < 10:
with open(bezug_file, "r") as f:
bezugwatt = f.read()
else:
for item in sresponse["result"]["items"]:
if "tagValues" in sresponse["result"]["items"][item]:
if "PowerConsumedFromGrid" in sresponse["result"]["items"][item]["tagValues"]:
if "value" in sresponse["result"]["items"][item]["tagValues"]["PowerConsumedFromGrid"]:
bezugw = int(sresponse["result"]["items"][item]["tagValues"]["PowerConsumedFromGrid"]["value"])
einspeisungw =$(echo $sresponse | jq '.result.items | .[] | select(.tagValues.PowerOut.value != null) | .tagValues.PowerOut.value' | head -n 1 | sed 's/\..*$//')
for item in sresponse["result"]["items"]:
if "tagValues" in sresponse["result"]["items"][item]:
if "PowerOut" in sresponse["result"]["items"][item]["tagValues"]:
if "value" in sresponse["result"]["items"][item]["tagValues"]["PowerOut"]:
bezugw = int(sresponse["result"]["items"][item]["tagValues"]["PowerOut"]["value"])
bezugwatt = int(bezugw - einspeisungw)
if solarwattmethod == 1: # Abruf über Gateway
sresponse = requests.get('http://'+speicher1_ip2+':8080/', timeout=3)

if len(str(sresponse)) < 10:
with open(bezug_file, "r") as f:
bezugwatt = f.read()
else:
bezugwatt = int(sresponse["FData"]["PGrid"])

debugLog("Netzbezug: "+str(bezugwatt)+" W")
with open(bezug_file, "w") as f:
f.write(str(bezugwatt))
21 changes: 3 additions & 18 deletions modules/bezug_solarworld/main.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
#!/bin/bash

# Auslesen eines Solarworl eManagers über die integrierte JSON-API
emanagerantwort=$(curl --connect-timeout 5 -s "$solarworld_emanagerip/rest/solarworld/lpvm/powerAndBatteryData")

em_in_watt=$(echo $emanagerantwort | jq '.PowerIn')
em_out_watt=$(echo $emanagerantwort | jq '.PowerOut')

# Bezug ist entweder -Out oder In; bei Einspeisung ist 'em_in_watt' immer 0
# use printf zum runden, LC_ALL=C wegen Dezimalpunkt
bezug_watt=$(LC_ALL=C printf "%.0f\n" $(echo "$em_in_watt - $em_out_watt" | bc))

#wenn eManager aus bzw. keine Antwort ersetze leeren Wert durch eine 0
ra='^-?[0-9]+$'

if ! [[ $bezug_watt =~ $ra ]] ; then
bezug_watt="0"
fi
echo $bezug_watt
echo $bezug_watt > /var/www/html/openWB/ramdisk/wattbezug
python3 /var/www/html/openWB/modules/bezug_solarworld/solarworld.py $solarworld_emanagerip
wattbezug=$(</var/www/html/openWB/ramdisk/wattbezug)
echo $wattbezug
Loading

0 comments on commit ee822ba

Please sign in to comment.