76_SMAInverter.pm 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. #################################################################################################################
  2. # $Id: 76_SMAInverter.pm 12736 2016-12-11 09:17:42Z nasseeder1 $
  3. #################################################################################################################
  4. #
  5. #
  6. # Copyright notice
  7. #
  8. # Published according Creative Commons : Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
  9. # Details: https://creativecommons.org/licenses/by-nc-sa/3.0/
  10. #
  11. # Credits:
  12. # - based on 77_SMASTP.pm by Volker Kettenbach with following credits:
  13. # - based on an Idea by SpenZerX and HDO
  14. # - Waldmensch for various improvements
  15. # - sbfspot (https://sbfspot.codeplex.com/)
  16. # - rewritten by Thomas Schoedl (sct14675) with inputs from Volker, waldmensch and DS_starter
  17. #
  18. # Description:
  19. # This is an FHEM-Module for SMA Inverters.
  20. # Tested on Sunny Tripower 6000TL-20 and Sunny Island 4.4
  21. #
  22. # Requirements:
  23. # This module requires:
  24. # - Perl Module: IO::Socket::INET
  25. # - Perl Module: DateTime
  26. #
  27. #
  28. #################################################################################################################
  29. # Versions History done by DS_Starter
  30. #
  31. # 2.8.1 06.12.2016 SMAInverter version as internal
  32. # 2.8 05.12.2016 changed commandsections to make sure getting only data from inverters with preset
  33. # $inv_susyid and $inv_serial
  34. # 2.7.4 04.12.2016 change loading of IO::Socket::INET, DateTime
  35. # 2.7.3 04.12.2016 commandref adapted
  36. # 2.7.2 03.12.2016 use Time::HiRes qw(gettimeofday tv_interval)
  37. # 2.7.1 02.12.2016 showproctime improved
  38. # 2.7 02.12.2016 showproctime added
  39. # 2.6.1 29.11.2016 getstatus_DoParse changed due to inititialized issues
  40. # 2.6 28.11.2016 bugfix warnings ParseDone redefine at startup, uninitialized value $avg if FHEM was
  41. # restarted in sleeptime, switched avg_energy to avg_power, commandref updated
  42. # 2.5.2 27.11.2016 bugfix average calc, bugfix warnings at startup
  43. # 2.5.1 26.11.2016 calc of averagebuf changed to 5, 10, 15 minutes
  44. # 2.5 26.11.2016 averagebuf changed, Attr timeout added
  45. # 2.4 26.11.2016 create ringbuffer for calculating average energy last 5, 10, 15 cycles
  46. # 2.3 25.11.2016 bugfixing
  47. # 2.2 24.11.2016 further optimize of non-blocking operation
  48. # 2.1 24.11.2016 avg_energy_lastcycles added
  49. # 2.0 24.11.2016 switched module to non-blocking operation
  50. # 1.8.4 23.11.2016 prepare non-blocking operation
  51. # 1.8.3 23.11.2016 readings opertime_start, opertime_stop
  52. # 1.8.2 22.11.2016 eliminate global vars, prepare non-blocking operation
  53. # 1.8.1 22.11.2016 eliminate global vars, create command array
  54. # 1.8 21.11.2016 eliminate $r_OK, $r_FAIL, create command-array
  55. # 1.7 21.11.2016 devtypes completed, minor bugfixes, commandref completed
  56. # 1.6.1 19.11.2016 bugfix perl warning during fhem start
  57. # 1.6 09.11.2016 added operation control by sunrise,sunset, Attr offset, suppressSleep added
  58. # 1.5 08.11.2016 added device classes hash
  59. # 1.4 07.11.2016 compatibility to SBFSpot improved, bilingual dependend on attr "language" of global-device,
  60. # added hash of SMA device types
  61. # 1.3 07.11.2016 Attr SBFSpotComp added to get compatibility mode with SBFSpot
  62. # 1.2 06.11.2016 function get data added, log output level changed to 4 in sub SMAInverter_Attr,
  63. # some code changes
  64. # 1.1 06.11.2016 Attr mode manual, automatic added
  65. # 1.0 06.11.2016 Attr disable added,
  66. # $globalName replaced by $name in all expressions (due to module redesign to non-blocking later)
  67. package main;
  68. use strict;
  69. use warnings;
  70. eval "use IO::Socket::INET;1" or my $MissModulSocket = "IO::Socket::INET";
  71. eval "use DateTime;1" or my $MissModulDateTime = "DateTime";
  72. use Time::HiRes qw(gettimeofday tv_interval);
  73. use Blocking;
  74. use Time::Local;
  75. my $SMAInverterVersion = "2.8.1";
  76. # Inverter Data fields and supported commands flags.
  77. # $inv_susyid
  78. # $inv_serial
  79. # $inv_SPOT_ETODAY # Today yield
  80. # $inv_SPOT_ETOTAL # Total yield
  81. # $inv_SPOT_PDC1 # DC power input 1
  82. # $inv_SPOT_PDC2 # DC power input 2
  83. # $inv_SPOT_PAC1 # Power L1
  84. # $inv_SPOT_PAC2 # Power L2
  85. # $inv_SPOT_PAC3 # Power L3
  86. # $inv_PACMAX1 # Nominal power in Ok Mode
  87. # $inv_PACMAX2 # Nominal power in Warning Mode
  88. # $inv_PACMAX3 # Nominal power in Fault Mode
  89. # $inv_PACMAX1_2 # Maximum active power device (Some inverters like SB3300/SB1200)
  90. # $inv_SPOT_PACTOT # Total Power
  91. # $inv_ChargeStatus # Battery Charge status
  92. # $inv_SPOT_UDC1 # DC voltage input
  93. # $inv_SPOT_UDC2 # DC voltage input
  94. # $inv_SPOT_IDC1 # DC current input
  95. # $inv_SPOT_IDC2 # DC current input
  96. # $inv_SPOT_UAC1 # Grid voltage phase L1
  97. # $inv_SPOT_UAC2 # Grid voltage phase L2
  98. # $inv_SPOT_UAC3 # Grid voltage phase L3
  99. # $inv_SPOT_IAC1 # Grid current phase L1
  100. # $inv_SPOT_IAC2 # Grid current phase L2
  101. # $inv_SPOT_IAC3 # Grid current phase L3
  102. # $inv_BAT_UDC # Battery Voltage
  103. # $inv_BAT_IDC # Battery Current
  104. # $inv_BAT_CYCLES # Battery recharge cycles
  105. # $inv_BAT_TEMP # Battery temperature
  106. # $inv_SPOT_FREQ # Grid Frequency
  107. # $inv_CLASS # Inverter Class
  108. # $inv_TYPE # Inverter Type
  109. # $inv_SPOT_OPERTM # Operation Time
  110. # $inv_SPOT_FEEDTM # Feed-in time
  111. # $inv_TEMP # Inverter temperature
  112. # $inv_GRIDRELAY # Grid Relay/Contactor Status
  113. # $inv_STATUS # Inverter Status
  114. # Aufbau Wechselrichter Type-Hash
  115. my %SMAInverter_devtypes = (
  116. 9015 => "SB 700",
  117. 9016 => "SB 700U",
  118. 9017 => "SB 1100",
  119. 9018 => "SB 1100U",
  120. 9019 => "SB 1100LV",
  121. 9020 => "SB 1700",
  122. 9021 => "SB 1900TLJ",
  123. 9022 => "SB 2100TL",
  124. 9023 => "SB 2500",
  125. 9024 => "SB 2800",
  126. 9025 => "SB 2800i",
  127. 9026 => "SB 3000",
  128. 9027 => "SB 3000US",
  129. 9028 => "SB 3300",
  130. 9029 => "SB 3300U",
  131. 9030 => "SB 3300TL",
  132. 9031 => "SB 3300TL HC",
  133. 9032 => "SB 3800",
  134. 9033 => "SB 3800U",
  135. 9034 => "SB 4000US",
  136. 9035 => "SB 4200TL",
  137. 9036 => "SB 4200TL HC",
  138. 9037 => "SB 5000TL",
  139. 9038 => "SB 5000TLW",
  140. 9039 => "SB 5000TL HC",
  141. 9066 => "SB 1200",
  142. 9067 => "STP 10000TL-10",
  143. 9068 => "STP 12000TL-10",
  144. 9069 => "STP 15000TL-10",
  145. 9070 => "STP 17000TL-10",
  146. 9084 => "WB 3600TL-20",
  147. 9085 => "WB 5000TL-20",
  148. 9086 => "SB 3800US-10",
  149. 9098 => "STP 5000TL-20",
  150. 9099 => "STP 6000TL-20",
  151. 9100 => "STP 7000TL-20",
  152. 9101 => "STP 8000TL-10",
  153. 9102 => "STP 9000TL-20",
  154. 9103 => "STP 8000TL-20",
  155. 9104 => "SB 3000TL-JP-21",
  156. 9105 => "SB 3500TL-JP-21",
  157. 9106 => "SB 4000TL-JP-21",
  158. 9107 => "SB 4500TL-JP-21",
  159. 9108 => "SCSMC",
  160. 9109 => "SB 1600TL-10",
  161. 9131 => "STP 20000TL-10",
  162. 9139 => "STP 20000TLHE-10",
  163. 9140 => "STP 15000TLHE-10",
  164. 9157 => "Sunny Island 2012",
  165. 9158 => "Sunny Island 2224",
  166. 9159 => "Sunny Island 5048",
  167. 9160 => "SB 3600TL-20",
  168. 9171 => "WB 3000TL-21",
  169. 9172 => "WB 3600TL-21",
  170. 9173 => "WB 4000TL-21",
  171. 9174 => "WB 5000TL-21",
  172. 9179 => "Multigate-10",
  173. 9180 => "Multigate-US-10",
  174. 9181 => "STP 20000TLEE-10",
  175. 9182 => "STP 15000TLEE-10",
  176. 9254 => "Sunny Island 3324",
  177. 9255 => "Sunny Island 4.0M",
  178. 9256 => "Sunny Island 4248",
  179. 9257 => "Sunny Island 4248U",
  180. 9258 => "Sunny Island 4500",
  181. 9259 => "Sunny Island 4548U",
  182. 9260 => "Sunny Island 5.4M",
  183. 9261 => "Sunny Island 5048U",
  184. 9262 => "Sunny Island 6048U",
  185. 9278 => "Sunny Island 3.0M",
  186. 9281 => "STP 10000TL-20",
  187. 9282 => "STP 11000TL-20",
  188. 9283 => "STP 12000TL-20",
  189. 9284 => "STP 20000TL-30",
  190. 9285 => "STP 25000TL-30",
  191. );
  192. # Wechselrichter Class-Hash DE
  193. my %SMAInverter_classesDE = (
  194. 8000 => "Alle Geräte",
  195. 8001 => "Solar-Wechselrichter",
  196. 8002 => "Wind-Wechselrichter",
  197. 8007 => "Batterie-Wechselrichter",
  198. 8033 => "Verbraucher",
  199. 8064 => "Sensorik allgemein",
  200. 8065 => "Stromzähler",
  201. 8128 => "Kommunikationsprodukte",
  202. );
  203. # Wechselrichter Class-Hash EN
  204. my %SMAInverter_classesEN = (
  205. 8000 => "All Devices",
  206. 8001 => "Solar Inverters",
  207. 8002 => "Wind Turbine Inverter",
  208. 8007 => "Batterie Inverters",
  209. 8033 => "Consumer",
  210. 8064 => "Sensor System in General",
  211. 8065 => "Electricity meter",
  212. 8128 => "Communication products",
  213. );
  214. ###############################################################
  215. # SMAInverter Initialize
  216. ###############################################################
  217. sub SMAInverter_Initialize($) {
  218. my ($hash) = @_;
  219. $hash->{DefFn} = "SMAInverter_Define";
  220. $hash->{UndefFn} = "SMAInverter_Undef";
  221. $hash->{GetFn} = "SMAInverter_Get";
  222. $hash->{AttrList} = "interval " .
  223. "detail-level:0,1,2 " .
  224. "disable:1,0 " .
  225. "mode:manual,automatic ".
  226. "offset ".
  227. "suppressSleep:1,0 ".
  228. "SBFSpotComp:1,0 " .
  229. "showproctime:1,0 ".
  230. "timeout " .
  231. "target-susyid " .
  232. "target-serial " .
  233. $readingFnAttributes;
  234. $hash->{AttrFn} = "SMAInverter_Attr";
  235. }
  236. ###############################################################
  237. # SMAInverter Define
  238. ###############################################################
  239. sub SMAInverter_Define($$) {
  240. my ($hash, $def) = @_;
  241. my @a = split("[ \t][ \t]*", $def);
  242. return "Error: Perl module ".$MissModulSocket." is missing.
  243. Install it on Debian with: sudo apt-get install libio-socket-multicast-perl" if($MissModulSocket);
  244. return "Error: Perl module ".$MissModulDateTime." is missing.
  245. Install it on Debian with: sudo apt-get install libdatetime-perl" if($MissModulDateTime);
  246. return "Wrong syntax: use define <name> SMAInverter <inv-userpwd> <inv-hostname/inv-ip > " if ((int(@a) < 4) and (int(@a) > 5));
  247. my $name = $hash->{NAME};
  248. $hash->{LASTUPDATE} = 0;
  249. $hash->{INTERVAL} = $hash->{HELPER}{INTERVAL} = AttrVal($name, "interval", 60);
  250. $hash->{VERSION} = $SMAInverterVersion;
  251. $hash->{HELPER}{FAULTEDCYCLES} = 0;
  252. delete($hash->{HELPER}{AVERAGEBUF}) if($hash->{HELPER}{AVERAGEBUF});
  253. # protocol related defaults
  254. $hash->{HELPER}{MYSUSYID} = 233; # random number, has to be different from any device in local network
  255. $hash->{HELPER}{MYSERIALNUMBER} = 123321123; # random number, has to be different from any device in local network
  256. $hash->{HELPER}{DEFAULT_TARGET_SUSYID} = 0xFFFF; # 0xFFFF is any susyid
  257. $hash->{HELPER}{DEFAULT_TARGET_SERIAL} = 0xFFFFFFFF; # 0xFFFFFFFF is any serialnumber
  258. $hash->{HELPER}{PKT_ID} = 0x8001; # Packet ID
  259. $hash->{HELPER}{MAXBYTES} = 300; # constant MAXBYTES scalar 300
  260. my ($IP,$Host,$Caps);
  261. my $Pass = $a[2]; # to do: check 1-12 Chars
  262. # extract IP or Hostname from $a[3]
  263. if (!defined $Host) {
  264. if ( $a[3] =~ /^([A-Za-z0-9_.])/ ) {
  265. $Host = $a[3];
  266. }
  267. }
  268. if (!defined $Host) {
  269. return "Argument:{$a[3]} not accepted as Host or IP. Read device specific help file.";
  270. }
  271. $hash->{PASS} = $Pass;
  272. $hash->{HOST} = $Host;
  273. InternalTimer(gettimeofday()+5, "SMAInverter_GetData", $hash, 0); # Start Hauptroutine
  274. return undef;
  275. }
  276. ###############################################################
  277. # SMAInverter Undefine
  278. ###############################################################
  279. sub SMAInverter_Undef($$) {
  280. my ($hash, $name) = @_;
  281. RemoveInternalTimer($hash);
  282. BlockingKill($hash->{HELPER}{RUNNING_PID});
  283. return undef;
  284. }
  285. ###############################################################
  286. # SMAInverter Get
  287. ###############################################################
  288. sub SMAInverter_Get($$) {
  289. my ($hash, @a) = @_;
  290. return "\"get X\" needs at least an argument" if ( @a < 2 );
  291. my $name = shift @a;
  292. my $opt = shift @a;
  293. my $timeout = AttrVal($name, "timeout", 60);
  294. my $getlist = "Unknown argument $opt, choose one of ".
  295. "data:noArg ";
  296. return "module is disabled" if(IsDisabled($name));
  297. if ($opt eq "data") {
  298. SMAInverter_GetData($hash);
  299. } else {
  300. return "$getlist";
  301. }
  302. return undef;
  303. }
  304. ###############################################################
  305. # SMAInverter Attr
  306. ###############################################################
  307. sub SMAInverter_Attr(@) {
  308. my ($cmd,$name,$aName,$aVal) = @_;
  309. # $cmd can be "del" or "set"
  310. # $name is device name
  311. # aName and aVal are Attribute name and value
  312. my $hash = $defs{$name};
  313. my $do;
  314. if ($aName eq "mode") {
  315. if ($cmd eq "set" && $aVal eq "manual") {
  316. $hash->{INTERVAL} = $aVal;
  317. } else {
  318. $hash->{INTERVAL} = $hash->{HELPER}{INTERVAL};
  319. }
  320. InternalTimer(time+5, 'SMAInverter_GetData', $hash, 0);
  321. }
  322. if ($aName eq "disable") {
  323. if($cmd eq "set") {
  324. $do = ($aVal) ? 1 : 0;
  325. }
  326. $do = 0 if($cmd eq "del");
  327. my $val = ($do == 1 ? "disabled" : "initialized");
  328. readingsSingleUpdate($hash, "state", $val, 1);
  329. if ($do == 0) {
  330. my $mode = AttrVal($name, "mode", "automatic");
  331. RemoveInternalTimer($hash);
  332. InternalTimer(time+5, 'SMAInverter_GetData', $hash, 0);
  333. } else {
  334. RemoveInternalTimer($hash);
  335. }
  336. }
  337. if ($aName eq "detail-level") {
  338. delete $defs{$name}{READINGS};
  339. }
  340. if ($aName eq "SBFSpotComp") {
  341. delete $defs{$name}{READINGS};
  342. }
  343. if ($aName eq "interval") {
  344. if ($cmd eq "set") {
  345. $hash->{HELPER}{INTERVAL} = $aVal;
  346. $hash->{INTERVAL} = $aVal if(AttrVal($name, "mode", "") ne "manual");
  347. delete($hash->{HELPER}{AVERAGEBUF}) if($hash->{HELPER}{AVERAGEBUF});
  348. Log3 $name, 3, "$name - Set $aName to $aVal";
  349. } else {
  350. $hash->{INTERVAL} = $hash->{HELPER}{INTERVAL} = 60;
  351. }
  352. }
  353. if ($cmd eq "set" && $aName eq "offset") {
  354. if($aVal !~ /^\d+$/ || $aVal < 0 || $aVal > 7200) { return "The Value of $aName is not valid. Use value between 0 ... 7200 !";}
  355. }
  356. if ($cmd eq "set" && $aName eq "timeout") {
  357. unless ($aVal =~ /^[0-9]+$/) { return " The Value for $aName is not valid. Use only figures 1-9 !";}
  358. }
  359. return;
  360. }
  361. ###############################################################
  362. # Hauptschleife Datenabruf
  363. ###############################################################
  364. sub SMAInverter_GetData($) {
  365. my ($hash) = @_;
  366. my $name = $hash->{NAME};
  367. my $interval = AttrVal($name, "interval", 60);
  368. my $timeout = AttrVal($name, "timeout", 60);
  369. RemoveInternalTimer($hash, "SMAInverter_GetData");
  370. if ($init_done != 1) {
  371. InternalTimer(gettimeofday()+5, "SMAInverter_GetData", $hash, 0);
  372. return;
  373. }
  374. return if(IsDisabled($name));
  375. if (exists($hash->{HELPER}{RUNNING_PID})) {
  376. Log3 ($name, 3, "SMAInverter $name - WARNING - old process $hash->{HELPER}{RUNNING_PID}{pid} will be killed now to start a new BlockingCall");
  377. BlockingKill($hash->{HELPER}{RUNNING_PID});
  378. }
  379. Log3 ($name, 4, "$name - ###############################################################");
  380. Log3 ($name, 4, "$name - ########## Begin of new SMAInverter get data cycle ##########");
  381. Log3 ($name, 4, "$name - ###############################################################");
  382. Log3 ($name, 4, "$name - timeout cycles since module start: $hash->{HELPER}{FAULTEDCYCLES}");
  383. # decide of operation
  384. if(AttrVal($name,"mode","automatic") eq "automatic") {
  385. # automatic operation mode
  386. $hash->{HELPER}{RUNNING_PID} = BlockingCall("getstatus_DoParse", "$name", "getstatus_ParseDone", $timeout, "SMAI_ParseAborted", $hash);
  387. InternalTimer(gettimeofday()+$interval, "SMAInverter_GetData", $hash, 0);
  388. } else {
  389. # manual operation mode
  390. $hash->{HELPER}{RUNNING_PID} = BlockingCall("getstatus_DoParse", "$name", "getstatus_ParseDone", $timeout, "SMAI_ParseAborted", $hash);
  391. }
  392. return;
  393. }
  394. ###############################################################
  395. # non-blocking Inverter Datenabruf
  396. ###############################################################
  397. sub getstatus_DoParse($) {
  398. my ($name) = @_;
  399. my $hash = $defs{$name};
  400. my $interval = AttrVal($name, "interval", 60);
  401. my $sc = AttrVal($name, "SBFSpotComp", 0);
  402. my ($sup_EnergyProduction,
  403. $sup_SpotDCPower,
  404. $sup_SpotACPower,
  405. $sup_MaxACPower,
  406. $sup_MaxACPower2,
  407. $sup_SpotACTotalPower,
  408. $sup_ChargeStatus,
  409. $sup_SpotDCVoltage,
  410. $sup_SpotACVoltage,
  411. $sup_BatteryInfo,
  412. $sup_SpotGridFrequency,
  413. $sup_TypeLabel,
  414. $sup_OperationTime,
  415. $sup_InverterTemperature,
  416. $sup_GridRelayStatus,
  417. $sup_DeviceStatus);
  418. my ($inv_TYPE, $inv_CLASS,
  419. $inv_SPOT_ETODAY, $inv_SPOT_ETOTAL,
  420. $inv_susyid,
  421. $inv_serial,
  422. $inv_SPOT_PDC1, $inv_SPOT_PDC2,
  423. $inv_SPOT_PAC1, $inv_SPOT_PAC2, $inv_SPOT_PAC3, $inv_SPOT_PACTOT,
  424. $inv_PACMAX1, $inv_PACMAX2, $inv_PACMAX3, $inv_PACMAX1_2,
  425. $inv_ChargeStatus,
  426. $inv_SPOT_UDC1, $inv_SPOT_UDC2,
  427. $inv_SPOT_IDC1, $inv_SPOT_IDC2,
  428. $inv_SPOT_UAC1, $inv_SPOT_UAC2, $inv_SPOT_UAC3,
  429. $inv_SPOT_IAC1, $inv_SPOT_IAC2, $inv_SPOT_IAC3,
  430. $inv_BAT_UDC, $inv_BAT_IDC,
  431. $inv_BAT_CYCLES,
  432. $inv_BAT_TEMP,
  433. $inv_SPOT_FREQ, $inv_SPOT_OPERTM, $inv_SPOT_FEEDTM, $inv_TEMP, $inv_GRIDRELAY, $inv_STATUS,);
  434. my @row_array;
  435. my @array;
  436. my $avg = 0;
  437. my ($ist,$bst,$irt,$brt,$rt);
  438. # Background-Startzeit
  439. $bst = [gettimeofday];
  440. Log3 ($name, 4, "$name -> Start BlockingCall getstatus_DoParse");
  441. # set dependency from surise/sunset used for inverter operation time
  442. my $offset = AttrVal($name,"offset",0);
  443. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  444. my ($sunrise_h,$sunrise_m,$sunrise_s) = split(":",sunrise_abs('-'.$offset));
  445. my ($sunset_h,$sunset_m,$sunset_s) = split(":",sunset_abs('+'.$offset));
  446. my $oper_start = DateTime->new(year=>$year+1900,month=>$mon+1,day=>$mday,hour=>$sunrise_h,minute=>$sunrise_m,second=>$sunrise_s,time_zone=>'local');
  447. my $oper_stop = DateTime->new(year=>$year+1900,month=>$mon+1,day=>$mday,hour=>$sunset_h,minute=>$sunset_m,second=>$sunset_s,time_zone=>'local');
  448. my $dt_now = DateTime->now(time_zone=>'local');
  449. Log3 $name, 4, "$name - current time: ".$dt_now->dmy('.')." ".$dt_now->hms;
  450. Log3 $name, 4, "$name - operation time begin: ".$oper_start->dmy('.')." ".$oper_start->hms;
  451. Log3 $name, 4, "$name - operation time end: ".$oper_stop->dmy('.')." ".$oper_stop->hms;
  452. my $opertime_start = $oper_start->dmy('.')." ".$oper_start->hms;
  453. my $opertime_stop = $oper_stop->dmy('.')." ".$oper_stop->hms;
  454. if (($oper_start <= $dt_now && $dt_now <= $oper_stop) || AttrVal($name,"suppressSleep",0)) {
  455. # normal operation or suppressed sleepmode
  456. # Abfrage Inverter Startzeit
  457. $ist = [gettimeofday];
  458. # Get the current attributes
  459. my $detail_level = AttrVal($name, "detail-level", 0);
  460. # Aufbau Command-Array
  461. my @commands = ("sup_TypeLabel", # Check TypeLabel
  462. "sup_EnergyProduction", # Check EnergyProduction
  463. "sup_SpotDCPower", # Check SpotDCPower
  464. "sup_SpotACPower", # Check SpotACPower
  465. "sup_SpotACTotalPower", # Check SpotACTotalPower
  466. "sup_ChargeStatus" # Check BatteryChargeStatus
  467. );
  468. if($detail_level > 0) {
  469. # Detail Level 1 or 2 >> get voltage and current levels
  470. push(@commands, "sup_SpotDCVoltage"); # Check SpotDCVoltage
  471. push(@commands, "sup_SpotACVoltage"); # Check SpotACVoltage
  472. push(@commands, "sup_BatteryInfo"); # Check BatteryInfo
  473. }
  474. if($detail_level > 1) {
  475. # Detail Level 2 >> get all data
  476. push(@commands, "sup_SpotGridFrequency"); # Check SpotGridFrequency
  477. push(@commands, "sup_OperationTime"); # Check OperationTime
  478. push(@commands, "sup_InverterTemperature"); # Check InverterTemperature
  479. push(@commands, "sup_MaxACPower"); # Check MaxACPower
  480. push(@commands, "sup_MaxACPower2"); # Check MaxACPower2
  481. push(@commands, "sup_GridRelayStatus"); # Check GridRelayStatus
  482. push(@commands, "sup_DeviceStatus"); # Check DeviceStatus
  483. }
  484. if(SMA_logon($hash->{HOST}, $hash->{PASS}, $hash)) {
  485. Log3 $name, 5, "$name - Logged in now";
  486. foreach my $i(@commands) {
  487. if ($i eq "sup_TypeLabel") {
  488. ($sup_TypeLabel,$inv_TYPE,$inv_CLASS,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x58000200, 0x00821E00, 0x008220FF);
  489. }
  490. elsif ($i eq "sup_EnergyProduction") {
  491. ($sup_EnergyProduction,$inv_SPOT_ETODAY,$inv_SPOT_ETOTAL,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x54000200, 0x00260100, 0x002622FF);
  492. }
  493. elsif ($i eq "sup_SpotDCPower") {
  494. ($sup_SpotDCPower,$inv_SPOT_PDC1,$inv_SPOT_PDC2,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x53800200, 0x00251E00, 0x00251EFF);
  495. }
  496. elsif ($i eq "sup_SpotACPower") {
  497. ($sup_SpotACPower,$inv_SPOT_PAC1,$inv_SPOT_PAC2,$inv_SPOT_PAC3,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00464000, 0x004642FF);
  498. }
  499. elsif ($i eq "sup_SpotACTotalPower") {
  500. ($sup_SpotACTotalPower,$inv_SPOT_PACTOT,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00263F00, 0x00263FFF);
  501. }
  502. elsif ($i eq "sup_ChargeStatus") {
  503. ($sup_ChargeStatus,$inv_ChargeStatus,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00295A00, 0x00295AFF);
  504. }
  505. elsif ($i eq "sup_SpotDCVoltage") {
  506. ($sup_SpotDCVoltage,$inv_SPOT_UDC1,$inv_SPOT_UDC2,$inv_SPOT_IDC1,$inv_SPOT_IDC2,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x53800200, 0x00451F00, 0x004521FF);
  507. }
  508. elsif ($i eq "sup_SpotACVoltage") {
  509. ($sup_SpotACVoltage,$inv_SPOT_UAC1,$inv_SPOT_UAC2,$inv_SPOT_UAC3,$inv_SPOT_IAC1,$inv_SPOT_IAC2,$inv_SPOT_IAC3,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00464800, 0x004655FF);
  510. }
  511. elsif ($i eq "sup_BatteryInfo") {
  512. ($sup_BatteryInfo,$inv_BAT_CYCLES,$inv_BAT_TEMP,$inv_BAT_UDC,$inv_BAT_IDC,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00491E00, 0x00495DFF);
  513. }
  514. elsif ($i eq "sup_SpotGridFrequency") {
  515. ($sup_SpotGridFrequency,$inv_SPOT_FREQ,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00465700, 0x004657FF);
  516. }
  517. elsif ($i eq "sup_OperationTime") {
  518. ($sup_OperationTime,$inv_SPOT_OPERTM,$inv_SPOT_FEEDTM,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x54000200, 0x00462E00, 0x00462FFF);
  519. }
  520. elsif ($i eq "sup_InverterTemperature") {
  521. ($sup_InverterTemperature,$inv_TEMP,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x52000200, 0x00237700, 0x002377FF);
  522. }
  523. elsif ($i eq "sup_MaxACPower") {
  524. ($sup_MaxACPower,$inv_PACMAX1,$inv_PACMAX2,$inv_PACMAX3,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00411E00, 0x004120FF);
  525. }
  526. elsif ($i eq "sup_MaxACPower2") {
  527. ($sup_MaxACPower2,$inv_PACMAX1_2,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51000200, 0x00832A00, 0x00832AFF);
  528. }
  529. elsif ($i eq "sup_GridRelayStatus") {
  530. ($sup_GridRelayStatus,$inv_GRIDRELAY,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51800200, 0x00416400, 0x004164FF);
  531. }
  532. elsif ($i eq "sup_DeviceStatus") {
  533. ($sup_DeviceStatus,$inv_STATUS,$inv_susyid,$inv_serial) = SMA_command($hash, $hash->{HOST}, 0x51800200, 0x00214800, 0x002148FF);
  534. }
  535. }
  536. # nothing more to do, just log out
  537. SMA_logout($hash,$hash->{HOST});
  538. # Inverter Laufzeit ermitteln
  539. $irt = tv_interval($ist);
  540. # Aufbau Ergebnis-Array
  541. push(@row_array, "modulstate normal"."\n");
  542. push(@row_array, "opertime_start ".$opertime_start."\n");
  543. push(@row_array, "opertime_stop ".$opertime_stop."\n");
  544. # Durchschnittswerteberechnung Energieerzeugung der letzten 5, 10, 15 Messungen
  545. my ($sum05, $sum10, $sum15);
  546. my $cnt05 = int(300/$interval); # Anzahl der Zyklen innerhalb 5 Minuten
  547. my $cnt10 = int(600/$interval); # Anzahl der Zyklen innerhalb 10 Minuten
  548. my $cnt15 = int(900/$interval); # Anzahl der Zyklen innerhalb 15 Minuten = Summe aller Messzyklen
  549. my $cntsum = $cnt15+1; # Sicherheitszuschlag Summe Anzahl aller Zyklen
  550. my @averagebuf;
  551. if ($sup_TypeLabel && $sup_EnergyProduction && $inv_CLASS eq 8001) {
  552. # only for this block because of warnings if values not set at restart
  553. no warnings 'uninitialized';
  554. if (!$hash->{HELPER}{AVERAGEBUF}) {
  555. for my $count (0..$cntsum) {
  556. # fill with new values
  557. $inv_SPOT_PACTOT = $inv_SPOT_PACTOT?$inv_SPOT_PACTOT:0;
  558. push(@averagebuf, $inv_SPOT_PACTOT);
  559. }
  560. } else {
  561. @averagebuf = split(/,/, $hash->{HELPER}{AVERAGEBUF})
  562. }
  563. # rechtes Element aus average buffer löschen
  564. pop(@averagebuf);
  565. # und links mit neuem Wert füllen
  566. unshift(@averagebuf, $inv_SPOT_PACTOT);
  567. $avg = join(',', @averagebuf);
  568. # calculate average energy and write to array for generate readings
  569. my $k = 1;
  570. my $avgsum = $averagebuf[0];
  571. while ($k < $cntsum) {
  572. $avgsum = $avgsum + $averagebuf[$k] if($averagebuf[$k]);
  573. if ($k == $cnt05) {
  574. $sum05 = $avgsum;
  575. Log3 $name, 5, "$name - CNT05: $cnt05 SUM05: $sum05";
  576. }
  577. if ($k == $cnt10) {
  578. $sum10 = $avgsum;
  579. Log3 $name, 5, "$name - CNT10: $cnt10 SUM10: $sum10";
  580. }
  581. if ($k == $cnt15) {
  582. $sum15 = $avgsum;
  583. Log3 $name, 5, "$name - CNT15: $cnt15 SUM15: $sum15";
  584. }
  585. $k++;
  586. }
  587. my $AvP05 = int( $sum05 / ($cnt05+1) );
  588. my $AvP10 = int( $sum10 / ($cnt10+1) );
  589. my $AvP15 = int( $sum15 / ($cnt15+1) );
  590. Log3 $name, 5, "$name - Content of Averagebuffer:";
  591. Log3 $name, 5, "$name - $avg";
  592. Log3 $name, 5, "$name - avg_power_lastminutes_05 = $AvP05, avg_power_lastminutes_10 = $AvP10, avg_power_lastminutes_15 = $AvP15";
  593. push(@row_array, "avg_power_lastminutes_05 ".$AvP05."\n"); # Average Energy (last) 5 minutes
  594. push(@row_array, "avg_power_lastminutes_10 ".$AvP10."\n"); # Average Energy (last) 10 minutes
  595. push(@row_array, "avg_power_lastminutes_15 ".$AvP15."\n"); # Average Energy (last) 15 minutes
  596. use warnings;
  597. }
  598. if ($sc) { # SBFSpot Kompatibilitätsmodus
  599. if($sup_EnergyProduction) {
  600. push(@row_array, "etotal ".($inv_SPOT_ETOTAL/1000)."\n");
  601. push(@row_array, "etoday ".($inv_SPOT_ETODAY/1000)."\n");
  602. }
  603. if($sup_SpotDCPower) {
  604. push(@row_array, "string_1_pdc ".sprintf("%.3f",$inv_SPOT_PDC1/1000)."\n");
  605. push(@row_array, "string_2_pdc ".sprintf("%.3f",$inv_SPOT_PDC2/1000)."\n");
  606. }
  607. if($sup_SpotACPower) {
  608. push(@row_array, "phase_1_pac ".sprintf("%.3f",$inv_SPOT_PAC1/1000)."\n");
  609. push(@row_array, "phase_2_pac ".sprintf("%.3f",$inv_SPOT_PAC2/1000)."\n");
  610. push(@row_array, "phase_3_pac ".sprintf("%.3f",$inv_SPOT_PAC3/1000)."\n");
  611. }
  612. if($sup_SpotACTotalPower) {
  613. push(@row_array, "total_pac ".sprintf("%.3f",$inv_SPOT_PACTOT/1000)."\n");
  614. push(@row_array, "state ".sprintf("%.3f",$inv_SPOT_PACTOT/1000)."\n");
  615. }
  616. if($sup_ChargeStatus) {
  617. push(@row_array, "chargestatus ".$inv_ChargeStatus."\n");
  618. }
  619. if($inv_CLASS && $inv_CLASS eq 8007) {
  620. if($inv_SPOT_PACTOT < 0) {
  621. push(@row_array, "power_out "."0"."\n");
  622. push(@row_array, "power_in ".(-1 * $inv_SPOT_PACTOT)."\n");
  623. } else {
  624. push(@row_array, "power_out ".$inv_SPOT_PACTOT."\n");
  625. push(@row_array, "power_in "."0"."\n");
  626. }
  627. }
  628. if($detail_level > 0) {
  629. # For Detail Level 1
  630. if($sup_SpotDCVoltage) {
  631. push(@row_array, "string_1_udc ".sprintf("%.2f",$inv_SPOT_UDC1)."\n");
  632. push(@row_array, "string_2_udc ".sprintf("%.2f",$inv_SPOT_UDC2)."\n");
  633. push(@row_array, "string_1_idc ".sprintf("%.3f",$inv_SPOT_IDC1)."\n");
  634. push(@row_array, "string_2_idc ".sprintf("%.3f",$inv_SPOT_IDC2)."\n");
  635. }
  636. if($sup_SpotACVoltage) {
  637. push(@row_array, "phase_1_uac ".sprintf("%.2f",$inv_SPOT_UAC1)."\n");
  638. push(@row_array, "phase_2_uac ".sprintf("%.2f",$inv_SPOT_UAC2)."\n");
  639. push(@row_array, "phase_3_uac ".sprintf("%.2f",$inv_SPOT_UAC3)."\n");
  640. push(@row_array, "phase_1_iac ".sprintf("%.3f",$inv_SPOT_IAC1)."\n");
  641. push(@row_array, "phase_2_iac ".sprintf("%.3f",$inv_SPOT_IAC2)."\n");
  642. push(@row_array, "phase_3_iac ".sprintf("%.3f",$inv_SPOT_IAC3)."\n");
  643. }
  644. if($sup_BatteryInfo) {
  645. push(@row_array, "bat_udc ".$inv_BAT_UDC."\n");
  646. push(@row_array, "bat_idc ".$inv_BAT_IDC."\n");
  647. }
  648. }
  649. if($detail_level > 1) {
  650. # For Detail Level 2
  651. if($sup_BatteryInfo) {
  652. push(@row_array,"bat_cycles ".$inv_BAT_CYCLES."\n");
  653. push(@row_array, "bat_temp ".$inv_BAT_TEMP."\n");
  654. }
  655. if($sup_SpotGridFrequency) {
  656. push(@row_array, "grid_freq. ".sprintf("%.2f",$inv_SPOT_FREQ)."\n");
  657. }
  658. if($sup_TypeLabel) {
  659. push(@row_array, "device_type ".devtype($inv_TYPE)."\n");
  660. push(@row_array, "device_class ".classtype($inv_CLASS)."\n");
  661. push(@row_array, "susyid ".$inv_susyid." - SN: ".$inv_serial."\n") if($inv_susyid && $inv_serial);
  662. push(@row_array, "device_name "."SN: ".$inv_serial."\n") if($inv_serial);
  663. push(@row_array, "serial_number ".$inv_serial."\n") if($inv_serial);
  664. }
  665. if($sup_MaxACPower) {
  666. push(@row_array, "pac_max_phase_1 ".$inv_PACMAX1."\n");
  667. push(@row_array, "pac_max_phase_2 ".$inv_PACMAX2."\n");
  668. push(@row_array, "pac_max_phase_3 ".$inv_PACMAX3."\n");
  669. }
  670. if($sup_MaxACPower2) {
  671. push(@row_array, "pac_max_phase_1_2 ".$inv_PACMAX1_2."\n");
  672. }
  673. if($sup_InverterTemperature) {
  674. push(@row_array, "device_temperature ".sprintf("%.1f",$inv_TEMP)."\n");
  675. }
  676. if($sup_OperationTime) {
  677. push(@row_array, "feed-in_time ".$inv_SPOT_FEEDTM."\n");
  678. push(@row_array, "operation_time ".$inv_SPOT_OPERTM."\n");
  679. }
  680. if($sup_GridRelayStatus) {
  681. push(@row_array, "gridrelay_status ".StatusText($inv_GRIDRELAY)."\n");
  682. }
  683. if($sup_DeviceStatus) {
  684. push(@row_array, "device_status ".StatusText($inv_STATUS)."\n");
  685. }
  686. }
  687. } else { # kein SBFSpot Compatibility Mode
  688. if($sup_EnergyProduction) {
  689. push(@row_array, "SPOT_ETOTAL ".$inv_SPOT_ETOTAL."\n");
  690. push(@row_array, "SPOT_ETODAY ".$inv_SPOT_ETODAY."\n");
  691. }
  692. if($sup_SpotDCPower) {
  693. push(@row_array, "SPOT_PDC1 ".$inv_SPOT_PDC1."\n");
  694. push(@row_array, "SPOT_PDC2 ".$inv_SPOT_PDC2."\n");
  695. }
  696. if($sup_SpotACPower) {
  697. push(@row_array, "SPOT_PAC1 ".$inv_SPOT_PAC1."\n");
  698. push(@row_array, "SPOT_PAC2 ".$inv_SPOT_PAC2."\n");
  699. push(@row_array, "SPOT_PAC3 ".$inv_SPOT_PAC3."\n");
  700. }
  701. if($sup_SpotACTotalPower) {
  702. push(@row_array, "SPOT_PACTOT ".$inv_SPOT_PACTOT."\n");
  703. push(@row_array, "state ".$inv_SPOT_PACTOT."\n");
  704. }
  705. if($sup_ChargeStatus) {
  706. push(@row_array, "ChargeStatus ".$inv_ChargeStatus."\n");
  707. }
  708. if($inv_CLASS && $inv_CLASS eq 8007) {
  709. if($inv_SPOT_PACTOT < 0) {
  710. push(@row_array, "POWER_OUT "."0"."\n");
  711. push(@row_array, "POWER_IN ".(-1 * $inv_SPOT_PACTOT)."\n");
  712. } else {
  713. push(@row_array, "POWER_OUT ".$inv_SPOT_PACTOT."\n");
  714. push(@row_array, "POWER_IN "."0"."\n");
  715. }
  716. }
  717. if($detail_level > 0) {
  718. # For Detail Level 1
  719. if($sup_SpotDCVoltage) {
  720. push(@row_array, "SPOT_UDC1 ".$inv_SPOT_UDC1."\n");
  721. push(@row_array, "SPOT_UDC2 ".$inv_SPOT_UDC2."\n");
  722. push(@row_array, "SPOT_IDC1 ".$inv_SPOT_IDC1."\n");
  723. push(@row_array, "SPOT_IDC2 ".$inv_SPOT_IDC2."\n");
  724. }
  725. if($sup_SpotACVoltage) {
  726. push(@row_array, "SPOT_UAC1 ".$inv_SPOT_UAC1."\n");
  727. push(@row_array, "SPOT_UAC2 ".$inv_SPOT_UAC2."\n");
  728. push(@row_array, "SPOT_UAC3 ".$inv_SPOT_UAC3."\n");
  729. push(@row_array, "SPOT_IAC1 ".$inv_SPOT_IAC1."\n");
  730. push(@row_array, "SPOT_IAC2 ".$inv_SPOT_IAC2."\n");
  731. push(@row_array, "SPOT_IAC3 ".$inv_SPOT_IAC3."\n");
  732. }
  733. if($sup_BatteryInfo) {
  734. push(@row_array, "BAT_UDC ".$inv_BAT_UDC."\n");
  735. push(@row_array, "BAT_IDC ".$inv_BAT_IDC."\n");
  736. }
  737. }
  738. if($detail_level > 1) {
  739. # For Detail Level 2
  740. if($sup_BatteryInfo) {
  741. push(@row_array, "BAT_CYCLES ".$inv_BAT_CYCLES."\n");
  742. push(@row_array, "BAT_TEMP ".$inv_BAT_TEMP."\n");
  743. }
  744. if($sup_SpotGridFrequency) {
  745. push(@row_array, "SPOT_FREQ ".$inv_SPOT_FREQ."\n");
  746. }
  747. if($sup_TypeLabel) {
  748. push(@row_array, "INV_TYPE ".devtype($inv_TYPE)."\n");
  749. push(@row_array, "INV_CLASS ".classtype($inv_CLASS)."\n");
  750. push(@row_array, "SUSyID ".$inv_susyid."\n") if($inv_susyid);
  751. push(@row_array, "Serialnumber ".$inv_serial."\n") if($inv_serial);
  752. }
  753. if($sup_MaxACPower) {
  754. push(@row_array, "INV_PACMAX1 ".$inv_PACMAX1."\n");
  755. push(@row_array, "INV_PACMAX2 ".$inv_PACMAX2."\n");
  756. push(@row_array, "INV_PACMAX3 ".$inv_PACMAX3."\n");
  757. }
  758. if($sup_MaxACPower2) {
  759. push(@row_array, "INV_PACMAX1_2 ".$inv_PACMAX1_2."\n");
  760. }
  761. if($sup_InverterTemperature) {
  762. push(@row_array, "INV_TEMP ".$inv_TEMP."\n");
  763. }
  764. if($sup_OperationTime) {
  765. push(@row_array, "SPOT_FEEDTM ".$inv_SPOT_FEEDTM."\n");
  766. push(@row_array, "SPOT_OPERTM ".$inv_SPOT_OPERTM."\n");
  767. }
  768. if($sup_GridRelayStatus) {
  769. push(@row_array, "INV_GRIDRELAY ".StatusText($inv_GRIDRELAY)."\n");
  770. }
  771. if($sup_DeviceStatus) {
  772. push(@row_array, "INV_STATUS ".StatusText($inv_STATUS)."\n");
  773. }
  774. }
  775. }
  776. } else {
  777. # Login failed/not possible
  778. push(@row_array, "state Login failed"."\n");
  779. push(@row_array, "modulstate login failed"."\n");
  780. }
  781. } else {
  782. # sleepmode at current time and not suppressed
  783. push(@row_array, "modulstate sleep"."\n");
  784. push(@row_array, "opertime_start ".$opertime_start."\n");
  785. push(@row_array, "opertime_stop ".$opertime_stop."\n");
  786. push(@row_array, "state done"."\n");
  787. }
  788. Log3 ($name, 5, "$name -> row_array before encoding:");
  789. foreach my $row (@row_array) {
  790. chomp $row;
  791. Log3 ($name, 5, "$name -> $row");
  792. }
  793. # encoding result
  794. my $rowlist = join('|', @row_array);
  795. $rowlist = encode_base64($rowlist,"");
  796. # Background-Laufzeit ermitteln
  797. $brt = tv_interval($bst);
  798. $rt = ($irt?$irt:'').",".$brt;
  799. Log3 ($name, 4, "$name -> BlockingCall getstatus_DoParse finished");
  800. return "$name|$rowlist|$avg|$rt";
  801. }
  802. ###############################################################
  803. # Auswertung non-blocking Inverter Datenabruf
  804. ###############################################################
  805. sub getstatus_ParseDone ($) {
  806. my ($string) = @_;
  807. my @a = split("\\|",$string);
  808. my $name = $a[0];
  809. my $hash = $defs{$name};
  810. my $rowlist = decode_base64($a[1]);
  811. $hash->{HELPER}{AVERAGEBUF} = $a[2] if($a[2]);
  812. my $rt = $a[3];
  813. my ($irt,$brt) = split(",", $rt);
  814. Log3 ($name, 4, "$name -> Start BlockingCall getstatus_ParseDone");
  815. # proctime Readings löschen
  816. if(!AttrVal($name, "showproctime", undef)) {
  817. delete($defs{$name}{READINGS}{inverter_processing_time});
  818. delete($defs{$name}{READINGS}{background_processing_time});
  819. } else {
  820. delete($defs{$name}{READINGS}{inverter_processing_time}) if(!$irt);
  821. }
  822. # Get current time
  823. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  824. $hash->{LASTUPDATE} = sprintf "%02d.%02d.%04d / %02d:%02d:%02d" , $mday , $mon+=1 ,$year+=1900 , $hour , $min , $sec ;
  825. my @row_array = split("\\|", $rowlist);
  826. Log3 ($name, 5, "$name -> row_array after decoding:");
  827. foreach my $row (@row_array) {
  828. chomp $row;
  829. Log3 ($name, 5, "$name -> $row");
  830. }
  831. readingsBeginUpdate($hash);
  832. foreach my $row (@row_array) {
  833. chomp $row;
  834. my @a = split(" ", $row, 2);
  835. readingsBulkUpdate($hash, $a[0], $a[1]);
  836. }
  837. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt)) if(AttrVal($name, "showproctime", undef));
  838. readingsBulkUpdate($hash, "inverter_processing_time", sprintf("%.4f",$irt)) if(AttrVal($name, "showproctime", undef) && $irt);
  839. readingsEndUpdate($hash, 1);
  840. delete($hash->{HELPER}{RUNNING_PID});
  841. Log3 ($name, 4, "$name -> BlockingCall getstatus_ParseDone finished");
  842. return;
  843. }
  844. ###############################################################
  845. # Abbruchroutine Timeout Inverter Abfrage
  846. ###############################################################
  847. sub SMAI_ParseAborted($) {
  848. my ($hash) = @_;
  849. my $name = $hash->{NAME};
  850. my $discycles = $hash->{HELPER}{FAULTEDCYCLES};
  851. # count of timeouts since module start
  852. $discycles++;
  853. $hash->{HELPER}{FAULTEDCYCLES} = $discycles;
  854. Log3 ($name, 1, "SMAInverter $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} timed out");
  855. readingsSingleUpdate($hash, "state", "timeout", 1);
  856. delete($hash->{HELPER}{RUNNING_PID});
  857. }
  858. ##########################################################################
  859. # SMA Command Execution
  860. ##########################################################################
  861. sub SMA_command($$$$$) {
  862. # Parameters: $hash - host - command - first - last
  863. my ($hash,$host,$command,$first,$last) = @_;
  864. my $name = $hash->{NAME};
  865. my $cmdheader = "534D4100000402A00000000100";
  866. my $pktlength = "26"; # length = 38 for data commands
  867. my $esignature = "0010606509A0";
  868. my ($inv_TYPE, $inv_CLASS,
  869. $inv_SPOT_ETODAY, $inv_SPOT_ETOTAL,
  870. $inv_susyid,
  871. $inv_serial,
  872. $inv_SPOT_PDC1, $inv_SPOT_PDC2,
  873. $inv_SPOT_PAC1, $inv_SPOT_PAC2, $inv_SPOT_PAC3, $inv_SPOT_PACTOT,
  874. $inv_PACMAX1, $inv_PACMAX2, $inv_PACMAX3, $inv_PACMAX1_2,
  875. $inv_ChargeStatus,
  876. $inv_SPOT_UDC1, $inv_SPOT_UDC2,
  877. $inv_SPOT_IDC1, $inv_SPOT_IDC2,
  878. $inv_SPOT_UAC1, $inv_SPOT_UAC2, $inv_SPOT_UAC3,
  879. $inv_SPOT_IAC1, $inv_SPOT_IAC2, $inv_SPOT_IAC3,
  880. $inv_BAT_UDC, $inv_BAT_IDC,
  881. $inv_BAT_CYCLES,
  882. $inv_BAT_TEMP,
  883. $inv_SPOT_FREQ, $inv_SPOT_OPERTM, $inv_SPOT_FEEDTM, $inv_TEMP, $inv_GRIDRELAY, $inv_STATUS);
  884. my $mysusyid = $hash->{HELPER}{MYSUSYID};
  885. my $myserialnumber = $hash->{HELPER}{MYSERIALNUMBER};
  886. my ($cmd, $myID, $target_ID, $spkt_ID, $cmd_ID);
  887. my ($socket,$data,$size,$data_ID);
  888. my ($i, $temp); # Variables for loops and calculation
  889. # Seriennummer und SuSyID des Ziel-WR setzen
  890. my $default_target_susyid = $hash->{HELPER}{DEFAULT_TARGET_SUSYID};
  891. my $default_target_serial = $hash->{HELPER}{DEFAULT_TARGET_SERIAL};
  892. my $target_susyid = AttrVal($name, "target-susyid", $default_target_susyid);
  893. my $target_serial = AttrVal($name, "target-serial", $default_target_serial);
  894. # Define own ID and target ID and packet ID
  895. $myID = ByteOrderShort(substr(sprintf("%04X",$mysusyid),0,4)) . ByteOrderLong(sprintf("%08X",$myserialnumber));
  896. $target_ID = ByteOrderShort(substr(sprintf("%04X",$target_susyid),0,4)) . ByteOrderLong(sprintf("%08X",$target_serial));
  897. # Increasing Packet ID
  898. $hash->{HELPER}{PKT_ID} = $hash->{HELPER}{PKT_ID} + 1;
  899. $spkt_ID = ByteOrderShort(sprintf("%04X",$hash->{HELPER}{PKT_ID}));
  900. $cmd_ID = ByteOrderLong(sprintf("%08X",$command)) . ByteOrderLong(sprintf("%08X",$first)) . ByteOrderLong(sprintf("%08X",$last));
  901. #build final command to send
  902. $cmd = $cmdheader . $pktlength . $esignature . $target_ID . "0000" . $myID . "0000" . "00000000" . $spkt_ID . $cmd_ID . "00000000";
  903. # flush after every write
  904. $| = 1;
  905. # Create Socket and check if successful
  906. $socket = new IO::Socket::INET (PeerHost => $host, PeerPort => 9522, Proto => 'udp',); # open Socket
  907. if (!$socket) {
  908. # in case of error
  909. Log3 $name, 1, "$name - ERROR. Can't open socket to inverter: $!";
  910. return 0;
  911. };
  912. # Send Data
  913. $data = pack("H*",$cmd);
  914. $socket->send($data);
  915. Log3 $name, 3, "$name - Send request $cmd_ID to $host on port 9522";
  916. Log3 $name, 5, "$name - send: $cmd";
  917. # Receive Data and do a first check regarding length
  918. # receive data
  919. $socket->recv($data, $hash->{HELPER}{MAXBYTES});
  920. $size = length($data);
  921. # check if something was received
  922. if (defined $size) {
  923. my $received = unpack("H*", $data);
  924. Log3 $name, 5, "$name - Received: $received";
  925. }
  926. # Nothing received -> exit
  927. if (not defined $size) {
  928. Log3 $name, 1, "$name - Nothing received...";
  929. return 0;
  930. } else {
  931. # We have received something!
  932. if ($size > 58) {
  933. # Check all parameters of answer
  934. my $r_susyid = unpack("v*", substr $data, 20, 2);
  935. my $r_serial = unpack("V*", substr $data, 22, 4);
  936. my $r_pkt_ID = unpack("v*", substr $data, 40, 2);
  937. my $r_error = unpack("V*", substr $data, 36, 4);
  938. if (($r_susyid ne $mysusyid) || ($r_serial ne $myserialnumber) || ($r_pkt_ID ne $hash->{HELPER}{PKT_ID}) || ($r_error ne 0)) {
  939. # Response does not match the parameters we have sent, maybe different target
  940. Log3 $name, 3, "$name - Inverter answer does not match our parameters.";
  941. Log3 $name, 5, "$name - Request/Response: SusyID $mysusyid/$r_susyid, Serial $myserialnumber/$r_serial, Packet ID $hash->{HELPER}{PKT_ID}/$r_pkt_ID, Error $r_error";
  942. $socket->close();
  943. return 0;
  944. }
  945. } else {
  946. Log3 $name, 3, "$name - Format of inverter response does not fit.";
  947. $socket->close();
  948. return 0;
  949. }
  950. }
  951. # All seems ok, data received
  952. $inv_susyid = unpack("v*", substr $data, 28, 2);
  953. $inv_serial = unpack("V*", substr $data, 30, 4);
  954. $socket->close();
  955. if (AttrVal($name, "target-serial", undef)) {
  956. return 0 unless($target_serial eq $inv_serial);
  957. }
  958. if (AttrVal($name, "target-susyid", undef)) {
  959. return 0 unless($target_susyid eq $inv_susyid);
  960. }
  961. # Check the data identifier
  962. $data_ID = unpack("v*", substr $data, 55, 2);
  963. Log3 $name, 5, "$name - Data identifier $data_ID";
  964. if($data_ID eq 0x2601) {
  965. $inv_SPOT_ETOTAL = unpack("V*", substr($data, 62, 4));
  966. $inv_SPOT_ETODAY = unpack("V*", substr $data, 78, 4);
  967. Log3 $name, 5, "$name - Found Data SPOT_ETOTAL=$inv_SPOT_ETOTAL and SPOT_ETODAY=$inv_SPOT_ETODAY";
  968. return (1,$inv_SPOT_ETODAY,$inv_SPOT_ETOTAL,$inv_susyid,$inv_serial);
  969. }
  970. if($data_ID eq 0x251E) {
  971. $inv_SPOT_PDC1 = unpack("V*", substr $data, 62, 4);
  972. $inv_SPOT_PDC2 = unpack("V*", substr $data, 90, 4);
  973. $inv_SPOT_PDC1 = ($inv_SPOT_PDC1 == 2147483648) ? 0 : $inv_SPOT_PDC1;
  974. $inv_SPOT_PDC2 = ($inv_SPOT_PDC2 == 2147483648) ? 0 : $inv_SPOT_PDC2;
  975. Log3 $name, 5, "$name - Found Data SPOT_PDC1=$inv_SPOT_PDC1 and SPOT_PDC2=$inv_SPOT_PDC2";
  976. return (1,$inv_SPOT_PDC1,$inv_SPOT_PDC2,$inv_susyid,$inv_serial);
  977. }
  978. if($data_ID eq 0x4640) {
  979. $inv_SPOT_PAC1 = unpack("l*", substr $data, 62, 4);
  980. if($inv_SPOT_PAC1 eq -2147483648) {$inv_SPOT_PAC1 = 0; } # Catch 0x80000000 as 0 value
  981. $inv_SPOT_PAC2 = unpack("l*", substr $data, 90, 4);
  982. if($inv_SPOT_PAC2 eq -2147483648) {$inv_SPOT_PAC2 = 0; } # Catch 0x80000000 as 0 value
  983. $inv_SPOT_PAC3 = unpack("l*", substr $data, 118, 4);
  984. if($inv_SPOT_PAC3 eq -2147483648) {$inv_SPOT_PAC3 = 0; } # Catch 0x80000000 as 0 value
  985. Log3 $name, 5, "$name - Found Data SPOT_PAC1=$inv_SPOT_PAC1 and SPOT_PAC2=$inv_SPOT_PAC2 and SPOT_PAC3=$inv_SPOT_PAC3";
  986. return (1,$inv_SPOT_PAC1,$inv_SPOT_PAC2,$inv_SPOT_PAC3,$inv_susyid,$inv_serial);
  987. }
  988. if($data_ID eq 0x411E) {
  989. $inv_PACMAX1 = unpack("V*", substr $data, 62, 4);
  990. $inv_PACMAX2 = unpack("V*", substr $data, 90, 4);
  991. $inv_PACMAX3 = unpack("V*", substr $data, 118, 4);
  992. Log3 $name, 5, "$name - Found Data INV_PACMAX1=$inv_PACMAX1 and INV_PACMAX2=$inv_PACMAX2 and INV_PACMAX3=$inv_PACMAX3";
  993. return (1,$inv_PACMAX1,$inv_PACMAX2,$inv_PACMAX3,$inv_susyid,$inv_serial);
  994. }
  995. if($data_ID eq 0x832A) {
  996. $inv_PACMAX1_2 = unpack("V*", substr $data, 62, 4);
  997. Log3 $name, 5, "$name - Found Data INV_PACMAX1_2=$inv_PACMAX1_2";
  998. return (1,$inv_PACMAX1_2,$inv_susyid,$inv_serial);
  999. }
  1000. if($data_ID eq 0x263F) {
  1001. $inv_SPOT_PACTOT = unpack("l*", substr $data, 62, 4);
  1002. if($inv_SPOT_PACTOT eq -2147483648) {$inv_SPOT_PACTOT = 0; } # Catch 0x80000000 as 0 value
  1003. Log3 $name, 5, "$name - Found Data SPOT_PACTOT=$inv_SPOT_PACTOT";
  1004. return (1,$inv_SPOT_PACTOT,$inv_susyid,$inv_serial);
  1005. }
  1006. if($data_ID eq 0x295A) {
  1007. $inv_ChargeStatus = unpack("V*", substr $data, 62, 4);
  1008. Log3 $name, 5, "$name - Found Data Battery Charge Status=$inv_ChargeStatus";
  1009. return (1,$inv_ChargeStatus,$inv_susyid,$inv_serial);
  1010. }
  1011. if($data_ID eq 0x451F) {
  1012. $inv_SPOT_UDC1 = unpack("l*", substr $data, 62, 4);
  1013. $inv_SPOT_UDC2 = unpack("l*", substr $data, 90, 4);
  1014. $inv_SPOT_IDC1 = unpack("l*", substr $data, 118, 4);
  1015. $inv_SPOT_IDC2 = unpack("l*", substr $data, 146, 4);
  1016. if(($inv_SPOT_UDC1 eq -2147483648) || ($inv_SPOT_UDC1 eq 0xFFFFFFFF)) {$inv_SPOT_UDC1 = 0; } else {$inv_SPOT_UDC1 = $inv_SPOT_UDC1 / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1017. if(($inv_SPOT_UDC2 eq -2147483648) || ($inv_SPOT_UDC2 eq 0xFFFFFFFF)) {$inv_SPOT_UDC2 = 0; } else {$inv_SPOT_UDC2 = $inv_SPOT_UDC2 / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1018. if(($inv_SPOT_IDC1 eq -2147483648) || ($inv_SPOT_IDC1 eq 0xFFFFFFFF)) {$inv_SPOT_IDC1 = 0; } else {$inv_SPOT_IDC1 = $inv_SPOT_IDC1 / 1000; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1019. if(($inv_SPOT_IDC2 eq -2147483648) || ($inv_SPOT_IDC2 eq 0xFFFFFFFF)) {$inv_SPOT_IDC2 = 0; } else {$inv_SPOT_IDC2 = $inv_SPOT_IDC2 / 1000; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1020. Log3 $name, 5, "$name - Found Data SPOT_UDC1=$inv_SPOT_UDC1 and SPOT_UDC2=$inv_SPOT_UDC2 and SPOT_IDC1=$inv_SPOT_IDC1 and SPOT_IDC2=$inv_SPOT_IDC2";
  1021. return (1,$inv_SPOT_UDC1,$inv_SPOT_UDC2,$inv_SPOT_IDC1,$inv_SPOT_IDC2,$inv_susyid,$inv_serial);
  1022. }
  1023. if($data_ID eq 0x4648) {
  1024. $inv_SPOT_UAC1 = unpack("l*", substr $data, 62, 4);
  1025. $inv_SPOT_UAC2 = unpack("l*", substr $data, 90, 4);
  1026. $inv_SPOT_UAC3 = unpack("l*", substr $data, 118, 4);
  1027. $inv_SPOT_IAC1 = unpack("l*", substr $data, 146, 4);
  1028. $inv_SPOT_IAC2 = unpack("l*", substr $data, 174, 4);
  1029. $inv_SPOT_IAC3 = unpack("l*", substr $data, 202, 4);
  1030. if(($inv_SPOT_UAC1 eq -2147483648) || ($inv_SPOT_UAC1 eq 0xFFFFFFFF) || $inv_SPOT_UAC1 < 0) {$inv_SPOT_UAC1 = 0; } else {$inv_SPOT_UAC1 = $inv_SPOT_UAC1 / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1031. if(($inv_SPOT_UAC2 eq -2147483648) || ($inv_SPOT_UAC2 eq 0xFFFFFFFF) || $inv_SPOT_UAC2 < 0) {$inv_SPOT_UAC2 = 0; } else {$inv_SPOT_UAC2 = $inv_SPOT_UAC2 / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1032. if(($inv_SPOT_UAC3 eq -2147483648) || ($inv_SPOT_UAC3 eq 0xFFFFFFFF) || $inv_SPOT_UAC3 < 0) {$inv_SPOT_UAC3 = 0; } else {$inv_SPOT_UAC3 = $inv_SPOT_UAC3 / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1033. if(($inv_SPOT_IAC1 eq -2147483648) || ($inv_SPOT_IAC1 eq 0xFFFFFFFF)) {$inv_SPOT_IAC1 = 0; } else {$inv_SPOT_IAC1 = $inv_SPOT_IAC1 / 1000; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1034. if(($inv_SPOT_IAC2 eq -2147483648) || ($inv_SPOT_IAC2 eq 0xFFFFFFFF)) {$inv_SPOT_IAC2 = 0; } else {$inv_SPOT_IAC2 = $inv_SPOT_IAC2 / 1000; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1035. if(($inv_SPOT_IAC3 eq -2147483648) || ($inv_SPOT_IAC3 eq 0xFFFFFFFF)) {$inv_SPOT_IAC3 = 0; } else {$inv_SPOT_IAC3 = $inv_SPOT_IAC3 / 1000; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1036. Log3 $name, 5, "$name - Found Data SPOT_UAC1=$inv_SPOT_UAC1 and SPOT_UAC2=$inv_SPOT_UAC2 and SPOT_UAC3=$inv_SPOT_UAC3 and SPOT_IAC1=$inv_SPOT_IAC1 and SPOT_IAC2=$inv_SPOT_IAC2 and SPOT_IAC3=$inv_SPOT_IAC3";
  1037. return (1,$inv_SPOT_UAC1,$inv_SPOT_UAC2,$inv_SPOT_UAC3,$inv_SPOT_IAC1,$inv_SPOT_IAC2,$inv_SPOT_IAC3,$inv_susyid,$inv_serial);
  1038. }
  1039. if($data_ID eq 0x491E) {
  1040. $inv_BAT_CYCLES = unpack("V*", substr $data, 62, 4);
  1041. $inv_BAT_TEMP = unpack("V*", substr $data, 90, 4) / 10;
  1042. $inv_BAT_UDC = unpack("V*", substr $data, 118, 4) / 100;
  1043. $inv_BAT_IDC = unpack("l*", substr $data, 146, 4);
  1044. if($inv_BAT_IDC eq -2147483648) {$inv_BAT_IDC = 0; } else { $inv_BAT_IDC = $inv_BAT_IDC / 1000;} # Catch 0x80000000 as 0 value
  1045. Log3 $name, 5, "$name - Found Data BAT_CYCLES=$inv_BAT_CYCLES and BAT_TEMP=$inv_BAT_TEMP and BAT_UDC=$inv_BAT_UDC and BAT_IDC=$inv_BAT_IDC";
  1046. return (1,$inv_BAT_CYCLES,$inv_BAT_TEMP,$inv_BAT_UDC,$inv_BAT_IDC,$inv_susyid,$inv_serial);
  1047. }
  1048. if($data_ID eq 0x2377) {
  1049. $inv_TEMP = unpack("l*", substr $data, 62, 4);
  1050. if($inv_TEMP eq -2147483648) {$inv_TEMP = 0; } else { $inv_TEMP = $inv_TEMP / 100;} # Catch 0x80000000 as 0 value
  1051. Log3 $name, 5, "$name - Found Data Inverter Temp=$inv_TEMP";
  1052. return (1,$inv_TEMP,$inv_susyid,$inv_serial);
  1053. }
  1054. if($data_ID eq 0x462E) {
  1055. $inv_SPOT_OPERTM = int(unpack("V*", substr $data, 62, 4) / 36) / 100;
  1056. $inv_SPOT_FEEDTM = int(unpack("V*", substr $data, 78, 4) / 36) / 100;
  1057. Log3 $name, 5, "$name - Found Data SPOT_OPERTM=$inv_SPOT_OPERTM and SPOT_FEEDTM=$inv_SPOT_FEEDTM";
  1058. return (1,$inv_SPOT_OPERTM,$inv_SPOT_FEEDTM,$inv_susyid,$inv_serial);
  1059. }
  1060. if($data_ID eq 0x4657) {
  1061. $inv_SPOT_FREQ = unpack("V*", substr $data, 62, 4);
  1062. if(($inv_SPOT_FREQ eq -2147483648) || ($inv_SPOT_FREQ eq 0xFFFFFFFF)) {$inv_SPOT_FREQ = 0; } else {$inv_SPOT_FREQ = $inv_SPOT_FREQ / 100; } # Catch 0x80000000 and 0xFFFFFFFF as 0 value
  1063. Log3 $name, 5, "$name - Found Data SPOT_FREQ=$inv_SPOT_FREQ";
  1064. return (1,$inv_SPOT_FREQ,$inv_susyid,$inv_serial);
  1065. }
  1066. if($data_ID eq 0x821E) {
  1067. $inv_CLASS = unpack("V*", substr $data, 102, 4) & 0x00FFFFFF;
  1068. $inv_TYPE = unpack("V*", substr $data, 142, 4) & 0x00FFFFFF;
  1069. Log3 $name, 5, "$name - Found Data CLASS=$inv_CLASS and TYPE=$inv_TYPE";
  1070. return (1,$inv_TYPE,$inv_CLASS,$inv_susyid,$inv_serial);
  1071. }
  1072. if($data_ID eq 0x4164) {
  1073. $i = 0;
  1074. $temp = 0;
  1075. $inv_GRIDRELAY = 0x00FFFFFD; # Code for No Information;
  1076. do {
  1077. $temp = unpack("V*", substr $data, 62 + $i*4, 4);
  1078. if(($temp & 0xFF000000) ne 0) { $inv_GRIDRELAY = $temp & 0x00FFFFFF; }
  1079. $i = $i + 1;
  1080. } while ((unpack("V*", substr $data, 62 + $i*4, 4) ne 0x00FFFFFE) && ($i < 5)); # 0x00FFFFFE is the end marker for attributes
  1081. Log3 $name, 5, "$name - Found Data INV_GRIDRELAY=$inv_GRIDRELAY";
  1082. return (1,$inv_GRIDRELAY,$inv_susyid,$inv_serial);
  1083. }
  1084. if($data_ID eq 0x2148) {
  1085. $i = 0;
  1086. $temp = 0;
  1087. $inv_STATUS = 0x00FFFFFD; # Code for No Information;
  1088. do {
  1089. $temp = unpack("V*", substr $data, 62 + $i*4, 4);
  1090. if(($temp & 0xFF000000) ne 0) { $inv_STATUS = $temp & 0x00FFFFFF; }
  1091. $i = $i + 1;
  1092. } while ((unpack("V*", substr $data, 62 + $i*4, 4) ne 0x00FFFFFE) && ($i < 5)); # 0x00FFFFFE is the end marker for attributes
  1093. Log3 $name, 5, "$name - Found Data inv_STATUS=$inv_STATUS";
  1094. return (1,$inv_STATUS,$inv_susyid,$inv_serial);
  1095. }
  1096. return 0;
  1097. }
  1098. ##########################################################################
  1099. # Login
  1100. ##########################################################################
  1101. sub SMA_logon($$$) {
  1102. # Parameters: host - passcode
  1103. my ($host,$pass,$hash) = @_;
  1104. my $cmdheader = "534D4100000402A00000000100";
  1105. my $pktlength = "3A"; # length = 58 for logon command
  1106. my $esignature = "001060650EA0";
  1107. my $name = $hash->{NAME};
  1108. my $mysusyid = $hash->{HELPER}{MYSUSYID};
  1109. my $myserialnumber = $hash->{HELPER}{MYSERIALNUMBER};
  1110. my $pkt_ID = $hash->{HELPER}{PKT_ID};
  1111. my ($cmd, $timestmp, $myID, $target_ID, $spkt_ID, $cmd_ID);
  1112. my ($socket,$data,$size);
  1113. # Seriennummer und SuSyID des Ziel-WR setzen
  1114. my $default_target_susyid = $hash->{HELPER}{DEFAULT_TARGET_SUSYID};
  1115. my $default_target_serial = $hash->{HELPER}{DEFAULT_TARGET_SERIAL};
  1116. my $target_susyid = AttrVal($name, "target-susyid", $default_target_susyid);
  1117. my $target_serial = AttrVal($name, "target-serial", $default_target_serial);
  1118. #Encode the password
  1119. my $encpasswd = "888888888888888888888888"; # template for password
  1120. for my $index (0..length $pass ) # encode password
  1121. {
  1122. substr($encpasswd,($index*2),2) = substr(sprintf ("%lX", (hex(substr($encpasswd,($index*2),2)) + ord(substr($pass,$index,1)))),0,2);
  1123. }
  1124. # Get current timestamp in epoch format (unix format)
  1125. $timestmp = ByteOrderLong(sprintf("%08X",int(time())));
  1126. # Define own ID and target ID and packet ID
  1127. $myID = ByteOrderShort(substr(sprintf("%04X",$mysusyid),0,4)) . ByteOrderLong(sprintf("%08X",$myserialnumber));
  1128. $target_ID = ByteOrderShort(substr(sprintf("%04X",$target_susyid),0,4)) . ByteOrderLong(sprintf("%08X",$target_serial));
  1129. $pkt_ID = 0x8001; # Reset to 0x8001
  1130. $spkt_ID = ByteOrderShort(sprintf("%04X",$pkt_ID));
  1131. #Logon command
  1132. $cmd_ID = "0C04FDFF" . "07000000" . "84030000"; # Logon command + User group "User" + (maybe) Timeout
  1133. #build final command to send
  1134. $cmd = $cmdheader . $pktlength . $esignature . $target_ID . "0001" . $myID . "0001" . "00000000" . $spkt_ID . $cmd_ID . $timestmp . "00000000" . $encpasswd . "00000000";
  1135. # flush after every write
  1136. $| = 1;
  1137. # Create Socket and check if successful
  1138. $socket = new IO::Socket::INET (PeerHost => $host, PeerPort => 9522, Proto => 'udp',); # open Socket
  1139. if (!$socket) {
  1140. # in case of error
  1141. Log3 $name, 1, "$name - ERROR - Can't open socket to inverter: $!";
  1142. return 0;
  1143. };
  1144. # Send Data
  1145. $data = pack("H*",$cmd);
  1146. $socket->send($data);
  1147. Log3 $name, 4, "$name - Send login to $host on Port 9522 with password $pass ";
  1148. Log3 $name, 5, "$name - Send: $cmd ";
  1149. # Receive Data and do a first check regarding length
  1150. eval {
  1151. $socket->recv($data, $hash->{HELPER}{MAXBYTES});
  1152. $size = length($data);
  1153. };
  1154. # check if something was received
  1155. if (defined $size) {
  1156. my $received = unpack("H*", $data);
  1157. Log3 $name, 5, "$name - Received: $received";
  1158. }
  1159. # Nothing received -> exit
  1160. if (not defined $size) {
  1161. Log3 $name, 1, "$name - Nothing received...";
  1162. # send: cmd_logout
  1163. $socket->close();
  1164. SMA_logout($hash,$host);
  1165. return 0;
  1166. } else {
  1167. # We have received something!
  1168. if ($size > 62) {
  1169. # Check all parameters of answer
  1170. my $r_susyid = unpack("v*", substr $data, 20, 2);
  1171. my $r_serial = unpack("V*", substr $data, 22, 4);
  1172. my $r_pkt_ID = unpack("v*", substr $data, 40, 2);
  1173. my $r_cmd_ID = unpack("V*", substr $data, 42, 4);
  1174. my $r_error = unpack("V*", substr $data, 36, 4);
  1175. if (($r_susyid ne $mysusyid) || ($r_serial ne $myserialnumber) || ($r_pkt_ID ne $pkt_ID) || ($r_cmd_ID ne 0xFFFD040D) || ($r_error ne 0)) {
  1176. # Response does not match the parameters we have sent, maybe different target
  1177. Log3 $name, 1, "$name - Inverter answer does not match our parameters.";
  1178. Log3 $name, 5, "$name - Request/Response: SusyID $mysusyid/$r_susyid, Serial $myserialnumber/$r_serial, Packet ID $hash->{HELPER}{PKT_ID}/$r_pkt_ID, Command 0xFFFD040D/$r_cmd_ID, Error $r_error";
  1179. # send: cmd_logout
  1180. $socket->close();
  1181. SMA_logout($hash,$host);
  1182. return 0;
  1183. }
  1184. } else {
  1185. Log3 $name, 1, "$name - Format of inverter response does not fit.";
  1186. # send: cmd_logout
  1187. $socket->close();
  1188. SMA_logout($hash,$host);
  1189. return 0;
  1190. }
  1191. }
  1192. # All seems ok, logged in!
  1193. my $inv_susyid = unpack("v*", substr $data, 28, 2);
  1194. my $inv_serial = unpack("V*", substr $data, 30, 4);
  1195. $socket->close();
  1196. if (AttrVal($name, "target-serial", undef)) {
  1197. return 0 unless($inv_serial eq $target_serial);
  1198. }
  1199. if (AttrVal($name, "target-susyid", undef)) {
  1200. return 0 unless($inv_susyid eq $target_susyid);
  1201. }
  1202. Log3 $name, 4, "$name - logged in to inverter serial: $inv_serial, susyid: $inv_susyid";
  1203. return 1;
  1204. }
  1205. ##########################################################################
  1206. # Logout
  1207. ##########################################################################
  1208. sub SMA_logout($$) {
  1209. # Parameters: host
  1210. my ($hash,$host) = @_;
  1211. my $name = $hash->{NAME};
  1212. my $cmdheader = "534D4100000402A00000000100";
  1213. my $pktlength = "22"; # length = 34 for logout command
  1214. my $esignature = "0010606508A0";
  1215. my $mysusyid = $hash->{HELPER}{MYSUSYID};
  1216. my $myserialnumber = $hash->{HELPER}{MYSERIALNUMBER};
  1217. my $pkt_ID = $hash->{HELPER}{PKT_ID};
  1218. my ($cmd, $myID, $target_ID, $spkt_ID, $cmd_ID);
  1219. my ($socket,$data,$size);
  1220. # Seriennummer und SuSyID des Ziel-WR setzen
  1221. my $default_target_susyid = $hash->{HELPER}{DEFAULT_TARGET_SUSYID};
  1222. my $default_target_serial = $hash->{HELPER}{DEFAULT_TARGET_SERIAL};
  1223. my $target_susyid = AttrVal($name, "target-susyid", $default_target_susyid);
  1224. my $target_serial = AttrVal($name, "target-serial", $default_target_serial);
  1225. # Define own ID and target ID and packet ID
  1226. $myID = ByteOrderShort(substr(sprintf("%04X",$mysusyid),0,4)) . ByteOrderLong(sprintf("%08X",$myserialnumber));
  1227. $target_ID = ByteOrderShort(substr(sprintf("%04X",$target_susyid),0,4)) . ByteOrderLong(sprintf("%08X",$target_serial));
  1228. # Increasing Packet ID
  1229. $hash->{HELPER}{PKT_ID} = $hash->{HELPER}{PKT_ID} + 1;
  1230. $spkt_ID = ByteOrderShort(sprintf("%04X",$hash->{HELPER}{PKT_ID}));
  1231. #Logout command
  1232. $cmd_ID = "0E01FDFF" . "FFFFFFFF"; # Logout command
  1233. #build final command to send
  1234. $cmd = $cmdheader . $pktlength . $esignature . $target_ID . "0003" . $myID . "0003" . "00000000" . $spkt_ID . $cmd_ID . "00000000";
  1235. # flush after every write
  1236. $| = 1;
  1237. # Create Socket and check if successful
  1238. $socket = new IO::Socket::INET (PeerHost => $host, PeerPort => 9522, Proto => 'udp',); # open Socket
  1239. if (!$socket) {
  1240. # in case of error
  1241. Log3 $name, 1, "$name - ERROR - Can't open socket to inverter: $!";
  1242. return 0;
  1243. };
  1244. # Send Data
  1245. $data = pack("H*",$cmd);
  1246. $socket->send($data);
  1247. Log3 $name, 4, "$name - Send logout to $host on Port 9522";
  1248. Log3 $name, 5, "$name - Send: $cmd ";
  1249. $target_serial = ($target_serial eq $default_target_serial)?"any inverter":$target_serial;
  1250. $target_susyid = ($target_susyid eq $default_target_susyid)?"any susyid":$target_susyid;
  1251. Log3 $name, 4, "$name - logged out now from inverter serial: $target_serial, susyid: $target_susyid";
  1252. $socket->close();
  1253. return 1;
  1254. }
  1255. ##########################################################################
  1256. # Hilfsroutinen
  1257. ##########################################################################
  1258. ##########################
  1259. sub ByteOrderShort($) {
  1260. my $input = $_[0];
  1261. my $output = "";
  1262. $output = substr($input, 2, 2) . substr($input, 0, 2);
  1263. return $output;
  1264. }
  1265. ##########################
  1266. sub ByteOrderLong($) {
  1267. my $input = $_[0];
  1268. my $output = "";
  1269. $output = substr($input, 6, 2) . substr($input, 4, 2) . substr($input, 2, 2) . substr($input, 0, 2);
  1270. return $output;
  1271. }
  1272. ##########################
  1273. sub StatusText($)
  1274. {
  1275. # Parameter is the code, return value is the Text or if not known then the code as string
  1276. my $code = $_[0];
  1277. if($code eq 51) { return (AttrVal("global", "language", "EN") eq "DE") ? "geschlossen" : "Closed"; }
  1278. if($code eq 311) { return (AttrVal("global", "language", "EN") eq "DE") ? "offen" : "Open"; }
  1279. if($code eq 16777213) { return (AttrVal("global", "language", "EN") eq "DE") ? "Information liegt nicht vor" : "No Information"; }
  1280. if($code eq 35) { return (AttrVal("global", "language", "EN") eq "DE") ? "Fehler" : "Fault"; }
  1281. if($code eq 303) { return "Off"; }
  1282. if($code eq 307) { return "Ok"; }
  1283. if($code eq 455) { return (AttrVal("global", "language", "EN") eq "DE") ? "Warnung" : "Warning"; }
  1284. return sprintf("%d", $code);
  1285. }
  1286. ##########################
  1287. # identify device type
  1288. sub devtype ($) {
  1289. my ($code) = @_;
  1290. unless (exists($SMAInverter_devtypes{$code})) { return $code;}
  1291. my $dev = $SMAInverter_devtypes{$code};
  1292. return ($dev);
  1293. }
  1294. ##########################
  1295. # identify device class
  1296. sub classtype ($) {
  1297. my ($code) = @_;
  1298. my $class;
  1299. if(AttrVal("global", "language", "EN") eq "DE") {
  1300. unless (exists($SMAInverter_classesDE{$code})) { return $code;}
  1301. $class = $SMAInverter_classesDE{$code};
  1302. } else {
  1303. unless (exists($SMAInverter_classesEN{$code})) { return $code;}
  1304. $class = $SMAInverter_classesEN{$code};
  1305. }
  1306. return ($class);
  1307. }
  1308. 1;
  1309. =pod
  1310. =item summary Integration of SMA Inverters over it's Speedwire (=Ethernet) Interface
  1311. =item summary_DE Integration von SMA Wechselrichtern über Speedwire (=Ethernet) Interface
  1312. =begin html
  1313. <a name="SMAInverter"></a>
  1314. <h3>SMAInverter</h3>
  1315. Module for the integration of a SMA Inverter over it's Speedwire (=Ethernet) Interface.<br>
  1316. Tested on Sunny Tripower 6000TL-20 and Sunny Island 4.4 with Speedwire/Webconnect Piggyback.
  1317. <br><br>
  1318. Questions and discussions about this module you can find in the FHEM-Forum link:<br>
  1319. <a href="https://forum.fhem.de/index.php/topic,56080.msg476525.html#msg476525">76_SMAInverter.pm - Abfrage von SMA Wechselrichter</a>.
  1320. <br><br>
  1321. <b>Requirements</b>
  1322. <br><br>
  1323. This module requires:
  1324. <ul>
  1325. <li>Perl Module: IO::Socket::INET (apt-get install libio-socket-multicast-perl) </li>
  1326. <li>Perl Module: Date::Time (apt-get install libdatetime-perl) </li>
  1327. <li>Perl Module: Time::HiRes</li>
  1328. <li>FHEM Module: 99_SUNRISE_EL.pm</li>
  1329. <li>FHEM Module: Blocking.pm</li>
  1330. </ul>
  1331. <br>
  1332. <br>
  1333. <b>Define</b>
  1334. <ul>
  1335. <code>define &lt;name&gt; SMAInverter &lt;pin&gt; &lt;hostname/ip&gt; </code><br>
  1336. <br>
  1337. <li>pin: User-Password of the SMA Inverter. Default is 0000. Can be changed by "Sunny Explorer" Windows Software</li>
  1338. <li>hostname/ip: Hostname or IP-Adress of the inverter (or it's speedwire piggyback module).</li>
  1339. <li>Port of the inverter is 9522 by default. Firewall has to allow connection on this port !</li>
  1340. </ul>
  1341. <b>Operation method</b>
  1342. <ul>
  1343. The module sends commands to the inverter and checks if they are supported by the inverter.<br>
  1344. In case of a positive answer the data is collected and displayed in the readings according to the detail-level. <br>
  1345. If more than one inverter is installed, set attributes "target-susyid" and "target-serial" with an appropriate value. <br><br>
  1346. The normal operation time of the inverter is supposed from sunrise to sunset. In that time period the inverter will be polled.
  1347. The time of sunrise and sunset will be calculated by functions of FHEM module 99_SUNRISE_EL.pm which is loaded automatically by default.
  1348. Therefore the global attribute "longitude" and "latitude" should be set to determine the position of the solar system
  1349. (see <a href="#SUNRISE_EL">Commandref SUNRISE_EL</a>). <br><br>
  1350. By the attribute "suppressSleep" the sleep mode between sunset and sunrise can be suppressed. Using attribute "offset" you may prefer the sunrise and
  1351. defer the sunset virtually. So the working period of the inverter will be extended. <br><br>
  1352. In operating mode "automatic" the inverter will be requested periodically corresponding the preset attribute "interval". The operating mode can be
  1353. switched to "manual" to realize the retrieval manually (e.g. to synchronize the requst with a SMA energy meter by notify). <br><br>
  1354. During inverter operating time the average energy production of the last 5, 10 and 15 minutes will be calculated and displayed in the readings
  1355. "avg_power_lastminutes_05", "avg_power_lastminutes_10" and "avg_power_lastminutes_15". <b>Note:</b> To permit a precise calculation, you should
  1356. also set the real request interval into the attribute "interval" although you would use the "manual" operation mode ! <br><br>
  1357. The retrieval of the inverter will be executed non-blocking. You can adjust the timeout value for this background process by attribute "timeout". <br>
  1358. </ul>
  1359. <b>Get</b>
  1360. <br>
  1361. <ul>
  1362. <code>get &lt;name&gt; data</code>
  1363. <br><br>
  1364. The request of the inverter will be executed. Those possibility is especifically created for the "manual" operation mode (see attribute "mode").
  1365. </ul>
  1366. <b>Attributes</b>
  1367. <ul>
  1368. <li><b>interval</b> : Queryintreval in seconds </li>
  1369. <li><b>detail-level</b> : "0" - Only Power and Energy / "1" - Including Voltage and Current / "2" - All values </li>
  1370. <li><b>disable</b> : 1 = the module is disabled </li>
  1371. <li><b>mode</b> : automatic = the inverter will be polled by preset interval, manual = query only by command "get &lt;name&gt; data" </li>
  1372. <li><b>offset</b> : time in seconds to prefer the sunrise respectively defer the sunset virtualy (0 ... 7200). You will be able to extend the working
  1373. period of the module. </li>
  1374. <li><b>SBFSpotComp</b> : 1 = the readings are created like SBFSpot-style </li>
  1375. <li><b>suppressSleep</b> : the sleep mode (after sunset, before sunrise) is deactivated and the inverter will be polled continuously. </li>
  1376. <li><b>showproctime</b> : shows processing time in background and wasted time to retrieve inverter data </li>
  1377. <li><b>target-susyid</b> : In case of a Multigate the target SUSyID can be defined. If more than one inverter is installed you have to
  1378. set the inverter-SUSyID to assign the inverter to the device definition.
  1379. Default is 0xFFFF, means any SUSyID</li>
  1380. <li><b>target-serial</b> : In case of a Multigate the target Serialnumber can be defined. If more than one inverter is installed you have to
  1381. set the inverter-Serialnumber to assign the inverter to the device definition.
  1382. Default is 0xFFFFFFFF, means any Serialnumber</li>
  1383. <li><b>timeout</b> : setup timeout of inverter data request (default 60s) </li>
  1384. </ul>
  1385. <b>Readings</b>
  1386. <ul>
  1387. <li><b>BAT_CYCLES / bat_cycles</b> : Battery recharge cycles </li>
  1388. <li><b>BAT_IDC / bat_idc</b> : Battery Current </li>
  1389. <li><b>BAT_TEMP / bat_temp</b> : Battery temperature </li>
  1390. <li><b>BAT_UDC / bat_udc</b> : Battery Voltage </li>
  1391. <li><b>ChargeStatus / chargestatus</b> : Battery Charge status </li>
  1392. <li><b>CLASS / device_class</b> : Inverter Class </li>
  1393. <li><b>PACMAX1 / pac_max_phase_1</b> : Nominal power in Ok Mode </li>
  1394. <li><b>PACMAX1_2 / pac_max_phase_1_2</b> : Maximum active power device (Some inverters like SB3300/SB1200) </li>
  1395. <li><b>PACMAX2 / pac_max_phase_2</b> : Nominal power in Warning Mode </li>
  1396. <li><b>PACMAX3 / pac_max_phase_3</b> : Nominal power in Fault Mode </li>
  1397. <li><b>Serialnumber / serial_number</b> : Inverter Serialnumber </li>
  1398. <li><b>SPOT_ETODAY / etoday</b> : Today yield </li>
  1399. <li><b>SPOT_ETOTAL / etotal</b> : Total yield </li>
  1400. <li><b>SPOT_FEEDTM / feed-in_time</b> : Feed-in time </li>
  1401. <li><b>SPOT_FREQ / grid_freq.</b> : Grid Frequency </li>
  1402. <li><b>SPOT_IAC1 / phase_1_iac</b> : Grid current phase L1 </li>
  1403. <li><b>SPOT_IAC2 / phase_2_iac</b> : Grid current phase L2 </li>
  1404. <li><b>SPOT_IAC3 / phase_3_iac</b> : Grid current phase L3 </li>
  1405. <li><b>SPOT_IDC1 / string_1_idc</b> : DC current input </li>
  1406. <li><b>SPOT_IDC2 / string_2_idc</b> : DC current input </li>
  1407. <li><b>SPOT_OPERTM / operation_time</b> : Operation Time </li>
  1408. <li><b>SPOT_PAC1 / phase_1_pac</b> : Power L1 </li>
  1409. <li><b>SPOT_PAC2 / phase_2_pac</b> : Power L2 </li>
  1410. <li><b>SPOT_PAC3 / phase_3_pac</b> : Power L3 </li>
  1411. <li><b>SPOT_PACTOT / total_pac</b> : Total Power </li>
  1412. <li><b>SPOT_PDC1 / string_1_pdc</b> : DC power input 1 </li>
  1413. <li><b>SPOT_PDC2 / string_2_pdc</b> : DC power input 2 </li>
  1414. <li><b>SPOT_UAC1 / phase_1_uac</b> : Grid voltage phase L1 </li>
  1415. <li><b>SPOT_UAC2 / phase_2_uac</b> : Grid voltage phase L2 </li>
  1416. <li><b>SPOT_UAC3 / phase_3_uac</b> : Grid voltage phase L3 </li>
  1417. <li><b>SPOT_UDC1 / string_1_udc</b> : DC voltage input </li>
  1418. <li><b>SPOT_UDC2 / string_2_udc</b> : DC voltage input </li>
  1419. <li><b>SUSyID / susyid</b> : Inverter SUSyID </li>
  1420. <li><b>INV_TEMP / device_temperature</b> : Inverter temperature </li>
  1421. <li><b>INV_TYPE / device_type</b> : Inverter Type </li>
  1422. <li><b>POWER_IN / power_in</b> : Battery Charging power </li>
  1423. <li><b>POWER_OUT / power_out</b> : Battery Discharging power </li>
  1424. <li><b>INV_GRIDRELAY / gridrelay_status</b> : Grid Relay/Contactor Status </li>
  1425. <li><b>INV_STATUS / device_status</b> : Inverter Status </li>
  1426. <li><b>opertime_start</b> : Begin of iverter operating time corresponding the calculated time of sunrise with consideration of the
  1427. attribute "offset" (if set) </li>
  1428. <li><b>opertime_stop</b> : End of iverter operating time corresponding the calculated time of sunrise with consideration of the
  1429. attribute "offset" (if set) </li>
  1430. <li><b>modulstate</b> : shows the current module state "normal" or "sleep" if the inverter won't be requested at the time. </li>
  1431. <li><b>avg_power_lastminutes_05</b> : average power of the last 5 minutes. </li>
  1432. <li><b>avg_power_lastminutes_10</b> : average power of the last 10 minutes. </li>
  1433. <li><b>avg_power_lastminutes_15</b> : average power of the last 15 minutes. </li>
  1434. <li><b>inverter_processing_time</b> : wasted time to retrieve the inverter data </li>
  1435. <li><b>background_processing_time</b> : total wasted time by background process (BlockingCall) </li>
  1436. </ul>
  1437. <br><br>
  1438. =end html
  1439. =begin html_DE
  1440. <a name="SMAInverter"></a>
  1441. <h3>SMAInverter</h3>
  1442. Modul zur Einbindung eines SMA Wechselrichters über Speedwire (Ethernet).<br>
  1443. Getestet mit Sunny Tripower 6000TL-20 und Sunny Island 4.4 mit Speedwire/Webconnect Piggyback.
  1444. <br><br>
  1445. Fragen und Diskussionen rund um dieses Modul finden sie im FHEM-Forum unter:<br>
  1446. <a href="https://forum.fhem.de/index.php/topic,56080.msg476525.html#msg476525">76_SMAInverter.pm - Abfrage von SMA Wechselrichter</a>.
  1447. <br><br>
  1448. <b>Voraussetzungen</b>
  1449. <br><br>
  1450. Dieses Modul benötigt:
  1451. <ul>
  1452. <li>Perl Modul: IO::Socket::INET (apt-get install libio-socket-multicast-perl) </li>
  1453. <li>Perl Modul: Datetime (apt-get install libdatetime-perl) </li>
  1454. <li>Perl Modul: Time::HiRes</li>
  1455. <li>FHEM Modul: 99_SUNRISE_EL.pm</li>
  1456. <li>FHEM Modul: Blocking.pm</li>
  1457. </ul>
  1458. <br>
  1459. <br>
  1460. <b>Define</b>
  1461. <ul>
  1462. <code>define &lt;name&gt; SMAInverter &lt;pin&gt; &lt;hostname/ip&gt;</code><br>
  1463. <br>
  1464. <li>pin: Benutzer-Passwort des SMA STP Wechselrichters. Default ist 0000. Kann über die Windows-Software "Sunny Explorer" geändert werden </li>
  1465. <li>hostname/ip: Hostname oder IP-Adresse des Wechselrichters (bzw. dessen Speedwire Moduls mit Ethernetanschluss) </li>
  1466. <li>Der Port des Wechselrichters ist 9522. Dieser Port muss in der Firewall freigeschaltet sein !</li>
  1467. </ul>
  1468. <b>Arbeitsweise</b>
  1469. <ul>
  1470. Das Modul schickt Befehle an den Wechselrichter und überprüft, ob diese unterstützt werden.<br>
  1471. Bei einer positiven Antwort werden die Daten gesammelt und je nach Detail-Level in den Readings dargestellt. <br>
  1472. Sind mehr als ein Wechselrichter installiert, sind die Attribute "target-susyid" und "target-serial" entsprechend zu setzen um die korrekte
  1473. Funktion zu gewährleisten. <br><br>
  1474. Die normale Betriebszeit des Wechselrichters wird in der Zeit vom Sonnenaufgang bis Sonnenuntergang angenommen. In dieser Periode werden die Wechselrichterdaten
  1475. abgefragt. Die Ermittlung von Sonnenaufgang / Sonnenuntergang wird über die Funktionen des FHEM-Moduls 99_SUNRISE_EL.pm vorgenommen. Zu diesem Zweck sollten die globalen
  1476. Attribute longitude und latitude gesetzt sein um den Standort der Anlage genau zu ermitteln. (siehe <a href="#SUNRISE_EL">Commandref SUNRISE_EL</a>) <br><br>
  1477. Mit dem Attribut "suppressSleep" kann der Schlafmodus unterdrückt werden. Das Attribut "offset" dient dazu den effektiven Zeitpunkt des Sonnenaufgangs / Sonnenuntergangs
  1478. um den Betrag "offset" vorzuziehen (Sonnenaufgang) bzw. zu verzögern (Sonnenuntergang) und somit die Abfrageperiode des Wechselrichters zu verlängern. <br><br>
  1479. Im Betriebsmodus "automatic" wird der Wechselrichter entsprechend des eingestellten Attributs "interval" abgefragt. Der Betriebsmodus kann in "manual"
  1480. umgestellt werden um eine manuelle Abfrage zu realisieren (z.B. Synchronisierung mit einem SMA Energymeter über ein Notify). <br><br>
  1481. Während der Betriebszeit des Wechselrichters wird die durchschnittliche Energieerzeugung der letzten 5, 10, 15 Minuten berechnet und in den Readings
  1482. "avg_power_lastminutes_05", "avg_power_lastminutes_10" und "avg_power_lastminutes_15" ausgegeben. <b>Hinweis:</b> Um eine korrekte Berechnung zu
  1483. ermöglichen, sollte auch im Betriebsmodus "manual" das tatsächliche Abfrageinterval im Attribute "interval" hinterlegt werden ! <br><br>
  1484. Die Abfrage des Wechselrichters wird non-blocking ausgeführt. Der Timeoutwert für diesen Hintergrundprozess kann mit dem Attribut "timeout" eingestellt werden. <br>
  1485. </ul>
  1486. <b>Get</b>
  1487. <br>
  1488. <ul>
  1489. <code>get &lt;name&gt; data</code>
  1490. <br><br>
  1491. Die Datenabfrage des Wechselrichters wird ausgeführt. Diese Möglichkeit ist speziell für den Betriebsmodus "manual" vorgesehen (siehe Attribut "mode").
  1492. </ul>
  1493. <b>Attribute</b>
  1494. <ul>
  1495. <li><b>interval</b> : Abfrageinterval in Sekunden </li>
  1496. <li><b>detail-level</b> : "0" - Nur Leistung und Energie / "1" - zusätzlich Strom und Spannung / "2" - Alle Werte </li>
  1497. <li><b>disable</b> : 1 = das Modul ist disabled </li>
  1498. <li><b>mode</b> : automatic = die Wechselrichterwerte werden im eingestellten Interval abgefragt, manual = Abfrage nur mit "get &lt;name&gt; data" </li>
  1499. <li><b>offset</b> : Zeit in Sekunden um die der Sonnenaufgang vorgezogen bzw. Sonnenuntergang verzögert wird (0 ... 7200). Dadurch wird die
  1500. effektive Aktivzeit des Moduls erweitert. </li>
  1501. <li><b>suppressSleep</b> : der Schlafmodus (nach Sonnenuntergang, vor Sonnenaufgang) wird ausgeschaltet und der WR abgefragt. </li>
  1502. <li><b>showproctime</b> : zeigt die für den Hintergrundprozess und die Abfrage des Wechselrichter verbrauchte Zeit. </li>
  1503. <li><b>SBFSpotComp</b> : 1 = die Readings werden kompatibel zu SBFSpot-Ausgaben erzeugt </li>
  1504. <li><b>target-susyid</b> : Im Falle eines Multigate kann die Ziel-SUSyID definiert werden. Ist mehr als ein Wechselrichter installiert,
  1505. muß die Wechselreichter-SUSyID gesetzt werden um den Wechselrichter der Device-Definition eindeutig zuzuweisen.
  1506. Default ist 0xFFFF (=keine Einschränkung)</li>
  1507. <li><b>target-serial</b> : Im Falle eines Multigate kann die Ziel-Seriennummer definiert werden. Ist mehr als ein Wechselrichter installiert,
  1508. muß die Wechselreichter-Seriennummer gesetzt werden um den Wechselrichter der Device-Definition eindeutig zuzuweisen.
  1509. Default ist 0xFFFFFFFF (=keine Einschränkung)</li>
  1510. <li><b>timeout</b> : Einstellung des timeout für die Wechselrichterabfrage (default 60s) </li>
  1511. </ul>
  1512. <b>Readings</b>
  1513. <ul>
  1514. <li><b>BAT_CYCLES / bat_cycles</b> : Akku Ladezyklen </li>
  1515. <li><b>BAT_IDC / bat_idc</b> : Akku Strom </li>
  1516. <li><b>BAT_TEMP / bat_temp</b> : Akku Temperatur </li>
  1517. <li><b>BAT_UDC / bat_udc</b> : Akku Spannung </li>
  1518. <li><b>ChargeStatus / chargestatus</b> : Akku Ladestand </li>
  1519. <li><b>CLASS / device_class</b> : Wechselrichter Klasse </li>
  1520. <li><b>PACMAX1 / pac_max_phase_1</b> : Nominelle Leistung in Ok Mode </li>
  1521. <li><b>PACMAX1_2 / pac_max_phase_1_2</b> : Maximale Leistung (für einige Wechselrichtertypen) </li>
  1522. <li><b>PACMAX2 / pac_max_phase_2</b> : Nominelle Leistung in Warning Mode </li>
  1523. <li><b>PACMAX3 / pac_max_phase_3</b> : Nominelle Leistung in Fault Mode </li>
  1524. <li><b>Serialnumber / serial_number</b> : Wechselrichter Seriennummer </li>
  1525. <li><b>SPOT_ETODAY / etoday</b> : Energie heute</li>
  1526. <li><b>SPOT_ETOTAL / etotal</b> : Energie Insgesamt </li>
  1527. <li><b>SPOT_FEEDTM / feed-in_time</b> : Einspeise-Stunden </li>
  1528. <li><b>SPOT_FREQ / grid_freq.</b> : Netz Frequenz </li>
  1529. <li><b>SPOT_IAC1 / phase_1_iac</b> : Netz Strom phase L1 </li>
  1530. <li><b>SPOT_IAC2 / phase_2_iac</b> : Netz Strom phase L2 </li>
  1531. <li><b>SPOT_IAC3 / phase_3_iac</b> : Netz Strom phase L3 </li>
  1532. <li><b>SPOT_IDC1 / string_1_idc</b> : DC Strom Eingang 1 </li>
  1533. <li><b>SPOT_IDC2 / string_2_idc</b> : DC Strom Eingang 2 </li>
  1534. <li><b>SPOT_OPERTM / operation_time</b> : Betriebsstunden </li>
  1535. <li><b>SPOT_PAC1 / phase_1_pac</b> : Leistung L1 </li>
  1536. <li><b>SPOT_PAC2 / phase_2_pac</b> : Leistung L2 </li>
  1537. <li><b>SPOT_PAC3 / phase_3_pac</b> : Leistung L3 </li>
  1538. <li><b>SPOT_PACTOT / total_pac</b> : Gesamtleistung </li>
  1539. <li><b>SPOT_PDC1 / string_1_pdc</b> : DC Leistung Eingang 1 </li>
  1540. <li><b>SPOT_PDC2 / string_2_pdc</b> : DC Leistung Eingang 2 </li>
  1541. <li><b>SPOT_UAC1 / phase_1_uac</b> : Netz Spannung phase L1 </li>
  1542. <li><b>SPOT_UAC2 / phase_2_uac</b> : Netz Spannung phase L2 </li>
  1543. <li><b>SPOT_UAC3 / phase_3_uac</b> : Netz Spannung phase L3 </li>
  1544. <li><b>SPOT_UDC1 / string_1_udc</b> : DC Spannung Eingang 1 </li>
  1545. <li><b>SPOT_UDC2 / string_2_udc</b> : DC Spannung Eingang 2 </li>
  1546. <li><b>SUSyID / susyid</b> : Wechselrichter SUSyID </li>
  1547. <li><b>INV_TEMP / device_temperature</b> : Wechselrichter Temperatur </li>
  1548. <li><b>INV_TYPE / device_type</b> : Wechselrichter Typ </li>
  1549. <li><b>POWER_IN / power_in</b> : Akku Ladeleistung </li>
  1550. <li><b>POWER_OUT / power_out</b> : Akku Entladeleistung </li>
  1551. <li><b>INV_GRIDRELAY / gridrelay_status</b> : Netz Relais Status </li>
  1552. <li><b>INV_STATUS / device_status</b> : Wechselrichter Status </li>
  1553. <li><b>opertime_start</b> : Beginn Aktivzeit des Wechselrichters entsprechend des ermittelten Sonnenaufgangs mit Berücksichtigung des
  1554. Attributs "offset" (wenn gesetzt) </li>
  1555. <li><b>opertime_stop</b> : Ende Aktivzeit des Wechselrichters entsprechend des ermittelten Sonnenuntergangs mit Berücksichtigung des
  1556. Attributs "offset" (wenn gesetzt) </li>
  1557. <li><b>modulstate</b> : zeigt den aktuellen Modulstatus "normal" oder "sleep" falls der Wechselrichter nicht abgefragt wird. </li>
  1558. <li><b>avg_power_lastminutes_05</b> : durchschnittlich erzeugte Leistung der letzten 5 Minuten. </li>
  1559. <li><b>avg_power_lastminutes_10</b> : durchschnittlich erzeugte Leistung der letzten 10 Minuten. </li>
  1560. <li><b>avg_power_lastminutes_15</b> : durchschnittlich erzeugte Leistung der letzten 15 Minuten. </li>
  1561. <li><b>inverter_processing_time</b> : verbrauchte Zeit um den Wechelrichter abzufragen. </li>
  1562. <li><b>background_processing_time</b> : gesamte durch den Hintergrundprozess (BlockingCall) verbrauchte Zeit. </li>
  1563. </ul>
  1564. <br><br>
  1565. =end html_DE
  1566. =cut