Benutzer-Werkzeuge

Webseiten-Werkzeuge


howto:wechselrichter_sma

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
Nächste ÜberarbeitungBeide Seiten der Revision
howto:wechselrichter_sma [2014/05/29 20:24] wf_bitplan.comhowto: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>
 #!/usr/bin/perl #!/usr/bin/perl
 use LWP::UserAgent; use LWP::UserAgent;
Zeile 125: Zeile 125:
 </code> </code>
  
-Um den Eigenverbrauch zu bestimmen bilde ich die Differenz zwischen der Einspeisung, gemessen am Stromzähler. ( mit vzlogger) und der erzeugten Strommenge des SMA. Da der SMA immer etwas verzögert die Werte anzeigt, lege ich nur einen Messpunkt alle 15 Minuten ab: Die Kanäle sind zuvor in der Weboberfläche anzulegen, und mit mysql die channel_id zu selektieren.+Um den Eigenverbrauch zu bestimmen bilde ich die Differenz zwischen der Einspeisung, gemessen am Stromzähler. ( mit vzlogger) und der erzeugten Strommenge des SMA. Da der SMA immer etwas verzögert die Werte anzeigt, lege ich nur einen Messpunkt alle 15 Minuten ab: Die Kanäle sind zuvor in der Weboberfläche anzulegen, und mit mysql die channel_id zu selektieren.  
 +Ä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`, `timestamp`, `value`) select 8,  timestamp_7,   data_7.value + data_1.value/1000     from (+INSERT INTO `data`( `channel_id`, `timestamp`, `value`) select 8,  timestamp_1,   (select data.value from data where channel_id = 7 and  data.timestamp <= data_1.timestamp order by timestamp desc limit 1) + data_1.value/1000     from  
 +(
 SELECT  SELECT 
 max(case when `channel_id`  = 1 then timestamp else 0 end) timestamp_1, max(case when `channel_id`  = 1 then timestamp else 0 end) timestamp_1,
 max(case when `channel_id`  = 7 then timestamp else 0 end) timestamp_7 max(case when `channel_id`  = 7 then timestamp else 0 end) timestamp_7
   FROM `data` where channel_id in (1,7)    FROM `data` where channel_id in (1,7) 
-group by floor(`timestamp`/60/1000/15) ) a inner join data data_7 on  timestamp_7  = data_7.timestamp and data_7.channel_id = 7 +group by floor(`timestamp`/60/1000/15)  
-inner join data data_1 on  timestamp_1  = data_1.timestamp and data_1.channel_id = 1 +) a inner join data data_1 on  timestamp_1  = data_1.timestamp and data_1.channel_id = 1 
-' | mysql --user=vz --password=fdfdfdfdf volkszaehler+' | mysql --user=vz --password=dsdsdsdsds volkszaehler
  
 </code> </code>
 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.
 <code> <code>
-1  * * * *    bash /home/markus/hack/Volksz/eigenverbrauch.bash+1  * * * *    bash /home/markus/hack/Volksz/eigenverbrauch.sh
 </code> </code>
  
 == 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 the function inverters as described in its comments+to configure the here-document part in the function inverters of this script
  
-the php script sma2vz.php needs to be adapted to your channel uuids and your middleware url.+there is a help screen available: 
 +<code bash sma2vz
 +./sma2vz --help 
 +   usage: ./sma2vz --vzurl=vzurl --cuuid_pwr=x --cuuid_kwh=y  
 +     [ --daytimeonly --lat=lattitude --lon=longitude] 
 +     [--loop --delay=delay]  
 +    | [--help]  
 +    | [--configure]  
 +     
 +     --vzurl=<url>  
 +        volkszaehler middleware url   
 +     
 +     --cuuid_pwr=<uuid>  
 +        channel uuid for power (watt) PV output  
 +     
 +     --cuuid_kwh=<uuid>   
 +       channel uuid for energy (kwH) PV total  
 +  
 +     --daytimeonly 
 +       do not post data at night (e.g. if your device does not supply data) 
 + 
 +     --lon=<longitude> 
 +       plant longitude geo coordinate 
 + 
 +     --lat=<lattitude> 
 +       plant lattitude geo coordinate 
 +     
 +     --loop   
 +       poll SMA inverters in a loop   
 +     
 +     --delay=<secs>   
 +       how many secs to wait between each reading (default: 15 secs) 
 +     
 +     --configure  
 +       create SMAsport configuration file(s)  
 +         modify ./sma2vz inverters shell function  
 +         here document to fit your plant's data 
 +</code> 
 + 
 +<code bash configure example> 
 +./sma2vz --lat 51.244 --lon 6.52 --configure 
 +</code>         
 + 
 +<code bash loop example> 
 +./sma2vz \ 
 +  --vzurl "http://capri/vz/middleware.php/data"
 +  --cuuid_pwr "7744bbf0-e74d-11e3-9ec7-xxxx"
 +  --cuuid_kwh "460feba0-e74f-11e3-8a0d-xxxx"
 +  --daytimeonly --lat 51.244 --lon 6.52 \ 
 +  --loop --delay 15 
 +</code> 
 + 
 +<code bash cron example> 
 +cd /home/wf/smaspot 
 +/home/wf/smaspot/sma2vz \ 
 +  --vzurl "http://capri/vz/middleware.php/data"
 +  --cuuid_pwr "7744bbf0-e74d-11e3-9ec7-xxxx"
 +  --cuuid_kwh "460feba0-e74f-11e3-8a0d-xxxx"
 +  --daytimeonly --lat 51.244 --lon 6.52 >> /var/log/energymeter.log 
 +</code> 
 + 
 +<code | logoutput example> 
 +2014-06-01 12:52:08 PV:  1409 Watt   6699.801 kwH sun rise:05:21 set:21:42 
 +2014-06-01 12:53:07 PV:  2176 Watt   6699.826 kwH sun rise:05:21 set:21:42 
 +2014-06-01 12:54:07 PV:  1699 Watt   6699.855 kwH sun rise:05:21 set:21:42 
 +2014-06-01 12:55:07 PV:  3375 Watt   6699.898 kwH sun rise:05:21 set:21:42 
 +2014-06-01 12:56:07 PV:  3578 Watt   6699.957 kwH sun rise:05:21 set:21:42 
 +</code>
  
 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: /home/wf/smaspot/RCS/sma2vz,v 1.8 2014/06/01 10:49:38 wf Exp wf $
  
 # #
Zeile 276: Zeile 346:
  local l_name=$2  local l_name=$2
  local l_password=$3  local l_password=$3
- local l_lat=$4 + local l_lon=$4 
- local l_lon=$5+ local l_lat=$5
  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:80:25:24:XX:YY 1300Watt password 51.244 6.52 /home/wf/smaspot +00:80:25:24:xx:xy 1300Watt password /home/wf/smaspot 
-00:80:25:29:ZZ:ZZ 4000Watt password 51.244 6.52 /home/wf/smaspot+00:80:25:29:xx:xy 4000Watt password /home/wf/smaspot
 EOF EOF
 } }
Zeile 494: Zeile 564:
 # #
 configure() { configure() {
-inverters | while read btaddr name password lon lat path; do+  checklonlat "configure" 
 +inverters | while read btaddr name password path; do
   echo "creating ${name}.cfg for bluetooth addr $btaddr" 1>&2   echo "creating ${name}.cfg for bluetooth addr $btaddr" 1>&2
   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=/tmp/smaspot$$ +  # temp filename base for SMAspor result 
-endless loop to read data +  tmp=/tmp/smaspot$$ 
-inverters | while read btaddr name password lon lat path; do +  # 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 "running smaspot with ${name}.cfg for bluetooth addr $btaddr" +    # uncomment to debug 
-  SMAspot -v -nocsv -cfg${name}.cfg > ${tmp}_${name} +    #echo "running smaspot with ${name}.cfg for bluetooth addr $btaddr" 
-  # 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   :   0.442kW+   #  ETotal: 5151.294kWh 
 +    #  Total Pac   :   0.442kW
  
-  # let's filter the result with awk +    # let's filter the result with awk 
-  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=":";doublequote="\x22" } BEGIN        { FS=":";doublequote="\x22" }
Zeile 546: Zeile 618:
   return result    return result 
 }'  }' 
 +  rm ${tmp}_${name}
 done done
 } }
  
-uncomment to create you configuration files +# 
-#configure+# show usage 
 +
 +usage() {  
 +cat << EOF 
 +   usage: $0 --vzurl=vzurl --cuuid_pwr=x --cuuid_kwh=y  
 +     [ --daytimeonly --lat=lattitude --lon=longitude] 
 +     [--loop --delay=delay]  
 +    | [--help]  
 +    | [--configure]  
 +     
 +     --vzurl=<url>  
 +        volkszaehler middleware url   
 +     
 +     --cuuid_pwr=<uuid>  
 +        channel uuid for power (watt) PV output  
 +     
 +     --cuuid_kwh=<uuid>   
 +       channel uuid for energy (kwH) PV total  
 +  
 +     --daytimeonly 
 +       do not post data at night (e.g. if your device does not supply data) 
 + 
 +     --lon=<longitude> 
 +       plant longitude geo coordinate 
 + 
 +     --lat=<lattitude> 
 +       plant lattitude geo coordinate 
 +     
 +     --loop   
 +       poll SMA inverters in a loop   
 +     
 +     --delay=<secs>   
 +       how many secs to wait between each reading (default: 15 secs) 
 +     
 +     --configure  
 +       create SMAsport configuration file(s)  
 +         modify $0 inverters shell function  
 +         here document to fit your plant's data 
 +EOF 
 +  exit 1 
 +
 + 
 + 
 +# show error and exit 
 +
 +error() { 
 +  local l_msg=$1 
 +  local l_hint=$2 
 +  echo "error $0: $l_msg" 1>&
 +  echo "  you might want to $l_hint !" 1>&
 +  exit 1 
 +
 + 
 +
 +# check that longitude and lattitude are supplied 
 +
 +checklonlat() { 
 +  local l_title="$1" 
 +  if [ "$lon" == ""
 +  then 
 +    error "option --lon" "supply lon longitude setting for $l_title to work" 
 +  fi 
 +  if [ "$lat" == ""
 +  then 
 +    error "option --lat" "supply lat lattitude setting for $l_title to work" 
 +  fi 
 +
 + 
 +# defaults
 delay=15 delay=15
-while [ 1 == 1 ]+maxloops=1 
 +daytimeonly=0 
 + 
 +# check command line arguments 
 +while [ $# -gt 0 ] 
 +do 
 +  arg="$1
 +  #echo "$arg" 
 +  case "$arg" in  
 +     --help) 
 +       usage 
 +       ;; 
 + 
 +     --vzurl)  
 +       vzurl="$2" 
 +       shift 
 +       ;; 
 + 
 +     --cuuid_pwr)  
 +       cuuid_pwr="$2" 
 +       shift 
 +       ;; 
 + 
 +     --cuuid_kwh)  
 +       cuuid_kwh="$2" 
 +       #echo $cuuid_kwh 
 +       shift 
 +       ;; 
 + 
 +     --daytimeonly) 
 +       daytimeonly=1
 +       ;; 
 + 
 +     --lat)  
 +       lat="$2" 
 +       shift 
 +       ;; 
 + 
 +     --lon)  
 +       lon="$2" 
 +       shift 
 +       ;; 
 + 
 +     --delay)  
 +       delay=$2      
 +       shift 
 +       ;; 
 + 
 +     --loop) 
 +       maxloops=10000000; 
 +       ;; 
 + 
 +     --configure) 
 + 
 +       echo "creating configuration files" 
 +       configure 
 +       exit 
 +       ;; 
 +     *) 
 +       echo >&2 "Invalid argument: $1" 
 +       exit 1 
 +     ;; 
 +   esac 
 +   shift 
 +done 
 + 
 +# check that options are set 
 +if [ "$vzurl" == ""
 +then 
 +  error "option --vzurl is missing" "check and use volkszaehler middleware url" 
 +fi 
 +if [ "${cuuid_pwr}" == ""
 +then 
 +  error "option --cuuid_pwr is missing" "check the uuid for the PV power (Watt) channel" 
 +fi 
 +if [ "$cuuid_kwh" == ""
 +then 
 +  error "option --cuuid_kwh" "check the uuid for the PV energy (kwH) channel" 
 +fi 
 +if [ "$daytimeonly" -eq 1 ] 
 +then 
 +  checklonlat "daytimeonly" 
 +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 --vzurl=$vzurl --cuuid_pwr=$cuuid_pwr --cuuid_kwh=$cuuid_kwh --daytimeonly=$daytimeonly --lat=$lat --lon=$lon
   # sleep a while   # sleep a while
-  sleep $delay+  if [ $maxloops -gt 1 ] 
 +  then 
 +    sleep $delay 
 +  fi 
 +  loop=`expr $loop + 1 `
 done done
  
 </code> </code>
-<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: /home/wf/smaspot/RCS/sma2vz.php,v 1.2014/05/29 18:09:19 wf Exp wf $+   * $Header: /home/wf/smaspot/RCS/sma2vz.php,v 1.2014/06/01 10:40:27 wf Exp wf $
    */    */
  
 // common code for reading and posting // common code for reading and posting
 require __DIR__.'/vzapihelper.php'; require __DIR__.'/vzapihelper.php';
-    
-   /** 
-    * 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? 
-    */ 
-   function youless2vz($url,$vzurl,$cuuid_pwr,$cuuid_cnt,$post_cnt) { 
-     // read data from youless 
-     $meter=readyouless($url); 
-     // get values  
-     $pwr=$meter["pwr"]; 
-     $cnt=$meter["cnt"]; 
-     // the level is not interesting for digital meters 
-     // if you have an analog meter you might want to use and show this 
-     // $lvl=$meter["lvl"]; 
  
-     // convert to php compatible value +  /** 
-     $cnt=str_replace(",",".",$cnt); +   * check the daytime values 
-     // calculate Wh from kWh +   *
-     $cnt1000=$cnt*1000+  function daytime($latitude,$longitude) { 
-     +    $result=array(); 
-     // get human readable time stamp  +    // 08:53 CEST 
-     $now=date("Y-m-d H:m:s");+    // $time_format 'H:i T'
 +    // 08:53 
 +    $time_format 'H:i';
  
-     // display progress (comment if you use this in cron job) +    // find time offset in hours 
-     echo "$now $cnt kWh $pwr Watt\n"+    $tzoffset = date("Z")/60 60;
-  +
-     // post data to middleware according to:  +
-     // http://wiki.volkszaehler.org/development/api/reference+
  
-     // adapt timestamp to volkszaehler conventions +    $zenith 90+(50/60); // True sunrise/sunset
-  $timestamp=time()*1000;+
  
-     # first post the power reading in Watt +    // determine sunrise time 
-     $posturl=$vzurl."/".$cuuid_pwr.".json"+    $sunrise date_sunrise(time(), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $tzoffset)
-     $fields=array("ts"=>$timestamp,"value" =$pwr ); +    $sunrise_time date($time_formatstrtotime(date("Y-m-d") . ' '. $sunrise)); 
-     $presult=postUrl($posturl,$fields);+    // determine sunset time 
 +    $sunset date_sunset(time(), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $tzoffset); 
 +    $sunset_time date($time_formatstrtotime(date("Y-m-d") . ' '$sunset));
  
-     # then the energy reading in Wh +    // check whether it's daytime 
-     if ($post_cnt) { +    $sunrise_epoch date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $zenith, $tzoffset)
-       $posturl=$vzurl."/".$cuuid_cnt.".json"+    $sunset_epoch  date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $zenith, $tzoffset); 
-       $fields=array("ts"=>$timestamp,"value" => $cnt1000 ); +    $time_epoch time(); // time now
-       $presult=postUrl($posturl,$fields); +
-     }+
  
-     // uncomment to debug +    $result["daytime"]=($time_epoch < $sunset_epoch and $time_epoch > $sunrise_epoch);
-     // echo $presult; +
-   }+
  
-  // modify the follwing parts to your needs +    $result["rise"] = $sunrise_time; 
-  // the url to read from +    $result["set"] = $sunset_time; 
-  $vzurl="http://capri/vz/middleware.php/data"; +    $result["time"] = date("Y-m-d H:i:s",time()); 
-  +    return $result; 
 +  }  
 + 
 +  $loop=0; 
 +  // possible command line options 
 +  // --vzurl= --cuuid_pwr= --cuuid_kwh= --daytimeonly 
 +  $longopts=array("vzurl:","cuuid_pwr:","cuuid_kwh:","lat:","lon:","daytimeonly:"); 
 +  $shortopts=""; 
 +  $options=getopt($shortopts,$longopts); 
 +  // the volkszaehler middleware url 
 +  $vzurl=checkoption("vzurl",$options);
   // channel uuids   // channel uuids
   // power (watts)   // power (watts)
-  $cuuid_pwr="7744bbf0-e74d-11e3-9ec7-xxxxx....";+  $cuuid_pwr=checkoption("cuuid_pwr",$options);
   // energy (kWh)   // energy (kWh)
-  $cuuid_kwh="460feba0-e74f-11e3-8a0d-xxxxx...."; +  $cuuid_kwh=checkoption("cuuid_kwh",$options)
 +  // daytimeonly?  
 +  $daytimeonly=checkoption("daytimeonly",$options); 
 +  if ($daytimeonly) { 
 +    $latitude=checkoption("lat",$options); 
 +    $longitude=checkoption("lon",$options); 
 +  } 
 +  
   $jsonlines=file("php://stdin");   $jsonlines=file("php://stdin");
   $tmeter=array();   $tmeter=array();
Zeile 645: Zeile 869:
     }     }
   }   }
-  var_dump($tmeter);+  #var_dump($tmeter); 
 +  //array(3) { ["etoday"]=> float(2.52) 
 +  // ["etotal"]=> float(6632.462) 
 +  //["totalpac"]=> float(2.478) } 
 +  $etotal=$tmeter["etotal"]; 
 +  $totalpac=$tmeter["totalpac"]*1000; 
 +  if ($daytimeonly) { 
 +    $daytime=daytime($latitude,$longitude); 
 +    if (!$daytime["daytime"]) { 
 +      printf("%s after sunset: %s wait for sunrise: %s\n",$daytime["time"],$daytime["set"],$daytime["rise"]); 
 +      exit(2);  
 +    } 
 +  }
   # total wattage of all inverters   # total wattage of all inverters
-  post2vz($vzurl,$cuuid_pwr,$tmeter["totalpac"]*1000);+  post2vz($vzurl,$cuuid_pwr,$totalpac);
   # total kwh of all inverters    # total kwh of all inverters 
-  post2vz($vzurl,$cuuid_kwh,$tmeter["etotal"]);+  post2vz($vzurl,$cuuid_kwh,$etotal); 
 +  if ($daytimeonly) { 
 +    printf("%s PV: % 5d Watt % 10.3f kwH sun rise:%s set:%s\n",$daytime["time"],$totalpac,$etotal,$daytime["rise"],$daytime["set"]);  
 +  } else { 
 +    printf("PV Current/Total: %4.0f Watt %.3f kwH\n",$totalpac,$etotal);  
 +  } 
 +  exit(0);
 ?> ?>
  
 </code> </code>
-<code php vzapihelper.php>+ 
 +<code php vzapihelper.php>
 <?php <?php
   /**   /**
    * vzapi helper functions    * vzapi helper functions
-   * $Header: /home/wf/youless/RCS/vzapihelper.php,v 1.2014/05/29 16:16:44 wf Exp $+   * $Header: /home/wf/youless/RCS/vzapihelper.php,v 1.2014/05/30 06:57:23 wf Exp $
    */    */
  
Zeile 726: Zeile 969:
      }      }
    }    }
 +
 +  /**
 +   * check that option $opt is available in $options
 +   * return the value if available
 +   */
 +  function checkoption($opt,$options) {
 +    if (array_key_exists($opt,$options))
 +      return $options[$opt];
 +    else
 +      die("option $opt missing!\n");
 +  }
  
 ?> ?>
  
 </code> </code>
howto/wechselrichter_sma.txt · Zuletzt geändert: 2018/04/01 15:56 von jau