77_SMAEM.pm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. ################################################################################################
  2. # $Id: 77_SMAEM.pm 15197 2017-10-04 18:45:25Z DS_Starter $
  3. #
  4. # Copyright notice
  5. #
  6. # (c) 2016 Copyright: Volker Kettenbach
  7. # e-mail: volker at kettenbach minus it dot de
  8. #
  9. # Credits:
  10. # - DS_Starter (Heiko) for persistent readings
  11. # and various improvements
  12. #
  13. # Description:
  14. # This is an FHEM-Module for the SMA Energy Meter,
  15. # a bidirectional energy meter/counter used in photovoltaics
  16. #
  17. # Requirements:
  18. # This module requires:
  19. # - Perl Module: IO::Socket::Multicast
  20. # On a Debian (based) system, these requirements can be fullfilled by:
  21. # - apt-get install install libio-socket-multicast-perl
  22. #
  23. # Origin:
  24. # https://github.com/kettenbach-it/FHEM-SMA-Speedwire
  25. #
  26. #################################################################################################
  27. # Versions History done by DS_Starter
  28. #
  29. # 3.0.0 29.09.2017 make SMAEM ready for multimeter usage
  30. # 2.9.1 29.05.2017 DbLog_splitFn added, some function names adapted
  31. # 2.9.0 25.05.2017 own SMAEM_setCacheValue, SMAEM_getCacheValue, new internal VERSION
  32. # 2.8.2 03.12.2016 Prefix SMAEMserialnumber for Reading "state" removed, commandref adapted
  33. # 2.8.1 02.12.2016 encode / decode $data
  34. # 2.8 02.12.2016 plausibility check of measured differences, attr diffAccept, timeout
  35. # validation checks, improvement of failure prevention
  36. # 2.7 01.12.2016 logging of discarded cycles
  37. # 2.6 01.12.2016 some improvements, better logging possibility
  38. # 2.5 30.11.2016 some improvements
  39. # 2.4 30.11.2016 some improvements, attributes disable, timeout for BlockingCall added
  40. # 2.3 30.11.2016 SMAEM_getsum, SMAEM_setsum changed
  41. # 2.2 29.11.2016 check error while writing values to file -> set state with error
  42. # 2.1 29.11.2016 move $hash->{GRIDin_SUM}, $hash->{GRIDOUT_SUM} calc to smaread_ParseDone,
  43. # some little improvements to logging process
  44. # 2.0 28.11.2016 switch to nonblocking
  45. package main;
  46. use strict;
  47. use warnings;
  48. use bignum;
  49. use IO::Socket::Multicast;
  50. use Blocking;
  51. my $SMAEMVersion = "3.0.0";
  52. ###############################################################
  53. # SMAEM Initialize
  54. ###############################################################
  55. sub SMAEM_Initialize($) {
  56. my ($hash) = @_;
  57. $hash->{ReadFn} = "SMAEM_Read";
  58. $hash->{DefFn} = "SMAEM_Define";
  59. $hash->{UndefFn} = "SMAEM_Undef";
  60. $hash->{DeleteFn} = "SMAEM_Delete";
  61. $hash->{DbLog_splitFn} = "SMAEM_DbLog_splitFn";
  62. $hash->{AttrFn} = "SMAEM_Attr";
  63. $hash->{AttrList} = "interval ".
  64. "disable:1,0 ".
  65. "diffAccept ".
  66. "disableSernoInReading:1,0 ".
  67. "feedinPrice ".
  68. "powerCost ".
  69. "timeout ".
  70. "$readingFnAttributes";
  71. }
  72. ###############################################################
  73. # SMAEM Define
  74. ###############################################################
  75. sub SMAEM_Define($$) {
  76. my ($hash, $def) = @_;
  77. my $name= $hash->{NAME};
  78. my ($success, $gridin_sum, $gridout_sum);
  79. $hash->{INTERVAL} = 60 ;
  80. $hash->{VERSION} = $SMAEMVersion;
  81. $hash->{HELPER}{FAULTEDCYCLES} = 0;
  82. $hash->{HELPER}{STARTTIME} = time();
  83. Log3 $hash, 3, "SMAEM $name - Opening multicast socket...";
  84. my $socket = IO::Socket::Multicast->new(
  85. Proto => 'udp',
  86. LocalPort => '9522',
  87. ReuseAddr => '1',
  88. ReusePort => defined(&ReusePort) ? 1 : 0,
  89. ) or return "Can't bind : $@";
  90. $socket->mcast_add('239.12.255.254');
  91. $hash->{TCPDev}= $socket;
  92. $hash->{FD} = $socket->fileno();
  93. delete($readyfnlist{"$name"});
  94. $selectlist{"$name"} = $hash;
  95. # gespeicherte Serialnummern lesen und extrahieren
  96. my $retcode = SMAEM_getserials($hash);
  97. $hash->{HELPER}{READFILEERROR} = $retcode if($retcode);
  98. if($hash->{HELPER}{ALLSERIALS}) {
  99. my @allserials = split(/_/,$hash->{HELPER}{ALLSERIALS});
  100. foreach(@allserials) {
  101. my $smaserial = $_;
  102. # gespeicherte Energiezählerwerte von File einlesen
  103. my $retcode = SMAEM_getsum($hash,$smaserial);
  104. $hash->{HELPER}{READFILEERROR} = $retcode if($retcode);
  105. }
  106. }
  107. return undef;
  108. }
  109. ###############################################################
  110. # SMAEM Undefine
  111. ###############################################################
  112. sub SMAEM_Undef($$) {
  113. my ($hash, $arg) = @_;
  114. my $name= $hash->{NAME};
  115. my $socket= $hash->{TCPDev};
  116. BlockingKill($hash->{HELPER}{RUNNING_PID}) if(defined($hash->{HELPER}{RUNNING_PID}));
  117. Log3 $hash, 3, "SMAEM $name - Closing multicast socket...";
  118. $socket->mcast_drop('239.12.255.254');
  119. my $ret = close($hash->{TCPDev});
  120. Log3 $hash, 4, "SMAEM $name - Close-ret: $ret";
  121. delete($hash->{TCPDev});
  122. delete($selectlist{"$name"});
  123. delete($hash->{FD});
  124. return;
  125. }
  126. ###############################################################
  127. # SMAEM Delete
  128. ###############################################################
  129. sub SMAEM_Delete {
  130. my ($hash, $arg) = @_;
  131. my $index = $hash->{TYPE}."_".$hash->{NAME}."_energysum";
  132. # gespeicherte Energiezählerwerte löschen
  133. setKeyValue($index, undef);
  134. return undef;
  135. }
  136. ###############################################################
  137. # SMAEM Attr
  138. ###############################################################
  139. sub SMAEM_Attr {
  140. my ($cmd,$name,$aName,$aVal) = @_;
  141. my $hash = $defs{$name};
  142. my $do;
  143. # $cmd can be "del" or "set"
  144. # $name is device name
  145. # aName and aVal are Attribute name and value
  146. if ($aName eq "interval") {
  147. if($cmd eq "set") {
  148. $hash->{INTERVAL} = $aVal;
  149. } else {
  150. $hash->{INTERVAL} = "60";
  151. }
  152. }
  153. if ($aName eq "disableSernoInReading") {
  154. delete $defs{$name}{READINGS};
  155. readingsSingleUpdate($hash, "state", "initialized", 1);
  156. }
  157. if ($aName eq "timeout" || $aName eq "diffAccept") {
  158. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  159. }
  160. if ($aName eq "disable") {
  161. if($cmd eq "set") {
  162. $do = ($aVal) ? 1 : 0;
  163. }
  164. $do = 0 if($cmd eq "del");
  165. my $val = ($do == 1 ? "disabled" : "initialized");
  166. readingsSingleUpdate($hash, "state", $val, 1);
  167. }
  168. return undef;
  169. }
  170. ###############################################################
  171. # SMAEM Read (Hauptschleife)
  172. ###############################################################
  173. # called from the global loop, when the select for hash->{FD} reports data
  174. sub SMAEM_Read($) {
  175. my ($hash) = @_;
  176. my $name= $hash->{NAME};
  177. my $socket= $hash->{TCPDev};
  178. my $timeout = AttrVal($name, "timeout", 60);
  179. my $data;
  180. return if(IsDisabled($name));
  181. return unless $socket->recv($data, 600); # Each SMAEM packet is 600 bytes of packed payload
  182. return if (time() <= $hash->{HELPER}{STARTTIME}+30);
  183. # decode serial number of dataset received
  184. my $hex = unpack('H*', $data);
  185. my $smaserial = hex(substr($hex,40,8));
  186. return if(!$smaserial);
  187. # alle Serialnummern in HELPER sammeln und ggf. speichern
  188. if(!defined($hash->{HELPER}{ALLSERIALS}) || $hash->{HELPER}{ALLSERIALS} !~ /$smaserial/) {
  189. my $sep = $hash->{HELPER}{ALLSERIALS}?"_":undef;
  190. if($sep) {
  191. $hash->{HELPER}{ALLSERIALS} = $hash->{HELPER}{ALLSERIALS}.$sep.$smaserial;
  192. } else {
  193. $hash->{HELPER}{ALLSERIALS} = $smaserial;
  194. }
  195. SMAEM_setserials($hash);
  196. }
  197. if ( !$hash->{HELPER}{'LASTUPDATE_'.$smaserial} || time() >= ($hash->{HELPER}{'LASTUPDATE_'.$smaserial}+$hash->{INTERVAL}) ) {
  198. Log3 ($name, 4, "SMAEM $name - ##############################################################");
  199. Log3 ($name, 4, "SMAEM $name - ### Begin of new SMA Energymeter $smaserial get data cycle ###");
  200. Log3 ($name, 4, "SMAEM $name - ##############################################################");
  201. Log3 ($name, 4, "SMAEM $name - discarded cycles since module start: $hash->{HELPER}{FAULTEDCYCLES}");
  202. if($hash->{HELPER}{RUNNING_PID}) {
  203. Log3 ($name, 3, "SMAEM $name - WARNING - old process $hash->{HELPER}{RUNNING_PID}{pid} has been killed to start a new BlockingCall");
  204. BlockingKill($hash->{HELPER}{RUNNING_PID});
  205. delete($hash->{HELPER}{RUNNING_PID});
  206. }
  207. # update time
  208. SMAEM_setlastupdate($hash,$smaserial);
  209. my $dataenc = encode_base64($data,"");
  210. $hash->{HELPER}{RUNNING_PID} = BlockingCall("SMAEM_DoParse", "$name|$dataenc|$smaserial", "SMAEM_ParseDone", $timeout, "SMAEM_ParseAborted", $hash);
  211. Log3 ($name, 4, "SMAEM $name - Blocking process with PID: $hash->{HELPER}{RUNNING_PID}{pid} started");
  212. } else {
  213. Log3 $hash, 5, "SMAEM $name: - received " . length($data) . " bytes but interval $hash->{INTERVAL}s isn't expired.";
  214. }
  215. return undef;
  216. }
  217. ###############################################################
  218. # non-blocking Inverter Datenabruf
  219. ###############################################################
  220. sub SMAEM_DoParse($) {
  221. my ($string) = @_;
  222. my ($name,$dataenc,$smaserial) = split("\\|", $string);
  223. my $hash = $defs{$name};
  224. my $data = decode_base64($dataenc);
  225. my $discycles = $hash->{HELPER}{FAULTEDCYCLES};
  226. my $diffaccept = AttrVal($name, "diffAccept", 10);
  227. my @row_array;
  228. my @array;
  229. Log3 ($name, 4, "SMAEM $name -> Start BlockingCall SMAEM_DoParse");
  230. my $gridinsum = $hash->{'GRIDIN_SUM_'.$smaserial} ?sprintf("%.4f",$hash->{'GRIDIN_SUM_'.$smaserial}):'';
  231. my $gridoutsum = $hash->{'GRIDOUT_SUM_'.$smaserial}?sprintf("%.4f",$hash->{'GRIDOUT_SUM_'.$smaserial}):'';
  232. # check if cacheSMAEM-file has been opened at module start and try again if not
  233. if($hash->{HELPER}{READFILEERROR}) {
  234. my $retcode = SMAEM_getsum($hash,$smaserial);
  235. if ($retcode) {
  236. my $error = encode_base64($retcode,"");
  237. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_DoParse finished");
  238. $discycles++;
  239. return "$name|''|''|''|$error|$discycles|''";
  240. } else {
  241. delete($hash->{HELPER}{READFILEERROR})
  242. }
  243. }
  244. # Format of the udp packets of the SMAEM:
  245. # http://www.sma.de/fileadmin/content/global/Partner/Documents/SMA_Labs/EMETER-Protokoll-TI-de-10.pdf
  246. # http://www.eb-systeme.de/?page_id=1240
  247. # Conversion like in this python code:
  248. # http://www.unifox.at/sma_energy_meter/
  249. # https://github.com/datenschuft/SMA-EM
  250. # unpack big-endian to 2-digit hex (bin2hex)
  251. my $hex = unpack('H*', $data);
  252. ################ Aufbau Ergebnis-Array ####################
  253. # Extract datasets from hex:
  254. # Generic:
  255. my $susyid = hex(substr($hex,36,4));
  256. # $smaserial = hex(substr($hex,40,8));
  257. my $milliseconds = hex(substr($hex,48,8));
  258. # Prestring with SMAEM and SERIALNO or not
  259. my $ps = (!AttrVal($name, "disableSernoInReading", undef)) ? "SMAEM".$smaserial."_" : "";
  260. # Counter Divisor: [Hex-Value]=Ws => Ws/1000*3600=kWh => divide by 3600000
  261. # Sum L1-3
  262. my $bezug_wirk = hex(substr($hex,64,8))/10;
  263. my $bezug_wirk_count = hex(substr($hex,80,16))/3600000;
  264. my $einspeisung_wirk = hex(substr($hex,104,8))/10;
  265. my $einspeisung_wirk_count = hex(substr($hex,120,16))/3600000;
  266. # calculation of GRID-hashes and persist to file
  267. Log3 ($name, 4, "SMAEM $name - old GRIDIN_SUM_$smaserial got from RAM: $gridinsum");
  268. Log3 ($name, 4, "SMAEM $name - old GRIDOUT_SUM_$smaserial got from RAM: $gridoutsum");
  269. my $plausibility_out = 0;
  270. if( !$gridoutsum || ($bezug_wirk_count && $bezug_wirk_count < $gridoutsum) ) {
  271. $gridoutsum = $bezug_wirk_count;
  272. Log3 ($name, 4, "SMAEM $name - gridoutsum_$smaserial new set: $gridoutsum");
  273. } else {
  274. if ($gridoutsum && $bezug_wirk_count >= $gridoutsum) {
  275. if(($bezug_wirk_count - $gridoutsum) <= $diffaccept) {
  276. # Plausibilitätscheck ob Differenz kleiner als erlaubter Wert -> Fehlerprävention
  277. my $diffb = ($bezug_wirk_count - $gridoutsum)>0 ? sprintf("%.4f",$bezug_wirk_count - $gridoutsum) : 0;
  278. Log3 ($name, 4, "SMAEM $name - bezug_wirk_count: $bezug_wirk_count");
  279. Log3 ($name, 4, "SMAEM $name - gridoutsum_$smaserial: $gridoutsum");
  280. Log3 ($name, 4, "SMAEM $name - diffb: $diffb");
  281. $gridoutsum = $bezug_wirk_count;
  282. push(@row_array, $ps."Bezug_WirkP_Zaehler_Diff ".$diffb."\n");
  283. push(@row_array, $ps."Bezug_WirkP_Kosten_Diff ".sprintf("%.4f", $diffb*AttrVal($name, "powerCost", 0))."\n");
  284. $plausibility_out = 1;
  285. } else {
  286. # Zyklus verwerfen wenn Plausibilität nicht erfüllt
  287. my $errtxt = "cycle discarded due to allowed diff GRIDOUT exceeding";
  288. my $error = encode_base64($errtxt,"");
  289. Log3 ($name, 1, "SMAEM $name - $errtxt");
  290. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_DoParse finished");
  291. $gridinsum = $einspeisung_wirk_count;
  292. $gridoutsum = $bezug_wirk_count;
  293. $discycles++;
  294. return "$name|''|$gridinsum|$gridoutsum|''|$discycles|''";
  295. }
  296. }
  297. }
  298. my $plausibility_in = 0;
  299. if( !$gridinsum || ($einspeisung_wirk_count && $einspeisung_wirk_count < $gridinsum) ) {
  300. $gridinsum = $einspeisung_wirk_count;
  301. Log3 ($name, 4, "SMAEM $name - gridinsum_$smaserial new set: $gridinsum");
  302. } else {
  303. if ($gridinsum && $einspeisung_wirk_count >= $gridinsum) {
  304. if(($einspeisung_wirk_count - $gridinsum) <= $diffaccept) {
  305. # Plausibilitätscheck ob Differenz kleiner als erlaubter Wert -> Fehlerprävention
  306. my $diffe = ($einspeisung_wirk_count - $gridinsum)>0 ? sprintf("%.4f",$einspeisung_wirk_count - $gridinsum) : 0;
  307. Log3 ($name, 4, "SMAEM $name - einspeisung_wirk_count: $einspeisung_wirk_count");
  308. Log3 ($name, 4, "SMAEM $name - gridinsum_$smaserial: $gridinsum");
  309. Log3 ($name, 4, "SMAEM $name - diffe: $diffe");
  310. $gridinsum = $einspeisung_wirk_count;
  311. push(@row_array, $ps."Einspeisung_WirkP_Zaehler_Diff ".$diffe."\n");
  312. push(@row_array, $ps."Einspeisung_WirkP_Verguet_Diff ".sprintf("%.4f", $diffe*AttrVal($name, "feedinPrice", 0))."\n");
  313. $plausibility_in = 1;
  314. } else {
  315. # Zyklus verwerfen wenn Plausibilität nicht erfüllt
  316. my $errtxt = "cycle discarded due to allowed diff GRIDIN exceeding";
  317. my $error = encode_base64($errtxt,"");
  318. Log3 ($name, 1, "SMAEM $name - $errtxt");
  319. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_DoParse finished");
  320. $gridinsum = $einspeisung_wirk_count;
  321. $gridoutsum = $bezug_wirk_count;
  322. $discycles++;
  323. return "$name|''|$gridinsum|$gridoutsum|''|$discycles|''";
  324. }
  325. }
  326. }
  327. # write GRIDIN_SUM and GRIDOUT_SUM to file if plausibility check ok
  328. Log3 ($name, 4, "SMAEM $name - plausibility check done: GRIDIN -> $plausibility_in, GRIDOUT -> $plausibility_out");
  329. my $retcode = SMAEM_setsum($hash,$smaserial,$gridinsum,$gridoutsum) if($plausibility_in && $plausibility_out);
  330. # error while writing values to file
  331. if ($retcode) {
  332. my $error = encode_base64($retcode,"");
  333. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_DoParse finished");
  334. $discycles++;
  335. return "$name|''|''|''|$error|$discycles|''";
  336. }
  337. push(@row_array, "state ".sprintf("%.1f", $einspeisung_wirk-$bezug_wirk)."\n");
  338. push(@row_array, $ps."Saldo_Wirkleistung ".sprintf("%.1f",$einspeisung_wirk-$bezug_wirk)."\n");
  339. push(@row_array, $ps."Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$einspeisung_wirk_count-$bezug_wirk_count)."\n");
  340. push(@row_array, $ps."Bezug_Wirkleistung ".sprintf("%.1f",$bezug_wirk)."\n");
  341. push(@row_array, $ps."Bezug_Wirkleistung_Zaehler ".sprintf("%.4f",$bezug_wirk_count)."\n");
  342. push(@row_array, $ps."Einspeisung_Wirkleistung ".sprintf("%.1f",$einspeisung_wirk)."\n");
  343. push(@row_array, $ps."Einspeisung_Wirkleistung_Zaehler ".sprintf("%.4f",$einspeisung_wirk_count)."\n");
  344. my $bezug_blind=hex(substr($hex,144,8))/10;
  345. my $bezug_blind_count=hex(substr($hex,160,16))/3600000;
  346. my $einspeisung_blind=hex(substr($hex,184,8))/10;
  347. my $einspeisung_blind_count=hex(substr($hex,200,16))/3600000;
  348. push(@row_array, $ps."Bezug_Blindleistung ".sprintf("%.1f",$bezug_blind)."\n");
  349. push(@row_array, $ps."Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$bezug_blind_count)."\n");
  350. push(@row_array, $ps."Einspeisung_Blindleistung ".sprintf("%.1f",$einspeisung_blind)."\n");
  351. push(@row_array, $ps."Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$einspeisung_blind_count)."\n");
  352. my $bezug_schein=hex(substr($hex,224,8))/10;
  353. my $bezug_schein_count=hex(substr($hex,240,16))/3600000;
  354. my $einspeisung_schein=hex(substr($hex,264,8))/10;
  355. my $einspeisung_schein_count=hex(substr($hex,280,16))/3600000;
  356. push(@row_array, $ps."Bezug_Scheinleistung ".sprintf("%.1f",$bezug_schein)."\n");
  357. push(@row_array, $ps."Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$bezug_schein_count)."\n");
  358. push(@row_array, $ps."Einspeisung_Scheinleistung ".sprintf("%.1f",$einspeisung_schein)."\n");
  359. push(@row_array, $ps."Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$einspeisung_schein_count)."\n");
  360. my $cosphi=hex(substr($hex,304,8))/1000;
  361. push(@row_array, $ps."CosPhi ".sprintf("%.3f",$cosphi)."\n");
  362. # L1
  363. my $l1_bezug_wirk=hex(substr($hex,320,8))/10;
  364. my $l1_bezug_wirk_count=hex(substr($hex,336,16))/3600000;
  365. my $l1_einspeisung_wirk=hex(substr($hex,360,8))/10;
  366. my $l1_einspeisung_wirk_count=hex(substr($hex,376,16))/3600000;
  367. push(@row_array, $ps."L1_Saldo_Wirkleistung ".sprintf("%.1f",$l1_einspeisung_wirk-$l1_bezug_wirk)."\n");
  368. push(@row_array, $ps."L1_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_wirk_count-$l1_bezug_wirk_count)."\n");
  369. push(@row_array, $ps."L1_Bezug_Wirkleistung ".sprintf("%.1f",$l1_bezug_wirk)."\n");
  370. push(@row_array, $ps."L1_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_bezug_wirk_count)."\n");
  371. push(@row_array, $ps."L1_Einspeisung_Wirkleistung ".sprintf("%.1f",$l1_einspeisung_wirk)."\n");
  372. push(@row_array, $ps."L1_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_wirk_count)."\n");
  373. my $l1_bezug_blind=hex(substr($hex,400,8))/10;
  374. my $l1_bezug_blind_count=hex(substr($hex,416,16))/3600000;
  375. my $l1_einspeisung_blind=hex(substr($hex,440,8))/10;
  376. my $l1_einspeisung_blind_count=hex(substr($hex,456,16))/3600000;
  377. push(@row_array, $ps."L1_Bezug_Blindleistung ".sprintf("%.1f",$l1_bezug_blind)."\n");
  378. push(@row_array, $ps."L1_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l1_bezug_blind_count)."\n");
  379. push(@row_array, $ps."L1_Einspeisung_Blindleistung ".sprintf("%.1f",$l1_einspeisung_blind)."\n");
  380. push(@row_array, $ps."L1_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_blind_count)."\n");
  381. my $l1_bezug_schein=hex(substr($hex,480,8))/10;
  382. my $l1_bezug_schein_count=hex(substr($hex,496,16))/3600000;
  383. my $l1_einspeisung_schein=hex(substr($hex,520,8))/10;
  384. my $l1_einspeisung_schein_count=hex(substr($hex,536,16))/3600000;
  385. push(@row_array, $ps."L1_Bezug_Scheinleistung ".sprintf("%.1f",$l1_bezug_schein)."\n");
  386. push(@row_array, $ps."L1_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l1_bezug_schein_count)."\n");
  387. push(@row_array, $ps."L1_Einspeisung_Scheinleistung ".sprintf("%.1f",$l1_einspeisung_schein)."\n");
  388. push(@row_array, $ps."L1_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_schein_count)."\n");
  389. my $l1_thd=hex(substr($hex,560,8))/1000;
  390. my $l1_v=hex(substr($hex,576,8))/1000;
  391. my $l1_cosphi=hex(substr($hex,592,8))/1000;
  392. push(@row_array, $ps."L1_THD ".sprintf("%.2f",$l1_thd)."\n");
  393. push(@row_array, $ps."L1_Spannung ".sprintf("%.1f",$l1_v)."\n");
  394. push(@row_array, $ps."L1_CosPhi ".sprintf("%.3f",$l1_cosphi)."\n");
  395. # L2
  396. my $l2_bezug_wirk=hex(substr($hex,608,8))/10;
  397. my $l2_bezug_wirk_count=hex(substr($hex,624,16))/3600000;
  398. my $l2_einspeisung_wirk=hex(substr($hex,648,8))/10;
  399. my $l2_einspeisung_wirk_count=hex(substr($hex,664,16))/3600000;
  400. push(@row_array, $ps."L2_Saldo_Wirkleistung ".sprintf("%.1f",$l2_einspeisung_wirk-$l2_bezug_wirk)."\n");
  401. push(@row_array, $ps."L2_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_wirk_count-$l2_bezug_wirk_count)."\n");
  402. push(@row_array, $ps."L2_Bezug_Wirkleistung ".sprintf("%.1f",$l2_bezug_wirk)."\n");
  403. push(@row_array, $ps."L2_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_bezug_wirk_count)."\n");
  404. push(@row_array, $ps."L2_Einspeisung_Wirkleistung ".sprintf("%.1f",$l2_einspeisung_wirk)."\n");
  405. push(@row_array, $ps."L2_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_wirk_count)."\n");
  406. my $l2_bezug_blind=hex(substr($hex,688,8))/10;
  407. my $l2_bezug_blind_count=hex(substr($hex,704,16))/3600000;
  408. my $l2_einspeisung_blind=hex(substr($hex,728,8))/10;
  409. my $l2_einspeisung_blind_count=hex(substr($hex,744,16))/3600000;
  410. push(@row_array, $ps."L2_Bezug_Blindleistung ".sprintf("%.1f",$l2_bezug_blind)."\n");
  411. push(@row_array, $ps."L2_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l2_bezug_blind_count)."\n");
  412. push(@row_array, $ps."L2_Einspeisung_Blindleistung ".sprintf("%.1f",$l2_einspeisung_blind)."\n");
  413. push(@row_array, $ps."L2_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_blind_count)."\n");
  414. my $l2_bezug_schein=hex(substr($hex,768,8))/10;
  415. my $l2_bezug_schein_count=hex(substr($hex,784,16))/3600000;
  416. my $l2_einspeisung_schein=hex(substr($hex,808,8))/10;
  417. my $l2_einspeisung_schein_count=hex(substr($hex,824,16))/3600000;
  418. push(@row_array, $ps."L2_Bezug_Scheinleistung ".sprintf("%.1f",$l2_bezug_schein)."\n");
  419. push(@row_array, $ps."L2_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l2_bezug_schein_count)."\n");
  420. push(@row_array, $ps."L2_Einspeisung_Scheinleistung ".sprintf("%.1f",$l2_einspeisung_schein)."\n");
  421. push(@row_array, $ps."L2_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_schein_count)."\n");
  422. my $l2_thd=hex(substr($hex,848,8))/1000;
  423. my $l2_v=hex(substr($hex,864,8))/1000;
  424. my $l2_cosphi=hex(substr($hex,880,8))/1000;
  425. push(@row_array, $ps."L2_THD ".sprintf("%.2f",$l2_thd)."\n");
  426. push(@row_array, $ps."L2_Spannung ".sprintf("%.1f",$l2_v)."\n");
  427. push(@row_array, $ps."L2_CosPhi ".sprintf("%.3f",$l2_cosphi)."\n");
  428. # L3
  429. my $l3_bezug_wirk=hex(substr($hex,896,8))/10;
  430. my $l3_bezug_wirk_count=hex(substr($hex,912,16))/3600000;
  431. my $l3_einspeisung_wirk=hex(substr($hex,936,8))/10;
  432. my $l3_einspeisung_wirk_count=hex(substr($hex,952,16))/3600000;
  433. push(@row_array, $ps."L3_Saldo_Wirkleistung ".sprintf("%.1f",$l3_einspeisung_wirk-$l3_bezug_wirk)."\n");
  434. push(@row_array, $ps."L3_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_wirk_count-$l3_bezug_wirk_count)."\n");
  435. push(@row_array, $ps."L3_Bezug_Wirkleistung ".sprintf("%.1f",$l3_bezug_wirk)."\n");
  436. push(@row_array, $ps."L3_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_bezug_wirk_count)."\n");
  437. push(@row_array, $ps."L3_Einspeisung_Wirkleistung ".sprintf("%.1f",$l3_einspeisung_wirk)."\n");
  438. push(@row_array, $ps."L3_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_wirk_count)."\n");
  439. my $l3_bezug_blind=hex(substr($hex,976,8))/10;
  440. my $l3_bezug_blind_count=hex(substr($hex,992,16))/3600000;
  441. my $l3_einspeisung_blind=hex(substr($hex,1016,8))/10;
  442. my $l3_einspeisung_blind_count=hex(substr($hex,1032,16))/3600000;
  443. push(@row_array, $ps."L3_Bezug_Blindleistung ".sprintf("%.1f",$l3_bezug_blind)."\n");
  444. push(@row_array, $ps."L3_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l3_bezug_blind_count)."\n");
  445. push(@row_array, $ps."L3_Einspeisung_Blindleistung ".sprintf("%.1f",$l3_einspeisung_blind)."\n");
  446. push(@row_array, $ps."L3_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_blind_count)."\n");
  447. my $l3_bezug_schein=hex(substr($hex,1056,8))/10;
  448. my $l3_bezug_schein_count=hex(substr($hex,1072,16))/3600000;
  449. my $l3_einspeisung_schein=hex(substr($hex,1096,8))/10;
  450. my $l3_einspeisung_schein_count=hex(substr($hex,1112,16))/3600000;
  451. push(@row_array, $ps."L3_Bezug_Scheinleistung ".sprintf("%.1f",$l3_bezug_schein)."\n");
  452. push(@row_array, $ps."L3_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l3_bezug_schein_count)."\n");
  453. push(@row_array, $ps."L3_Einspeisung_Scheinleistung ".sprintf("%.1f",$l3_einspeisung_schein)."\n");
  454. push(@row_array, $ps."L3_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_schein_count)."\n");
  455. my $l3_thd=hex(substr($hex,1136,8))/1000;
  456. my $l3_v=hex(substr($hex,1152,8))/1000;
  457. my $l3_cosphi=hex(substr($hex,1168,8))/1000;
  458. push(@row_array, $ps."L3_THD ".sprintf("%.2f",$l3_thd)."\n");
  459. push(@row_array, $ps."L3_Spannung ".sprintf("%.1f",$l3_v)."\n");
  460. push(@row_array, $ps."L3_CosPhi ".sprintf("%.3f",$l3_cosphi)."\n");
  461. Log3 ($name, 5, "$name - row_array before encoding:");
  462. foreach my $row (@row_array) {
  463. chomp $row;
  464. Log3 ($name, 5, "SMAEM $name - $row");
  465. }
  466. # encoding result
  467. my $rowlist = join('|', @row_array);
  468. $rowlist = encode_base64($rowlist,"");
  469. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_DoParse finished");
  470. return "$name|$rowlist|$gridinsum|$gridoutsum|''|$discycles|$smaserial";
  471. }
  472. ###############################################################
  473. # Auswertung non-blocking Inverter Datenabruf
  474. ###############################################################
  475. sub SMAEM_ParseDone ($) {
  476. my ($string) = @_;
  477. my @a = split("\\|",$string);
  478. my $name = $a[0];
  479. my $hash = $defs{$name};
  480. my $rowlist = decode_base64($a[1]);
  481. my $gridinsum = $a[2];
  482. my $gridoutsum = $a[3];
  483. my $error = decode_base64($a[4]) if($a[4]);
  484. my $discycles = $a[5];
  485. my $smaserial = $a[6];
  486. Log3 ($name, 4, "SMAEM $name -> Start BlockingCall SMAEM_ParseDone");
  487. $hash->{HELPER}{FAULTEDCYCLES} = $discycles;
  488. # update time
  489. SMAEM_setlastupdate($hash,$smaserial);
  490. if ($error) {
  491. readingsSingleUpdate($hash, "state", $error, 1);
  492. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_ParseDone finished");
  493. delete($hash->{HELPER}{RUNNING_PID});
  494. return;
  495. }
  496. $hash->{'GRIDIN_SUM_'.$smaserial} = $gridinsum;
  497. $hash->{'GRIDOUT_SUM_'.$smaserial} = $gridoutsum;
  498. Log3($name, 4, "SMAEM $name - wrote new energy values to INTERNALS - GRIDIN_SUM_$smaserial: $gridinsum, GRIDOUT_SUM_$smaserial: $gridoutsum");
  499. my @row_array = split("\\|", $rowlist);
  500. Log3 ($name, 5, "SMAEM $name - row_array after decoding:");
  501. foreach my $row (@row_array) {
  502. chomp $row;
  503. Log3 ($name, 5, "SMAEM $name - $row");
  504. }
  505. readingsBeginUpdate($hash);
  506. foreach my $row (@row_array) {
  507. chomp $row;
  508. my @a = split(" ", $row, 2);
  509. readingsBulkUpdate($hash, $a[0], $a[1]);
  510. }
  511. readingsEndUpdate($hash, 1);
  512. delete($hash->{HELPER}{RUNNING_PID});
  513. Log3 ($name, 4, "SMAEM $name -> BlockingCall SMAEM_ParseDone finished");
  514. return;
  515. }
  516. ###############################################################
  517. # Abbruchroutine Timeout Inverter Abfrage
  518. ###############################################################
  519. sub SMAEM_ParseAborted($) {
  520. my ($hash) = @_;
  521. my $name = $hash->{NAME};
  522. my $discycles = $hash->{HELPER}{FAULTEDCYCLES};
  523. $discycles++;
  524. $hash->{HELPER}{FAULTEDCYCLES} = $discycles;
  525. Log3 ($name, 1, "SMAEM $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} timed out");
  526. readingsSingleUpdate($hash, "state", "timeout", 1);
  527. delete($hash->{HELPER}{RUNNING_PID});
  528. }
  529. ###############################################################
  530. # DbLog_splitFn
  531. ###############################################################
  532. sub SMAEM_DbLog_splitFn($) {
  533. my ($event,$device) = @_;
  534. my ($reading, $value, $unit) = "";
  535. # Log3 ($device, 5, "SMAEM $device - splitFn splits event: ".$event);
  536. my @parts = split(/ /,$event,3);
  537. $reading = $parts[0];
  538. $reading =~ tr/://d;
  539. $value = $parts[1];
  540. if($reading =~ m/.*leistung$/) {
  541. $unit = 'W';
  542. } elsif($reading =~ m/.*Spannung/) {
  543. $unit = 'V';
  544. } elsif($reading =~ m/.*leistung_Zaehler$/) {
  545. $unit = 'kWh';
  546. } elsif($reading =~ m/.*THD$/) {
  547. $unit = '%';
  548. } else {
  549. if(!defined($parts[1])) {
  550. $reading = "state";
  551. $value = $event;
  552. $unit = 'W';
  553. } else {
  554. $value = $parts[1];
  555. $value = $value." ".$parts[2] if(defined($parts[2]));
  556. }
  557. }
  558. Log3 ($device, 5, "SMAEM $device - splitFn returns Reading: ".$reading.", Value: ".
  559. defined($value)?$value:''.", Unit: ".defined($unit)?$unit:'');
  560. return ($reading, $value, $unit);
  561. }
  562. ###############################################################
  563. # Hilfsroutinen
  564. ###############################################################
  565. ###############################################################
  566. ### alle Serial-Nummern in cacheSMAEM speichern
  567. sub SMAEM_setserials ($) {
  568. my ($hash) = @_;
  569. my $name = $hash->{NAME};
  570. my ($index,$retcode,$as);
  571. my $modpath = AttrVal("global", "modpath", undef);
  572. $as = $hash->{HELPER}{ALLSERIALS};
  573. $index = $hash->{TYPE}."_".$hash->{NAME}."_allserials";
  574. $retcode = SMAEM_setCacheValue($index,$as);
  575. if ($retcode) {
  576. Log3($name, 1, "SMAEM $name - ERROR while saving all serial numbers - $retcode");
  577. } else {
  578. Log3($name, 4, "SMAEM $name - all serial numbers were saved to $modpath/FHEM/FhemUtils/cacheSMAEM");
  579. }
  580. return ($retcode);
  581. }
  582. ###############################################################
  583. ### Summenwerte für GridIn, GridOut speichern
  584. sub SMAEM_setsum ($$$$) {
  585. my ($hash,$smaserial,$gridinsum,$gridoutsum) = @_;
  586. my $name = $hash->{NAME};
  587. my ($index,$retcode,$sumstr);
  588. my $modpath = AttrVal("global", "modpath", undef);
  589. $sumstr = $gridinsum."_".$gridoutsum;
  590. $index = $hash->{TYPE}."_".$hash->{NAME}."_".$smaserial;
  591. $retcode = SMAEM_setCacheValue($index,$sumstr);
  592. if ($retcode) {
  593. Log3($name, 1, "SMAEM $name - ERROR while saving summary of energy values - $retcode");
  594. } else {
  595. Log3($name, 4, "SMAEM $name - new energy values saved to $modpath/FHEM/FhemUtils/cacheSMAEM");
  596. Log3($name, 4, "SMAEM $name - GRIDIN_SUM_$smaserial: $gridinsum, GRIDOUT_SUM_$smaserial: $gridoutsum");
  597. }
  598. return ($retcode);
  599. }
  600. ###############################################################
  601. ### Schreibroutine in eigenes Keyvalue-File
  602. sub SMAEM_setCacheValue($$) {
  603. my ($key,$value) = @_;
  604. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheSMAEM";
  605. my $param = {
  606. FileName => $fName,
  607. ForceType => "file",
  608. };
  609. my ($err, @old) = FileRead($param);
  610. SMAEM_createCacheFile() if($err);
  611. my @new;
  612. my $fnd;
  613. foreach my $l (@old) {
  614. if($l =~ m/^$key:/) {
  615. $fnd = 1;
  616. push @new, "$key:$value" if(defined($value));
  617. } else {
  618. push @new, $l;
  619. }
  620. }
  621. push @new, "$key:$value" if(!$fnd && defined($value));
  622. return FileWrite($param, @new);
  623. }
  624. ###############################################################
  625. ### gespeicherte Serial-Nummern auslesen
  626. sub SMAEM_getserials ($) {
  627. my ($hash) = @_;
  628. my $name = $hash->{NAME};
  629. my ($index,$retcode,$serials);
  630. my $modpath = AttrVal("global", "modpath", undef);
  631. $index = $hash->{TYPE}."_".$hash->{NAME}."_allserials";
  632. ($retcode, $serials) = SMAEM_getCacheValue($index);
  633. if ($retcode) {
  634. Log3($name, 1, "SMAEM $name - $retcode") if ($retcode);
  635. Log3($name, 3, "SMAEM $name - Create new cacheFile $modpath/FHEM/FhemUtils/cacheSMAEM");
  636. $retcode = SMAEM_createCacheFile();
  637. } else {
  638. if ($serials) {
  639. $hash->{HELPER}{ALLSERIALS} = $serials;
  640. Log3 ($name, 3, "SMAEM $name - read saved serial numbers from $modpath/FHEM/FhemUtils/cacheSMAEM");
  641. }
  642. }
  643. return ($retcode);
  644. }
  645. ###############################################################
  646. ### Summenwerte für GridIn, GridOut auslesen
  647. sub SMAEM_getsum ($$) {
  648. my ($hash,$smaserial) = @_;
  649. my $name = $hash->{NAME};
  650. my ($index,$retcode,$sumstr);
  651. my $modpath = AttrVal("global", "modpath", undef);
  652. $index = $hash->{TYPE}."_".$hash->{NAME}."_".$smaserial;
  653. ($retcode, $sumstr) = SMAEM_getCacheValue($index);
  654. if ($retcode) {
  655. Log3($name, 1, "SMAEM $name - $retcode") if ($retcode);
  656. } else {
  657. if ($sumstr) {
  658. ($hash->{'GRIDIN_SUM_'.$smaserial}, $hash->{'GRIDOUT_SUM_'.$smaserial}) = split(/_/, $sumstr);
  659. Log3 ($name, 3, "SMAEM $name - read saved energy values from $modpath/FHEM/FhemUtils/cacheSMAEM");
  660. Log3 ($name, 3, "SMAEM $name - GRIDIN_SUM_$smaserial: $hash->{'GRIDIN_SUM_'.$smaserial}, GRIDOUT_SUM_$smaserial: $hash->{'GRIDOUT_SUM_'.$smaserial}");
  661. }
  662. }
  663. return ($retcode);
  664. }
  665. ###############################################################
  666. ### Leseroutine aus eigenem Keyvalue-File
  667. sub SMAEM_getCacheValue($) {
  668. my ($key) = @_;
  669. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheSMAEM";
  670. my $param = {
  671. FileName => $fName,
  672. ForceType => "file",
  673. };
  674. my ($err, @l) = FileRead($param);
  675. return ($err, undef) if($err);
  676. for my $l (@l) {
  677. return (undef, $1) if($l =~ m/^$key:(.*)/);
  678. }
  679. return (undef, undef);
  680. }
  681. ###############################################################
  682. ### Anlegen eigenes Keyvalue-File wenn nicht vorhanden
  683. sub SMAEM_createCacheFile {
  684. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheSMAEM";
  685. my $param = {
  686. FileName => $fName,
  687. ForceType => "file",
  688. };
  689. my @new;
  690. push(@new, "# This file is auto generated from 77_SMAEM.",
  691. "# Please do not modify, move or delete it.",
  692. "");
  693. return FileWrite($param, @new);
  694. }
  695. ###############################################################
  696. ### $update time of last update
  697. sub SMAEM_setlastupdate ($$) {
  698. my ($hash,$smaserial) = @_;
  699. my $name = $hash->{NAME};
  700. return if(!$smaserial); # Abbruch wenn keine Seriennummer extrahiert
  701. $hash->{HELPER}{'LASTUPDATE_'.$smaserial} = time();
  702. my ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime();
  703. $hash->{'LASTUPDATE_'.$smaserial} = sprintf "%02d.%02d.%04d / %02d:%02d:%02d" , $mday , $mon+=1 ,$year+=1900 , $hour , $min , $sec ;
  704. Log3 ($name, 4, "SMAEM $name - last update time set to: $hash->{'LASTUPDATE_'.$smaserial}");
  705. return;
  706. }
  707. 1;
  708. =pod
  709. =item summary Integration of SMA Energy Meters
  710. =item summary_DE Integration von SMA Energy Meter
  711. =begin html
  712. <a name="SMAEM"></a>
  713. <h3>SMAEM</h3>
  714. <br>
  715. <a name="SMAEMdefine"></a>
  716. <b>Define</b>
  717. <ul>
  718. <code>define &lt;name&gt; SMAEM </code><br>
  719. <br>
  720. Defines a SMA Energy Meter (SMAEM), a bidirectional energy meter/counter used in photovoltaics.
  721. <br><br>
  722. You need at least one SMAEM on your local subnet or behind a multicast enabled network of routers to receive multicast messages from the SMAEM over the
  723. multicast group 239.12.255.254 on udp/9522. Multicast messages are sent by SMAEM once a second (firmware 1.02.04.R, March 2016).
  724. <br><br>
  725. The update interval will be set by attribute "interval". If not set, it defaults to 60s. Since the SMAEM sends updates once a second, you can
  726. update the readings once a second by lowering the interval to 1 (Not recommended, since it puts FHEM under heavy load).
  727. <br><br>
  728. The parameter "disableSernoInReading" changes the way readings are named: if disableSernoInReading is false or unset, the readings will be named
  729. "SMAEM&lt;serialnumber_&gt;.....".
  730. If set to true, the prefix "SMAEM&lt;serialnumber_&gt;" is skipped.
  731. Set this to true if you only have one SMAEM device on your network and you want shorter reading names.
  732. If unsure, leave it unset.
  733. <br><br>
  734. You need the perl module IO::Socket::Multicast. Under Debian (based) systems it can be installed with <code>apt-get install libio-socket-multicast-perl</code>.
  735. </ul>
  736. <br>
  737. <br>
  738. <a name="SMAEMattr"></a>
  739. <b>Attribute</b>
  740. <ul>
  741. <li><b>disableSernoInReading</b> : prevents the prefix "SMAEM&lt;serialnumber_&gt;....." </li>
  742. <li><b>feedinPrice</b> : the individual amount of refund of one kilowatt hour</li>
  743. <li><b>interval</b> : evaluation interval in seconds </li>
  744. <li><b>disable</b> : 1 = the module is disabled </li>
  745. <li><b>diffAccept</b> : diffAccept determines the threshold, up to that a calaculated difference between two
  746. straight sequently meter readings (Readings with *_Diff) should be commenly accepted (default = 10). <br>
  747. Hence faulty DB entries with a disproportional high difference values will be eliminated, don't
  748. tamper the result and the measure cycles will be discarded. </li>
  749. <li><b>powerCost</b> : the individual amount of power cost per kWh </li>
  750. <li><b>timeout</b> : adjustment timeout of backgound processing (default 60s). The value of timeout has to be higher than the value of "interval". </li>
  751. </ul>
  752. <br>
  753. <a name="SMAEMreadings"></a>
  754. <b>Readings</b> <br><br>
  755. The created readings of SMAEM mostly are self-explanatory.
  756. However there are readings what maybe need some explanation. <br>
  757. <ul>
  758. <li><b>&lt;Phase&gt;_THD</b> : (Total Harmonic Distortion) - Proportion or quota of total effective value
  759. of all harmonic component to effective value of fundamental component.
  760. Total ratio of harmonic component and interference of pure sinusoidal wave
  761. in %.
  762. It is a rate of interferences. d is 0, if sinusoidal voltage exists and a sinusoidal
  763. current exists as well. As larger d, as more harmonic component are existing.
  764. According EN 50160/1999 the value mustn't exceed 8 %.
  765. If a current interference is so powerful that it is causing a voltage interference of
  766. more than 5 % (THD), that points to an issue with electrical potential. </li>
  767. </ul>
  768. <br>
  769. =end html
  770. =begin html_DE
  771. <a name="SMAEM"></a>
  772. <h3>SMAEM</h3>
  773. <br>
  774. <a name="SMAEMdefine"></a>
  775. <b>Define</b>
  776. <ul>
  777. <code>define &lt;name&gt; SMAEM </code><br>
  778. <br>
  779. Definiert ein SMA Energy Meter (SMAEM), einen bidirektionalen Stromzähler, der häufig in Photovolatikanlagen der Firma SMA zum Einsatz kommt.
  780. <br><br>
  781. Sie brauchen mindest ein SMAEM in Ihrem lokalen Netzwerk oder hinter einen multicastfähigen Netz von Routern, um die Daten des SMAEM über die
  782. Multicastgruppe 239.12.255.254 auf udp/9522 zu empfangen. Die Multicastpakete werden vom SMAEM einmal pro Sekunde ausgesendet (firmware 1.02.04.R, März 2016).
  783. <br><br>
  784. Das update interval kann über das Attribut "interval" gesetzt werden. Wenn es nicht gesetzt wird, werden updates per default alle 60 Sekunden durchgeführt.
  785. Da das SMAEM seine Daten sekündlich aktualisiert, kann das update interval auf bis zu einer Sekunde reduziert werden. Das wird nicht empfohlen, da FHEM
  786. sonst unter große Last gesetzt wird.
  787. <br><br>
  788. Der Parameter "disableSernoInReading" ändert die Art und Weise, wie die Readings des SMAEN bezeichnet werden: ist der Parameter false
  789. oder nicht gesetzt, werden die Readings mit "SMAEM&lt;serialnumber_&gt;....." bezeichnet.
  790. Wird der Parameter auf true gesetzt, wird das Prefix "SMAEM&lt;serialnumber_&gt;....." weg gelassen.
  791. Sie können diesen Parameter auf true setzen, wenn Sie nicht mehr als ein SMAEM-Gerät in Ihrem Netzwerk haben und kürzere Namen für die Readings wünschen.
  792. Falls Sie unsicher sind, setzen Sie diesen Parameter nicht.
  793. <br><br>
  794. Sie benötigen das Perl-Module IO::Socket::Multicast für dieses FHEM Modul. Unter Debian (basierten) System, kann dies
  795. mittels <code>apt-get install libio-socket-multicast-perl</code> installiert werden.
  796. </ul>
  797. <br>
  798. <a name="SMAEMattr"></a>
  799. <b>Attribute</b>
  800. <ul>
  801. <li><b>disableSernoInReading</b> : unterdrückt das Prefix "SMAEM&lt;serialnumber_&gt;....." </li>
  802. <li><b>feedinPrice</b> : die individuelle Höhe der Vergütung pro Kilowattstunde </li>
  803. <li><b>interval</b> : Auswertungsinterval in Sekunden </li>
  804. <li><b>disable</b> : 1 = das Modul ist disabled </li>
  805. <li><b>diffAccept</b> : diffAccept legt fest, bis zu welchem Schwellenwert eine berechnete positive Werte-Differenz
  806. zwischen zwei unmittelbar aufeinander folgenden Zählerwerten (Readings mit *_Diff) akzeptiert werden
  807. soll (Standard ist 10). <br>
  808. Damit werden eventuell fehlerhafte Differenzen mit einem unverhältnismäßig hohen Differenzwert von der Berechnung
  809. ausgeschlossen und der Messzyklus verworfen. </li>
  810. <li><b>powerCost</b> : die individuelle Höhe der Stromkosten pro Kilowattstunde </li>
  811. <li><b>timeout</b> : Einstellung timeout für Hintergrundverarbeitung (default 60s). Der timeout-Wert muss größer als das Wert von "interval" sein. </li>
  812. </ul>
  813. <br>
  814. <a name="SMAEMreadings"></a>
  815. <b>Readings</b> <br><br>
  816. Die meisten erzeugten Readings von SMAEM sind selbsterklärend.
  817. Es gibt allerdings Readings die einer Erläuterung bedürfen. <br>
  818. <ul>
  819. <li><b>&lt;Phase&gt;_THD</b> : (Total Harmonic Distortion) - Verzerrungs- oder Gesamtklirrfaktor - Verhältnis oder
  820. Anteil des Gesamteffektivwert aller Oberschwingungen zum Effektivwert der
  821. Grundschwingung. Gesamtanteil an Oberschwingungen und Störung der reinen Sinuswelle
  822. in % bzw. Verhältnis vom nutzbaren Grundschwingungsstrom zu den
  823. nicht nutzbaren Oberschwingungsströmen. Es ist ein Maß für Störungen. d ist 0, wenn bei
  824. sinusförmiger Spannung ein sinusförmiger Strom fließt. Je größer d, um so mehr
  825. Oberschwingungen sind vorhanden. Nach EN 50160/1999 z.B. darf der Wert 8 % nicht
  826. überschreiten. Wenn eine Stromstörung so stark ist, dass sie eine Spannungsstörung
  827. (THD) von über 5 % verursacht, deutet dies auf ein Potentialproblem hin. </li>
  828. </ul>
  829. <br>
  830. =end html_DE