76_SMAInverter.pm 82 KB

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