59_Twilight.pm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. # $Id: 59_Twilight.pm 16005 2018-01-27 06:05:51Z igami $
  2. ##############################################################################
  3. #
  4. # 59_Twilight.pm
  5. # Copyright by Sebastian Stuecker
  6. # erweitert von Dietmar Ortmann
  7. # Maintained by igami since 02-2018
  8. #
  9. # used algorithm see: http://lexikon.astronomie.info/zeitgleichung/
  10. #
  11. # Sun position computing
  12. # Copyright (C) 2013 Julian Pawlowski, julian.pawlowski AT gmail DOT com
  13. # based on Twilight.tcl http://www.homematic-wiki.info/mw/index.php/TCLScript:twilight
  14. # With contribution from http://www.ip-symcon.de/forum/threads/14925-Sonnenstand-berechnen-(Azimut-amp-Elevation)
  15. #
  16. # e-mail: omega at online dot de
  17. #
  18. # This file is part of fhem.
  19. #
  20. # Fhem is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation, either version 2 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # Fhem is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  32. #
  33. ##############################################################################
  34. package main;
  35. use strict;
  36. use warnings;
  37. use POSIX;
  38. use HttpUtils;
  39. use Math::Trig;
  40. use Time::Local 'timelocal_nocheck';
  41. sub Twilight_calc($$);
  42. sub Twilight_my_gmt_offset();
  43. sub Twilight_midnight_seconds($);
  44. ################################################################################
  45. sub Twilight_Initialize($)
  46. {
  47. my ($hash) = @_;
  48. # Consumer
  49. $hash->{DefFn} = "Twilight_Define";
  50. $hash->{UndefFn} = "Twilight_Undef";
  51. $hash->{GetFn} = "Twilight_Get";
  52. $hash->{AttrList}= "$readingFnAttributes " ."useExtWeather";
  53. return undef;
  54. }
  55. ################################################################################
  56. sub Twilight_Get($@)
  57. {
  58. my ($hash, @a) = @_;
  59. return "argument is missing" if(int(@a) != 2);
  60. my $reading= $a[1];
  61. my $value;
  62. if(defined($hash->{READINGS}{$reading})) {
  63. $value= $hash->{READINGS}{$reading}{VAL};
  64. } else {
  65. return "no such reading: $reading";
  66. }
  67. return "$a[0] $reading => $value";
  68. }
  69. ################################################################################
  70. sub Twilight_Define($$)
  71. {
  72. my ($hash, $def) = @_;
  73. my @a = split("[ \t][ \t]*", $def);
  74. return "syntax: define <name> Twilight <latitude> <longitude> [indoor_horizon [Weather]]"
  75. if(int(@a) < 4 && int(@a) > 6);
  76. $hash->{STATE} = "0";
  77. my $latitude;
  78. my $longitude;
  79. my $name = $a[0];
  80. if ($a[2] =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $a[2] !~ /^[\. ]*$/ ) {
  81. $latitude = $a[2];
  82. if($latitude > 90){$latitude = 90;}
  83. if($latitude < -90){$latitude = -90;}
  84. }else{
  85. return "Argument Latitude is not a valid number";
  86. }
  87. if ($a[3] =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $a[3] !~ /^[\. ]*$/ ) {
  88. $longitude = $a[3];
  89. if($longitude > 180){$longitude = 180;}
  90. if($longitude < -180){$longitude = -180;}
  91. }else{
  92. return "Argument Longitude is not a valid number";
  93. }
  94. my $weather = 0;
  95. my $indoor_horizon = 0;
  96. if(int(@a)>5) { $weather=$a[5] }
  97. if(int(@a)>4) { if ($a[4] =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $a[4] !~ /^[\. ]*$/ ) {
  98. $indoor_horizon = $a[4];
  99. if($indoor_horizon > 20) { $indoor_horizon=20;}
  100. # minimal indoor_horizon makes values like civil_sunset and civil_sunrise
  101. if($indoor_horizon < -6) { $indoor_horizon= -6;}
  102. }else{
  103. return "Argument Indoor_Horizon is not a valid number";}
  104. }
  105. $hash->{WEATHER_HORIZON} = 0;
  106. $hash->{INDOOR_HORIZON} = $indoor_horizon;
  107. $hash->{LATITUDE} = $latitude;
  108. $hash->{LONGITUDE} = $longitude;
  109. $hash->{WEATHER} = $weather;
  110. $hash->{VERSUCHE} = 0;
  111. $hash->{DEFINE} = 1;
  112. $hash->{CONDITION} = 50;
  113. $hash->{SUNPOS_OFFSET} = 5*60;
  114. $attr{$name}{verbose} = 4 if ($name =~ /^tst.*$/ );
  115. my $mHash = { HASH=>$hash };
  116. Twilight_sunpos($mHash);
  117. Twilight_Midnight($mHash);
  118. delete $hash->{DEFINE};
  119. return undef;
  120. }
  121. ################################################################################
  122. sub Twilight_Undef($$) {
  123. my ($hash, $arg) = @_;
  124. foreach my $key (keys %{$hash->{TW}}) {
  125. myRemoveInternalTimer($key, $hash);
  126. }
  127. myRemoveInternalTimer ("Midnight", $hash);
  128. myRemoveInternalTimer ("weather", $hash);
  129. myRemoveInternalTimer ("sunpos", $hash);
  130. return undef;
  131. }
  132. ################################################################################
  133. sub myInternalTimer($$$$$) {
  134. my ($modifier, $tim, $callback, $hash, $waitIfInitNotDone) = @_;
  135. my $timerName = "$hash->{NAME}_$modifier";
  136. my $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier};
  137. if (defined($hash->{TIMER}{$timerName})) {
  138. Log3 $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first";
  139. stacktrace();
  140. } else {
  141. $hash->{TIMER}{$timerName} = $mHash;
  142. }
  143. Log3 $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim);
  144. InternalTimer($tim, $callback, $mHash, $waitIfInitNotDone);
  145. return $mHash;
  146. }
  147. ################################################################################
  148. sub myRemoveInternalTimer($$) {
  149. my ($modifier, $hash) = @_;
  150. my $timerName = "$hash->{NAME}_$modifier";
  151. my $myHash = $hash->{TIMER}{$timerName};
  152. if (defined($myHash)) {
  153. delete $hash->{TIMER}{$timerName};
  154. Log3 $hash, 5, "[$hash->{NAME}] removing Timer: $timerName";
  155. RemoveInternalTimer($myHash);
  156. }
  157. }
  158. ################################################################################
  159. #sub myRemoveInternalTimerByName($){
  160. # my ($name) = @_;
  161. # foreach my $a (keys %intAt) {
  162. # my $nam = "";
  163. # my $arg = $intAt{$a}{ARG};
  164. # if (ref($arg) eq "HASH" && defined($arg->{NAME}) ) {
  165. # $nam = $arg->{NAME} if (ref($arg) eq "HASH" && defined($arg->{NAME}) );
  166. # }
  167. # delete($intAt{$a}) if($nam =~ m/^$name/g);
  168. # }
  169. #}
  170. ################################################################################
  171. sub myGetHashIndirekt ($$) {
  172. my ($myHash, $function) = @_;
  173. if (!defined($myHash->{HASH})) {
  174. Log 3, "[$function] myHash not valid";
  175. return undef;
  176. };
  177. return $myHash->{HASH};
  178. }
  179. ################################################################################
  180. sub Twilight_midnight_seconds($) {
  181. my ($now) = @_;
  182. my @time = localtime($now);
  183. my $secs = ($time[2] * 3600) + ($time[1] * 60) + $time[0];
  184. return $secs;
  185. }
  186. ################################################################################
  187. #sub Twilight_ssTimeAsEpoch($) {
  188. # my ($zeit) = @_;
  189. # my ($hour, $min, $sec) = split(":",$zeit);
  190. #
  191. # my $days=0;
  192. # if ($hour>=24) {$days = 1; $hour -=24};
  193. #
  194. # my @jetzt_arr = localtime(time());
  195. # #Stunden Minuten Sekunden
  196. # $jetzt_arr[2] = $hour; $jetzt_arr[1] = $min; $jetzt_arr[0] = $sec;
  197. # $jetzt_arr[3] += $days;
  198. # my $next = timelocal_nocheck(@jetzt_arr);
  199. #
  200. # return $next;
  201. #}
  202. ################################################################################
  203. sub Twilight_calc($$) {
  204. my ($deg, $idx) = @_;
  205. my $midnight = time() - Twilight_midnight_seconds(time());
  206. my $sr = sunrise_abs("Horizon=$deg");
  207. my $ss = sunset_abs ("Horizon=$deg");
  208. my ($srhour, $srmin, $srsec) = split(":",$sr); $srhour -= 24 if($srhour>=24);
  209. my ($sshour, $ssmin, $sssec) = split(":",$ss); $sshour -= 24 if($sshour>=24);
  210. my $sr1 = $midnight + 3600*$srhour+60*$srmin+$srsec;
  211. my $ss1 = $midnight + 3600*$sshour+60*$ssmin+$sssec;
  212. return (0,0) if (abs ($sr1 - $ss1) < 30);
  213. #return Twilight_ssTimeAsEpoch($sr) + 0.01*$idx,
  214. # Twilight_ssTimeAsEpoch($ss) - 0.01*$idx;
  215. return ($sr1 + 0.01*$idx), ($ss1 - 0.01*$idx);
  216. }
  217. ################################################################################
  218. sub Twilight_TwilightTimes(@) {
  219. my ($hash, $whitchTimes, $xml) = @_;
  220. my $Name = $hash->{NAME};
  221. my $horizon = $hash->{HORIZON};
  222. my $swip = $hash->{SWIP} ;
  223. my $lat = $attr{global}{latitude}; $attr{global}{latitude} = $hash->{LATITUDE};
  224. my $long = $attr{global}{longitude}; $attr{global}{longitude} = $hash->{LONGITUDE};
  225. # ------------------------------------------------------------------------------
  226. my $idx = -1;
  227. my @horizons = ("_astro:-18", "_naut:-12", "_civil:-6",":0", "_indoor:$hash->{INDOOR_HORIZON}", "_weather:$hash->{WEATHER_HORIZON}");
  228. foreach my $horizon (@horizons) {
  229. $idx++; next if ($whitchTimes eq "weather" && !($horizon =~ m/weather/) );
  230. my ($name, $deg) = split(":", $horizon);
  231. my $sr = "sr$name"; my $ss = "ss$name";
  232. $hash->{TW}{$sr}{NAME} = $sr; $hash->{TW}{$ss}{NAME} = $ss;
  233. $hash->{TW}{$sr}{DEG} = $deg; $hash->{TW}{$ss}{DEG} = $deg;
  234. $hash->{TW}{$sr}{LIGHT} = $idx+1;$hash->{TW}{$ss}{LIGHT} = $idx;
  235. $hash->{TW}{$sr}{STATE} = $idx+1;$hash->{TW}{$ss}{STATE} = 12 - $idx;
  236. $hash->{TW}{$sr}{SWIP} = $swip; $hash->{TW}{$ss}{SWIP} = $swip;
  237. ($hash->{TW}{$sr}{TIME}, $hash->{TW}{$ss}{TIME}) = Twilight_calc ($deg, $idx);
  238. if ($hash->{TW}{$sr}{TIME} == 0) {
  239. Log3 $hash, 4, "[$Name] hint: $hash->{TW}{$sr}{NAME}, $hash->{TW}{$ss}{NAME} are not defined(HORIZON=$deg)";
  240. }
  241. }
  242. $attr{global}{latitude} = $lat;
  243. $attr{global}{longitude} = $long;
  244. # ------------------------------------------------------------------------------
  245. readingsBeginUpdate ($hash);
  246. foreach my $ereignis (keys %{$hash->{TW}}) {
  247. next if ($whitchTimes eq "weather" && !($ereignis =~ m/weather/) );
  248. readingsBulkUpdate($hash, $ereignis, $hash->{TW}{$ereignis}{TIME} == 0 ? "undefined" : FmtTime($hash->{TW}{$ereignis}{TIME}));
  249. }
  250. if ($hash->{CONDITION} != 50 ) {
  251. readingsBulkUpdate ($hash,"condition", $hash->{CONDITION});
  252. readingsBulkUpdate ($hash,"condition_txt",$hash->{CONDITION_TXT});
  253. }
  254. readingsEndUpdate ($hash, defined($hash->{LOCAL} ? 0 : 1));
  255. # ------------------------------------------------------------------------------
  256. my @horizonsOhneDeg = map {my($e, $deg)=split(":",$_); "$e"} @horizons;
  257. my @ereignisse = ((map {"sr$_"}@horizonsOhneDeg),(map {"ss$_"} reverse @horizonsOhneDeg),"sr$horizonsOhneDeg[0]");
  258. map { $hash->{TW}{$ereignisse[$_]}{NAMENEXT} = $ereignisse[$_+1] } 0..$#ereignisse-1;
  259. # ------------------------------------------------------------------------------
  260. my $myHash;
  261. my $now = time();
  262. my $secSinceMidnight = Twilight_midnight_seconds($now);
  263. my $lastMitternacht = $now-$secSinceMidnight;
  264. my $nextMitternacht = ($secSinceMidnight > 12*3600) ? $lastMitternacht+24*3600 : $lastMitternacht;
  265. my $jetztIstMitternacht = abs($now+5-$nextMitternacht)<=10;
  266. my @keyListe = qw "DEG LIGHT STATE SWIP TIME NAMENEXT";
  267. foreach my $ereignis (sort keys %{$hash->{TW}}) {
  268. next if ($whitchTimes eq "weather" && !($ereignis =~ m/weather/) );
  269. myRemoveInternalTimer($ereignis, $hash); # if(!$jetztIstMitternacht);
  270. if($hash->{TW}{$ereignis}{TIME} > 0) {
  271. $myHash = myInternalTimer($ereignis, $hash->{TW}{$ereignis}{TIME}, "Twilight_fireEvent", $hash, 0);
  272. map {$myHash->{$_} = $hash->{TW}{$ereignis}{$_} } @keyListe;
  273. }
  274. }
  275. # ------------------------------------------------------------------------------
  276. return 1;
  277. }
  278. ################################################################################
  279. sub Twilight_fireEvent($) {
  280. my ($myHash) = @_;
  281. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  282. return if (!defined($hash));
  283. my $name = $hash->{NAME};
  284. my $event = $myHash->{MODIFIER};
  285. my $deg = $myHash->{DEG};
  286. my $light = $myHash->{LIGHT};
  287. my $state = $myHash->{STATE};
  288. my $swip = $myHash->{SWIP};
  289. my $eventTime = $myHash->{TIME};
  290. my $nextEvent = $myHash->{NAMENEXT};
  291. my $delta = int($eventTime - time());
  292. my $oldState = ReadingsVal($name,"state","0");
  293. my $nextEventTime = ($hash->{TW}{$nextEvent}{TIME} > 0) ? FmtTime($hash->{TW}{$nextEvent}{TIME}) : "undefined";
  294. my $doTrigger = !(defined($hash->{LOCAL})) && ( abs($delta)<6 || $swip && $state gt $oldState);
  295. #Log3 $hash, 3, "[$hash->{NAME}] swip-delta-oldState-doTrigger===>$swip/$delta/$oldState/$doTrigger";
  296. Log3 $hash, 4,
  297. sprintf ("[$hash->{NAME}] %-10s %-19s ", $event, FmtDateTime($eventTime)).
  298. sprintf ("(%2d/$light/%+5.1f°/$doTrigger) ", $state, $deg).
  299. sprintf ("===> %-10s %-19s ", $nextEvent, $nextEventTime);
  300. readingsBeginUpdate($hash);
  301. readingsBulkUpdate ($hash, "state", $state);
  302. readingsBulkUpdate ($hash, "light", $light);
  303. readingsBulkUpdate ($hash, "horizon", $deg);
  304. readingsBulkUpdate ($hash, "aktEvent", $event);
  305. readingsBulkUpdate ($hash, "nextEvent", $nextEvent);
  306. readingsBulkUpdate ($hash, "nextEventTime", $nextEventTime);
  307. readingsEndUpdate ($hash, $doTrigger);
  308. }
  309. ################################################################################
  310. sub Twilight_Midnight($) {
  311. my ($myHash) = @_;
  312. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  313. return if (!defined($hash));
  314. $hash->{SWIP} = 0;
  315. my $param = Twilight_CreateHttpParameterAndGetData($myHash, "Mid");
  316. }
  317. ################################################################################
  318. # {Twilight_WeatherTimerUpdate( {HASH=$defs{"Twilight"}} ) }
  319. sub Twilight_WeatherTimerUpdate($) {
  320. my ($myHash) = @_;
  321. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  322. return if (!defined($hash));
  323. $hash->{SWIP} = 1;
  324. my $param = Twilight_CreateHttpParameterAndGetData($myHash, "weather");
  325. }
  326. ################################################################################
  327. sub Twilight_CreateHttpParameterAndGetData($$) {
  328. my ($myHash, $mode) = @_;
  329. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  330. return if (!defined($hash));
  331. my $location = $hash->{WEATHER};
  332. my $verbose = AttrVal($hash->{NAME}, "verbose", 3 );
  333. my $URL = "http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20weather.forecast%%20where%%20woeid=%s%%20and%%20u=%%27c%%27&format=%s&env=store%%3A%%2F%%2Fdatatables.org%%2Falltableswithkeys";
  334. my $url = sprintf($URL, $location, "json");
  335. Log3 $hash, 4, "[$hash->{NAME}] url=$url";
  336. my $param = {
  337. url => $url,
  338. timeout => defined($hash->{DEFINE}) ? 10 :10,
  339. hash => $hash,
  340. method => "GET",
  341. loglevel => 4-($verbose-3),
  342. header => "User-Agent: Mozilla/5.0\r\nAccept: application/xml",
  343. callback => \&Twilight_WeatherCallback,
  344. mode => $mode };
  345. if (defined($hash->{DEFINE})) {
  346. delete $param->{callback};
  347. my ($err, $result) = HttpUtils_BlockingGet($param);
  348. Twilight_WeatherCallback($param, $err, $result);
  349. } else {
  350. HttpUtils_NonblockingGet($param);
  351. }
  352. }
  353. ################################################################################
  354. sub Twilight_WeatherCallback(@) {
  355. my ($param, $err, $result) = @_;
  356. my $hash = $param->{hash};
  357. return if (!defined($hash));
  358. if ($err) {
  359. Log3 $hash, 3, "[$hash->{NAME}] got no weather info from yahoo. Error code: $err";
  360. $result = undef;
  361. } else {
  362. Log3 $hash, 4, "[$hash->{NAME}] got weather info from yahoo for $hash->{WEATHER}";
  363. Log3 $hash, 5, "[$hash->{NAME}] answer=$result" if defined $result;
  364. }
  365. Twilight_getWeatherHorizon($hash, $result);
  366. #$hash->{CONDITION} = 50;
  367. if ($hash->{CONDITION} == 50 && $hash->{VERSUCHE} <= 10) {
  368. $hash->{VERSUCHE} += 1;
  369. Twilight_RepeatTimerSet($hash, $param->{mode});
  370. return;
  371. }
  372. Twilight_TwilightTimes ($hash, $param->{mode}, $result);
  373. Log3 $hash, 3, "[$hash->{NAME}] " . ($hash->{VERSUCHE}+1) . " attempt(s) needed to get valid weather data from yahoo" if ($hash->{CONDITION} != 50 && $hash->{VERSUCHE} > 0);
  374. Log3 $hash, 3, "[$hash->{NAME}] " . ($hash->{VERSUCHE}+1) . " attempt(s) needed got NO valid weather data from yahoo" if ($hash->{CONDITION} == 50 && $hash->{VERSUCHE} > 0);
  375. $hash->{VERSUCHE} = 0;
  376. Twilight_StandardTimerSet ($hash);
  377. }
  378. ################################################################################
  379. sub Twilight_RepeatTimerSet($$) {
  380. my ($hash, $mode) = @_;
  381. my $midnight = time() + 60;
  382. myRemoveInternalTimer("Midnight", $hash);
  383. if ($mode eq "Mid") {
  384. myInternalTimer ("Midnight", $midnight, "Twilight_Midnight", $hash, 0);
  385. } else {
  386. myInternalTimer ("Midnight", $midnight, "Twilight_WeatherTimerUpdate", $hash, 0);
  387. }
  388. }
  389. ################################################################################
  390. sub Twilight_StandardTimerSet($) {
  391. my ($hash) = @_;
  392. my $midnight = time() - Twilight_midnight_seconds(time()) + 24*3600 + 1;
  393. myRemoveInternalTimer ("Midnight", $hash);
  394. myInternalTimer ("Midnight", $midnight, "Twilight_Midnight", $hash, 0);
  395. Twilight_WeatherTimerSet ($hash);
  396. }
  397. ################################################################################
  398. sub Twilight_WeatherTimerSet($) {
  399. my ($hash) = @_;
  400. my $now = time();
  401. myRemoveInternalTimer ("weather", $hash);
  402. foreach my $key ("sr_weather", "ss_weather") {
  403. my $tim = $hash->{TW}{$key}{TIME};
  404. if ($tim-60*60>$now+60) {
  405. myInternalTimer ("weather", $tim-60*60, "Twilight_WeatherTimerUpdate", $hash, 0);
  406. last;
  407. }
  408. }
  409. }
  410. ################################################################################
  411. sub Twilight_sunposTimerSet($) {
  412. my ($hash) = @_;
  413. myRemoveInternalTimer ("sunpos", $hash);
  414. myInternalTimer ("sunpos", time()+$hash->{SUNPOS_OFFSET}, "Twilight_sunpos", $hash, 0);
  415. }
  416. ################################################################################
  417. sub Twilight_getWeatherHorizon(@)
  418. {
  419. my ($hash, $result) = @_;
  420. my $location=$hash->{WEATHER};
  421. if ($location == 0) {
  422. $hash->{WEATHER_HORIZON}="0";
  423. $hash->{CONDITION}="0";
  424. return 1;
  425. }
  426. my $mod = "[".$hash->{NAME} ."] ";
  427. my @faktor_cond_code = (10,10,10,10, 9, 7, 7, 7, 7, 7,
  428. 7, 5, 5, 5, 5, 7, 7, 5, 5, 5,
  429. 7, 5, 5, 5, 5, 5, 2, 4, 4, 2,
  430. 2, 0, 0, 0, 0, 5, 0, 8, 8, 8,
  431. 6, 8, 6, 5, 2, 5, 6, 6, 0, 0,
  432. 0);
  433. # condition codes are described in FHEM wiki and in the documentation of the yahoo weather API
  434. my ($cond_code, $cond_txt, $temperatur, $aktTemp);
  435. if (defined($result)) {
  436. # ersetze in result(json) ": durch "=>
  437. # dadurch entsteht ein Perlausdruck, der direkt geparst werden kann
  438. my $perlAusdruck = $result;
  439. #$perlAusdruck = "<h1>could";
  440. $perlAusdruck =~ s/("[\w ]+")(\s*)(:)/$1=>/g;
  441. $perlAusdruck =~ s/null/undef/g;
  442. $perlAusdruck =~ s/true/1/g;
  443. $perlAusdruck =~ s/false/0/g;
  444. $perlAusdruck = 'return ' .$perlAusdruck;
  445. my $anonymSub = eval "sub {$perlAusdruck}";
  446. Log3 $hash, 3, "[$hash->{NAME}] error $@ parsing $result" if($@);
  447. if (!$@) {
  448. my $resHash = $anonymSub->() if ($anonymSub gt "");
  449. Log3 $hash, 3, "[$hash->{NAME}] error $@ parsing $result" if($@);
  450. #Log3 $hash, 3, "jsonAsPerl". Dumper $resHash->{query}{results}{channel}{item}{condition};
  451. if (!$@) {
  452. $cond_code = $resHash->{query}{results}{channel}{item}{condition}{code};
  453. $cond_txt = $resHash->{query}{results}{channel}{item}{condition}{text};
  454. $temperatur = $resHash->{query}{results}{channel}{item}{condition}{temp};
  455. }
  456. }
  457. }
  458. # wenn kein Code ermittelt werden kann, wird ein Pseudocode gesetzt
  459. if (!defined($cond_code) ) {
  460. $cond_code = "50"; # eigener neutraler Code
  461. $cond_txt = "undefined";
  462. $temperatur = "undefined";
  463. } else {
  464. $hash->{WEATHER_CORRECTION} = $faktor_cond_code[$cond_code] / 25 * 20;
  465. $hash->{WEATHER_HORIZON} = $hash->{WEATHER_CORRECTION} + $hash->{INDOOR_HORIZON};
  466. $hash->{CONDITION} = $cond_code;
  467. $hash->{CONDITION_TXT} = $cond_txt;
  468. $hash->{TEMPERATUR} = $temperatur;
  469. Log3 $hash, 4, "[$hash->{NAME}] $cond_code=$cond_txt $temperatur, correction: $hash->{WEATHER_CORRECTION}°";
  470. }
  471. my $doy = strftime("%j",localtime);
  472. my $declination = 0.4095*sin(0.016906*($doy-80.086));
  473. if($hash->{WEATHER_HORIZON} > (89-$hash->{LATITUDE}+$declination) ){
  474. $hash->{WEATHER_HORIZON} = 89-$hash->{LATITUDE}+$declination;
  475. }
  476. return 1;
  477. }
  478. ################################################################################
  479. sub Twilight_sunpos($)
  480. {
  481. my ($myHash) = @_;
  482. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  483. return if (!defined($hash));
  484. my $hashName = $hash->{NAME};
  485. return "" if(AttrVal($hashName, "disable", undef));
  486. my $tn = TimeNow();
  487. my ($dSeconds,$dMinutes,$dHours,$iDay,$iMonth,$iYear,$wday,$yday,$isdst) = gmtime(time);
  488. $iMonth++;
  489. $iYear += 100;
  490. my $dLongitude = $hash->{LONGITUDE};
  491. my $dLatitude = $hash->{LATITUDE};
  492. Log3 $hash, 5, "Compute sunpos for latitude $dLatitude , longitude $dLongitude" if($dHours == 0 && $dMinutes <= 6 );
  493. my $pi=3.14159265358979323846;
  494. my $twopi=(2*$pi);
  495. my $rad=($pi/180);
  496. my $dEarthMeanRadius=6371.01; # In km
  497. my $dAstronomicalUnit=149597890; # In km
  498. # Calculate difference in days between the current Julian Day
  499. # and JD 2451545.0, which is noon 1 January 2000 Universal Time
  500. # Calculate time of the day in UT decimal hours
  501. my $dDecimalHours=$dHours + $dMinutes/60.0 + $dSeconds/3600.0;
  502. # Calculate current Julian Day
  503. my $iYfrom2000=$iYear;#expects now as YY ;
  504. my $iA=(14 - ($iMonth)) / 12;
  505. my $iM=($iMonth) + 12 * $iA -3;
  506. my $liAux3=(153 * $iM + 2)/5;
  507. my $liAux4=365 * ($iYfrom2000 - $iA);
  508. my $liAux5=( $iYfrom2000 - $iA)/4;
  509. my $dElapsedJulianDays=($iDay + $liAux3 + $liAux4 + $liAux5 + 59)+ -0.5 + $dDecimalHours/24.0;
  510. # Calculate ecliptic coordinates (ecliptic longitude and obliquity of the
  511. # ecliptic in radians but without limiting the angle to be less than 2*Pi
  512. # (i.e., the result may be greater than 2*Pi)
  513. my $dOmega = 2.1429 - 0.0010394594 * $dElapsedJulianDays;
  514. my $dMeanLongitude = 4.8950630 + 0.017202791698 * $dElapsedJulianDays; # Radians
  515. my $dMeanAnomaly = 6.2400600 + 0.0172019699 * $dElapsedJulianDays;
  516. my $dEclipticLongitude = $dMeanLongitude + 0.03341607 * sin( $dMeanAnomaly ) + 0.00034894 * sin( 2 * $dMeanAnomaly ) -0.0001134 -0.0000203 * sin($dOmega);
  517. my $dEclipticObliquity = 0.4090928 - 6.2140e-9 * $dElapsedJulianDays +0.0000396 * cos($dOmega);
  518. # Calculate celestial coordinates ( right ascension and declination ) in radians
  519. # but without limiting the angle to be less than 2*Pi (i.e., the result may be
  520. # greater than 2*Pi)
  521. my $dSin_EclipticLongitude=sin( $dEclipticLongitude );
  522. my $dY1=cos( $dEclipticObliquity ) * $dSin_EclipticLongitude;
  523. my $dX1=cos( $dEclipticLongitude );
  524. my $dRightAscension=atan2( $dY1,$dX1 );
  525. if ( $dRightAscension < 0.0 ) { $dRightAscension=$dRightAscension + $twopi };
  526. my $dDeclination=asin( sin( $dEclipticObliquity )* $dSin_EclipticLongitude );
  527. # Calculate local coordinates ( azimuth and zenith angle ) in degrees
  528. my $dGreenwichMeanSiderealTime=6.6974243242 + 0.0657098283 * $dElapsedJulianDays + $dDecimalHours;
  529. my $dLocalMeanSiderealTime=($dGreenwichMeanSiderealTime*15 + $dLongitude)* $rad;
  530. my $dHourAngle=$dLocalMeanSiderealTime - $dRightAscension;
  531. my $dLatitudeInRadians=$dLatitude * $rad;
  532. my $dCos_Latitude=cos( $dLatitudeInRadians );
  533. my $dSin_Latitude=sin( $dLatitudeInRadians );
  534. my $dCos_HourAngle=cos( $dHourAngle );
  535. my $dZenithAngle=(acos( $dCos_Latitude * $dCos_HourAngle * cos($dDeclination) + sin( $dDeclination )* $dSin_Latitude));
  536. my $dY=-sin( $dHourAngle );
  537. my $dX=tan( $dDeclination )* $dCos_Latitude - $dSin_Latitude * $dCos_HourAngle;
  538. my $dAzimuth=atan2( $dY, $dX );
  539. if ( $dAzimuth < 0.0 ) {$dAzimuth=$dAzimuth + $twopi};
  540. $dAzimuth=$dAzimuth / $rad;
  541. # Parallax Correction
  542. my $dParallax=($dEarthMeanRadius / $dAstronomicalUnit) * sin( $dZenithAngle);
  543. $dZenithAngle=($dZenithAngle + $dParallax) / $rad;
  544. my $dElevation=90 - $dZenithAngle;
  545. my $twilight = int(($dElevation+12.0)/18.0 * 1000)/10;
  546. $twilight = 100 if ($twilight>100);
  547. $twilight = 0 if ($twilight< 0);
  548. my $twilight_weather ;
  549. if( (my $ExtWeather = AttrVal($hashName, "useExtWeather", "")) eq "") {
  550. $twilight_weather = int(($dElevation-$hash->{WEATHER_HORIZON}+12.0)/18.0 * 1000)/10;
  551. Log3 $hash, 5, "[$hash->{NAME}] " . "Original weather readings";
  552. } else {
  553. my($extDev,$extReading) = split(":",$ExtWeather);
  554. my $extWeatherHorizont = ReadingsVal($extDev,$extReading,-1);
  555. if ($extWeatherHorizont >= 0){
  556. $extWeatherHorizont = 100 if ($extWeatherHorizont > 100);
  557. Log3 $hash, 5, "[$hash->{NAME}] " . "New weather readings from: ".$extDev.":".$extReading.":".$extWeatherHorizont;
  558. $twilight_weather = $twilight - int(0.007 * ($extWeatherHorizont ** 2)); ## SCM: 100% clouds => 30% light (rough estimation)
  559. } else {
  560. $twilight_weather = int(($dElevation-$hash->{WEATHER_HORIZON}+12.0)/18.0 * 1000)/10;
  561. Log3 $hash, 3, "[$hash->{NAME}] " . "Error with external readings from: ".$extDev.":".$extReading." , taking original weather readings";
  562. }
  563. }
  564. $twilight_weather = 100 if ($twilight_weather>100);
  565. $twilight_weather = 0 if ($twilight_weather< 0);
  566. # set readings
  567. $dAzimuth = int(100*$dAzimuth )/100;
  568. $dElevation = int(100*$dElevation)/100;
  569. my $compassPoint = Twilight_CompassPoint($dAzimuth);
  570. readingsBeginUpdate($hash);
  571. readingsBulkUpdate ($hash, "azimuth", $dAzimuth );
  572. readingsBulkUpdate ($hash, "elevation", $dElevation );
  573. readingsBulkUpdate ($hash, "twilight", $twilight );
  574. readingsBulkUpdate ($hash, "twilight_weather", $twilight_weather );
  575. readingsBulkUpdate ($hash, "compasspoint", $compassPoint);
  576. readingsEndUpdate ($hash, defined($hash->{LOCAL} ? 0 : 1));
  577. Twilight_sunposTimerSet($hash);
  578. return undef;
  579. }
  580. ################################################################################
  581. sub Twilight_CompassPoint($) {
  582. my ($azimuth) = @_;
  583. my $compassPoint = "unknown";
  584. if ($azimuth < 22.5) {
  585. $compassPoint = "north";
  586. } elsif ($azimuth < 45) {
  587. $compassPoint = "north-northeast";
  588. } elsif ($azimuth < 67.5) {
  589. $compassPoint = "northeast";
  590. } elsif ($azimuth < 90) {
  591. $compassPoint = "east-northeast";
  592. } elsif ($azimuth < 112.5){
  593. $compassPoint = "east";
  594. } elsif ($azimuth < 135) {
  595. $compassPoint = "east-southeast";
  596. } elsif ($azimuth < 157.5){
  597. $compassPoint = "southeast";
  598. } elsif ($azimuth < 180) {
  599. $compassPoint = "south-southeast";
  600. } elsif ($azimuth < 202.5){
  601. $compassPoint = "south";
  602. } elsif ($azimuth < 225) {
  603. $compassPoint = "south-southwest";
  604. } elsif ($azimuth < 247.5){
  605. $compassPoint = "southwest";
  606. } elsif ($azimuth < 270) {
  607. $compassPoint = "west-southwest";
  608. } elsif ($azimuth < 292.5){
  609. $compassPoint = "west";
  610. } elsif ($azimuth < 315) {
  611. $compassPoint = "west-northwest";
  612. } elsif ($azimuth < 337.5){
  613. $compassPoint = "northwest";
  614. } elsif ($azimuth <= 361) {
  615. $compassPoint = "north-northwest";
  616. }
  617. return $compassPoint;
  618. }
  619. sub twilight($$$$) {
  620. my ($twilight, $reading, $min, $max) = @_;
  621. my $t = hms2h(ReadingsVal($twilight,$reading,0));
  622. $t = hms2h($min) if(defined($min) && (hms2h($min) > $t));
  623. $t = hms2h($max) if(defined($max) && (hms2h($max) < $t));
  624. return h2hms_fmt($t);
  625. }
  626. 1;
  627. =pod
  628. =item device
  629. =item summary delivers twilight and other sun related events for use in notify
  630. =item summary_DE liefert Dämmerungs Sonnen basierte Ereignisse, für notify
  631. =begin html
  632. <a name="Twilight"></a>
  633. <h3>Twilight</h3>
  634. <ul>
  635. <br>
  636. <a name="Twilightdefine"></a>
  637. <b>Define</b>
  638. <ul>
  639. <code>define &lt;name&gt; Twilight &lt;latitude&gt; &lt;longitude&gt; [&lt;indoor_horizon&gt; [&lt;Weather_Position&gt;]]</code><br>
  640. <br>
  641. Defines a virtual device for Twilight calculations <br><br>
  642. <b>latitude, longitude</b>
  643. <br>
  644. The parameters <b>latitude</b> and <b>longitude</b> are decimal numbers which give the position on earth for which the twilight states shall be calculated.
  645. <br><br>
  646. <b>indoor_horizon</b>
  647. <br>
  648. The parameter <b>indoor_horizon</b> gives a virtual horizon, that shall be used for calculation of indoor twilight. Minimal value -6 means indoor values are the same like civil values.
  649. indoor_horizon 0 means indoor values are the same as real values. indoor_horizon > 0 means earlier indoor sunset resp. later indoor sunrise.
  650. <br><br>
  651. <b>Weather_Position</b>
  652. <br>
  653. The parameter <b>Weather_Position</b> is the yahoo weather id used for getting the weather condition. Go to http://weather.yahoo.com/ and enter a city or zip code. In the upcoming webpage, the id is a the end of the URL. Example: Munich, Germany -> 676757
  654. <br><br>
  655. A Twilight device periodically calculates the times of different twilight phases throughout the day.
  656. It calculates a virtual "light" element, that gives an indicator about the amount of the current daylight.
  657. Besides the location on earth it is influenced by a so called "indoor horizon" (e.g. if there are high buildings, mountains) as well as by weather conditions. Very bad weather conditions lead to a reduced daylight for nearly the whole day.
  658. The light calculated spans between 0 and 6, where the values mean the following:
  659. <br><br>
  660. <b>light</b>
  661. <br>
  662. <code>0 - total night, sun is at least -18 degree below horizon</code><br>
  663. <code>1 - astronomical twilight, sun is between -12 and -18 degree below horizon</code><br>
  664. <code>2 - nautical twilight, sun is between -6 and -12 degree below horizon</code><br>
  665. <code>3 - civil twilight, sun is between 0 and -6 degree below horizon</code><br>
  666. <code>4 - indoor twilight, sun is between the indoor_horizon and 0 degree below horizon (not used if indoor_horizon=0)</code><br>
  667. <code>5 - weather twilight, sun is between indoor_horizon and a virtual weather horizon (the weather horizon depends on weather conditions (optional)</code><br>
  668. <code>6 - maximum daylight</code><br>
  669. <br>
  670. <b>Azimut, Elevation, Twilight</b>
  671. <br>
  672. The module calculates additionally the <b>azimuth</b> and the <b>elevation</b> of the sun. The values can be used to control a roller shutter.
  673. <br><br>
  674. As a new (twi)light value the reading <b>Twilight</b> ist added. It is derived from the elevation of the sun with the formula: (Elevation+12)/18 * 100). The value allows a more detailed
  675. control of any lamp during the sunrise/sunset phase. The value ist betwenn 0% and 100% when the elevation is between -12&deg; and 6&deg;.
  676. <br><br>
  677. You must know, that depending on the latitude, the sun will not reach any elevation. In june/july the sun never falls in middle europe
  678. below -18&deg;. In more northern countries(norway ...) the sun may not go below 0&deg;.
  679. <br><br>
  680. Any control depending on the value of Twilight must
  681. consider these aspects.
  682. <br><br>
  683. Example:
  684. <pre>
  685. define myTwilight Twilight 49.962529 10.324845 3 676757
  686. </pre>
  687. </ul>
  688. <br>
  689. <a name="Twilightset"></a>
  690. <b>Set </b>
  691. <ul>
  692. N/A
  693. </ul>
  694. <br>
  695. <a name="Twilightget"></a>
  696. <b>Get</b>
  697. <ul>
  698. <code>get &lt;name&gt; &lt;reading&gt;</code><br><br>
  699. <table>
  700. <tr><td><b>light</b></td><td>the current virtual daylight value</td></tr>
  701. <tr><td><b>nextEvent</b></td><td>the name of the next event</td></tr>
  702. <tr><td><b>nextEventTime</b></td><td>the time when the next event will probably happen (during light phase 5 and 6 this is updated when weather conditions change</td></tr>
  703. <tr><td><b>sr_astro</b></td><td>time of astronomical sunrise</td></tr>
  704. <tr><td><b>sr_naut</b></td><td>time of nautical sunrise</td></tr>
  705. <tr><td><b>sr_civil</b></td><td>time of civil sunrise</td></tr>
  706. <tr><td><b>sr</b></td><td>time of sunrise</td></tr>
  707. <tr><td><b>sr_indoor</b></td><td>time of indoor sunrise</td></tr>
  708. <tr><td><b>sr_weather</b></td><td>time of weather sunrise</td></tr>
  709. <tr><td><b>ss_weather</b></td><td>time of weather sunset</td></tr>
  710. <tr><td><b>ss_indoor</b></td><td>time of indoor sunset</td></tr>
  711. <tr><td><b>ss</b></td><td>time of sunset</td></tr>
  712. <tr><td><b>ss_civil</b></td><td>time of civil sunset</td></tr>
  713. <tr><td><b>ss_nautic</b></td><td>time of nautic sunset</td></tr>
  714. <tr><td><b>ss_astro</b></td><td>time of astro sunset</td></tr>
  715. <tr><td><b>azimuth</b></td><td>the current azimuth of the sun 0&deg; ist north 180&deg; is south</td></tr>
  716. <tr><td><b>compasspoint</b></td><td>a textual representation of the compass point</td></tr>
  717. <tr><td><b>elevation</b></td><td>the elevaltion of the sun</td></tr>
  718. <tr><td><b>twilight</b></td><td>a percetal value of a new (twi)light value: (elevation+12)/18 * 100) </td></tr>
  719. <tr><td><b>twilight_weather</b></td><td>a percetal value of a new (twi)light value: (elevation-WEATHER_HORIZON+12)/18 * 100). So if there is weather, it
  720. is always a little bit darker than by fair weather</td></tr>
  721. <tr><td><b>condition</b></td><td>the yahoo condition weather code</td></tr>
  722. <tr><td><b>condition_txt</b></td><td>the yahoo condition weather code as textual representation</td></tr>
  723. <tr><td><b>horizon</b></td><td>value auf the actual horizon 0&deg;, -6&deg;, -12&deg;, -18&deg;</td></tr>
  724. </table>
  725. </ul>
  726. <br>
  727. <a name="Twilightattr"></a>
  728. <b>Attributes</b>
  729. <ul>
  730. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  731. <li><b>useExtWeather &lt;device&gt;:&lt;reading&gt;</b></li>
  732. use data from other devices to calculate <b>twilight_weather</b>.<br/>
  733. The reading used shoud be in the range of 0 to 100 like the reading <b>c_clouds</b> in an <b><a href="#openweathermap">openweathermap</a></b> device, where 0 is clear sky and 100 are overcast clouds.<br/>
  734. With the use of this attribute weather effects like heavy rain or thunderstorms are neglegted for the calculation of the <b>twilight_weather</b> reading.<br/>
  735. </ul>
  736. <br>
  737. <a name="Twilightfunc"></a>
  738. <b>Functions</b>
  739. <ul>
  740. <li><b>twilight</b>(<b>$twilight</b>, <b>$reading</b>, <b>$min</b>, <b>$max</b>)</li> - implements a routine to compute the twilighttimes like sunrise with min max values.<br><br>
  741. <table>
  742. <tr><td><b>$twilight</b></td><td>name of the twilight instance</td></tr>
  743. <tr><td><b>$reading</b></td><td>name of the reading to use example: ss_astro, ss_weather ...</td></tr>
  744. <tr><td><b>$min</b></td><td>parameter min time - optional</td></tr>
  745. <tr><td><b>$max</b></td><td>parameter max time - optional</td></tr>
  746. </table>
  747. </ul>
  748. <br>
  749. Example:
  750. <pre>
  751. define BlindDown at *{twilight("myTwilight","sr_indoor","7:30","9:00")} set xxxx position 100
  752. # xxxx is a defined blind
  753. </pre>
  754. </ul>
  755. =end html
  756. =begin html_DE
  757. <a name="Twilight"></a>
  758. <h3>Twilight</h3>
  759. <ul>
  760. <br>
  761. <a name="Twilightdefine"></a>
  762. <b>Define</b>
  763. <ul>
  764. <code>define &lt;name&gt; Twilight &lt;latitude&gt; &lt;longitude&gt; [&lt;indoor_horizon&gt; [&lt;Weather_Position&gt;]]</code><br>
  765. <br>
  766. Erstellt ein virtuelles Device f&uuml;r die D&auml;mmerungsberechnung (Zwielicht)<br><br>
  767. <b>latitude, longitude (geografische L&auml;nge & Breite)</b>
  768. <br>
  769. Die Parameter <b>latitude</b> und <b>longitude</b> sind Dezimalzahlen welche die Position auf der Erde bestimmen, f&uuml;r welche der Dämmerungs-Status berechnet werden soll.
  770. <br><br>
  771. <b>indoor_horizon</b>
  772. <br>
  773. Der Parameter <b>indoor_horizon</b> bestimmt einen virtuellen Horizont, der f&uuml;r die Berechnung der D&auml;mmerung innerhalb von R&auml;men genutzt werden kann. Minimalwert ist -6 (ergibt gleichen Wert wie Zivile D&auml;mmerung). Bei 0 fallen
  774. indoor- und realer D&aumlmmerungswert zusammen. Werte gr&oumlsser 0 ergeben fr&uumlhere Werte für den Abend bzw. sp&aumltere f&uumlr den Morgen.
  775. <br><br>
  776. <b>Weather_Position</b>
  777. <br>
  778. Der Parameter <b>Weather_Position</b> ist die Yahoo! Wetter-ID welche f&uuml;r den Bezug der Wetterinformationen gebraucht wird. Gehe auf http://weather.yahoo.com/ und gebe einen Ort (ggf. PLZ) ein. In der URL der daraufhin geladenen Seite ist an letzter Stelle die ID. Beispiel: München, Deutschland -> 676757
  779. <br><br>
  780. Ein Twilight-Device berechnet periodisch die D&auml;mmerungszeiten und -phasen w&auml;hrend des Tages.
  781. Es berechnet ein virtuelles "Licht"-Element das einen Indikator f&uuml;r die momentane Tageslichtmenge ist.
  782. Neben der Position auf der Erde wird es vom sog. "indoor horizon" (Beispielsweise hohe Geb&auml;de oder Berge)
  783. und dem Wetter beeinflusst. Schlechtes Wetter f&uuml;hrt zu einer Reduzierung des Tageslichts f&uuml;r den ganzen Tag.
  784. Das berechnete Licht liegt zwischen 0 und 6 wobei die Werte folgendes bedeuten:<br><br>
  785. <b>light</b>
  786. <br>
  787. <code>0 - Totale Nacht, die Sonne ist mind. -18 Grad hinter dem Horizont</code><br>
  788. <code>1 - Astronomische D&auml;mmerung, die Sonne ist zw. -12 und -18 Grad hinter dem Horizont</code><br>
  789. <code>2 - Nautische D&auml;mmerung, die Sonne ist zw. -6 and -12 Grad hinter dem Horizont</code><br>
  790. <code>3 - Zivile/B&uuml;rgerliche D&auml;mmerung, die Sonne ist zw. 0 and -6 hinter dem Horizont</code><br>
  791. <code>4 - "indoor twilight", die Sonne ist zwischen dem Wert indoor_horizon und 0 Grad hinter dem Horizont (wird nicht verwendet wenn indoor_horizon=0)</code><br>
  792. <code>5 - Wetterbedingte D&auml;mmerung, die Sonne ist zwischen indoor_horizon und einem virtuellen Wetter-Horizonz (der Wetter-Horizont ist Wetterabh&auml;ngig (optional)</code><br>
  793. <code>6 - Maximales Tageslicht</code><br>
  794. <br>
  795. <b>Azimut, Elevation, Twilight (Seitenwinkel, Höhenwinkel, D&auml;mmerung)</b>
  796. <br>
  797. Das Modul berechnet zus&auml;tzlich Azimuth und Elevation der Sonne. Diese Werte k&ouml;nnen zur Rolladensteuerung verwendet werden.<br><br>
  798. Das Reading <b>Twilight</b> wird als neuer "(twi)light" Wert hinzugef&uuml;gt. Er wird aus der Elevation der Sonne mit folgender Formel abgeleitet: (Elevation+12)/18 * 100). Das erlaubt eine detailliertere Kontrolle der Lampen w&auml;hrend Sonnenauf - und untergang. Dieser Wert ist zwischen 0% und 100% wenn die Elevation zwischen -12&deg; und 6&deg;
  799. <br><br>
  800. Wissenswert dazu ist, dass die Sonne, abh&auml;gnig vom Breitengrad, bestimmte Elevationen nicht erreicht. Im Juni und Juli liegt die Sonne in Mitteleuropa nie unter -18&deg;. In n&ouml;rdlicheren Gebieten (Norwegen, ...) kommt die Sonne beispielsweise nicht &uuml;ber 0&deg.
  801. <br><br>
  802. All diese Aspekte m&uuml;ssen ber&uuml;cksichtigt werden bei Schaltungen die auf Twilight basieren.
  803. <br><br>
  804. Beispiel:
  805. <pre>
  806. define myTwilight Twilight 49.962529 10.324845 3 676757
  807. </pre>
  808. </ul>
  809. <br>
  810. <a name="Twilightset"></a>
  811. <b>Set </b>
  812. <ul>
  813. N/A
  814. </ul>
  815. <br>
  816. <a name="Twilightget"></a>
  817. <b>Get</b>
  818. <ul>
  819. <code>get &lt;name&gt; &lt;reading&gt;</code><br><br>
  820. <table>
  821. <tr><td><b>light</b></td><td>der aktuelle virtuelle Tageslicht-Wert</td></tr>
  822. <tr><td><b>nextEvent</b></td><td>Name des n&auml;chsten Events</td></tr>
  823. <tr><td><b>nextEventTime</b></td><td>die Zeit wann das n&auml;chste Event wahrscheinlich passieren wird (w&auml;hrend Lichtphase 5 und 6 wird dieser Wert aktualisiert wenn sich das Wetter &auml;ndert)</td></tr>
  824. <tr><td><b>sr_astro</b></td><td>Zeit des astronomitschen Sonnenaufgangs</td></tr>
  825. <tr><td><b>sr_naut</b></td><td>Zeit des nautischen Sonnenaufgangs</td></tr>
  826. <tr><td><b>sr_civil</b></td><td>Zeit des zivilen/b&uuml;rgerlichen Sonnenaufgangs</td></tr>
  827. <tr><td><b>sr</b></td><td>Zeit des Sonnenaufgangs</td></tr>
  828. <tr><td><b>sr_indoor</b></td><td>Zeit des "indoor" Sonnenaufgangs</td></tr>
  829. <tr><td><b>sr_weather</b></td><td>"Wert" des Wetters beim Sonnenaufgang</td></tr>
  830. <tr><td><b>ss_weather</b></td><td>"Wert" des Wetters beim Sonnenuntergang</td></tr>
  831. <tr><td><b>ss_indoor</b></td><td>Zeit des "indoor" Sonnenuntergangs</td></tr>
  832. <tr><td><b>ss</b></td><td>Zeit des Sonnenuntergangs</td></tr>
  833. <tr><td><b>ss_civil</b></td><td>Zeit des zivilen/b&uuml;rgerlichen Sonnenuntergangs</td></tr>
  834. <tr><td><b>ss_nautic</b></td><td>Zeit des nautischen Sonnenuntergangs</td></tr>
  835. <tr><td><b>ss_astro</b></td><td>Zeit des astro. Sonnenuntergangs</td></tr>
  836. <tr><td><b>azimuth</b></td><td>aktueller Azimuth der Sonne. 0&deg; ist Norden 180&deg; ist S&uuml;den</td></tr>
  837. <tr><td><b>compasspoint</b></td><td>Ein Wortwert des Kompass-Werts</td></tr>
  838. <tr><td><b>elevation</b></td><td>the elevaltion of the sun</td></tr>
  839. <tr><td><b>twilight</b></td><td>Prozentualer Wert eines neuen "(twi)light" Wertes: (elevation+12)/18 * 100) </td></tr>
  840. <tr><td><b>twilight_weather</b></td><td>Prozentualer Wert eines neuen "(twi)light" Wertes: (elevation-WEATHER_HORIZON+12)/18 * 100). Wenn ein Wetterwert vorhanden ist, ist es immer etwas dunkler als bei klarem Wetter.</td></tr>
  841. <tr><td><b>condition</b></td><td>Yahoo! Wetter code</td></tr>
  842. <tr><td><b>condition_txt</b></td><td>Yahoo! Wetter code als Text</td></tr>
  843. <tr><td><b>horizon</b></td><td>Wert des aktuellen Horizont 0&deg;, -6&deg;, -12&deg;, -18&deg;</td></tr>
  844. </table>
  845. </ul>
  846. <br>
  847. <a name="Twilightattr"></a>
  848. <b>Attributes</b>
  849. <ul>
  850. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  851. <li><b>useExtWeather &lt;device&gt;:&lt;reading&gt;</b></li>
  852. Nutzt Daten von einem anderen Device um <b>twilight_weather</b> zu berechnen.<br/>
  853. Das Reading sollte sich im Intervall zwischen 0 und 100 bewegen, z.B. das Reading <b>c_clouds</b> in einem<b><a href="#openweathermap">openweathermap</a></b> device, bei dem 0 heiteren und 100 bedeckten Himmel bedeuten.
  854. Wird diese Attribut genutzt , werden Wettereffekte wie Starkregen oder Gewitter fuer die Berechnung von <b>twilight_weather</b> nicht mehr herangezogen.
  855. </ul>
  856. <br>
  857. <a name="Twilightfunc"></a>
  858. <b>Functions</b>
  859. <ul>
  860. <li><b>twilight</b>(<b>$twilight</b>, <b>$reading</b>, <b>$min</b>, <b>$max</b>)</li> - implementiert eine Routine um die D&auml;mmerungszeiten wie Sonnenaufgang mit min und max Werten zu berechnen.<br><br>
  861. <table>
  862. <tr><td><b>$twilight</b></td><td>Name der twiligh Instanz</td></tr>
  863. <tr><td><b>$reading</b></td><td>Name des zu verwendenden Readings. Beispiel: ss_astro, ss_weather ...</td></tr>
  864. <tr><td><b>$min</b></td><td>Parameter min time - optional</td></tr>
  865. <tr><td><b>$max</b></td><td>Parameter max time - optional</td></tr>
  866. </table>
  867. </ul>
  868. <br>
  869. Anwendungsbeispiel:
  870. <pre>
  871. define BlindDown at *{twilight("myTwilight","sr_indoor","7:30","9:00")} set xxxx position 100
  872. # xxxx ist ein definiertes Rollo
  873. </pre>
  874. </ul>
  875. =end html_DE
  876. =cut