howto:wechselrichter_sma
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende ÜberarbeitungNächste ÜberarbeitungBeide Seiten der Revision | ||
howto:wechselrichter_sma [2014/05/29 20:24] – wf_bitplan.com | howto:wechselrichter_sma [2018/01/25 16:04] – code jau | ||
---|---|---|---|
Zeile 62: | Zeile 62: | ||
Anbei das Script: | Anbei das Script: | ||
- | <code perl |sma.pl> | + | <code perl sma.pl> |
# | # | ||
use LWP:: | use LWP:: | ||
Zeile 125: | Zeile 125: | ||
</ | </ | ||
- | Um den Eigenverbrauch zu bestimmen bilde ich die Differenz zwischen der Einspeisung, | + | Um den Eigenverbrauch zu bestimmen bilde ich die Differenz zwischen der Einspeisung, |
+ | Änderung 04.06.2014: Die Berechnung des Gesamtverbrauchs ist aktualisiert. | ||
== Originalscript von Markus == | == Originalscript von Markus == | ||
- | <code bash|eigenverbrauch.bash> | + | <code bash eigenverbrauch.sh> |
#!/bin/bash | #!/bin/bash | ||
echo ' | echo ' | ||
Zeile 150: | Zeile 151: | ||
echo ' | echo ' | ||
- | INSERT INTO `data`( `channel_id`, | + | INSERT INTO `data`( `channel_id`, |
+ | ( | ||
SELECT | SELECT | ||
max(case when `channel_id` | max(case when `channel_id` | ||
max(case when `channel_id` | max(case when `channel_id` | ||
FROM `data` where channel_id in (1,7) | FROM `data` where channel_id in (1,7) | ||
- | group by floor(`timestamp`/ | + | group by floor(`timestamp`/ |
- | inner join data data_1 on timestamp_1 | + | ) a inner join data data_1 on timestamp_1 |
- | ' | mysql --user=vz --password=fdfdfdfdf | + | ' | mysql --user=vz --password=dsdsdsdsds |
</ | </ | ||
auch diese Script wird mit cron gestartet, aber nur ein mal die Stunde. | auch diese Script wird mit cron gestartet, aber nur ein mal die Stunde. | ||
< | < | ||
- | 1 * * * * bash / | + | 1 * * * * bash / |
</ | </ | ||
== Modifiziertes Script (Versuch von Wolfgang) == | == Modifiziertes Script (Versuch von Wolfgang) == | ||
- | <code bash|eigenverbrauch.bash> | + | <code bash eigenverbrauch.sh> |
#!/bin/bash | #!/bin/bash | ||
# calculate own PV usage | # calculate own PV usage | ||
Zeile 250: | Zeile 252: | ||
- the php helper script vzapihelper.php (same as in the youless page) | - the php helper script vzapihelper.php (same as in the youless page) | ||
- | the bash script sma2vz has a function configure - this needs to run once so you might want to comment it out | + | the bash script sma2vz has a function configure - this needs to run once you might want |
- | after you modified | + | to configure the here-document part in the function inverters |
- | the php script | + | there is a help screen available: |
+ | <code bash sma2vz> | ||
+ | ./sma2vz --help | ||
+ | | ||
+ | [ --daytimeonly --lat=lattitude --lon=longitude] | ||
+ | | ||
+ | | [--help] | ||
+ | | [--configure] | ||
+ | |||
+ | | ||
+ | volkszaehler middleware url | ||
+ | |||
+ | | ||
+ | channel uuid for power (watt) PV output | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | do not post data at night (e.g. if your device does not supply data) | ||
+ | |||
+ | | ||
+ | plant longitude geo coordinate | ||
+ | |||
+ | | ||
+ | plant lattitude geo coordinate | ||
+ | |||
+ | | ||
+ | poll SMA inverters in a loop | ||
+ | |||
+ | | ||
+ | how many secs to wait between each reading (default: 15 secs) | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | here document | ||
+ | </ | ||
+ | |||
+ | <code bash configure example> | ||
+ | ./sma2vz --lat 51.244 --lon 6.52 --configure | ||
+ | </ | ||
+ | |||
+ | <code bash loop example> | ||
+ | ./sma2vz \ | ||
+ | --vzurl " | ||
+ | --cuuid_pwr " | ||
+ | --cuuid_kwh " | ||
+ | --daytimeonly --lat 51.244 --lon 6.52 \ | ||
+ | --loop --delay 15 | ||
+ | </ | ||
+ | |||
+ | <code bash cron example> | ||
+ | cd / | ||
+ | / | ||
+ | --vzurl " | ||
+ | --cuuid_pwr " | ||
+ | --cuuid_kwh " | ||
+ | --daytimeonly --lat 51.244 --lon 6.52 >> / | ||
+ | </ | ||
+ | |||
+ | <code | logoutput example> | ||
+ | 2014-06-01 12:52:08 PV: 1409 Watt | ||
+ | 2014-06-01 12:53:07 PV: 2176 Watt | ||
+ | 2014-06-01 12:54:07 PV: 1699 Watt | ||
+ | 2014-06-01 12:55:07 PV: 3375 Watt | ||
+ | 2014-06-01 12:56:07 PV: 3578 Watt | ||
+ | </ | ||
Any feedback please to wf (at) bitplan.com - enjoy! | Any feedback please to wf (at) bitplan.com - enjoy! | ||
- | <code bash | sma2vz> | + | <code bash sma2vz> |
#/bin/bash | #/bin/bash | ||
# SMAspot with Volkszaehler | # SMAspot with Volkszaehler | ||
# WF 2014-05-29 | # WF 2014-05-29 | ||
+ | # $Header: / | ||
# | # | ||
Zeile 276: | Zeile 346: | ||
local l_name=$2 | local l_name=$2 | ||
local l_password=$3 | local l_password=$3 | ||
- | | + | |
- | | + | |
local l_path=$6 | local l_path=$6 | ||
cat << EOF | cat << EOF | ||
Zeile 485: | Zeile 555: | ||
# fifth column is the longitude | # fifth column is the longitude | ||
cat << EOF | cat << EOF | ||
- | 00: | + | 00: |
- | 00: | + | 00: |
EOF | EOF | ||
} | } | ||
Zeile 494: | Zeile 564: | ||
# | # | ||
configure() { | configure() { | ||
- | inverters | while read btaddr name password | + | checklonlat " |
+ | inverters | while read btaddr name password path; do | ||
echo " | echo " | ||
smaspot_config $btaddr $name $password $lon $lat $path > ${name}.cfg | smaspot_config $btaddr $name $password $lon $lat $path > ${name}.cfg | ||
Zeile 504: | Zeile 575: | ||
# | # | ||
getsmameter() { | getsmameter() { | ||
- | tmp=/ | + | # temp filename base for SMAspor result |
- | # endless loop to read data | + | |
- | inverters | while read btaddr name password | + | # read data from all inverters |
- | # single shot run of SMAspot with no CVS export | + | inverters | while read btaddr name password path; do |
- | # uncomment to debug | + | # single shot run of SMAspot with no CVS export |
- | #echo " | + | # uncomment to debug |
- | SMAspot -v -nocsv -cfg${name}.cfg > ${tmp}_${name} | + | #echo " |
- | # the lines we need look like: | + | ./SMAspot -v -nocsv -cfg${name}.cfg > ${tmp}_${name} |
- | # EToday: 3.358kWh | + | # the lines we need look like: |
- | # ETotal: 5151.294kWh | + | # EToday: 3.358kWh |
- | # Total Pac : | + | |
+ | # Total Pac : | ||
- | | + | |
- | cat ${tmp}_${name} | awk ' | + | cat ${tmp}_${name} | awk ' |
# set the field separator fitting the x: y format | # set the field separator fitting the x: y format | ||
BEGIN { FS=":"; | BEGIN { FS=":"; | ||
Zeile 546: | Zeile 618: | ||
return result | return result | ||
}' | }' | ||
+ | rm ${tmp}_${name} | ||
done | done | ||
} | } | ||
- | # uncomment | + | # |
- | #configure | + | # show usage |
+ | # | ||
+ | usage() { | ||
+ | cat << EOF | ||
+ | | ||
+ | [ --daytimeonly --lat=lattitude --lon=longitude] | ||
+ | | ||
+ | | [--help] | ||
+ | | [--configure] | ||
+ | |||
+ | | ||
+ | volkszaehler middleware url | ||
+ | |||
+ | | ||
+ | channel uuid for power (watt) PV output | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | do not post data at night (e.g. if your device does not supply data) | ||
+ | |||
+ | | ||
+ | plant longitude geo coordinate | ||
+ | |||
+ | | ||
+ | plant lattitude geo coordinate | ||
+ | |||
+ | | ||
+ | poll SMA inverters in a loop | ||
+ | |||
+ | | ||
+ | how many secs to wait between each reading (default: 15 secs) | ||
+ | |||
+ | | ||
+ | create | ||
+ | | ||
+ | here document to fit your plant' | ||
+ | EOF | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # show error and exit | ||
+ | # | ||
+ | error() { | ||
+ | local l_msg=$1 | ||
+ | local l_hint=$2 | ||
+ | echo "error $0: $l_msg" | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # check that longitude and lattitude are supplied | ||
+ | # | ||
+ | checklonlat() { | ||
+ | local l_title=" | ||
+ | if [ " | ||
+ | then | ||
+ | error " | ||
+ | fi | ||
+ | if [ " | ||
+ | then | ||
+ | error " | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | # defaults | ||
delay=15 | delay=15 | ||
- | while [ 1 == 1 ] | + | maxloops=1 |
+ | daytimeonly=0 | ||
+ | |||
+ | # check command line arguments | ||
+ | while [ $# -gt 0 ] | ||
+ | do | ||
+ | arg="$1" | ||
+ | #echo " | ||
+ | case " | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | vzurl=" | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | #echo $cuuid_kwh | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | | ||
+ | ;; | ||
+ | |||
+ | | ||
+ | |||
+ | echo " | ||
+ | | ||
+ | | ||
+ | ;; | ||
+ | *) | ||
+ | echo >&2 " | ||
+ | exit 1 | ||
+ | ;; | ||
+ | | ||
+ | | ||
+ | done | ||
+ | |||
+ | # check that options are set | ||
+ | if [ " | ||
+ | then | ||
+ | error " | ||
+ | fi | ||
+ | if [ " | ||
+ | then | ||
+ | error " | ||
+ | fi | ||
+ | if [ " | ||
+ | then | ||
+ | error " | ||
+ | fi | ||
+ | if [ " | ||
+ | then | ||
+ | checklonlat " | ||
+ | fi | ||
+ | |||
+ | loop=0 | ||
+ | while [ $loop -lt $maxloops | ||
do | do | ||
# get the SMA meter readings | # get the SMA meter readings | ||
- | getsmameter | php -f sma2vz.php | + | getsmameter | php sma2vz.php |
# sleep a while | # sleep a while | ||
- | sleep $delay | + | |
+ | then | ||
+ | | ||
+ | fi | ||
+ | loop=`expr $loop + 1 ` | ||
done | done | ||
</ | </ | ||
- | <code php | sma2vz.php> | + | <code php sma2vz.php> |
<?php | <?php | ||
/** | /** | ||
* read meter data from SMA device | * read meter data from SMA device | ||
* and post it to volkszaehler | * and post it to volkszaehler | ||
- | * $Header: / | + | * $Header: / |
*/ | */ | ||
// common code for reading and posting | // common code for reading and posting | ||
require __DIR__.'/ | require __DIR__.'/ | ||
- | |||
- | /** | ||
- | * transfer youless reading to vz middleware | ||
- | * param 1: url to read from youless | ||
- | * param 2: vzurl - middleware url of volkszaehler | ||
- | * param 3: channel uuid for power reading in Watt | ||
- | * param 4: channel uuid for energy reading in kWh | ||
- | * param 5: shall we post the cnt reading? | ||
- | */ | ||
- | | ||
- | // read data from youless | ||
- | | ||
- | // get values | ||
- | | ||
- | | ||
- | // the level is not interesting for digital meters | ||
- | // if you have an analog meter you might want to use and show this | ||
- | // $lvl=$meter[" | ||
- | // convert to php compatible value | + | |
- | $cnt=str_replace(","," | + | * check the daytime values |
- | | + | */ |
- | | + | |
- | | + | $result=array(); |
- | // get human readable time stamp | + | // 08:53 CEST |
- | | + | // |
+ | // 08:53 | ||
+ | $time_format | ||
- | // display progress (comment if you use this in cron job) | + | |
- | echo "$now $cnt kWh $pwr Watt\n"; | + | $tzoffset = date(" |
- | + | ||
- | // post data to middleware according to: | + | |
- | // http:// | + | |
- | // adapt timestamp to volkszaehler conventions | + | |
- | $timestamp=time()*1000; | + | |
- | # first post the power reading in Watt | + | |
- | $posturl=$vzurl."/" | + | |
- | | + | $sunrise_time |
- | | + | // determine sunset time |
+ | $sunset | ||
+ | $sunset_time | ||
- | # then the energy reading in Wh | + | |
- | | + | $sunrise_epoch |
- | | + | $sunset_epoch |
- | | + | $time_epoch |
- | | + | |
- | } | + | |
- | // uncomment to debug | + | $result[" |
- | | + | |
- | } | + | |
- | | + | $result[" |
- | // the url to read from | + | $result[" |
- | $vzurl="http://capri/vz/middleware.php/ | + | $result[" |
- | | + | return $result; |
+ | } | ||
+ | |||
+ | $loop=0; | ||
+ | | ||
+ | // --vzurl= --cuuid_pwr= --cuuid_kwh= --daytimeonly | ||
+ | $longopts=array("vzurl:"," | ||
+ | $shortopts=""; | ||
+ | $options=getopt($shortopts, | ||
+ | | ||
+ | | ||
// channel uuids | // channel uuids | ||
// power (watts) | // power (watts) | ||
- | $cuuid_pwr=" | + | $cuuid_pwr=checkoption("cuuid_pwr",$options); |
// energy (kWh) | // energy (kWh) | ||
- | $cuuid_kwh=" | + | $cuuid_kwh=checkoption("cuuid_kwh",$options); |
+ | // daytimeonly? | ||
+ | $daytimeonly=checkoption(" | ||
+ | if ($daytimeonly) { | ||
+ | $latitude=checkoption(" | ||
+ | $longitude=checkoption(" | ||
+ | } | ||
+ | | ||
$jsonlines=file(" | $jsonlines=file(" | ||
$tmeter=array(); | $tmeter=array(); | ||
Zeile 645: | Zeile 869: | ||
} | } | ||
} | } | ||
- | var_dump($tmeter); | + | |
+ | //array(3) { [" | ||
+ | // [" | ||
+ | // | ||
+ | $etotal=$tmeter[" | ||
+ | $totalpac=$tmeter[" | ||
+ | if ($daytimeonly) { | ||
+ | $daytime=daytime($latitude, | ||
+ | if (!$daytime[" | ||
+ | printf(" | ||
+ | exit(2); | ||
+ | } | ||
+ | } | ||
# total wattage of all inverters | # total wattage of all inverters | ||
- | post2vz($vzurl, | + | post2vz($vzurl, |
# total kwh of all inverters | # total kwh of all inverters | ||
- | post2vz($vzurl, | + | post2vz($vzurl, |
+ | if ($daytimeonly) { | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | exit(0); | ||
?> | ?> | ||
</ | </ | ||
- | <code php | vzapihelper.php> | + | |
+ | <code php vzapihelper.php> | ||
<?php | <?php | ||
/** | /** | ||
* vzapi helper functions | * vzapi helper functions | ||
- | * $Header: / | + | * $Header: / |
*/ | */ | ||
Zeile 726: | Zeile 969: | ||
} | } | ||
} | } | ||
+ | |||
+ | /** | ||
+ | * check that option $opt is available in $options | ||
+ | * return the value if available | ||
+ | */ | ||
+ | function checkoption($opt, | ||
+ | if (array_key_exists($opt, | ||
+ | return $options[$opt]; | ||
+ | else | ||
+ | die(" | ||
+ | } | ||
?> | ?> | ||
</ | </ |
howto/wechselrichter_sma.txt · Zuletzt geändert: 2018/04/01 15:56 von jau