77_SMAEM.pm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. ################################################################################################
  2. # $Id: 77_SMAEM.pm 12945 2017-01-03 14:22:56Z vk $
  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. # 2.8.2 03.12.2016 Prefix SMAEMserialnumber for Reading "state" removed, commandref adapted
  30. # 2.8.1 02.12.2016 encode / decode $data
  31. # 2.8 02.12.2016 plausibility check of measured differences, attr diffAccept, timeout
  32. # validation checks, improvement of failure prevention
  33. # 2.7 01.12.2016 logging of discarded cycles
  34. # 2.6 01.12.2016 some improvements, better logging possibility
  35. # 2.5 30.11.2016 some improvements
  36. # 2.4 30.11.2016 some improvements, attributes disable, timeout for BlockingCall added
  37. # 2.3 30.11.2016 getsum, setsum changed
  38. # 2.2 29.11.2016 check error while writing values to file -> set state with error
  39. # 2.1 29.11.2016 move $hash->{GRIDin_SUM}, $hash->{GRIDOUT_SUM} calc to smaread_ParseDone,
  40. # some little improvements to logging process
  41. # 2.0 28.11.2016 switch to nonblocking
  42. package main;
  43. use strict;
  44. use warnings;
  45. use bignum;
  46. use IO::Socket::Multicast;
  47. # use Scalar::Util qw(looks_like_number);
  48. use Blocking;
  49. ###############################################################
  50. # SMAEM Initialize
  51. ###############################################################
  52. sub SMAEM_Initialize($) {
  53. my ($hash) = @_;
  54. $hash->{ReadFn} = "SMAEM_Read";
  55. $hash->{DefFn} = "SMAEM_Define";
  56. $hash->{UndefFn} = "SMAEM_Undef";
  57. $hash->{DeleteFn} = "SMAEM_Delete";
  58. #$hash->{WriteFn} = "SMAEM_Write";
  59. #$hash->{ReadyFn} = "SMAEM_Ready";
  60. #$hash->{GetFn} = "SMAEM_Get";
  61. #$hash->{SetFn} = "SMAEM_Set";
  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->{LASTUPDATE} = 0;
  81. $hash->{HELPER}{LASTUPDATE} = 0;
  82. $hash->{HELPER}{FAULTEDCYCLES} = 0;
  83. $hash->{HELPER}{STARTTIME} = time();
  84. Log3 $hash, 3, "SMAEM $name - Opening multicast socket...";
  85. my $socket = IO::Socket::Multicast->new(
  86. Proto => 'udp',
  87. LocalPort => '9522',
  88. ReuseAddr => '1',
  89. ReusePort => defined(&ReusePort) ? 1 : 0,
  90. ) or return "Can't bind : $@";
  91. $socket->mcast_add('239.12.255.254');
  92. $hash->{TCPDev}= $socket;
  93. $hash->{FD} = $socket->fileno();
  94. delete($readyfnlist{"$name"});
  95. $selectlist{"$name"} = $hash;
  96. # gespeicherte Energiezählerwerte von File einlesen
  97. my $retcode = getsum($hash);
  98. if ($retcode) {
  99. $hash->{HELPER}{READFILEERROR} = $retcode;
  100. }
  101. return undef;
  102. }
  103. ###############################################################
  104. # SMAEM Undefine
  105. ###############################################################
  106. sub SMAEM_Undef($$) {
  107. my ($hash, $arg) = @_;
  108. my $name= $hash->{NAME};
  109. my $socket= $hash->{TCPDev};
  110. BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
  111. Log3 $hash, 3, "SMAEM $name - Closing multicast socket...";
  112. $socket->mcast_drop('239.12.255.254');
  113. my $ret = close($hash->{TCPDev});
  114. Log3 $hash, 4, "SMAEM $name - Close-ret: $ret";
  115. delete($hash->{TCPDev});
  116. delete($selectlist{"$name"});
  117. delete($hash->{FD});
  118. return;
  119. }
  120. ###############################################################
  121. # SMAEM Delete
  122. ###############################################################
  123. sub SMAEM_Delete {
  124. my ($hash, $arg) = @_;
  125. my $index = $hash->{TYPE}."_".$hash->{NAME}."_energysum";
  126. # gespeicherte Energiezählerwerte löschen
  127. setKeyValue($index, undef);
  128. return undef;
  129. }
  130. ###############################################################
  131. # SMAEM Attr
  132. ###############################################################
  133. sub SMAEM_Attr {
  134. my ($cmd,$name,$aName,$aVal) = @_;
  135. my $hash = $defs{$name};
  136. my $do;
  137. # $cmd can be "del" or "set"
  138. # $name is device name
  139. # aName and aVal are Attribute name and value
  140. if ($aName eq "interval") {
  141. if($cmd eq "set") {
  142. $hash->{INTERVAL} = $aVal;
  143. } else {
  144. $hash->{INTERVAL} = "60";
  145. }
  146. }
  147. if ($aName eq "disableSernoInReading") {
  148. delete $defs{$name}{READINGS};
  149. readingsSingleUpdate($hash, "state", "initialized", 1);
  150. }
  151. if ($aName eq "timeout" || $aName eq "diffAccept") {
  152. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  153. }
  154. if ($aName eq "disable") {
  155. if($cmd eq "set") {
  156. $do = ($aVal) ? 1 : 0;
  157. }
  158. $do = 0 if($cmd eq "del");
  159. my $val = ($do == 1 ? "disabled" : "initialized");
  160. readingsSingleUpdate($hash, "state", $val, 1);
  161. }
  162. return undef;
  163. }
  164. ###############################################################
  165. # SMAEM Read (Hauptschleife)
  166. ###############################################################
  167. # called from the global loop, when the select for hash->{FD} reports data
  168. sub SMAEM_Read($) {
  169. my ($hash) = @_;
  170. my $name= $hash->{NAME};
  171. my $socket= $hash->{TCPDev};
  172. my $timeout = AttrVal($name, "timeout", 60);
  173. my $data;
  174. return if(IsDisabled($name));
  175. return unless $socket->recv($data, 600); # Each SMAEM packet is 600 bytes of packed payload
  176. if (time() <= $hash->{HELPER}{STARTTIME}+30) {
  177. return;
  178. }
  179. if ( $hash->{HELPER}{LASTUPDATE} == 0 || time() >= ($hash->{HELPER}{LASTUPDATE}+$hash->{INTERVAL}) ) {
  180. Log3 ($name, 4, "SMAEM $name - ###############################################################");
  181. Log3 ($name, 4, "SMAEM $name - ######### Begin of new SMA Energymeter get data cycle #########");
  182. Log3 ($name, 4, "SMAEM $name - ###############################################################");
  183. Log3 ($name, 4, "SMAEM $name - discarded cycles since module start: $hash->{HELPER}{FAULTEDCYCLES}");
  184. if($hash->{helper}{RUNNING_PID}) {
  185. Log3 ($name, 3, "SMAEM $name - WARNING - old process $hash->{HELPER}{RUNNING_PID}{pid} has been killed to start a new BlockingCall");
  186. BlockingKill($hash->{HELPER}{RUNNING_PID});
  187. delete($hash->{HELPER}{RUNNING_PID});
  188. }
  189. # update time
  190. lastupdate_set($hash);
  191. my $dataenc = encode_base64($data,"");
  192. $hash->{HELPER}{RUNNING_PID} = BlockingCall("smaemread_DoParse", "$name|$dataenc", "smaemread_ParseDone", $timeout, "smaemread_ParseAborted", $hash);
  193. Log3 ($name, 4, "SMAEM $name - Blocking process with PID: $hash->{HELPER}{RUNNING_PID}{pid} started");
  194. } else {
  195. Log3 $hash, 5, "SMAEM $name: - received " . length($data) . " bytes but interval $hash->{INTERVAL}s isn't expired.";
  196. }
  197. return undef;
  198. }
  199. ###############################################################
  200. # non-blocking Inverter Datenabruf
  201. ###############################################################
  202. sub smaemread_DoParse($) {
  203. my ($string) = @_;
  204. my ($name, $dataenc) = split("\\|", $string);
  205. my $hash = $defs{$name};
  206. my $data = decode_base64($dataenc);
  207. my $discycles = $hash->{HELPER}{FAULTEDCYCLES};
  208. my $diffaccept = AttrVal($name, "diffAccept", 10);
  209. my @row_array;
  210. my @array;
  211. Log3 ($name, 4, "SMAEM $name -> Start BlockingCall smaemread_DoParse");
  212. my $gridinsum = $hash->{GRIDIN_SUM} ?sprintf("%.4f",$hash->{GRIDIN_SUM}):'';
  213. my $gridoutsum = $hash->{GRIDOUT_SUM}?sprintf("%.4f",$hash->{GRIDOUT_SUM}):'';
  214. # check if uniqueID-file has been opened at module start and try again if not
  215. if($hash->{HELPER}{READFILEERROR}) {
  216. my $retcode = getsum($hash);
  217. if ($retcode) {
  218. my $error = encode_base64($retcode,"");
  219. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_DoParse finished");
  220. $discycles++;
  221. return "$name|''|''|''|$error|$discycles";
  222. } else {
  223. delete($hash->{HELPER}{READFILEERROR})
  224. }
  225. }
  226. # Format of the udp packets of the SMAEM:
  227. # http://www.sma.de/fileadmin/content/global/Partner/Documents/SMA_Labs/EMETER-Protokoll-TI-de-10.pdf
  228. # http://www.eb-systeme.de/?page_id=1240
  229. # Conversion like in this python code:
  230. # http://www.unifox.at/sma_energy_meter/
  231. # https://github.com/datenschuft/SMA-EM
  232. # unpack big-endian to 2-digit hex (bin2hex)
  233. my $hex=unpack('H*', $data);
  234. ################ Aufbau Ergebnis-Array ####################
  235. # Extract datasets from hex:
  236. # Generic:
  237. my $susyid=hex(substr($hex,36,4));
  238. my $smaserial=hex(substr($hex,40,8));
  239. my $milliseconds=hex(substr($hex,48,8));
  240. # Prestring with NAME and SERIALNO or not
  241. my $ps = (!AttrVal($name, "disableSernoInReading", undef)) ? "SMAEM".$smaserial."_" : "";
  242. # Counter Divisor: [Hex-Value]=Ws => Ws/1000*3600=kWh => divide by 3600000
  243. # Sum L1-3
  244. my $bezug_wirk=hex(substr($hex,64,8))/10;
  245. my $bezug_wirk_count=hex(substr($hex,80,16))/3600000;
  246. my $einspeisung_wirk=hex(substr($hex,104,8))/10;
  247. my $einspeisung_wirk_count=hex(substr($hex,120,16))/3600000;
  248. # calculation of GRID-hashes and persist to file
  249. Log3 ($name, 4, "SMAEM $name - old GRIDIN_SUM got from RAM: $gridinsum");
  250. Log3 ($name, 4, "SMAEM $name - old GRIDOUT_SUM got from RAM: $gridoutsum");
  251. my $plausibility_out = 0;
  252. if( !$gridoutsum || ($bezug_wirk_count && $bezug_wirk_count < $gridoutsum) ) {
  253. $gridoutsum = $bezug_wirk_count;
  254. Log3 ($name, 4, "SMAEM $name - gridoutsum new set: $gridoutsum");
  255. } else {
  256. if ($gridoutsum && $bezug_wirk_count >= $gridoutsum) {
  257. if(($bezug_wirk_count - $gridoutsum) <= $diffaccept) {
  258. # Plausibilitätscheck ob Differenz kleiner als erlaubter Wert -> Fehlerprävention
  259. my $diffb = ($bezug_wirk_count - $gridoutsum)>0 ? sprintf("%.4f",$bezug_wirk_count - $gridoutsum) : 0;
  260. Log3 ($name, 4, "SMAEM $name - bezug_wirk_count: $bezug_wirk_count");
  261. Log3 ($name, 4, "SMAEM $name - gridoutsum: $gridoutsum");
  262. Log3 ($name, 4, "SMAEM $name - diffb: $diffb");
  263. $gridoutsum = $bezug_wirk_count;
  264. push(@row_array, $ps."Bezug_WirkP_Zaehler_Diff ".$diffb."\n");
  265. push(@row_array, $ps."Bezug_WirkP_Kosten_Diff ".sprintf("%.4f", $diffb*AttrVal($name, "powerCost", 0))."\n");
  266. $plausibility_out = 1;
  267. } else {
  268. # Zyklus verwerfen wenn Plusibilität nicht erfüllt
  269. my $errtxt = "cycle discarded due to allowed diff GRIDOUT exceeding";
  270. my $error = encode_base64($errtxt,"");
  271. Log3 ($name, 1, "SMAEM $name - $errtxt");
  272. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_DoParse finished");
  273. $gridinsum = $einspeisung_wirk_count;
  274. $gridoutsum = $bezug_wirk_count;
  275. $discycles++;
  276. return "$name|''|$gridinsum|$gridoutsum|''|$discycles";
  277. }
  278. }
  279. }
  280. my $plausibility_in = 0;
  281. if( !$gridinsum || ($einspeisung_wirk_count && $einspeisung_wirk_count < $gridinsum) ) {
  282. $gridinsum = $einspeisung_wirk_count;
  283. Log3 ($name, 4, "SMAEM $name - gridinsum new set: $gridinsum");
  284. } else {
  285. if ($gridinsum && $einspeisung_wirk_count >= $gridinsum) {
  286. if(($einspeisung_wirk_count - $gridinsum) <= $diffaccept) {
  287. # Plausibilitätscheck ob Differenz kleiner als erlaubter Wert -> Fehlerprävention
  288. my $diffe = ($einspeisung_wirk_count - $gridinsum)>0 ? sprintf("%.4f",$einspeisung_wirk_count - $gridinsum) : 0;
  289. Log3 ($name, 4, "SMAEM $name - einspeisung_wirk_count: $einspeisung_wirk_count");
  290. Log3 ($name, 4, "SMAEM $name - gridinsum: $gridinsum");
  291. Log3 ($name, 4, "SMAEM $name - diffe: $diffe");
  292. $gridinsum = $einspeisung_wirk_count;
  293. push(@row_array, $ps."Einspeisung_WirkP_Zaehler_Diff ".$diffe."\n");
  294. push(@row_array, $ps."Einspeisung_WirkP_Verguet_Diff ".sprintf("%.4f", $diffe*AttrVal($name, "feedinPrice", 0))."\n");
  295. $plausibility_in = 1;
  296. } else {
  297. # Zyklus verwerfen wenn Plusibilität nicht erfüllt
  298. my $errtxt = "cycle discarded due to allowed diff GRIDIN exceeding";
  299. my $error = encode_base64($errtxt,"");
  300. Log3 ($name, 1, "SMAEM $name - $errtxt");
  301. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_DoParse finished");
  302. $gridinsum = $einspeisung_wirk_count;
  303. $gridoutsum = $bezug_wirk_count;
  304. $discycles++;
  305. return "$name|''|$gridinsum|$gridoutsum|''|$discycles";
  306. }
  307. }
  308. }
  309. # write GRIDIN_SUM and GRIDOUT_SUM to file if plausibility check ok
  310. Log3 ($name, 4, "SMAEM $name - plausibility check done: GRIDIN -> $plausibility_in, GRIDOUT -> $plausibility_out");
  311. my $retcode = setsum($hash, $gridinsum, $gridoutsum) if($plausibility_in && $plausibility_out);
  312. # error while writing values to file
  313. if ($retcode) {
  314. my $error = encode_base64($retcode,"");
  315. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_DoParse finished");
  316. $discycles++;
  317. return "$name|''|''|''|$error|$discycles";
  318. }
  319. push(@row_array, "state ".sprintf("%.1f", $einspeisung_wirk-$bezug_wirk)."\n");
  320. push(@row_array, $ps."Saldo_Wirkleistung ".sprintf("%.1f",$einspeisung_wirk-$bezug_wirk)."\n");
  321. push(@row_array, $ps."Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$einspeisung_wirk_count-$bezug_wirk_count)."\n");
  322. push(@row_array, $ps."Bezug_Wirkleistung ".sprintf("%.1f",$bezug_wirk)."\n");
  323. push(@row_array, $ps."Bezug_Wirkleistung_Zaehler ".sprintf("%.4f",$bezug_wirk_count)."\n");
  324. push(@row_array, $ps."Einspeisung_Wirkleistung ".sprintf("%.1f",$einspeisung_wirk)."\n");
  325. push(@row_array, $ps."Einspeisung_Wirkleistung_Zaehler ".sprintf("%.4f",$einspeisung_wirk_count)."\n");
  326. my $bezug_blind=hex(substr($hex,144,8))/10;
  327. my $bezug_blind_count=hex(substr($hex,160,16))/3600000;
  328. my $einspeisung_blind=hex(substr($hex,184,8))/10;
  329. my $einspeisung_blind_count=hex(substr($hex,200,16))/3600000;
  330. push(@row_array, $ps."Bezug_Blindleistung ".sprintf("%.1f",$bezug_blind)."\n");
  331. push(@row_array, $ps."Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$bezug_blind_count)."\n");
  332. push(@row_array, $ps."Einspeisung_Blindleistung ".sprintf("%.1f",$einspeisung_blind)."\n");
  333. push(@row_array, $ps."Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$einspeisung_blind_count)."\n");
  334. my $bezug_schein=hex(substr($hex,224,8))/10;
  335. my $bezug_schein_count=hex(substr($hex,240,16))/3600000;
  336. my $einspeisung_schein=hex(substr($hex,264,8))/10;
  337. my $einspeisung_schein_count=hex(substr($hex,280,16))/3600000;
  338. push(@row_array, $ps."Bezug_Scheinleistung ".sprintf("%.1f",$bezug_schein)."\n");
  339. push(@row_array, $ps."Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$bezug_schein_count)."\n");
  340. push(@row_array, $ps."Einspeisung_Scheinleistung ".sprintf("%.1f",$einspeisung_schein)."\n");
  341. push(@row_array, $ps."Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$einspeisung_schein_count)."\n");
  342. my $cosphi=hex(substr($hex,304,8))/1000;
  343. push(@row_array, $ps."CosPhi ".sprintf("%.3f",$cosphi)."\n");
  344. # L1
  345. my $l1_bezug_wirk=hex(substr($hex,320,8))/10;
  346. my $l1_bezug_wirk_count=hex(substr($hex,336,16))/3600000;
  347. my $l1_einspeisung_wirk=hex(substr($hex,360,8))/10;
  348. my $l1_einspeisung_wirk_count=hex(substr($hex,376,16))/3600000;
  349. push(@row_array, $ps."L1_Saldo_Wirkleistung ".sprintf("%.1f",$l1_einspeisung_wirk-$l1_bezug_wirk)."\n");
  350. push(@row_array, $ps."L1_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_wirk_count-$l1_bezug_wirk_count)."\n");
  351. push(@row_array, $ps."L1_Bezug_Wirkleistung ".sprintf("%.1f",$l1_bezug_wirk)."\n");
  352. push(@row_array, $ps."L1_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_bezug_wirk_count)."\n");
  353. push(@row_array, $ps."L1_Einspeisung_Wirkleistung ".sprintf("%.1f",$l1_einspeisung_wirk)."\n");
  354. push(@row_array, $ps."L1_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_wirk_count)."\n");
  355. my $l1_bezug_blind=hex(substr($hex,400,8))/10;
  356. my $l1_bezug_blind_count=hex(substr($hex,416,16))/3600000;
  357. my $l1_einspeisung_blind=hex(substr($hex,440,8))/10;
  358. my $l1_einspeisung_blind_count=hex(substr($hex,456,16))/3600000;
  359. push(@row_array, $ps."L1_Bezug_Blindleistung ".sprintf("%.1f",$l1_bezug_blind)."\n");
  360. push(@row_array, $ps."L1_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l1_bezug_blind_count)."\n");
  361. push(@row_array, $ps."L1_Einspeisung_Blindleistung ".sprintf("%.1f",$l1_einspeisung_blind)."\n");
  362. push(@row_array, $ps."L1_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_blind_count)."\n");
  363. my $l1_bezug_schein=hex(substr($hex,480,8))/10;
  364. my $l1_bezug_schein_count=hex(substr($hex,496,16))/3600000;
  365. my $l1_einspeisung_schein=hex(substr($hex,520,8))/10;
  366. my $l1_einspeisung_schein_count=hex(substr($hex,536,16))/3600000;
  367. push(@row_array, $ps."L1_Bezug_Scheinleistung ".sprintf("%.1f",$l1_bezug_schein)."\n");
  368. push(@row_array, $ps."L1_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l1_bezug_schein_count)."\n");
  369. push(@row_array, $ps."L1_Einspeisung_Scheinleistung ".sprintf("%.1f",$l1_einspeisung_schein)."\n");
  370. push(@row_array, $ps."L1_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l1_einspeisung_schein_count)."\n");
  371. my $l1_thd=hex(substr($hex,560,8))/1000;
  372. my $l1_v=hex(substr($hex,576,8))/1000;
  373. my $l1_cosphi=hex(substr($hex,592,8))/1000;
  374. push(@row_array, $ps."L1_THD ".sprintf("%.2f",$l1_thd)."\n");
  375. push(@row_array, $ps."L1_Spannung ".sprintf("%.1f",$l1_v)."\n");
  376. push(@row_array, $ps."L1_CosPhi ".sprintf("%.3f",$l1_cosphi)."\n");
  377. # L2
  378. my $l2_bezug_wirk=hex(substr($hex,608,8))/10;
  379. my $l2_bezug_wirk_count=hex(substr($hex,624,16))/3600000;
  380. my $l2_einspeisung_wirk=hex(substr($hex,648,8))/10;
  381. my $l2_einspeisung_wirk_count=hex(substr($hex,664,16))/3600000;
  382. push(@row_array, $ps."L2_Saldo_Wirkleistung ".sprintf("%.1f",$l2_einspeisung_wirk-$l2_bezug_wirk)."\n");
  383. push(@row_array, $ps."L2_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_wirk_count-$l2_bezug_wirk_count)."\n");
  384. push(@row_array, $ps."L2_Bezug_Wirkleistung ".sprintf("%.1f",$l2_bezug_wirk)."\n");
  385. push(@row_array, $ps."L2_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_bezug_wirk_count)."\n");
  386. push(@row_array, $ps."L2_Einspeisung_Wirkleistung ".sprintf("%.1f",$l2_einspeisung_wirk)."\n");
  387. push(@row_array, $ps."L2_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_wirk_count)."\n");
  388. my $l2_bezug_blind=hex(substr($hex,688,8))/10;
  389. my $l2_bezug_blind_count=hex(substr($hex,704,16))/3600000;
  390. my $l2_einspeisung_blind=hex(substr($hex,728,8))/10;
  391. my $l2_einspeisung_blind_count=hex(substr($hex,744,16))/3600000;
  392. push(@row_array, $ps."L2_Bezug_Blindleistung ".sprintf("%.1f",$l2_bezug_blind)."\n");
  393. push(@row_array, $ps."L2_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l2_bezug_blind_count)."\n");
  394. push(@row_array, $ps."L2_Einspeisung_Blindleistung ".sprintf("%.1f",$l2_einspeisung_blind)."\n");
  395. push(@row_array, $ps."L2_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_blind_count)."\n");
  396. my $l2_bezug_schein=hex(substr($hex,768,8))/10;
  397. my $l2_bezug_schein_count=hex(substr($hex,784,16))/3600000;
  398. my $l2_einspeisung_schein=hex(substr($hex,808,8))/10;
  399. my $l2_einspeisung_schein_count=hex(substr($hex,824,16))/3600000;
  400. push(@row_array, $ps."L2_Bezug_Scheinleistung ".sprintf("%.1f",$l2_bezug_schein)."\n");
  401. push(@row_array, $ps."L2_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l2_bezug_schein_count)."\n");
  402. push(@row_array, $ps."L2_Einspeisung_Scheinleistung ".sprintf("%.1f",$l2_einspeisung_schein)."\n");
  403. push(@row_array, $ps."L2_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l2_einspeisung_schein_count)."\n");
  404. my $l2_thd=hex(substr($hex,848,8))/1000;
  405. my $l2_v=hex(substr($hex,864,8))/1000;
  406. my $l2_cosphi=hex(substr($hex,880,8))/1000;
  407. push(@row_array, $ps."L2_THD ".sprintf("%.2f",$l2_thd)."\n");
  408. push(@row_array, $ps."L2_Spannung ".sprintf("%.1f",$l2_v)."\n");
  409. push(@row_array, $ps."L2_CosPhi ".sprintf("%.3f",$l2_cosphi)."\n");
  410. # L3
  411. my $l3_bezug_wirk=hex(substr($hex,896,8))/10;
  412. my $l3_bezug_wirk_count=hex(substr($hex,912,16))/3600000;
  413. my $l3_einspeisung_wirk=hex(substr($hex,936,8))/10;
  414. my $l3_einspeisung_wirk_count=hex(substr($hex,952,16))/3600000;
  415. push(@row_array, $ps."L3_Saldo_Wirkleistung ".sprintf("%.1f",$l3_einspeisung_wirk-$l3_bezug_wirk)."\n");
  416. push(@row_array, $ps."L3_Saldo_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_wirk_count-$l3_bezug_wirk_count)."\n");
  417. push(@row_array, $ps."L3_Bezug_Wirkleistung ".sprintf("%.1f",$l3_bezug_wirk)."\n");
  418. push(@row_array, $ps."L3_Bezug_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_bezug_wirk_count)."\n");
  419. push(@row_array, $ps."L3_Einspeisung_Wirkleistung ".sprintf("%.1f",$l3_einspeisung_wirk)."\n");
  420. push(@row_array, $ps."L3_Einspeisung_Wirkleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_wirk_count)."\n");
  421. my $l3_bezug_blind=hex(substr($hex,976,8))/10;
  422. my $l3_bezug_blind_count=hex(substr($hex,992,16))/3600000;
  423. my $l3_einspeisung_blind=hex(substr($hex,1016,8))/10;
  424. my $l3_einspeisung_blind_count=hex(substr($hex,1032,16))/3600000;
  425. push(@row_array, $ps."L3_Bezug_Blindleistung ".sprintf("%.1f",$l3_bezug_blind)."\n");
  426. push(@row_array, $ps."L3_Bezug_Blindleistung_Zaehler ".sprintf("%.1f",$l3_bezug_blind_count)."\n");
  427. push(@row_array, $ps."L3_Einspeisung_Blindleistung ".sprintf("%.1f",$l3_einspeisung_blind)."\n");
  428. push(@row_array, $ps."L3_Einspeisung_Blindleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_blind_count)."\n");
  429. my $l3_bezug_schein=hex(substr($hex,1056,8))/10;
  430. my $l3_bezug_schein_count=hex(substr($hex,1072,16))/3600000;
  431. my $l3_einspeisung_schein=hex(substr($hex,1096,8))/10;
  432. my $l3_einspeisung_schein_count=hex(substr($hex,1112,16))/3600000;
  433. push(@row_array, $ps."L3_Bezug_Scheinleistung ".sprintf("%.1f",$l3_bezug_schein)."\n");
  434. push(@row_array, $ps."L3_Bezug_Scheinleistung_Zaehler ".sprintf("%.1f",$l3_bezug_schein_count)."\n");
  435. push(@row_array, $ps."L3_Einspeisung_Scheinleistung ".sprintf("%.1f",$l3_einspeisung_schein)."\n");
  436. push(@row_array, $ps."L3_Einspeisung_Scheinleistung_Zaehler ".sprintf("%.1f",$l3_einspeisung_schein_count)."\n");
  437. my $l3_thd=hex(substr($hex,1136,8))/1000;
  438. my $l3_v=hex(substr($hex,1152,8))/1000;
  439. my $l3_cosphi=hex(substr($hex,1168,8))/1000;
  440. push(@row_array, $ps."L3_THD ".sprintf("%.2f",$l3_thd)."\n");
  441. push(@row_array, $ps."L3_Spannung ".sprintf("%.1f",$l3_v)."\n");
  442. push(@row_array, $ps."L3_CosPhi ".sprintf("%.3f",$l3_cosphi)."\n");
  443. Log3 ($name, 5, "$name - row_array before encoding:");
  444. foreach my $row (@row_array) {
  445. chomp $row;
  446. Log3 ($name, 5, "SMAEM $name - $row");
  447. }
  448. # encoding result
  449. my $rowlist = join('|', @row_array);
  450. $rowlist = encode_base64($rowlist,"");
  451. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_DoParse finished");
  452. return "$name|$rowlist|$gridinsum|$gridoutsum|''|$discycles";
  453. }
  454. ###############################################################
  455. # Auswertung non-blocking Inverter Datenabruf
  456. ###############################################################
  457. sub smaemread_ParseDone ($) {
  458. my ($string) = @_;
  459. my @a = split("\\|",$string);
  460. my $name = $a[0];
  461. my $hash = $defs{$name};
  462. my $rowlist = decode_base64($a[1]);
  463. my $gridinsum = $a[2];
  464. my $gridoutsum = $a[3];
  465. my $error = decode_base64($a[4]) if($a[4]);
  466. my $discycles = $a[5];
  467. Log3 ($name, 4, "SMAEM $name -> Start BlockingCall smaemread_ParseDone");
  468. $hash->{HELPER}{FAULTEDCYCLES} = $discycles;
  469. # update time
  470. lastupdate_set($hash);
  471. if ($error) {
  472. readingsSingleUpdate($hash, "state", $error, 1);
  473. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_ParseDone finished");
  474. delete($hash->{HELPER}{RUNNING_PID});
  475. return;
  476. }
  477. $hash->{GRIDIN_SUM} = $gridinsum;
  478. $hash->{GRIDOUT_SUM} = $gridoutsum;
  479. Log3($name, 4, "SMAEM $name - wrote new energy values to INTERNALS - GRIDIN_SUM: $gridinsum, GRIDOUT_SUM: $gridoutsum");
  480. my @row_array = split("\\|", $rowlist);
  481. Log3 ($name, 5, "SMAEM $name - row_array after decoding:");
  482. foreach my $row (@row_array) {
  483. chomp $row;
  484. Log3 ($name, 5, "SMAEM $name - $row");
  485. }
  486. readingsBeginUpdate($hash);
  487. foreach my $row (@row_array) {
  488. chomp $row;
  489. my @a = split(" ", $row, 2);
  490. readingsBulkUpdate($hash, $a[0], $a[1]);
  491. }
  492. readingsEndUpdate($hash, 1);
  493. delete($hash->{HELPER}{RUNNING_PID});
  494. Log3 ($name, 4, "SMAEM $name -> BlockingCall smaemread_ParseDone finished");
  495. return;
  496. }
  497. ###############################################################
  498. # Abbruchroutine Timeout Inverter Abfrage
  499. ###############################################################
  500. sub smaemread_ParseAborted($) {
  501. my ($hash) = @_;
  502. my $name = $hash->{NAME};
  503. my $discycles = $hash->{HELPER}{FAULTEDCYCLES};
  504. $discycles++;
  505. $hash->{HELPER}{FAULTEDCYCLES} = $discycles;
  506. Log3 ($name, 1, "SMAEM $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} timed out");
  507. readingsSingleUpdate($hash, "state", "timeout", 1);
  508. delete($hash->{HELPER}{RUNNING_PID});
  509. }
  510. ###############################################################
  511. # Hilfsroutinen
  512. ###############################################################
  513. ###############################################################
  514. ### Summenwerte für GridIn, GridOut speichern
  515. sub setsum ($$$) {
  516. my ($hash, $gridinsum, $gridoutsum) = @_;
  517. my $name = $hash->{NAME};
  518. my $index;
  519. my $retcode = 0;
  520. my $sumstr;
  521. my $modpath = AttrVal("global", "modpath", undef);
  522. $sumstr = $gridinsum."_".$gridoutsum;
  523. $index = $hash->{TYPE}."_".$hash->{NAME}."_energysum";
  524. $retcode = setKeyValue($index, $sumstr);
  525. if ($retcode) {
  526. Log3($name, 1, "SMAEM $name - ERROR while saving summary of energy values - $retcode");
  527. } else {
  528. Log3($name, 4, "SMAEM $name - new energy values saved to $modpath/FHEM/FhemUtils/uniqueID:");
  529. Log3($name, 4, "SMAEM $name - GRIDIN_SUM: $gridinsum, GRIDOUT_SUM: $gridoutsum");
  530. }
  531. return ($retcode);
  532. }
  533. ###############################################################
  534. ### Summenwerte für GridIn, GridOut abtufen
  535. sub getsum ($) {
  536. my ($hash) = @_;
  537. my $name = $hash->{NAME};
  538. my $index;
  539. my $retcode = 0;
  540. my $sumstr;
  541. my $modpath = AttrVal("global", "modpath", undef);
  542. $index = $hash->{TYPE}."_".$hash->{NAME}."_energysum";
  543. ($retcode, $sumstr) = getKeyValue($index);
  544. if ($retcode) {
  545. Log3($name, 1, "SMAEM $name - ERROR while reading saved energy values from $modpath/FHEM/FhemUtils/uniqueID:");
  546. Log3($name, 1, "SMAEM $name - $retcode");
  547. } else {
  548. if ($sumstr) {
  549. ($hash->{GRIDIN_SUM}, $hash->{GRIDOUT_SUM}) = split(/_/, $sumstr);
  550. Log3 ($name, 3, "SMAEM $name - read saved energy values from $modpath/FHEM/FhemUtils/uniqueID:");
  551. Log3 ($name, 3, "SMAEM $name - GRIDIN_SUM: $hash->{GRIDIN_SUM}, GRIDOUT_SUM: $hash->{GRIDOUT_SUM}");
  552. }
  553. }
  554. return ($retcode);
  555. }
  556. ###############################################################
  557. ### $update time of last update
  558. sub lastupdate_set ($) {
  559. my ($hash) = @_;
  560. my $name = $hash->{NAME};
  561. $hash->{HELPER}{LASTUPDATE} = time();
  562. my ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime();
  563. $hash->{LASTUPDATE} = sprintf "%02d.%02d.%04d / %02d:%02d:%02d" , $mday , $mon+=1 ,$year+=1900 , $hour , $min , $sec ;
  564. Log3 ($name, 4, "SMAEM $name - last update time set to: $hash->{LASTUPDATE}");
  565. }
  566. 1;
  567. =pod
  568. =item summary Integration of SMA Energy Meters
  569. =item summary_DE Integration von SMA Energy Meter
  570. =begin html
  571. <a name="SMAEM"></a>
  572. <h3>SMAEM</h3>
  573. <br>
  574. <a name="SMAEMdefine"></a>
  575. <b>Define</b>
  576. <ul>
  577. <code>define &lt;name&gt; SMAEM </code><br>
  578. <br>
  579. Defines a SMA Energy Meter (SMAEM), a bidirectional energy meter/counter used in photovoltaics.
  580. <br><br>
  581. 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
  582. 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).
  583. <br><br>
  584. 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
  585. update the readings once a second by lowering the interval to 1 (Not recommended, since it puts FHEM under heavy load).
  586. <br><br>
  587. The parameter "disableSernoInReading" changes the way readings are named: if disableSernoInReading is false or unset, the readings will be named
  588. "SMAEM&lt;serialnumber_&gt;.....".
  589. If set to true, the prefix "SMAEM&lt;serialnumber_&gt;" is skipped.
  590. Set this to true if you only have one SMAEM device on your network and you want shorter reading names.
  591. If unsure, leave it unset.
  592. <br><br>
  593. 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>.
  594. </ul>
  595. <br>
  596. <br>
  597. <a name="SMAEMattr"></a>
  598. <b>Attribute</b>
  599. <ul>
  600. <li><b>disableSernoInReading</b> : prevents the prefix "SMAEM&lt;serialnumber_&gt;....." </li>
  601. <li><b>feedinPrice</b> : the individual amount of refund of one kilowatt hour</li>
  602. <li><b>interval</b> : evaluation interval in seconds </li>
  603. <li><b>disable</b> : 1 = the module is disabled </li>
  604. <li><b>diffAccept</b> : diffAccept determines the threshold, up to that a calaculated difference between two
  605. straight sequently meter readings (Readings with *_Diff) should be commenly accepted (default = 10). <br>
  606. Hence faulty DB entries with a disproportional high difference values will be eliminated, don't
  607. tamper the result and the measure cycles will be discarded. </li>
  608. <li><b>powerCost</b> : die individuelle Höhe der Stromkosten pro Kilowattstunde </li>
  609. <li><b>timeout</b> : Einstellung des timeout für die Wechselrichterabfrage (default 60s) </li>
  610. </ul>
  611. =end html
  612. =begin html_DE
  613. <a name="SMAEM"></a>
  614. <h3>SMAEM</h3>
  615. <br>
  616. <a name="SMAEMdefine"></a>
  617. <b>Define</b>
  618. <ul>
  619. <code>define &lt;name&gt; SMAEM </code><br>
  620. <br>
  621. Definiert ein SMA Energy Meter (SMAEM), einen bidirektionalen Stromzähler, der häufig in Photovolatikanlagen der Firma SMA zum Einsatz kommt.
  622. <br><br>
  623. Sie brauchen mindest ein SMAEM in Ihrem lokalen Netzwerk oder hinter einemmulticast fähigen Netz von Routern, um die Daten des SMAEM über die
  624. 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).
  625. <br><br>
  626. Das update interval kann über das Attribut "interval" gesetzt werden. Wenn es nicht gesetzt wird, werden updates per default alle 60 Sekunden durchgeführt.
  627. 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
  628. sonst unter große Last gesetzt wird.
  629. <br><br>
  630. Der Parameter "disableSernoInReading" ändert die Art und Weise, wie die Readings des SMAEN bezeichnet werden: ist der Parameter false
  631. oder nicht gesetzt, werden die Readings mit "SMAEM&lt;serialnumber_&gt;....." bezeichnet.
  632. Wird der Parameter auf true gesetzt, wird das Prefix "SMAEM&lt;serialnumber_&gt;....." weg gelassen.
  633. 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.
  634. Falls Sie unsicher sind, setzen Sie diesen Parameter nicht.
  635. <br><br>
  636. Sie benötigen das Perl-Module IO::Socket::Multicast für dieses FHEM Modul. Unter Debian (basierten) System, kann dies
  637. mittels <code>apt-get install libio-socket-multicast-perl</code> installiert werden.
  638. </ul>
  639. <br>
  640. <br>
  641. <a name="SMAEMattr"></a>
  642. <b>Attribute</b>
  643. <ul>
  644. <li><b>disableSernoInReading</b> : unterdrückt das Prefix "SMAEM&lt;serialnumber_&gt;....." </li>
  645. <li><b>feedinPrice</b> : die individuelle Höhe der Vergütung pro Kilowattstunde </li>
  646. <li><b>interval</b> : Auswertungsinterval in Sekunden </li>
  647. <li><b>disable</b> : 1 = das Modul ist disabled </li>
  648. <li><b>diffAccept</b> : diffAccept legt fest, bis zu welchem Schwellenwert eine berechnete positive Werte-Differenz
  649. zwischen zwei unmittelbar aufeinander folgenden Zählerwerten (Readings mit *_Diff) akzeptiert werden
  650. soll (Standard ist 10). <br>
  651. Damit werden eventuell fehlerhafte Differenzen mit einem unverhältnismäßig hohen Differenzwert von der Berechnung
  652. ausgeschlossen und der Messzyklus verworfen. </li>
  653. <li><b>powerCost</b> : die individuelle Höhe der Stromkosten pro Kilowattstunde </li>
  654. <li><b>timeout</b> : Einstellung des timeout für die Wechselrichterabfrage (default 60s) </li>
  655. </ul>
  656. =end html_DE