95_YAAHM.pm 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026
  1. ########################################################################################
  2. #
  3. # YAAHM.pm
  4. #
  5. # Yet Another Auto Home Module for FHEM
  6. #
  7. # Problem: Wenn man trotz Feiertag geweckt werden möchte, wird zwar die Weckzeit richtig gesetzt -
  8. # aber da "Wakeup" voM Timer ausgelöst wird und der nach den Zusatzbedingungen daytype etc fragt, gehen die rollläden eben nicht auf
  9. #
  10. # Prof. Dr. Peter A. Henning
  11. #
  12. # $Id: 95_YAAHM.pm 17475 2018-10-07 11:20:01Z phenning $
  13. #
  14. ########################################################################################
  15. #
  16. # This programm is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # The GNU General Public License can be found at
  22. # http://www.gnu.org/copyleft/gpl.html.
  23. # A copy is found in the textfile GPL.txt and important notices to the license
  24. # from the author is found in LICENSE.txt distributed with these scripts.
  25. #
  26. # This script is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. ########################################################################################
  32. package main;
  33. use strict;
  34. use warnings;
  35. use vars qw(%defs); # FHEM device/button definitions
  36. use vars qw($FW_ME);
  37. use vars qw($FW_inform);
  38. use vars qw($FW_headerlines);
  39. use vars qw($FW_id);
  40. use Data::Dumper;
  41. use Math::Trig;
  42. use JSON; # imports encode_json, decode_json, to_json and from_json.
  43. #########################
  44. # Global variables
  45. my $yaahmname;
  46. my $yaahmlinkname = "Profile"; # link text
  47. my $yaahmhiddenroom = "ProfileRoom"; # hidden room
  48. my $yaahmpublicroom = "Unsorted"; # public room
  49. my $yaahmversion = "2.02";
  50. my $firstcall = 1;
  51. my %yaahm_transtable_EN = (
  52. "ok" => "OK",
  53. "notok" => "Not OK",
  54. "start" => "Start",
  55. "status" => "Status",
  56. "notstarted" => "Not started",
  57. "next" => "Next",
  58. "manual" => "Manual Time",
  59. "exceptly" => "exceptionally",
  60. "undecid" => "not decidable",
  61. "off" => "off",
  62. "swoff" => "switched off",
  63. "and" => "and",
  64. "clock" => "",
  65. "active" => "Active",
  66. "inactive" => "Inactive",
  67. "overview" => "Summary",
  68. "name" => "Name",
  69. "event" => "Event",
  70. "time" => "Time",
  71. "timer" => "Timer",
  72. "action" => "Action",
  73. "weekly" => "Weekly ",
  74. "day" => "Day",
  75. "daytime" => "Daytime",
  76. "nighttime" => "Nighttime",
  77. "daylight" => "Daylight",
  78. "daytype" => "Day Type",
  79. "daily" => "Daily ",
  80. "type" => "Type",
  81. "description" => "Description",
  82. "profile" => "Profile",
  83. "profiles" => "Profiles",
  84. "transition" => "Transition to",
  85. "onlposfrm" => "only possible from",
  86. "notposfrm" => "not possible from",
  87. #--
  88. "aftermidnight" => "After Midnight",
  89. "beforesunrise" => "Before Sunrise",
  90. "sunrise" => "Sunrise",
  91. "aftersunrise" => "After Sunrise",
  92. "wakeup" => "WakeUp",
  93. "morning" => "Morning",
  94. "noon" => "Noon",
  95. "afternoon" => "Afternoon",
  96. "evening" => "Evening",
  97. "beforesunset" => "Before Sunset",
  98. "sunset" => "Sunset",
  99. "aftersunset" => "After Sunset",
  100. "sleep" => "Sleep",
  101. "night" => "Night",
  102. "beforemidnight" => "Before Midnight",
  103. #--
  104. "date" => "Date",
  105. "today" => "Today",
  106. "tomorrow" => "Tomorrow",
  107. "workday" => "Workday",
  108. "weekend" => "Weekend",
  109. "vacation" => "Vacation",
  110. "holiday" => "Holiday",
  111. "weekday" => "Day of Week",
  112. #--
  113. "mode" => "Mode",
  114. "normal" => "Normal",
  115. "party" => "Party",
  116. "absence" => "Absence",
  117. "donotdisturb" => "DoNotDisturb",
  118. #--
  119. "state" => "Security",
  120. "secstate" => "Device states",
  121. "unlocked" => "Unlocked",
  122. "locked" => "Locked",
  123. "unsecured" => "Not Secured",
  124. "secured" => "Secured",
  125. "protected" => "Protected",
  126. "guarded" => "Guarded",
  127. #--
  128. "monday" => ["Monday","Mon"],
  129. "tuesday" => ["Tuesday","Tue"],
  130. "wednesday" => ["Wednesday","Wed"],
  131. "thursday" => ["Thursday","Thu"],
  132. "friday" => ["Friday","Fri"],
  133. "saturday" => ["Saturday","Sat"],
  134. "sunday" => ["Sunday","Sun"],
  135. #--
  136. "spring" => "Spring",
  137. "summer" => "Summer",
  138. "fall" => "Fall",
  139. "winter" => "Winter"
  140. );
  141. my %yaahm_transtable_DE = (
  142. "ok" => "OK",
  143. "notok" => "Nicht OK",
  144. "start" => "Start",
  145. "status" => "Status",
  146. "notstarted" => "Nicht gestartet",
  147. "next" => "Nächste",
  148. "manual" => "Manuelle Zeit",
  149. "clock" => "Uhr",
  150. "exceptly" => "ausnahmsweise",
  151. "undecid" => "nicht bestimmbar",
  152. "off" => "Aus",
  153. "swoff" => "ausgeschaltet",
  154. "and" => "und",
  155. "active" => "Aktiv",
  156. "inactive" => "Inaktiv",
  157. "overview" => "Zusammenfassung",
  158. "name" => "Name",
  159. "event" => "Event",
  160. "time" => "Zeit",
  161. "timer" => "Timer",
  162. "action" => "Aktion",
  163. "weekly" => "Wochen-",
  164. "day" => "Tag",
  165. "daytime" => "Tageszeit",
  166. "nighttime" => "Nachtzeit",
  167. "daylight" => "Tageslicht",
  168. "daytype" => "Tagestyp",
  169. "daily" => "Tages-",
  170. "type" => "Typ",
  171. "description" => "Beschreibung",
  172. "profile" => "Profil",
  173. "profiles" => "Profile",
  174. "transition" => "Übergang zu",
  175. "onlposfrm" => "nur möglich aus",
  176. "notposfrm" => "nicht möglich aus",
  177. #--
  178. "aftermidnight" => "Nach Mitternacht",
  179. "beforesunrise" => "Vor Sonnenaufgang",
  180. "sunrise" => "Sonnenaufgang",
  181. "aftersunrise" => "Nach Sonnenaufgang",
  182. "wakeup" => "Wecken",
  183. "morning" => "Morgen",
  184. "noon" => "Mittag",
  185. "afternoon" => "Nachmittag",
  186. "evening" => "Abend",
  187. "beforesunset" => "Vor Sonnenuntergang",
  188. "sunset" => "Sonnenuntergang",
  189. "aftersunset" => "Nach Sonnenuntergang",
  190. "sleep" => "Schlafen",
  191. "night" => "Nacht",
  192. "beforemidnight" => "Vor Mitternacht",
  193. #--
  194. "date" => "Termin",
  195. "today" => "Heute",
  196. "tomorrow" => "Morgen",
  197. "workday" => "Arbeitstag",
  198. "weekend" => "Wochenende",
  199. "vacation" => "Ferientag",
  200. "holiday" => "Feiertag",
  201. "weekday" => "Wochentag",
  202. #--
  203. "mode" => "Modus",
  204. "normal" => "Normal",
  205. "party" => "Party",
  206. "absence" => "Abwesenheit",
  207. "donotdisturb" => "Nicht Stören",
  208. #--
  209. "state" => "Sicherheit",
  210. "secstate" => "Device States",
  211. "unlocked" => "Unverschlossen",
  212. "locked" => "Verschlossen",
  213. "unsecured" => "Nicht Gesichert",
  214. "secured" => "Gesichert",
  215. "protected" => "Geschützt",
  216. "guarded" => "Überwacht",
  217. #--
  218. "monday" => ["Montag","Mo"],
  219. "tuesday" => ["Dienstag","Di"],
  220. "wednesday" => ["Mittwoch","Mi"],
  221. "thursday" => ["Donnerstag","Do"],
  222. "friday" => ["Freitag","Fr"],
  223. "saturday" => ["Samstag","Sa"],
  224. "sunday" => ["Sonntag","So"],
  225. #--
  226. "spring" => "Frühling",
  227. "summer" => "Sommer",
  228. "fall" => "Herbst",
  229. "winter" => "Winter"
  230. );
  231. my $yaahm_tt;
  232. #-- default values, need to be overwritten from save file
  233. # first and second parameter
  234. # entries in the default table with no time entry are single-timers
  235. # entries in the default table with only first time are single-timers
  236. # entries in the default table with only second time are single-timer offsets
  237. # entries in the default table with first an second time are two-timer periods
  238. # third parameter
  239. # fourth parameter
  240. my %defaultdailytable = (
  241. "aftermidnight" => [undef,"00:01",undef,undef],
  242. "beforesunrise" => [undef,"01:00",undef,undef],
  243. "sunrise" => [undef,undef,undef,undef],
  244. "aftersunrise" => [undef,"01:00",undef,undef],
  245. "wakeup" => ["06:15",undef,undef,undef],
  246. "morning" => ["08:00",undef,undef,undef],
  247. "noon" => ["13:00",undef,undef,undef],
  248. "afternoon" => ["14:00",undef,undef,undef],
  249. "evening" => ["18:30",undef,undef,undef],
  250. "beforesunset" => [undef,"01:00",undef,undef],
  251. "sunset" => [undef,undef,undef,undef],
  252. "aftersunset" => [undef,"01:00",undef,undef],
  253. "sleep" => ["22:30",undef,undef,undef],
  254. "night" => ["22:00",undef,undef,undef],
  255. "beforemidnight" => [undef,"00:05",undef,undef]);
  256. my %dailytable = ();
  257. sub YAAHM_dsort {
  258. $dailytable{$a}[0] cmp $dailytable{$b}[0]
  259. }
  260. my @weeklytable = (
  261. "monday",
  262. "tuesday",
  263. "wednesday",
  264. "thursday",
  265. "friday",
  266. "saturday",
  267. "sunday");
  268. my %defaultwakeuptable = (
  269. "name" => "",
  270. "action" => "",
  271. "monday" => "06:15",
  272. "tuesday" => "06:15",
  273. "wednesday" => "06:15",
  274. "thursday" => "06:15",
  275. "friday" => "06:15",
  276. "saturday" => "off",
  277. "sunday" => "off");
  278. my %defaultsleeptable = (
  279. "name" => "",
  280. "action" => "",
  281. "monday" => "22:30",
  282. "tuesday" => "22:30",
  283. "wednesday" => "22:30",
  284. "thursday" => "22:30",
  285. "friday" => "23:00",
  286. "saturday" => "23:00",
  287. "sunday" => "22:30");
  288. my @daytype = (
  289. "workday",
  290. "vacation",
  291. "weekend",
  292. "holiday");
  293. my %defaultdayproperties = (
  294. "date" => "",
  295. "weekday" => "",
  296. "daytype" => 0,
  297. "desc" => "",
  298. "season" => "");
  299. my @times = (keys %defaultdailytable);
  300. my @modes = (
  301. "normal","party","absence","donotdisturb");
  302. my @states = (
  303. "unsecured","secured","protected","guarded");
  304. my @seasons = (
  305. "winter","spring","summer","fall");
  306. #-- modes or day types that affect the profile
  307. my @profmode = ("party","absence","donotdisturb");
  308. my @profday = ("vacation","holiday");
  309. #-- color schemes
  310. my @csmode;
  311. my @csmode1 = ("#53f3c7","#8bfa56","#ff9458","#fd5777");
  312. my @csstate;
  313. my @csstate1 = ("#53f3c7","#ff9458","#f554e2","#fd5777");
  314. #-- temporary fix for update purpose
  315. sub YAAHM_restore($$){};
  316. sub YAAHM_setWeeklyTime($){};
  317. #########################################################################################
  318. #
  319. # YAAHM_Initialize
  320. #
  321. # Parameter hash = hash of device addressed
  322. #
  323. #########################################################################################
  324. sub YAAHM_Initialize ($) {
  325. my ($hash) = @_;
  326. $hash->{DefFn} = "YAAHM_Define";
  327. $hash->{SetFn} = "YAAHM_Set";
  328. $hash->{GetFn} = "YAAHM_Get";
  329. $hash->{UndefFn} = "YAAHM_Undef";
  330. $hash->{AttrFn} = "YAAHM_Attr";
  331. my $attst = "linkname publicroom hiddenroom lockstate:locked,unlocked simulation:0,1 norepeat:0,1 ".
  332. "modecolor0 modecolor1 modecolor2 modecolor3 statecolor0 statecolor1 statecolor2 statecolor3 ".
  333. "timeHelper modeHelper modeAuto:0,1 stateDevices:textField-long stateInterval noicons:0,1 stateWarning stateHelper stateAuto:0,1 ".
  334. "holidayDevices:textField-long vacationDevices:textField-long specialDevices:textField-long";
  335. $hash->{AttrList} = $attst;
  336. if( !defined($yaahm_tt) ){
  337. #-- in any attribute redefinition readjust language
  338. my $lang = AttrVal("global","language","EN");
  339. if( $lang eq "DE"){
  340. $yaahm_tt = \%yaahm_transtable_DE;
  341. }else{
  342. $yaahm_tt = \%yaahm_transtable_EN;
  343. }
  344. }
  345. $yaahmlinkname = $yaahm_tt->{"profiles"};
  346. #-- default colors
  347. @csmode = @csmode1;
  348. @csstate = @csstate1;
  349. $data{FWEXT}{YAAHMx}{LINK} = "?room=".$yaahmhiddenroom;
  350. $data{FWEXT}{YAAHMx}{NAME} = $yaahmlinkname;
  351. $data{FWEXT}{"/YAAHM_timewidget"}{FUNC} = "YAAHM_timewidget";
  352. $data{FWEXT}{"/YAAHM_timewidget"}{FORKABLE} = 0;
  353. return undef;
  354. }
  355. #########################################################################################
  356. #
  357. # YAAHM_Define - Implements DefFn function
  358. #
  359. # Parameter hash = hash of device addressed, def = definition string
  360. #
  361. #########################################################################################
  362. sub YAAHM_Define ($$) {
  363. my ($hash, $def) = @_;
  364. my $now = time();
  365. my $name = $hash->{NAME};
  366. my $TYPE = $hash->{TYPE};
  367. $hash->{VERSION} = $yaahmversion;
  368. $yaahmname = $name;
  369. #-- readjust language
  370. my $lang = AttrVal("global","language","EN");
  371. if( $lang eq "DE"){
  372. $yaahm_tt = \%yaahm_transtable_DE;
  373. }else{
  374. $yaahm_tt = \%yaahm_transtable_EN;
  375. }
  376. #-- default colors
  377. @csmode = @csmode1;
  378. @csstate = @csstate1;
  379. # NOTIFYDEV
  380. my $NOTIFYDEV = "global,$name";
  381. unless ( defined( $hash->{NOTIFYDEV} ) && $hash->{NOTIFYDEV} eq $NOTIFYDEV )
  382. {
  383. $hash->{NOTIFYDEV} = $NOTIFYDEV;
  384. #$changed = 1;
  385. }
  386. readingsSingleUpdate( $hash, "state", "Initialized", 1 );
  387. $yaahmlinkname = defined($attr{$name}{"linkname"}) ? $attr{$name}{"linkname"} : $yaahmlinkname;
  388. $yaahmhiddenroom = defined($attr{$name}{"hiddenroom"}) ? $attr{$name}{"hiddenroom"} : $yaahmhiddenroom;
  389. $data{FWEXT}{YAAHMx}{LINK} = "?room=".$yaahmhiddenroom;
  390. $data{FWEXT}{YAAHMx}{NAME} = $yaahmlinkname;
  391. $attr{$name}{"room"} = $yaahmhiddenroom;
  392. my $date = YAAHM_restore($hash,0);
  393. #-- data seems to be ok, restore
  394. if( defined($date) ){
  395. YAAHM_restore($hash,1);
  396. Log3 $name,1,"[YAAHM_Define] data hash restored from save file with date $date";
  397. #-- intialization
  398. }else{
  399. Log3 $name,1,"[YAAHM_Define] data hash is initialized";
  400. #-- clone daily default profile
  401. $hash->{DATA}{"DT"} = {%defaultdailytable};
  402. #-- clone weekly default profile
  403. $hash->{DATA}{"WT"} = ();
  404. push(@{$hash->{DATA}{"WT"}},{%defaultwakeuptable});
  405. $hash->{DATA}{"WT"}[0]{"name"} = $yaahm_tt->{"wakeup"};
  406. push(@{$hash->{DATA}{"WT"}},{%defaultsleeptable});
  407. $hash->{DATA}{"WT"}[1]{"name"} = $yaahm_tt->{"sleep"};
  408. #-- clone days for today and tomorrow
  409. $hash->{DATA}{"DD"} = ();
  410. push(@{$hash->{DATA}{"DD"}},{%defaultdayproperties});
  411. push(@{$hash->{DATA}{"DD"}},{%defaultdayproperties});
  412. #-- initial mode and state
  413. $hash->{DATA}{"HSM"}{"state"}="unsecured";
  414. $hash->{DATA}{"HSM"}{"mode"}="normal";
  415. }
  416. #-- determine Astro device
  417. if( !exists($modules{Astro}{defptr}) ){
  418. Log3 $name,1,"[YAAHM] does not find an Astro device, loading module Astro separately";
  419. require "95_Astro.pm";
  420. }else{
  421. my @keys = sort keys %{$modules{Astro}{defptr}};
  422. Log3 $name,1,"[YAAHM] finds ".int(@keys)." Astro devices, module not loaded separately";
  423. }
  424. #--
  425. $modules{YAAHM}{defptr}{$name} = $hash;
  426. RemoveInternalTimer($hash);
  427. InternalTimer ($now + 5, 'YAAHM_CreateEntry', $hash, 0);
  428. return;
  429. }
  430. #########################################################################################
  431. #
  432. # YAAHM_Undef - Implements Undef function
  433. #
  434. # Parameter hash = hash of device addressed, def = definition string
  435. #
  436. #########################################################################################
  437. sub YAAHM_Undef ($$) {
  438. my ($hash,$arg) = @_;
  439. my $name = $hash->{NAME};
  440. RemoveInternalTimer($hash);
  441. delete $data{FWEXT}{YAAHMx};
  442. if (defined $defs{$name."_weblink"}) {
  443. FW_fC("delete ".$name."_weblink");
  444. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Weblink ".$name."_weblink deleted";
  445. }
  446. if (defined $defs{$name."_shortlink"}) {
  447. FW_fC("delete ".$name."_shortlink");
  448. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Weblink ".$name."_shortlink deleted";
  449. }
  450. delete($modules{YAAHM}{defptr});
  451. return undef;
  452. }
  453. #########################################################################################
  454. #
  455. # YAAHM_Attr - Implements Attr function
  456. #
  457. # Parameter hash = hash of device addressed, ???
  458. #
  459. #########################################################################################
  460. sub YAAHM_Attr($$$) {
  461. my ($cmd, $name, $attrName, $attrVal) = @_;
  462. my $hash = $defs{"$name"};
  463. #-- in any attribute redefinition readjust language
  464. my $lang = AttrVal("global","language","EN");
  465. if( $lang eq "DE"){
  466. $yaahm_tt = \%yaahm_transtable_DE;
  467. }else{
  468. $yaahm_tt = \%yaahm_transtable_EN;
  469. }
  470. #---------------------------------------
  471. if ( $attrName eq "timeHelper" ) {
  472. my $dh = (defined($attr{$name}{"timeHelper"})) ? $attr{$name}{"timeHelper"} : undef;
  473. #-- remove this function from all entries
  474. if( $cmd eq "del" ){
  475. foreach my $key (keys %defaultdailytable){
  476. my $xval = $hash->{DATA}{"DT"}{$key}[2];
  477. if( $xval =~ /^{$dh/){
  478. my @cmds = split(',',$xval);
  479. shift(@cmds);
  480. $xval = join(',',@cmds);
  481. $hash->{DATA}{"DT"}{$key}[2] = $xval;
  482. }
  483. }
  484. }
  485. #---------------------------------------
  486. }elsif ( ($cmd eq "set") && ($attrName =~ /modecolor(\d)/) ) {
  487. my $ci = $1;
  488. if( $ci >= 0 && $ci <= 3 ){
  489. $csmode[$ci] = $attrVal;
  490. }
  491. #---------------------------------------
  492. }elsif ( ($cmd eq "del") && ($attrName =~ /modecolor(\d)/) ) {
  493. my $ci = $1;
  494. if( $ci >= 0 && $ci <= 3 ){
  495. $csmode[$ci] = $csmode1[$ci];
  496. }
  497. #---------------------------------------
  498. }elsif ( ($cmd eq "set") && ($attrName =~ /statecolor(\d)/) ) {
  499. my $ci = $1;
  500. if( $ci >= 0 && $ci <= 3 ){
  501. $csstate[$ci] = $attrVal;
  502. }
  503. #---------------------------------------
  504. }elsif ( ($cmd eq "del") && ($attrName =~ /statecolor(\d)/) ) {
  505. my $ci = $1;
  506. if( $ci >= 0 && $ci <= 3 ){
  507. $csstate[$ci] = $csstate1[$ci];
  508. }
  509. #---------------------------------------
  510. }elsif ( ($cmd eq "set") && ($attrName eq "linkname") ) {
  511. $yaahmlinkname = $attrVal;
  512. $data{FWEXT}{YAAHMx}{NAME} = $yaahmlinkname;
  513. #---------------------------------------
  514. }elsif ( ($cmd eq "set") && ($attrName eq "publicroom") ) {
  515. $yaahmpublicroom = $attrVal;
  516. FW_fC("attr ".$name."_shortlink room ".$yaahmpublicroom);
  517. #---------------------------------------
  518. }elsif ( ($cmd eq "set") && ($attrName eq "hiddenroom") ){
  519. #-- remove old hiddenroom from FHEMWEB instances
  520. foreach my $dn (sort keys %defs) {
  521. if ($defs{$dn}{TYPE} eq "FHEMWEB" && $defs{$dn}{NAME} !~ /FHEMWEB:/) {
  522. my $hr = AttrVal($defs{$dn}{NAME}, "hiddenroom", "");
  523. $hr =~ s/$yaahmhiddenroom//;
  524. $hr =~ s/,,//;
  525. $hr =~ s/,$//;
  526. FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$hr);
  527. }
  528. }
  529. #-- new value
  530. $yaahmhiddenroom = $attrVal;
  531. $data{FWEXT}{YAAHMx}{LINK} = "?room=".$yaahmhiddenroom;
  532. FW_fC("attr ".$name."_weblink room ".$yaahmhiddenroom);
  533. #-- place into FHEMWEB instances
  534. foreach my $dn (sort keys %defs) {
  535. if ($defs{$dn}{TYPE} eq "FHEMWEB" && $defs{$dn}{NAME} !~ /FHEMWEB:/) {
  536. my $hr = AttrVal($defs{$dn}{NAME}, "hiddenroom", "");
  537. if (index($hr,$yaahmhiddenroom) == -1){
  538. if ($hr eq "") {
  539. FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$yaahmhiddenroom);
  540. }else {
  541. FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$hr.",".$yaahmhiddenroom);
  542. }
  543. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Added hidden room '".$yaahmhiddenroom."' to ".$defs{$dn}{NAME};
  544. }
  545. }
  546. }
  547. #---------------------------------------
  548. }elsif ( ($cmd eq "delete") && ($attrName eq "stateDevices") ) {
  549. fhem("deletereading $name sdev_housestate");
  550. fhem("deletereading $name sec_housestate");
  551. fhem("deletereading $name sym_housestate");
  552. YAAHM_RemoveInternalTimer("check",$hash);
  553. #---------------------------------------
  554. }elsif ( ($cmd eq "set") && ($attrName eq "stateInterval") ) {
  555. my $next = gettimeofday()+AttrVal($name,"stateInterval",60)*60;
  556. YAAHM_RemoveInternalTimer("check",$hash);
  557. YAAHM_InternalTimer("check",$next, "YAAHM_checkstate", $hash, 0);
  558. #---------------------------------------
  559. }elsif ( ($cmd eq "delete") && ($attrName eq "stateInterval") ) {
  560. my $next = gettimeofday()+3600;
  561. YAAHM_RemoveInternalTimer("check",$hash);
  562. YAAHM_InternalTimer("check",$next, "YAAHM_checkstate", $hash, 0);
  563. #---------------------------------------
  564. }elsif ( $attrName eq "holidayDevices" ) {
  565. return "Value for $attrName has invalid format"
  566. unless ( $cmd eq "del" || $attrVal =~ m/^[A-Za-z\d._]+(?:,[A-Za-z\d._]*)*$/ );
  567. #---------------------------------------
  568. }elsif ( $attrName eq "vacationDevices" ) {
  569. return "Value for $attrName has invalid format"
  570. unless ( $cmd eq "del" || $attrVal =~ m/^[A-Za-z\d._]+(?:,[A-Za-z\d._]*)*$/ );
  571. }
  572. return;
  573. }
  574. #########################################################################################
  575. #
  576. # YAAHM_CreateEntry - Puts the YAAHM entry into the FHEM menu
  577. #
  578. # Parameter hash = hash of device addressed
  579. #
  580. #########################################################################################
  581. sub YAAHM_CreateEntry($) {
  582. my ($hash) = @_;
  583. my $name = $hash->{NAME};
  584. $yaahmlinkname = defined($attr{$name}{"linkname"}) ? $attr{$name}{"linkname"} : $yaahmlinkname;
  585. $yaahmpublicroom = defined($attr{$name}{"publicroom"}) ? $attr{$name}{"publicroom"} : $yaahmpublicroom;
  586. $yaahmhiddenroom = defined($attr{$name}{"hiddenroom"}) ? $attr{$name}{"hiddenroom"} : $yaahmhiddenroom;
  587. #-- this is the long YAAHM entry
  588. FW_fC("defmod ".$name."_weblink weblink htmlCode {YAAHM_Longtable(\"".$name."\")}");
  589. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Weblink ".$name."_weblink created";
  590. FW_fC("attr ".$name."_weblink room ".$yaahmhiddenroom)
  591. if(!defined($attr{$name."_weblink"}{"room"}));
  592. #-- this is the short YAAHM entry
  593. FW_fC("defmod ".$name."_shortlink weblink htmlCode {YAAHM_Shorttable(\"".$name."\")}");
  594. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Weblink ".$name."_shortlink created";
  595. FW_fC("attr ".$name."_shortlink room ".$yaahmpublicroom)
  596. if(!defined($attr{$name."_shortlink"}{"room"}));
  597. foreach my $dn (sort keys %defs) {
  598. if ($defs{$dn}{TYPE} eq "FHEMWEB" && $defs{$dn}{NAME} !~ /FHEMWEB:/) {
  599. my $hr = AttrVal($defs{$dn}{NAME}, "hiddenroom", "");
  600. if (index($hr,$yaahmhiddenroom) == -1){
  601. if ($hr eq "") {
  602. FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$yaahmhiddenroom);
  603. }else {
  604. FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$hr.",".$yaahmhiddenroom);
  605. }
  606. Log3 $hash, 3, "[".$name. " V".$yaahmversion."]"." Added hidden room '".$yaahmhiddenroom."' to ".$defs{$dn}{NAME};
  607. }
  608. }
  609. }
  610. #-- Start updater
  611. InternalTimer(gettimeofday()+ 3, "YAAHM_updater",$hash,0);
  612. YAAHM_InternalTimer("check",time()+ 5, "YAAHM_checkstate", $hash, 0);
  613. }
  614. #########################################################################################
  615. #
  616. # YAAHM_Set - Implements the Set function
  617. #
  618. # Parameter hash = hash of device addressed
  619. #
  620. #########################################################################################
  621. sub YAAHM_Set($@) {
  622. my ( $hash, $name, $cmd, @args ) = @_;
  623. my $imax;
  624. my $if;
  625. my $msg;
  626. my $exec = ( defined($attr{$name}{"simulation"})&&$attr{$name}{"simulation"}==1 ) ? 0 : 1;
  627. #-----------------------------------------------------------
  628. if ( $cmd =~ /^manualnext.*/ ) {
  629. #--timer address
  630. if( $args[0] =~ /^\d+/ ) {
  631. #-- check if valid
  632. if( $args[0] >= int(@{$hash->{DATA}{"WT"}}) ){
  633. $msg = "Error, timer number ".$args[0]." does not exist, number must be smaller than ".int( @{$hash->{DATA}{"WT"}});
  634. Log3 $name,1,"[YAAHM_Set] ".$msg;
  635. return $msg;
  636. }
  637. $cmd = "next_".$args[0];
  638. }else{
  639. my $if = undef;
  640. for( my $i=0;$i<int(@{$hash->{DATA}{"WT"}});$i++){
  641. $if = $i
  642. if ($hash->{DATA}{"WT"}[$i]{"name"} eq $args[0] );
  643. };
  644. #-- check if valid
  645. if( !defined($if) ){
  646. $msg = "Error: timer name ".$args[0]." not found";
  647. Log3 $name,1,"[YAAHM_Set] ".$msg;
  648. return $msg;
  649. }
  650. $cmd = "next_".$if;
  651. }
  652. return YAAHM_nextWeeklyTime($name,$cmd,$args[1],$exec);
  653. #-----------------------------------------------------------
  654. }elsif ( $cmd =~ /^checkstate.*/ ) {
  655. YAAHM_InternalTimer("check",time()+ $args[0], "YAAHM_checkstate", $hash, 0);
  656. #-----------------------------------------------------------
  657. }elsif ( $cmd =~ /^correctstate.*/ ) {
  658. YAAHM_correctstate($hash);
  659. #-----------------------------------------------------------
  660. }elsif ( $cmd =~ /^time.*/ ) {
  661. return YAAHM_time($name,$args[0],$exec);
  662. #-----------------------------------------------------------
  663. }elsif ( $cmd =~ /^mode.*/ ) {
  664. return YAAHM_mode($name,$args[0],$exec);
  665. #-----------------------------------------------------------
  666. }elsif ( $cmd =~ /^state.*/ ) {
  667. return YAAHM_state($name,$args[0],$exec);
  668. #-----------------------------------------------------------
  669. }elsif ( $cmd =~ /^lock(ed)?$/ ) {
  670. readingsSingleUpdate( $hash, "lockstate", "locked", 0 );
  671. return;
  672. #-----------------------------------------------------------
  673. } elsif ( $cmd =~ /^unlock(ed)?$/ ) {
  674. readingsSingleUpdate( $hash, "lockstate", "unlocked", 0 );
  675. return;
  676. #-----------------------------------------------------------
  677. } elsif ( $cmd =~ /^save/ ) {
  678. return YAAHM_save($hash);
  679. #-----------------------------------------------------------
  680. } elsif ( $cmd =~ /^restore/ ) {
  681. return YAAHM_restore($hash,1);
  682. #-----------------------------------------------------------
  683. } elsif ( $cmd =~ /^initialize/ ) {
  684. $firstcall = 1;
  685. YAAHM_updater($hash);
  686. YAAHM_InternalTimer("check",time()+ 5, "YAAHM_checkstate", $hash, 0);
  687. #-----------------------------------------------------------
  688. } elsif ( $cmd eq "createWeekly" ){
  689. return "[YAAHM] missing name for new weekly profile"
  690. if( !defined($args[0]) );
  691. #-- find index
  692. $imax = int(@{$hash->{DATA}{"WT"}});
  693. $if= undef;
  694. for( my $j=0;$j<$imax;$j++){
  695. if($hash->{DATA}{"WT"}[$j]{"name"} eq $args[0]){
  696. $if = $j;
  697. last;
  698. }
  699. }
  700. return "[YAAHM] name $args[0] for weekly profile to be created is already in use"
  701. if( defined($if) );
  702. #-- clone wakeuptable
  703. push(@{$hash->{DATA}{"WT"}},{%defaultwakeuptable});
  704. $hash->{DATA}{"WT"}[$imax]{"name"} = $args[0];
  705. #-- save everything
  706. YAAHM_save($hash);
  707. fhem("save");
  708. return "[YAAHM] weekly profile $args[0] created successfully";
  709. #-----------------------------------------------------------
  710. } elsif ( $cmd eq "deleteWeekly" ){
  711. return "[YAAHM] missing name for weekly profile to be deleted"
  712. if( !defined($args[0]) );
  713. return "[YAAHM] Default weekly profile cannot be deleted"
  714. if( ($args[0] eq $yaahm_tt->{"wakeup"}) || ($args[0] eq $yaahm_tt->{"sleep"}) );
  715. #-- find index
  716. $imax = int(@{$hash->{DATA}{"WT"}});
  717. $if= undef;
  718. for( my $j=2;$j<$imax;$j++){
  719. if($hash->{DATA}{"WT"}[$j]{"name"} eq $args[0]){
  720. $if = $j;
  721. last;
  722. }
  723. }
  724. return "[YAAHM] name $args[0] for weekly profile to be deleted is not known"
  725. if( !defined($if) );
  726. splice(@{$hash->{DATA}{"WT"}},$if,1);
  727. #-- delete timer
  728. fhem("delete ".$name.".wtimer_".$if.".IF");
  729. #-- delete readings
  730. for( my $j=$if;$j<$imax-1;$j++){
  731. $hash->{READINGS}{"ring_".$j}{VAL} = $hash->{READINGS}{"ring_".($j+1)}{VAL};
  732. $hash->{READINGS}{"ring_".$j."_1"}{VAL} = $hash->{READINGS}{"ring_".($j+1)."_1"}{VAL};
  733. $hash->{READINGS}{"next_".$j}{VAL} = $hash->{READINGS}{"next_".($j+1)}{VAL};
  734. $hash->{READINGS}{"today_".$j}{VAL} = $hash->{READINGS}{"today_".($j+1)}{VAL};
  735. $hash->{READINGS}{"today_".$j."_e"}{VAL} = $hash->{READINGS}{"today_".($j+1)."_e"}{VAL};
  736. $hash->{READINGS}{"tomorrow_".$j}{VAL} = $hash->{READINGS}{"tomorrow_".($j+1)}{VAL};
  737. $hash->{READINGS}{"tomorrow_".$j."_e"}{VAL} = $hash->{READINGS}{"tomorrow_".($j+1)."_e"}{VAL};
  738. $hash->{READINGS}{"tr_wake_".$j}{VAL} = $hash->{READINGS}{"tr_wake".($j+1)}{VAL};
  739. }
  740. fhem("deletereading ".$name." ring_".($imax-1));
  741. fhem("deletereading ".$name." ring_".($imax-1)."_1");
  742. fhem("deletereading ".$name." next_".($imax-1));
  743. fhem("deletereading ".$name." today_".($imax-1));
  744. fhem("deletereading ".$name." today_".($imax-1)."_e");
  745. fhem("deletereading ".$name." tomorrow_".($imax-1));
  746. fhem("deletereading ".$name." tomorrow_".($imax-1)."_e");
  747. fhem("deletereading ".$name." tr_wake_".($imax-1));
  748. #-- save everything
  749. YAAHM_save($hash);
  750. fhem("save");
  751. return "[YAAHM] weekly profile $args[0] deleted successfully";
  752. #-----------------------------------------------------------
  753. } else {
  754. my $str = "";
  755. return "[YAAHM] Unknown argument " . $cmd . ", choose one of".
  756. " manualnext time:".join(',',@times)." mode:".join(',',@modes).
  757. " state:".join(',',@states)." locked:noArg unlocked:noArg save:noArg correctstate:noArg checkstate:0,5,10 restore:noArg initialize:noArg createWeekly deleteWeekly";
  758. }
  759. }
  760. #########################################################################################
  761. #
  762. # YAAHM_Get - Implements the Get function
  763. #
  764. # Parameter hash = hash of device addressed
  765. #
  766. #########################################################################################
  767. sub YAAHM_Get($@) {
  768. my ($hash, @args) = @_;
  769. my $res = "";
  770. my $msg;
  771. my $name = $args[0];
  772. my $arg = (defined($args[1]) ? $args[1] : "");
  773. if ($arg eq "version") {
  774. return "YAAHM.version => $yaahmversion";
  775. }elsif ($arg eq "test") {
  776. YAAHM_testWeeklyTime($hash);
  777. return "ok";
  778. }elsif ( $arg eq "next" || $arg eq "sayNext" ){
  779. my $if;
  780. #--timer address
  781. if( $args[2] =~ /^\d+/ ) {
  782. #-- check if valid
  783. if( $args[2] >= int(@{$hash->{DATA}{"WT"}}) ){
  784. $msg = "Error, timer number ".$args[2]." does not exist, number musst be smaller than ".int( @{$hash->{DATA}{"WT"}});
  785. Log3 $name,1,"[YAAHM_Get] ".$msg;
  786. return $msg;
  787. }
  788. $if=$args[2];
  789. }else{
  790. $if = undef;
  791. for( my $i=0;$i<int(@{$hash->{DATA}{"WT"}});$i++){
  792. $if = $i
  793. if ($hash->{DATA}{"WT"}[$i]{"name"} eq $args[1] );
  794. };
  795. #-- check if valid
  796. if( !defined($if) ){
  797. $msg = "Error: timer name ".$args[2]." not found";
  798. Log3 $name,1,"[YAAHM_Get] ".$msg;
  799. return $msg;
  800. }
  801. }
  802. if( $arg eq "next" ){
  803. return YAAHM_sayWeeklyTime($hash,$if,0);
  804. }else{
  805. return YAAHM_sayWeeklyTime($hash,$if,1);
  806. }
  807. }elsif ($arg eq "template") {
  808. $res = "sub HouseTimeHelper(\@){\n".
  809. " my (\$event,\$param1,\$param2) = \@_;\n\n".
  810. " Log 1,\"[HouseTimeHelper] event=\$event\";\n\n".
  811. " my \$time = ReadingsVal(\"".$name."\",\"housetime\",\"\");\n".
  812. " my \$phase = ReadingsVal(\"".$name."\",\"housephase\",\"\");\n".
  813. " my \$state = ReadingsVal(\"".$name."\",\"housestate\",\"\");\n".
  814. " my \$party = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"party\") ? 1 : 0;\n".
  815. " my \$absence = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"absence\") ? 1 : 0;\n".
  816. " my \$dndist = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"donotdisturb\") ? 1 : 0;\n".
  817. " my \$todaytype = ReadingsVal(\"".$name."\",\"tr_todayType\",\"\");\n".
  818. " my \$todaydesc = ReadingsVal(\"".$name."\",\"todayDesc\",\"\");\n".
  819. " my \$tomorrowtype = ReadingsVal(\"".$name."\",\"tr_tomorrowType\",\"\");\n".
  820. " my \$tomorrowdesc = ReadingsVal(\"".$name."\",\"tomorrowDesc\",\"\");\n";
  821. #-- iterate through table
  822. foreach my $key (sort YAAHM_dsort keys %dailytable){
  823. $res .= " #---------------------------------------------------------------------\n";
  824. my $if = ($key eq "aftermidnight") ? "if" : "}elsif";
  825. $res .= " ".$if."( \$event eq \"".$key."\" ){\n\n";
  826. }
  827. $res .= " }\n}\n";
  828. $res .= "sub HouseStateHelper(\@){\n".
  829. " my (\$event,\$param1,\$param2) = \@_;\n\n".
  830. " Log 1,\"[HouseStateHelper] event=\$event\";\n\n".
  831. " my \$time = ReadingsVal(\"".$name."\",\"housetime\",\"\");\n".
  832. " my \$phase = ReadingsVal(\"".$name."\",\"housephase\",\"\");\n".
  833. " my \$state = ReadingsVal(\"".$name."\",\"housestate\",\"\");\n".
  834. " my \$party = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"party\") ? 1 : 0;\n".
  835. " my \$absence = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"absence\") ? 1 : 0;\n".
  836. " my \$dndist = (ReadingsVal(\"".$name."\",\"housemode\",\"\") eq \"donotdisturb\") ? 1 : 0;\n";
  837. #-- iterate through table
  838. for( my $i=0;$i<int(@states);$i++) {
  839. $res .= " #---------------------------------------------------------------------\n";
  840. my $if = ($i == 0) ? "if" : "}elsif";
  841. $res .= " ".$if."( \$event eq \"".$states[$i]."\" ){\n\n";
  842. }
  843. $res .= " }\n}\n";
  844. return $res;
  845. } else {
  846. $res = "0,1";
  847. for(my $i = 2; $i<int( @{$hash->{DATA}{"WT"}});$i++){
  848. $res .= ",".$i;
  849. }
  850. return "Unknown argument $arg choose one of next:".$res." sayNext:".$res." version:noArg template:noArg";
  851. }
  852. }
  853. #########################################################################################
  854. #
  855. # YAAHM_save
  856. #
  857. # Parameter hash = hash of the YAAHM device
  858. #
  859. #########################################################################################
  860. sub YAAHM_save($) {
  861. my ($hash) = @_;
  862. my $date = TimeNow();
  863. $hash->{DATA}{"savedate"} = $date;
  864. readingsSingleUpdate( $hash, "savedate", $date, 1 );
  865. my $jhash0 = toJSON($hash->{DATA});
  866. my $error = FileWrite("YAAHMFILE",$jhash0);
  867. #Log 1,"[YAAHM_save] error=$error";
  868. return;
  869. }
  870. #########################################################################################
  871. #
  872. # YAAHM_restore
  873. #
  874. # Parameter hash = hash of the YAAHM device
  875. #
  876. #########################################################################################
  877. sub YAAHM_restore($$) {
  878. my ($hash,$doit) = @_;
  879. my $name = $hash->{NAME};
  880. my ($error,$jhash0) = FileRead("YAAHMFILE");
  881. if( defined($error) && $error ne "" ){
  882. Log3 $name,1,"[YAAHM_restore] read error=$error";
  883. return undef;
  884. }
  885. my $json = JSON->new->utf8;
  886. my $jhash1 = eval{ $json->decode( $jhash0 ) };
  887. my $date = $jhash1->{"savedate"};
  888. #-- just for the first time, reading an old savefile
  889. $date = localtime(time)
  890. if( !defined($date));
  891. readingsSingleUpdate( $hash, "savedate", $date, 0 );
  892. if( $doit==1 ){
  893. $hash->{DATA} = {%{$jhash1}};
  894. Log3 $name,5,"[YAAHM_restore] Data hash restored from save file with date ".$date;
  895. return 1;
  896. }else{
  897. return $date;
  898. }
  899. }
  900. #########################################################################################
  901. #
  902. # YAAHM_setParm - Receives parameter values from the javascript FE
  903. #
  904. # Parameter name = name of the YAAHM device
  905. #
  906. #########################################################################################
  907. sub YAAHM_setParm($@) {
  908. my ($name, @a) = @_;
  909. my $hash = $defs{$name};
  910. my $cmd = $a[0];
  911. my $key = $a[1];
  912. my $msg = "";
  913. my $val;
  914. #-- daily profile
  915. # start, end/offset, execution, active in mode / daytype
  916. if ($cmd eq "dt") {
  917. for( my $i=1;$i<5;$i++){
  918. $val = $a[$i+1];
  919. if( ($val eq "undef")||($val eq "") ){
  920. $val = undef;
  921. }elsif( ($i<3) && ($val !~ /\d?\d:\d\d/)){
  922. $msg = "wrong time specification $val for key $key, must be hh:mm";
  923. Log 1,"[YAAHM_setParm] ".$msg;
  924. $val = "00:00";
  925. }elsif( $i<3 ){
  926. my ($hour,$min) = split(':',$val);
  927. if( $hour>23 || $min>59 ){
  928. $msg = "wrong time specification $val for key $key > 23:59";
  929. Log 1,"[YAAHM_setParm] ".$msg;
  930. $val = "00:00";
  931. }
  932. }
  933. $hash->{DATA}{"DT"}{$key}[$i-1]=$val;
  934. }
  935. return $msg;
  936. #-- weekly profile
  937. }elsif ($cmd eq "wt") {
  938. #-- action
  939. $hash->{DATA}{"WT"}[$a[1]]{"action"} = $a[2];
  940. #-- next time
  941. $val = $a[3];
  942. if( ($val eq "undef")||($val eq "") ){
  943. $val = undef;
  944. }elsif( $val =~/^off/ ){
  945. #-- ok
  946. }elsif( $val =~ /\d?\d:\d\d/ ){
  947. #-- ok
  948. my ($hour,$min) = split(':',$val);
  949. if( $hour>23 || $min>59 ){
  950. $msg = "wrong time specification next=$val for weekly timer > 23:59".$a[1];
  951. Log 1,"[YAAHM_setParm] ".$msg;
  952. $val = "off";
  953. }
  954. }else{
  955. $msg = "wrong time specification next=$val for weekly timer ".$a[1].", must be hh:mm of 'off'";
  956. Log 1,"[YAAHM_setParm] ".$msg;
  957. $val = "off";
  958. }
  959. #-- next waketime
  960. $hash->{DATA}{"WT"}[$a[1]]{"next"} = $val;
  961. #-- activity party/absence
  962. $hash->{DATA}{"WT"}[$a[1]]{"acti_m"} = $a[4];
  963. #-- activity vacation/holiday
  964. $hash->{DATA}{"WT"}[$a[1]]{"acti_d"} = $a[5];
  965. #-- weekdays
  966. for( my $i=0;$i<7;$i++){
  967. $val = $a[$i+6];
  968. if( ($val eq "undef")||($val eq "") ){
  969. $val = undef;
  970. }elsif( $val =~/^off/ ){
  971. #-- ok
  972. }elsif( $val =~ /\d?\d:\d\d/ ){
  973. #-- ok
  974. my ($hour,$min) = split(':',$val);
  975. if( $hour>23 || $min>59 ){
  976. $msg = "wrong time specification $val for weekly timer > 23:59 ".$a[1];
  977. Log 1,"[YAAHM_setParm] ".$msg;
  978. $val = "off";
  979. }
  980. }else{
  981. $msg = "wrong time specification $val for weekly timer ".$a[1].", must be hh:mm or 'off'";
  982. Log 1,"[YAAHM_setParm] ".$msg;
  983. $val = "off";
  984. }
  985. $hash->{DATA}{"WT"}[$a[1]]{$weeklytable[$i]} = $val;
  986. }
  987. return $msg;
  988. }
  989. }
  990. #########################################################################################
  991. #
  992. # YAAHM_time - Change the house time (aftermidnight .. beforemidnight)
  993. #
  994. # Parameter name = name of the YAAHM device
  995. #
  996. # Careful: $exec=0 is needed in case we want to prevent execution of helper here
  997. #
  998. #########################################################################################
  999. sub YAAHM_time {
  1000. my ($name,$targettime,$exec) = @_;
  1001. my $hash = $defs{$name};
  1002. my $prevtime = defined($hash->{DATA}{"HSM"}{"time"}) ? $hash->{DATA}{"HSM"}{"time"} : "";
  1003. my $currmode = defined($hash->{DATA}{"HSM"}{"mode"}) ? $hash->{DATA}{"HSM"}{"mode"} : "normal";
  1004. my $currstate = defined($hash->{DATA}{"HSM"}{"state"}) ? $hash->{DATA}{"HSM"}{"state"} : "unsecured";
  1005. my $msg = "";
  1006. #-- local checks
  1007. #-- double change
  1008. #if( $prevtime eq $targettime ){
  1009. # $msg = "Transition skipped, the time event $targettime has been executed already";
  1010. #}
  1011. #-- don't
  1012. if( $msg ne "" ){
  1013. Log3 $name,1,"[YAAHM_time] ".$msg;
  1014. return $msg;
  1015. }
  1016. #-- doit
  1017. my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time);
  1018. my $lval = sprintf("%02d%02d",$hour,$min);
  1019. my $mval = $dailytable{"morning"}[0];
  1020. my $nval = $dailytable{"night"}[0];
  1021. #-- is this a daily timer event ?
  1022. my $regex = "((".join(")|(",@times)."))";
  1023. my $isdaily = ( $targettime =~ /$regex/ )?1:0;
  1024. if( $isdaily ){
  1025. my $tval = $dailytable{$targettime}[0];
  1026. $mval =~ s/://;
  1027. $nval =~ s/://;
  1028. $tval =~ s/://;
  1029. #-- targetphase always according to real time, not to command time
  1030. my $targetphase = ( ($lval >= $mval) && ( $nval > $lval ) ) ? "daytime" : "nighttime";
  1031. #-- iterate through table to find next event
  1032. my $nexttime;
  1033. my $sval;
  1034. my $oval="0000";
  1035. foreach my $key (sort YAAHM_dsort keys %dailytable){
  1036. $nexttime = $key;
  1037. $sval = $dailytable{$key}[0];
  1038. next
  1039. if (!defined($sval));
  1040. $sval =~ s/://;
  1041. last
  1042. if ( ($lval <= $sval) && ( $lval > $oval ) );
  1043. $oval = $sval;
  1044. }
  1045. my $ma = defined($attr{$name}{"modeAuto"}) && ($attr{$name}{"modeAuto"} == 1);
  1046. my $sa = defined($attr{$name}{"stateAuto"}) && ($attr{$name}{"stateAuto"} == 1);
  1047. #-- automatically leave party mode at morning time or when going to bed
  1048. # TODO
  1049. # TODO Fehler ! Wird nach party um Mitternacht aufgerufen und sichert das Haus, aber in jeder Zeile ein fehler undefined
  1050. if( $currmode eq "party" && $targettime =~ /(morning)|(sleep)/ && $ma ){
  1051. Log3 $name, 1,"[YAAHM_time] Leaving party mode because modeAuto=1";
  1052. $msg = YAAHM_mode($name,"normal",$exec)."\n";
  1053. if( $currstate eq "unsecured" && $targettime eq "sleep" && $sa ){
  1054. Log3 $name, 1,"[YAAHM_time] Securing the house because stateAuto=1";
  1055. $msg .= YAAHM_state($name,"secured",$exec)."\n"
  1056. };
  1057. #-- automatically leave absence mode at wakeup time
  1058. # Ist das wirklich clever ? Was passiert, wenn der Wecker nicht ausgestellt wurde
  1059. }elsif( $currmode eq "absence" && $targettime =~ /(wakeup)/ && $ma ){
  1060. Log3 $name, 1,"[YAAHM_time] Leaving absence mode because modeAuto=1";
  1061. $msg = YAAHM_mode($name,"normal",$exec)."\n";
  1062. #-- automatically leave donotdisturb mode at any time event
  1063. }elsif( $currmode eq "donotdisturb" && $ma ){
  1064. Log3 $name, 1,"[YAAHM_time] Leaving donotdisturb mode because modeAuto=1";
  1065. $msg = YAAHM_mode($name,"normal",$exec)."\n";
  1066. #-- automatically secure the house at night time or when going to bed (if not absence, and if not party)
  1067. }elsif( $currmode eq "normal" && $currstate eq "unsecured" && $targettime =~ /(night)|(sleep)/ && $sa ){
  1068. Log3 $name, 1,"[YAAHM_time] Securing the house because stateAuto=1";
  1069. $msg = YAAHM_state($name,"secured",$exec)."\n";
  1070. }
  1071. $hash->{DATA}{"HSM"}{"time"} = $targettime;
  1072. YAAHM_checkMonthly($hash,'event',$targettime);
  1073. readingsBeginUpdate($hash);
  1074. readingsBulkUpdate($hash,"prev_housetime",$prevtime);
  1075. readingsBulkUpdate($hash,"next_housetime",$nexttime);
  1076. readingsBulkUpdate($hash,"housetime",$targettime);
  1077. readingsBulkUpdate($hash,"tr_housetime",$yaahm_tt->{$targettime});
  1078. readingsBulkUpdate($hash,"housephase",$targetphase);
  1079. readingsBulkUpdate($hash,"tr_housephase",$yaahm_tt->{$targetphase});
  1080. }
  1081. #-- before fixing new times
  1082. # if manual wake/sleep/timer, the time for the current day needs to be set to empty,
  1083. # but second execution on the same day is blocked if attribute norepeat is set.
  1084. if( $targettime eq "wakeup" ){
  1085. $hash->{DATA}{"WT"}[0]{"next"} = "";
  1086. readingsBulkUpdate($hash,"next_0","");
  1087. }elsif( $targettime eq "sleep" ){
  1088. $hash->{DATA}{"WT"}[1]{"next"} = "";
  1089. readingsBulkUpdate($hash,"next_1","");
  1090. }elsif( $targettime =~ /timer_(\d+)/ ){
  1091. my $i = $1;
  1092. $hash->{DATA}{"WT"}[$i]{"next"} = "";
  1093. readingsBulkUpdate($hash,"next_".$i,"");
  1094. }
  1095. readingsEndUpdate($hash,1);
  1096. YAAHM_setWeeklyTime($hash);
  1097. #-- helper function not executed, e.g. by call from external timer
  1098. return
  1099. if( !defined($exec) || $exec==0);
  1100. #-- execute the helper function
  1101. my $xval;
  1102. my $ival;
  1103. my $wupn;
  1104. #-- after fixing new time
  1105. #-- TODO here: what should we do, if the timer is NOT enabled and we get up or go to bed anyhow ???
  1106. if( $targettime =~ /((wakeup)|(sleep)|(timer_(\d+)))/ ){
  1107. my $timerno="";
  1108. if( $targettime eq "wakeup" ){
  1109. $timerno=0;
  1110. }elsif( $targettime eq "sleep" ){
  1111. $timerno=1;
  1112. }elsif( $targettime =~ /timer_(\d+)/ ){
  1113. $timerno = $1;
  1114. }
  1115. $wupn = $hash->{DATA}{"WT"}[$timerno]{"name"};
  1116. $ival = (ReadingsVal($name.".wtimer_".$timerno.".IF","mode","") ne "disabled");
  1117. $xval = $ival ? $hash->{DATA}{"WT"}[$timerno]{"action"} : "";
  1118. my $norep = AttrVal($name,"norepeat",0);
  1119. if( $exec==1 ){
  1120. if( $hash->{DATA}{"WT"}[$timerno]{"done"} && $norep ){
  1121. $msg = "Not executing action for timer $targettime, already done";
  1122. Log3 $name,3,"[YAAHM_time] ".$msg;
  1123. return $msg;
  1124. }else{
  1125. Log3 $name,1,"[YAAHM_time] executing action $xval for timer $targettime";
  1126. fhem($xval);
  1127. $hash->{DATA}{"WT"}[$timerno]{"done"} = 1;
  1128. return;
  1129. }
  1130. }elsif( $exec==0 ){
  1131. $msg .= "Simulation ".$xval." from timer ".$targettime;
  1132. $msg .= " (disabled)"
  1133. if !$ival;
  1134. Log3 $name,1,"[YAAHM_time] ".$msg;
  1135. return $msg;
  1136. }
  1137. #-- any other
  1138. }else{
  1139. $xval = $dailytable{$targettime}[2];
  1140. $msg = "Simulation ".$xval;
  1141. if( $exec==1 ){
  1142. Log3 $name,1,"[YAAHM_time] executing $xval";
  1143. fhem($xval);
  1144. return
  1145. }elsif( $exec==0 ){
  1146. Log3 $name,1,"[YAAHM_time] ".$msg;
  1147. return $msg;
  1148. }
  1149. }
  1150. }
  1151. #########################################################################################
  1152. #
  1153. # YAAHM_nextWeeklyTime - set the next weekly time
  1154. #
  1155. # Parameter name = name of device addressed
  1156. #
  1157. #########################################################################################
  1158. sub YAAHM_nextWeeklyTime {
  1159. my ($name,$cmd,$time,$exec) = @_;
  1160. my $hash = $defs{$name};
  1161. my $msg;
  1162. #--determine which timer (duplicate check when coming from set)
  1163. $cmd =~ /.*next_([0-9]+)$/;
  1164. my $i = $1;
  1165. if( $i >= int( @{$hash->{DATA}{"WT"}}) ){
  1166. $msg = "Error, timer number $i does not exist, number musst be smaller than ".int( @{$hash->{DATA}{"WT"}});
  1167. Log3 $name,1,"[YAAHM_nextWeeklyTime] ".$msg;
  1168. return $msg;
  1169. }
  1170. $time = lc($time);
  1171. #-- check value - may be empty
  1172. if( $time eq "" || $time eq "default" ){
  1173. $time = "";
  1174. #-- nontrivial
  1175. }else{
  1176. #-- off=ok, do nothing
  1177. if( $time eq "off"){
  1178. #-- time=ok, check
  1179. }elsif( $time =~ /(\d?\d):(\d\d)(:(\d\d))?/ ){
  1180. if( $1 >= 24 || $2 >= 60){
  1181. $msg = "Error, time specification $time for timer ".$cmd." > 23:59 ";
  1182. Log3 $name,1,"[YAAHM_nextWeeklyTime] ".$msg;
  1183. return $msg;
  1184. }
  1185. $time = sprintf("%02d:\%02d",$1,$2);
  1186. }else{
  1187. $msg = "Error, time specification $time invalid for timer ".$cmd.", must be hh:mm";;
  1188. Log3 $name,1,"[YAAHM_nextWeeklyTime] ".$msg;
  1189. return $msg;
  1190. }
  1191. }
  1192. #-- all logic in setweeklytime
  1193. $hash->{DATA}{"WT"}[$i]{"next"} = $time;
  1194. $hash->{DATA}{"WT"}[$i]{"done"} = 0;
  1195. YAAHM_setWeeklyTime($hash);
  1196. }
  1197. #########################################################################################
  1198. #
  1199. # YAAHM_mode - Change the house mode (normal, party, absence)
  1200. #
  1201. # Parameter name = name of the YAAHM device
  1202. #
  1203. #########################################################################################
  1204. sub YAAHM_mode {
  1205. my ($name,$targetmode,$exec) = @_;
  1206. my $hash = $defs{$name};
  1207. my $prevmode = defined($hash->{DATA}{"HSM"}{"mode"}) ? $hash->{DATA}{"HSM"}{"mode"} : "normal";
  1208. my $currstate = defined($hash->{DATA}{"HSM"}{"state"}) ? $hash->{DATA}{"HSM"}{"state"} : "unsecured";
  1209. my $msg = "";
  1210. my $tr_msg = "";
  1211. #-- local checks
  1212. #-- double change
  1213. if( $prevmode eq $targetmode ){
  1214. $msg = "transition skipped, we are already in $targetmode mode";
  1215. #-- transition into party and absence is only possible from normal mode
  1216. }elsif( $prevmode ne "normal" && $targetmode ne "normal"){
  1217. $msg = "Transition into $targetmode mode is only possible from normal mode";
  1218. $tr_msg = $yaahm_tt->{transition}.' "'.$yaahm_tt->{$targetmode}.'" '.$yaahm_tt->{"onlposfrm"}.' '.$yaahm_tt->{"mode"}.'="'.$yaahm_tt->{"normal"}.'"';
  1219. #-- global checks
  1220. #-- transition into party mode only possible in unlocked state
  1221. }elsif( $targetmode eq "party" && $currstate ne "unsecured" ){
  1222. $msg = "Transition into party mode is only possible from unsecured state";
  1223. $tr_msg = $yaahm_tt->{transition}.' "'.$yaahm_tt->{$targetmode}.'" '.$yaahm_tt->{"onlposfrm"}.' '.$yaahm_tt->{"state"}.'="'.$yaahm_tt->{"unsecured"}.'"';
  1224. }
  1225. #-- don't
  1226. if( $msg ne "" ){
  1227. Log3 $name,1,"[YAAHM_mode] ".$msg;
  1228. readingsSingleUpdate($hash,"tr_errmsg",$tr_msg,1);
  1229. return $msg;
  1230. }
  1231. $hash->{DATA}{"HSM"}{"mode"} = $targetmode;
  1232. readingsBeginUpdate($hash);
  1233. readingsBulkUpdate($hash,"tr_errmsg","");
  1234. readingsBulkUpdate($hash,"prev_housemode",$prevmode);
  1235. readingsBulkUpdate($hash,"housemode",$targetmode);
  1236. readingsBulkUpdate($hash,"tr_housemode",$yaahm_tt->{$targetmode});
  1237. readingsEndUpdate($hash,1);
  1238. #-- doit, if not simulation
  1239. if (defined($attr{$name}{"modeHelper"})){
  1240. if( !defined($exec) || $exec==1 ){
  1241. fhem("{".$attr{$name}{"modeHelper"}."('".$targetmode."')}");
  1242. }else{
  1243. $msg = "Simulation {".$attr{$name}{"modeHelper"}."('".$targetmode."')}";
  1244. Log3 $name,1,"[YAAHM_mode] ".$msg;
  1245. return $msg;
  1246. }
  1247. }
  1248. }
  1249. #########################################################################################
  1250. #
  1251. # YAAHM_state - Change the house state (unscured, secured, guarded)
  1252. #
  1253. # Parameter name = name of the YAAHM device
  1254. #
  1255. #########################################################################################
  1256. sub YAAHM_state {
  1257. my ($name,$targetstate,$exec) = @_;
  1258. my $hash = $defs{$name};
  1259. my $prevstate = defined($hash->{DATA}{"HSM"}{"state"}) ? $hash->{DATA}{"HSM"}{"state"} : "unsecured";
  1260. my $currmode = defined($hash->{DATA}{"HSM"}{"mode"}) ? $hash->{DATA}{"HSM"}{"mode"} : "normal";
  1261. my $msg = "";
  1262. my $tr_msg = "";
  1263. #-- local checks
  1264. #-- double change
  1265. #if( $prevstate eq $targetstate ){
  1266. # $msg = "transition skipped, we are already in $targetstate state";
  1267. #-- global checks
  1268. #-- changing away from unlocked in party mode is not possible
  1269. if( $targetstate ne "unlocked" && $currmode eq "party" ){
  1270. $msg = "Not possible in party mode";
  1271. $tr_msg = $yaahm_tt->{transition}.' "'.$yaahm_tt->{$targetstate}.'" '.$yaahm_tt->{"notposfrm"}.' '.$yaahm_tt->{"mode"}.'="'.$yaahm_tt->{"party"}.'"';
  1272. }
  1273. #-- don't
  1274. if( $msg ne "" ){
  1275. Log3 $name,1,"[YAAHM_state] ".$msg;
  1276. readingsSingleUpdate($hash,"tr_errmsg",$tr_msg,1);
  1277. return $msg;
  1278. }
  1279. $hash->{DATA}{"HSM"}{"state"} = $targetstate;
  1280. readingsBeginUpdate($hash);
  1281. readingsBulkUpdate($hash,"tr_errmsg","");
  1282. readingsBulkUpdate($hash,"prev_housestate",$prevstate);
  1283. readingsBulkUpdate($hash,"housestate",$targetstate);
  1284. readingsBulkUpdate($hash,"tr_housestate",$yaahm_tt->{$targetstate});
  1285. readingsEndUpdate($hash,1);
  1286. #-- doit, if not simulation
  1287. if (defined($attr{$name}{"stateHelper"})){
  1288. if( !defined($exec) || $exec==1 ){
  1289. fhem("{".$attr{$name}{"stateHelper"}."('".$targetstate."')}");
  1290. }else{
  1291. $msg = "Simulation {".$attr{$name}{"stateHelper"}."('".$targetstate."')}";
  1292. Log3 $name,1,"[YAAHM_state] ".$msg;
  1293. return $msg;
  1294. }
  1295. }
  1296. YAAHM_InternalTimer("check",time()+ 30, "YAAHM_checkstate", $hash, 0);
  1297. }
  1298. #########################################################################################
  1299. #
  1300. # YAAHM_checkstate - check state devices
  1301. #
  1302. # Parameter someHash = either internal hash of timer
  1303. # => need to dereference it for getting device hash
  1304. # or device hash
  1305. #
  1306. #########################################################################################
  1307. sub YAAHM_checkstate($) {
  1308. my ($someHash) = @_;
  1309. my $hash;
  1310. if( defined($someHash->{HASH}) ){
  1311. $hash = $someHash->{HASH};
  1312. }else{
  1313. $hash = $someHash;
  1314. }
  1315. my $name = $hash->{NAME};
  1316. my $next;
  1317. $next = gettimeofday()+AttrVal($name,"stateInterval",60)*60;
  1318. YAAHM_RemoveInternalTimer("check",$hash);
  1319. YAAHM_InternalTimer("check",$next, "YAAHM_checkstate", $hash, 0);
  1320. Log3 $name, 5,"[YAAHM_checkstate] on device ".$hash->{NAME}." called";
  1321. my $istate;
  1322. my $cstate = defined($hash->{DATA}{"HSM"}{"state"}) ? $hash->{DATA}{"HSM"}{"state"} : "";
  1323. return undef
  1324. if( !defined($attr{$name}{"stateDevices"}) );
  1325. for($istate=0;$istate<int(@states);$istate++){
  1326. last
  1327. if($states[$istate] eq $cstate);
  1328. }
  1329. my (@devlist,@devl);
  1330. my ($dev,$devs,$devh,);
  1331. my @devf = ();
  1332. my $isf = 0;
  1333. @devlist = split(',',$attr{$name}{"stateDevices"});
  1334. foreach my $devc (@devlist) {
  1335. @devl = split(':',$devc);
  1336. $dev = $devl[0];
  1337. $devs = $devl[$istate+1];
  1338. if( defined($devs) && ($devs ne "") ){
  1339. $devh = Value($dev);
  1340. if( $devs ne $devh ){
  1341. $isf = 1;
  1342. push(@devf,"<tr><td style=\"text-align:left;padding:5px\">".$dev."</td><td style=\"text-align:left;padding:5px\"><div style=\"color:red\">".$yaahm_tt->{'notok'}.
  1343. "</div></td><td style=\"text-align:left;padding:5px\">".$devh."</td></tr>");
  1344. if( defined(AttrVal($name,"stateWarning",undef)) ){
  1345. fhem("{".AttrVal($name,"stateWarning",undef)."($dev,$devs,$devh)}");
  1346. }
  1347. }else{
  1348. push(@devf,"<tr><td style=\"text-align:left;padding:5px\">".$dev."</td><td style=\"text-align:left;padding:5px\"><div style=\"color:green\">".$yaahm_tt->{'ok'}."</div></td><td></td></tr>");
  1349. }
  1350. }
  1351. }
  1352. readingsBeginUpdate($hash);
  1353. readingsBulkUpdate($hash,"sdev_housestate","<html><table>".join("<br/>",@devf)."</table></html>");
  1354. readingsBulkUpdate($hash,"sec_housestate",(($isf==0)?"secure":"insecure"));
  1355. readingsBulkUpdate($hash,"sym_housestate",(($isf==0)?"<html><div style=\"color:green\">&#x2713;</div></html>":"<html><div style=\"color:red\">&#x274c;</div></html>"));
  1356. readingsEndUpdate($hash,1);
  1357. return undef
  1358. }
  1359. #########################################################################################
  1360. #
  1361. # YAAHM_correctstate - correct state devices
  1362. #
  1363. # Parameter someHash = either internal hash of timer
  1364. # => need to dereference it for getting device hash
  1365. # or device hash
  1366. #
  1367. #########################################################################################
  1368. sub YAAHM_correctstate($) {
  1369. my ($someHash) = @_;
  1370. my $hash;
  1371. if( defined($someHash->{HASH}) ){
  1372. $hash = $someHash->{HASH};
  1373. }else{
  1374. $hash = $someHash;
  1375. }
  1376. my $name = $hash->{NAME};
  1377. Log3 $name, 1,"[YAAHM_correctstate] on device ".$hash->{NAME}." called";
  1378. my $istate;
  1379. my $cstate = defined($hash->{DATA}{"HSM"}{"state"}) ? $hash->{DATA}{"HSM"}{"state"} : "";
  1380. return undef
  1381. if( !defined($attr{$name}{"stateDevices"}) );
  1382. for($istate=0;$istate<int(@states);$istate++){
  1383. last
  1384. if($states[$istate] eq $cstate);
  1385. }
  1386. my (@devlist,@devl);
  1387. my ($dev,$devs,$devh,);
  1388. my @devf = ();
  1389. my $isf = 0;
  1390. @devlist = split(',',$attr{$name}{"stateDevices"});
  1391. foreach my $devc (@devlist) {
  1392. @devl = split(':',$devc);
  1393. $dev = $devl[0];
  1394. $devs = $devl[$istate+1];
  1395. if( defined($devs) && ($devs ne "") ){
  1396. $devh = Value($dev);
  1397. if( $devs ne $devh ){
  1398. Log3 $name, 1,"[YAAHM_correctstate] calling set ".$dev." ".$devs;
  1399. fhem("set ".$dev." ".$devs);
  1400. $isf = 1;
  1401. }
  1402. }
  1403. }
  1404. YAAHM_checkstate($hash)
  1405. if($isf == 1);
  1406. return undef
  1407. }
  1408. #########################################################################################
  1409. #
  1410. # YAAHM_informer - Tell FHEMWEB to inform this page
  1411. #
  1412. # Parameter me = hash of FHEMWEB instance
  1413. #
  1414. #########################################################################################
  1415. sub YAAHM_informer($) {
  1416. my ($me) = @_;
  1417. $me->{inform}{type} = "status";
  1418. $me->{inform}{filter} = "YYY";
  1419. #$me->{inform}{since} = time()-5;
  1420. $me->{inform}{fmt} = "JSON";
  1421. my $filter = $me->{inform}{filter};
  1422. my %h = map { $_ => 1 } devspec2array($filter);
  1423. $h{global} = 1 if( $me->{inform}{addglobal} );
  1424. $h{"#FHEMWEB:$FW_wname"} = 1;
  1425. $me->{inform}{devices} = \%h;
  1426. %FW_visibleDeviceHash = FW_visibleDevices();
  1427. $me->{NTFY_ORDER} = $FW_cname; # else notifyfn won't be called
  1428. %ntfyHash = ();
  1429. }
  1430. #########################################################################################
  1431. #
  1432. # YAAHM_startDayTimer - start the daily timer function
  1433. #
  1434. # Parameter name = name of the YAAHM device
  1435. #
  1436. #########################################################################################
  1437. sub YAAHM_startDayTimer($) {
  1438. my ($name) = @_;
  1439. my $hash = $defs{$name};
  1440. my $res = "defmod $name.dtimer.IF DOIF ";
  1441. my $msg;
  1442. #--cleanup after definition fault
  1443. fhem("deletereading $name t_aftermidnight")
  1444. if( ReadingsVal($name,"t_aftermidnight",undef) );
  1445. fhem("deletereading $name t_aftersunrise")
  1446. if( ReadingsVal($name,"t_aftersunrise",undef) );
  1447. fhem("deletereading $name t_aftersunset")
  1448. if( ReadingsVal($name,"t_aftersunset",undef) );
  1449. fhem("deletereading $name t_beforemidnight")
  1450. if( ReadingsVal($name,"t_beforemidnight",undef) );
  1451. fhem("deletereading $name t_beforesunrise")
  1452. if( ReadingsVal($name,"t_beforesunrise",undef) );
  1453. fhem("deletereading $name t_beforesunset")
  1454. if( ReadingsVal($name,"t_beforesunset",undef) );
  1455. delete $hash->{DATA}{"DT"}{"daytime"};
  1456. delete $hash->{DATA}{"DT"}{"nighttime"};
  1457. #-- TODO check for plausibility
  1458. #--aftermidnight must be >= 00:01
  1459. my $check=$hash->{DATA}{"DT"}{"aftermidnight"}[0];
  1460. $check =~ s/://;
  1461. if( $check <= 0 ){
  1462. Log3 $name,1,"[YAAHM_startDayTimer] aftermidnight is minimum 00:01, used this value";
  1463. $hash->{DATA}{"DT"}{"aftermidnight"}[0] = "00:01";
  1464. }
  1465. #-- Internal timer for night time
  1466. my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time);
  1467. my $nval = $hash->{DATA}{"DT"}{"night"}[0];
  1468. if( $nval !~ /\d\d:\d\d/ ){
  1469. $msg = "Error in night time specification";
  1470. Log3 1,$name,"[YAAHM_startDayTimer] ".$msg;
  1471. return $msg;
  1472. }
  1473. my ($hourn,$minn) = split(':',$nval);
  1474. my $deltan = ($hourn-$hour)*3600+($minn-$min)*60-$sec;
  1475. my $delta = $deltan;
  1476. $delta += 86400
  1477. if( $delta<0 );
  1478. YAAHM_RemoveInternalTimer ("nighttime", $hash);
  1479. YAAHM_InternalTimer ("nighttime", gettimeofday()+$delta, "YAAHM_tonight", $hash, 0);
  1480. #-- Internal timer for daytime
  1481. my $mval = $hash->{DATA}{"DT"}{"morning"}[0];
  1482. if( $mval !~ /\d\d:\d\d/ ){
  1483. $msg = "Error in morning time specification";
  1484. Log3 1,$name,"[YAAHM_startDayTimer] ".$msg;
  1485. return $msg;
  1486. }
  1487. ($hourn,$minn) = split(':',$mval);
  1488. my $deltam = ($hourn-$hour)*3600+($minn-$min)*60-$sec;
  1489. $delta = $deltam;
  1490. $delta += 86400
  1491. if( $delta<0 );
  1492. YAAHM_RemoveInternalTimer ("daytime", $hash);
  1493. YAAHM_InternalTimer ("daytime", gettimeofday()+$delta, "YAAHM_today", $hash, 0);
  1494. #-- currently day or night ?
  1495. my $currtime = (($deltam < 0) && ($deltan > 0)) ? "daytime" : "nighttime";
  1496. #-- put data into readings
  1497. readingsBeginUpdate($hash);
  1498. readingsBulkUpdate($hash,"housephase",$currtime);
  1499. readingsBulkUpdate($hash,"tr_housephase",$yaahm_tt->{$currtime});
  1500. #-- compose external timer
  1501. foreach my $key (sort YAAHM_dsort keys %defaultdailytable){
  1502. next if( !defined($hash->{DATA}{"DT"}{$key}[2]) );
  1503. my $f1 = defined($defaultdailytable{$key}[0]);
  1504. my $f2 = defined($defaultdailytable{$key}[1]);
  1505. my $f3 = defined($hash->{DATA}{"DT"}{$key}[2]) && $hash->{DATA}{"DT"}{$key}[2] ne "";
  1506. #-- to prevent double execution of functions do this in YAAHM_time, NOT in timer
  1507. my $xval = "{YAAHM_time('".$name."','".$key."',1)}";
  1508. #-- entries in the default table with no entry are single-timers
  1509. if( !$f1 and !$f2 ){
  1510. $res .= "([[".$name.":s_".$key."]])\n(".$xval.")\nDOELSEIF"
  1511. if( $f3 );
  1512. #-- entries in the default table with only first time are single-timers
  1513. }elsif( $f1 and !$f2 ){
  1514. $res .= "([[".$name.":s_".$key."]])\n(".$xval.")\nDOELSEIF"
  1515. if( $f3 );
  1516. #-- entries in the default table with only second time are single-timer offsets
  1517. }elsif( !$f1 and $f2 ){
  1518. $res .= "([[".$name.":s_".$key."]])\n(".$xval.")\nDOELSEIF"
  1519. if( $f3 );
  1520. #-- entries in the default table with first and second time are two-timer periods
  1521. }elsif( $f1 and $f2 ){
  1522. $res .= "([[".$name.":s_".$key."]-[".$name.":t_".$key."]])\n(".$xval.")\nDOELSEIF"
  1523. if( $f3 );
  1524. #-- something wrong
  1525. }else{
  1526. $msg = "Daily timer $name.dtimer.IF NOT started, something wrong with entry ".$key;
  1527. Log 1,"[YAAHM_startDayTimer] ".$msg;
  1528. return $msg;
  1529. }
  1530. }
  1531. readingsEndUpdate($hash,1);
  1532. #-- take out last DOELSEIF
  1533. $res =~ s/\nDOELSEIF$//;
  1534. fhem($res);
  1535. fhem("attr $name.dtimer.IF do always");
  1536. fhem("set $name.dtimer.IF enable");
  1537. #-- save everything
  1538. YAAHM_save($hash);
  1539. fhem("save");
  1540. return "Daily timer $name.dtimer.IF started";
  1541. }
  1542. #########################################################################################
  1543. #
  1544. # YAAHM_startWeeklyTimer - start the Weekly timer function
  1545. #
  1546. # Parameter name = name of the YAAHM device
  1547. #
  1548. #########################################################################################
  1549. sub YAAHM_startWeeklyTimer($) {
  1550. my ($name) = @_;
  1551. my $hash = $defs{$name};
  1552. my $res;
  1553. my $wupn;
  1554. YAAHM_setWeeklyTime($hash);
  1555. #-- start timer
  1556. for( my $i=0;$i < int( @{$hash->{DATA}{"WT"}} );$i++){
  1557. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  1558. $res = "defmod ".$name.".wtimer_".$i.".IF DOIF ([".$name.":ring_".$i."] eq \"off\")\n()\nDOELSEIF\n(([[".$name.":ring_".$i."]])";
  1559. #-- check for activity description
  1560. my $g4a = defined($hash->{DATA}{"WT"}[$i]{"acti_m"}) ? $hash->{DATA}{"WT"}[$i]{"acti_m"} : "";
  1561. my $g4b = defined($hash->{DATA}{"WT"}[$i]{"acti_d"}) ? $hash->{DATA}{"WT"}[$i]{"acti_d"} : "";
  1562. my $v4a = ($g4a ne "") ? "(normal)|(".join(')|(',split(',',$g4a)).")" : "(normal)";
  1563. my $v4b = ($g4b ne "") ? "(workday)|(weekend)|(".join(')|(',split(',',$g4b)).")" : "(workday)|(weekend)";
  1564. $res .= "\nand ((([" .$name. ":housemode] =~ \"".$v4a."\")";
  1565. $res .= "\nand ([" .$name. ":todayType] =~ \"".$v4b."\")) ";
  1566. $res .= "\nor ([".$name.":ring_".$i."x] =~ \".*\(man\)\")) ";
  1567. #-- to prevent double execution of functions do this in YAAHM_time, NOT in timer
  1568. my $xval;
  1569. if( $i==0 ){
  1570. $xval = "{YAAHM_time('".$name."','wakeup',1)}";
  1571. }elsif( $i==1 ){
  1572. $xval = "{YAAHM_time('".$name."','sleep',1)}";
  1573. }else{
  1574. $xval = "{YAAHM_time('".$name."','timer_".$i."',1)}";
  1575. }
  1576. #-- action
  1577. $res .= ")\n(".$xval.")";
  1578. #-- doit
  1579. fhem($res);
  1580. fhem("attr ".$name.".wtimer_".$i.".IF do always");
  1581. fhem("set ".$name.".wtimer_".$i.".IF enable");
  1582. }
  1583. #-- save everything
  1584. YAAHM_save($hash);
  1585. fhem("save");
  1586. return "Weekly timers started";
  1587. }
  1588. #########################################################################################
  1589. #
  1590. # YAAHM_setWeeklyTime - set the Weekly times into readings
  1591. #
  1592. # Parameter hash = hash of device addressed
  1593. #
  1594. #########################################################################################
  1595. sub YAAHM_setWeeklyTime($) {
  1596. my ($hash) = @_;
  1597. my $name = $hash->{NAME};
  1598. #-- weekly profile times
  1599. my ($sg0,$sg1,$ring_0x,$ring_1x,$ring_0e,$ring_1e,$ring_0,$ring_1,$ng);
  1600. #-- iterate over timers
  1601. for( my $i=0;$i<int( @{$hash->{DATA}{"WT"}} );$i++){
  1602. #-- obtain next time spec => will override all
  1603. $ng = $hash->{DATA}{"WT"}[$i]{ "next" };
  1604. #-- highest priority is a disabled timer - no wakeup at all
  1605. if( ReadingsVal($name.".wtimer_".$i.".IF","mode","") eq "disabled" ){
  1606. $sg0 = "off";
  1607. $sg1 = "off";
  1608. $ring_0e = "disabled (timer)";
  1609. $ring_1e = "disabled (timer)";
  1610. #-- if the timer is enabled, we'll use its timing values
  1611. }else{
  1612. $sg0 = $hash->{DATA}{"WT"}[$i]{ $weeklytable[$hash->{DATA}{"DD"}[0]{"weekday"}] } ;
  1613. $sg1 = $hash->{DATA}{"WT"}[$i]{ $weeklytable[$hash->{DATA}{"DD"}[1]{"weekday"}] };
  1614. $ring_0e = "enabled";
  1615. $ring_1e = "enabled";
  1616. #-- next higher priority for "off" is daytype
  1617. my $wupad = $hash->{DATA}{"WT"}[$i]{"acti_d"}.",workday,weekend";
  1618. #-- start with tomorrow
  1619. if( index($wupad, $hash->{DATA}{"DD"}[1]{"daytype"}) == -1 ){
  1620. $ring_1x = "off (".substr(ReadingsVal($name,"tr_tomorrowType",""),0,3).")";
  1621. $ring_1e = "disabled (".ReadingsVal($name,"tomorrowType","").")";
  1622. }elsif( ($hash->{DATA}{"DD"}[1]{"vacflag"} == 1 ) && index($wupad,"vacation") == -1 ){
  1623. $ring_1x = "off (".substr($yaahm_tt->{"vacation"},0,3).")";
  1624. $ring_1e = "disabled (vacation)";
  1625. }else{
  1626. $ring_1x = $sg1;
  1627. }
  1628. #-- because today we might also have an influence of housemode
  1629. if( index($wupad, $hash->{DATA}{"DD"}[0]{"daytype"}) == -1 ){
  1630. $ring_0x = "off (".substr(ReadingsVal($name,"tr_todayType",""),0,3).")";
  1631. $ring_0e = "disabled (".ReadingsVal($name,"todayType","").")";
  1632. }elsif( ($hash->{DATA}{"DD"}[0]{"vacflag"} == 1 ) && index($wupad,"vacation") == -1 ){
  1633. $ring_0x = "off (".substr($yaahm_tt->{"vacation"},0,3).")";
  1634. $ring_0e = "disabled (vacation)";
  1635. }else{
  1636. #-- next higher priority for "off" (only today !) is housemode
  1637. my $wupam = $hash->{DATA}{"WT"}[$i]{"acti_m"}.",normal";
  1638. if( index($wupam, ReadingsVal($name,"housemode","")) == -1 ){
  1639. $ring_0x = "off (".substr(ReadingsVal($name,"tr_housemode",""),0,3).")";
  1640. $ring_0e = "disabled (".ReadingsVal($name,"housemode","").")";
  1641. }else{
  1642. $ring_0x = $sg0;
  1643. }
  1644. }
  1645. }
  1646. #-- no "next" time specification
  1647. if( !defined($ng) || $ng eq "" ){
  1648. $ring_0 = $sg0;
  1649. $ring_1 = $sg1;
  1650. #-- highest priority is a "next" time specification
  1651. }else{
  1652. #-- current time
  1653. my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time);
  1654. my $lga = sprintf("%02d%02d",$hour,$min);
  1655. #-- today's waketime
  1656. my $tga = $sg0;
  1657. $tga =~ s/://;
  1658. #-- tomorrow's waketime
  1659. my $tgm = $sg1;
  1660. $tgm =~ s/://;
  1661. #-- "next" input
  1662. my $nga = (defined $ng)?$ng:"";
  1663. $nga =~ s/://;
  1664. #-- "next" is the same as todays waketime and todays waketime not over => restore !
  1665. if( ($nga eq $tga) && ($tga > $lga)){
  1666. $ring_0 = $sg0;
  1667. $ring_1 = $sg1;
  1668. $ng = "";
  1669. $hash->{DATA}{"WT"}[$i]{ "next" }="";
  1670. #-- "next" is the same as tomorrows waketime and todays waketime over => restore !
  1671. }elsif( ($nga eq $tgm) && ($tga < $lga)){
  1672. $ring_0 = $sg0;
  1673. $ring_1 = $sg1;
  1674. $ng = "";
  1675. $hash->{DATA}{"WT"}[$i]{ "next" }="";
  1676. #-- "next" is off
  1677. }elsif( $nga eq "off" ){
  1678. #-- today's waketime not over => we mean today
  1679. if( $tga ne "off" && ($tga > $lga)){
  1680. if( $ring_0x !~ /^off/ ){
  1681. $ring_0x = "off (man)";
  1682. $ring_0 = "off";
  1683. $ring_1 = $sg1;
  1684. }
  1685. #-- today's waketime over => we mean tomorrow
  1686. }else{
  1687. if( $ring_1x !~ /^off/ ){
  1688. $ring_1x = "off (man)";
  1689. $ring_0 = $sg0;
  1690. $ring_1 = "$sg1 (off)";
  1691. }
  1692. }
  1693. #-- "next" is nontrivial timespec
  1694. }else{
  1695. #-- "next" after current time => we mean today
  1696. if( $nga > $lga ){
  1697. #-- the same as original waketime => restore ! (do we come here at all ?)
  1698. #if( $ng eq $sg0 ){
  1699. # $ring_0x = $sg0;
  1700. # $ring_0 = $sg0;
  1701. # $ng = "";
  1702. # $hash->{DATA}{"WT"}[$i]{ "next" } = "";
  1703. #-- new manual waketime tomorrow
  1704. #}else{
  1705. $ring_0x = "$ng (man)";
  1706. $ring_0 = $ng;
  1707. #}
  1708. $ring_1 = $sg1;
  1709. #-- "next" before current time => we mean tomorrow
  1710. }else{
  1711. #-- the same as original waketime => restore ! (do we come here at all ?)
  1712. #if( $ng eq $sg1 ){
  1713. # $ring_0x = $sg1;
  1714. # $ring_1 = $sg1;
  1715. # $ng = "";
  1716. # $hash->{DATA}{"WT"}[$i]{ "next" } = "";
  1717. #}else{
  1718. $ring_1x = "$ng (man)";
  1719. $ring_1 = "$sg1 ($ng)";
  1720. #}
  1721. $ring_0 = $sg0;
  1722. }
  1723. }
  1724. }
  1725. $hash->{DATA}{"WT"}[$i]{"ring_0"} = $ring_0;
  1726. $hash->{DATA}{"WT"}[$i]{"ring_1"} = $ring_1;
  1727. $hash->{DATA}{"WT"}[$i]{"ring_0x"} = $ring_0x;
  1728. $hash->{DATA}{"WT"}[$i]{"ring_1x"} = $ring_1x;
  1729. $hash->{DATA}{"WT"}[$i]{"ring_0e"} = $ring_0e;
  1730. $hash->{DATA}{"WT"}[$i]{"ring_1e"} = $ring_1e;
  1731. #Log 1,"====> AFTER FINAL CHECK TIMER $i sg0=$sg0 ring_0x=$ring_0x sg1=$sg1 ring_1x=$ring_1x ng=$ng";
  1732. #Log 1," ".$hash->{DATA}{"WT"}[$i]{"ring_0x"}." ".$hash->{DATA}{"WT"}[$i]{"ring_1x"};
  1733. #-- notation:
  1734. # today_i is today's waketime of timer i
  1735. # tomorrow_i is tomorrow's waketime of timer i
  1736. # timers have additional conditions for activation according
  1737. # to housemode and daytype, these conditions are checked in the timer device
  1738. # devices and are not part of the table. But we have a reading:
  1739. # today_i_e is a copy of the condition checked in the timer device
  1740. # (housemode and daytype)
  1741. # tomorrow_i_e is not a complete copy of the condition checked in the timer device,
  1742. # (daytype only, because housemode of tomorrow is not known)
  1743. # ring_[i]_1 is tomorrow's ring time of timer i
  1744. readingsBeginUpdate($hash);
  1745. readingsBulkUpdate( $hash, "today_".$i,$sg0 );
  1746. readingsBulkUpdate( $hash, "tomorrow_".$i,$sg1 );
  1747. readingsBulkUpdate( $hash, "today_".$i."_e",$ring_0e );
  1748. readingsBulkUpdate( $hash, "tomorrow_".$i."_e",$ring_1e );
  1749. readingsBulkUpdate( $hash, "ring_".$i,$ring_0 );
  1750. readingsBulkUpdate( $hash, "ring_".$i."_1",$ring_1 );
  1751. readingsBulkUpdate( $hash, "ring_".$i."x",$ring_0x );
  1752. readingsBulkUpdate( $hash, "ring_".$i."_1x",$ring_1x );
  1753. readingsBulkUpdate( $hash, "next_".$i,$ng );
  1754. readingsEndUpdate($hash,1);
  1755. YAAHM_sayWeeklyTime($hash,$i,0);
  1756. }
  1757. }
  1758. #########################################################################################
  1759. #
  1760. # YAAHM_sayWeeklyTime - say the next weekly time
  1761. #
  1762. # Parameter hash = hash of YAAHM dvice
  1763. # timer = number of timer
  1764. # sp = 1 => output for speech device
  1765. #
  1766. #########################################################################################
  1767. sub YAAHM_sayWeeklyTime($$$) {
  1768. my ($hash,$timer,$sp) = @_;
  1769. my $name = $hash->{NAME};
  1770. my ($tod,$tom,$ton,$hl,$ml,$tl,$ht,$mt,$tt,$tsay,$chg,$msg,$hw,$mw,$pt,$rea,$done,$norep);
  1771. #--determine which timer (duplicate check when coming from set)
  1772. if( $timer >= int( @{$hash->{DATA}{"WT"}}) ){
  1773. $msg = "Error, timer number $timer does not exist, number musst be smaller than ".int( @{$hash->{DATA}{"WT"}});
  1774. Log3 $name,1,"[YAAHM_sayWeeklyTime] ".$msg;
  1775. return $msg;
  1776. }
  1777. #-- init message
  1778. $msg = $hash->{DATA}{"WT"}[$timer]{"name"};
  1779. #-- get timer values from readings, because these include vacation settings and special time
  1780. $tod = $hash->{DATA}{"WT"}[$timer]{"ring_0x"};
  1781. $tom = $hash->{DATA}{"WT"}[$timer]{"ring_1x"};
  1782. $done = $hash->{DATA}{"WT"}[$timer]{"done"};
  1783. $norep = AttrVal($name,"norepeat",0);
  1784. #-- current local time
  1785. ($hl,$ml) = split(':',strftime('%H:%M', localtime(time)));
  1786. $tl = 60*$hl+$ml;
  1787. #-- today off AND tomorrow any time or off => compare this time with current time
  1788. if( $tod =~ /^off.*/ ){
  1789. #-- tomorrow any time
  1790. if( $tom =~ /(\d?\d):(\d\d)(:(\d\d))?/ && $tom !~ /.*\(off\)$/ ){
  1791. #Log 1,"===========> |$1|$2|$3|$4";
  1792. ($ht,$mt) = split('[\s:]',$tom);
  1793. $tt=60*$ht+$mt;
  1794. #-- wakeup tomorrow later than now
  1795. if( $tt < $tl ){
  1796. $hw = $1*1;
  1797. $mw = $2*1;
  1798. $pt = sprintf("%d:%02d",$hw,$mw)." ".lc($yaahm_tt->{"tomorrow"});
  1799. $msg .= " ".lc($yaahm_tt->{"tomorrow"})." $hw ".$yaahm_tt->{"clock"};
  1800. $msg .=" $mw"
  1801. if( $mw != 0 );
  1802. }else{
  1803. $pt = "off ".lc($yaahm_tt->{"today"});
  1804. $msg .= " ".lc($yaahm_tt->{"today"})." ".$yaahm_tt->{"swoff"};
  1805. }
  1806. }elsif( $tom =~ /^off/ || $tom =~ /.*\(off\)$/ ){
  1807. $pt = "off ".lc($yaahm_tt->{"today"})." ".$yaahm_tt->{"and"}." ".lc($yaahm_tt->{"tomorrow"});
  1808. $msg .= " ".lc($yaahm_tt->{"today"})." ".$yaahm_tt->{"and"}." ".lc($yaahm_tt->{"tomorrow"})." ".$yaahm_tt->{"swoff"};
  1809. }else{
  1810. $pt = $yaahm_tt->{"undecid"};
  1811. $msg .= " ".$yaahm_tt->{"undecid"};
  1812. }
  1813. #-- today nontrivial => compare this time with current time
  1814. }elsif( $tod =~ /(\d?\d):(\d\d)(:(\d\d))?/ ){
  1815. #Log 1,"===========> |$1|$2|$3|$4";
  1816. ($ht,$mt) = split('[\s:]',$tod);
  1817. $tt=60*$ht+$mt;
  1818. #-- wakeup later today, and ( not yet done - or done, but repeat possible)
  1819. if( ($tt >= $tl) && (($done == 0) | (($done == 1)&&($norep == 0))) ){
  1820. $hw = $1*1;
  1821. $mw = $2*1;
  1822. $pt = sprintf("%d:%02d",$hw,$mw)." ".lc($yaahm_tt->{"today"});
  1823. $msg .= " ".lc($yaahm_tt->{"today"})." $hw ".$yaahm_tt->{"clock"};
  1824. $msg .=" $mw"
  1825. if( $mw != 0 );
  1826. #-- todays time already past => tomorrow - but this may be off
  1827. }elsif( ($tom eq "off") || ($tom =~ /.*\(off\)/) ){
  1828. $pt = "off ".lc($yaahm_tt->{"tomorrow"});
  1829. $msg .= " ".lc($yaahm_tt->{"tomorrow"})." ".$yaahm_tt->{"swoff"};
  1830. }elsif( $tom =~ /(\d?\d):(\d\d)(:(\d\d))?( \((\d?\d):(\d\d)(:(\d\d))?\))?/ ){
  1831. #Log 1,"===========> |$1|$2|$3|$4|$5|$6";
  1832. if( defined($5) && $5 ne ""){
  1833. $hw = $6*1;
  1834. $mw = $7*1;
  1835. }else{
  1836. $hw = $1*1;
  1837. $mw = $2*1;
  1838. }
  1839. $pt = sprintf("%d:%02d",$hw,$mw)." ".lc($yaahm_tt->{"tomorrow"});
  1840. $msg .= " ".lc($yaahm_tt->{"tomorrow"})." $hw ".$yaahm_tt->{"clock"};
  1841. $msg .=" $mw"
  1842. if( $mw != 0 );
  1843. }elsif( $tom =~ /^off/ || $tom =~ /.*\(off\)$/ ){
  1844. $pt = "off ".lc($yaahm_tt->{"tomorrow"});
  1845. $msg .= " ".lc($yaahm_tt->{"tomorrow"})." ".$yaahm_tt->{"swoff"};
  1846. }else{
  1847. $pt = $yaahm_tt->{"undecid"};
  1848. $msg .= " ".$yaahm_tt->{"undecid"};
  1849. }
  1850. }else{
  1851. $pt = $yaahm_tt->{"undecid"};
  1852. $msg .= " ".$yaahm_tt->{"undecid"};
  1853. }
  1854. $hash->{DATA}{"WT"}[$timer]{"wake"} = $pt;
  1855. readingsSingleUpdate($hash,"tr_wake_".$timer,$pt,1);
  1856. if( $sp==0 ){
  1857. return $pt
  1858. }else{
  1859. return $msg;
  1860. }
  1861. }
  1862. #########################################################################################
  1863. #
  1864. # YAAHM_checkMonthly - check Monthly calendar at each
  1865. #
  1866. # Parameter hash = hash of device addressed
  1867. #
  1868. #########################################################################################
  1869. sub YAAHM_checkMonthly($$$) {
  1870. my ($hash,$event,$param) = @_;
  1871. my $name = $hash->{NAME};
  1872. my ($ret,$line,$fline,$date);
  1873. my (@lines,@chunks,@tday,@eday,@sday,@tmor,@two);
  1874. my ($stoday,$stom,$stwom,$tod);
  1875. my $todaylong = "";
  1876. my $tomlong = "";
  1877. my $twodaylong= "";
  1878. #-- Vorschau täglich oder wenn neu gestartet
  1879. if( ($event eq "test") || (($event eq 'event') && ($param eq 'aftermidnight')) ){
  1880. my $specialDevs = AttrVal( $name, "specialDevices", "" );
  1881. foreach my $specialDev ( split( /,/, $specialDevs ) ) {
  1882. my ($todaydesc,$tomdesc,$twomdesc);
  1883. #-- device of type holiday
  1884. if( IsDevice( $specialDev, "holiday" )){
  1885. $stoday = strftime('%m-%d', localtime(time));
  1886. $stom = strftime('%m-%d', localtime(time+86400));
  1887. $stwom = strftime('%m-%d', localtime(time+2*86400));
  1888. $tod = holiday_refresh( $specialDev, $stoday );
  1889. if ( $tod ne "none" ) {
  1890. $todaydesc .= $tod.",";
  1891. Log3 $name, 5,"[YAAHM] found today=special date \"$tod\" in holiday $specialDev";
  1892. }
  1893. $tod = holiday_refresh( $specialDev, $stom );
  1894. if ( $tod ne "none" ) {
  1895. $tomdesc .= $tod.",";
  1896. Log3 $name, 5,"[YAAHM] found tomorrow=special date \"$tod\" in holiday $specialDev";
  1897. }
  1898. $tod = holiday_refresh( $specialDev, $stwom );
  1899. if ( $tod ne "none" ) {
  1900. $twomdesc .= $tod.",";
  1901. Log3 $name, 5,"[YAAHM] found twodays=special date \"$tod\" in holiday $specialDev";
  1902. }
  1903. #-- device of type calendar
  1904. }elsif( IsDevice($specialDev, "Calendar" )){
  1905. $stoday = strftime('%d.%m.%Y', localtime(time));
  1906. $stom = strftime('%d.%m.%Y', localtime(time+86400));
  1907. $stwom = strftime('%d.%m.%Y', localtime(time+2*86400));
  1908. @tday = split('\.',$stoday);
  1909. @tmor = split('\.',$stom);
  1910. @two = split('\.',$stwom);
  1911. $fline=Calendar_Get($defs{$specialDev},"get","full","mode=alarm|start|upcoming");
  1912. #-- more complicated to check here,
  1913. # format is '<id> upcoming [<datetoannounce> <timetoannouce>] <datestart> <timestart>-<dateend> <timeend> [<description>]
  1914. my ($cstart,$cdesc);
  1915. if($fline){
  1916. #chomp($fline);
  1917. @lines = split('\n',$fline);
  1918. foreach $fline (@lines){
  1919. chomp($fline);
  1920. @chunks = split(' ',$fline);
  1921. if( int(@chunks)>=7 ){
  1922. $cstart = 4;
  1923. $cdesc = 7;
  1924. }else{
  1925. $cstart = 2;
  1926. $cdesc = 5,
  1927. }
  1928. @sday = split('\.',$chunks[$cstart]);
  1929. $tod = ($chunks[$cdesc]) ? $chunks[$cdesc] : "???";
  1930. #-- today
  1931. my $rets = ($sday[2]-$tday[2])*365+($sday[1]-$tday[1])*31+($sday[0]-$tday[0]);
  1932. if( $rets==0 ){
  1933. $todaydesc .= $tod.",";
  1934. Log3 $name, 5,"[YAAHM] found today=special date \"$tod\" in calendar $specialDev";
  1935. }
  1936. $rets = ($sday[2]-$tmor[2])*365+($sday[1]-$tmor[1])*31+($sday[0]-$tmor[0]);
  1937. if( $rets==0 ){
  1938. $tomdesc .= $tod.",";
  1939. Log3 $name, 5,"[YAAHM] found tomorrow=special date \"$tod\" in calendar $specialDev";
  1940. }
  1941. $rets = ($sday[2]-$two[2])*365+($sday[1]-$two[1])*31+($sday[0]-$two[0]);
  1942. if( $rets==0 ){
  1943. $twomdesc .= $tod.",";
  1944. Log3 $name, 5,"[YAAHM] found twodays=special date \"$tod\" in calendar $specialDev";
  1945. }
  1946. }
  1947. }
  1948. }else{
  1949. Log3 $name, 1,"[YAAHM] unknown special device $specialDev";
  1950. }
  1951. #-- accumulate descriptions
  1952. $todaylong .= $todaydesc
  1953. if($todaydesc);
  1954. $tomlong .= $tomdesc
  1955. if($tomdesc);
  1956. $twodaylong .= $twomdesc
  1957. if($twomdesc);
  1958. }
  1959. $todaylong =~ s/,$//;
  1960. $tomlong =~ s/,$//;
  1961. $twodaylong =~ s/,$//;
  1962. $hash->{DATA}{"DD"}[0]{"special"} = $todaylong;
  1963. $hash->{DATA}{"DD"}[1]{"special"} = $tomlong;
  1964. #-- put into readings
  1965. readingsBeginUpdate($hash);
  1966. readingsBulkUpdateIfChanged( $hash, "todaySpecial",$todaylong );
  1967. readingsBulkUpdateIfChanged( $hash, "tomorrowSpecial",$tomlong);
  1968. readingsBulkUpdateIfChanged( $hash, "twodaysSpecial",$twodaylong);
  1969. readingsEndUpdate($hash,1);
  1970. }
  1971. }
  1972. #########################################################################################
  1973. #
  1974. # YAAHM_today - internal function to switch into daytime
  1975. #
  1976. # Parameter timerHash = internal hash of timer
  1977. # => need to dereference it for getting device hash
  1978. #
  1979. #########################################################################################
  1980. sub YAAHM_today($) {
  1981. my ($timerHash) = @_;
  1982. my $next;
  1983. my $hash = $timerHash->{HASH};
  1984. my $name = $hash->{NAME};
  1985. $next = gettimeofday()+86400;
  1986. YAAHM_RemoveInternalTimer("today",$hash);
  1987. YAAHM_InternalTimer("today",$next, "YAAHM_today", $hash, 0);
  1988. Log 1,"[YAAHM_today] on device ".$hash->{NAME}." called for this day";
  1989. readingsBeginUpdate($hash);
  1990. readingsBulkUpdate($hash,"housephase","daytime");
  1991. readingsBulkUpdate($hash,"tr_housephase",$yaahm_tt->{"daytime"});
  1992. readingsEndUpdate($hash,1);
  1993. return undef;
  1994. }
  1995. #########################################################################################
  1996. #d
  1997. # YAAHM_tonight - internal function to switch into nighttime
  1998. #
  1999. # Parameter timerHash = internal hash of timer
  2000. # => need to dereference it for getting device hash
  2001. #
  2002. #########################################################################################
  2003. sub YAAHM_tonight($) {
  2004. my ($timerHash) = @_;
  2005. my $next;
  2006. my $hash = $timerHash->{HASH};
  2007. my $name = $hash->{NAME};
  2008. $next = gettimeofday()+86400;
  2009. YAAHM_RemoveInternalTimer("tonight",$hash);
  2010. YAAHM_InternalTimer("tonight",$next, "YAAHM_tonight", $hash, 0);
  2011. Log 1,"[YAAHM_tonight] on device ".$hash->{NAME}." called for this day";
  2012. readingsBeginUpdate($hash);
  2013. readingsBulkUpdate($hash,"housephase","nighttime");
  2014. readingsBulkUpdate($hash,"tr_housephase",$yaahm_tt->{"nighttime"});
  2015. readingsEndUpdate($hash,1);
  2016. return undef;
  2017. }
  2018. #########################################################################################
  2019. #
  2020. # YAAHM_updater - internal update function 1 minute after midnight
  2021. #
  2022. # Parameter timerHash = on first call, device hash.
  2023. # = on later calls: internal hash of timer
  2024. # => need to dereference it for getting device hash
  2025. #
  2026. #########################################################################################
  2027. sub YAAHM_updater($) {
  2028. my ($timerHash) = @_;
  2029. my $hash;
  2030. my $next;
  2031. #-- start timer for updates - when device is reloaded
  2032. if( defined($firstcall) && ($firstcall==1) ){
  2033. #-- timerHash is device hash
  2034. $hash = $timerHash;
  2035. my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time);
  2036. $next = gettimeofday()+(23-$hour)*3600+(59-$min)*60+(59-$sec)+34;
  2037. $firstcall=0;
  2038. #-- continue timer for updates
  2039. }else{
  2040. #-- timerHash is internal hash
  2041. $hash = $timerHash->{HASH};
  2042. $next = gettimeofday()+86400;
  2043. }
  2044. #-- safeguard if hash is not properly indirected
  2045. if( defined($hash->{HASH}) ){
  2046. #Log 1,"WARNING ! HASH indirection not ok. firstcall=$firstcall";
  2047. $hash = $hash->{HASH};
  2048. }
  2049. YAAHM_RemoveInternalTimer("aftermidnight",$hash);
  2050. YAAHM_InternalTimer("aftermidnight",$next, "YAAHM_updater", $hash, 0);
  2051. Log 1,"[YAAHM_updater] on device ".$hash->{NAME}." called for this day";
  2052. YAAHM_GetDayStatus($hash);
  2053. return undef;
  2054. }
  2055. #########################################################################################
  2056. #
  2057. # YAAHM_InternalTimer - start named internal timer
  2058. #
  2059. # Parameter modifier = name suffix
  2060. # tim = time
  2061. # callback = callback function
  2062. #
  2063. #
  2064. #########################################################################################
  2065. sub YAAHM_InternalTimer($$$$$) {
  2066. my ($modifier, $tim, $callback, $hash, $waitIfInitNotDone) = @_;
  2067. my $mHash;
  2068. if ($modifier eq "") {
  2069. $mHash = $hash;
  2070. } else {
  2071. my $timerName = "$hash->{NAME}_$modifier";
  2072. if (exists ($hash->{TIMER}{$timerName})) {
  2073. $mHash = $hash->{TIMER}{$timerName};
  2074. } else {
  2075. $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier};
  2076. $hash->{TIMER}{$timerName} = $mHash;
  2077. }
  2078. }
  2079. InternalTimer($tim, $callback, $mHash, $waitIfInitNotDone);
  2080. }
  2081. #########################################################################################
  2082. #
  2083. # YAAHM_RemoveInternalTimer - kill named internal timer
  2084. #
  2085. # Parameter
  2086. #
  2087. #########################################################################################
  2088. sub YAAHM_RemoveInternalTimer($$) {
  2089. my ($modifier, $hash) = @_;
  2090. my $timerName = "$hash->{NAME}_$modifier";
  2091. if ($modifier eq "") {
  2092. RemoveInternalTimer($hash);
  2093. } else {
  2094. my $myHash = $hash->{TIMER}{$timerName};
  2095. if (defined($myHash)) {
  2096. delete $hash->{TIMER}{$timerName};
  2097. RemoveInternalTimer($myHash);
  2098. }
  2099. }
  2100. }
  2101. #########################################################################################
  2102. #
  2103. # YAAHM_GetDayStatus
  2104. #
  2105. # Parameter hash = hash of device addressed
  2106. #
  2107. #########################################################################################
  2108. sub YAAHM_GetDayStatus($) {
  2109. my ($hash) = @_;
  2110. my $name = $hash->{NAME};
  2111. #-- readjust language
  2112. my $lang = AttrVal("global","language","EN");
  2113. if( $lang eq "DE"){
  2114. $yaahm_tt = \%yaahm_transtable_DE;
  2115. }else{
  2116. $yaahm_tt = \%yaahm_transtable_EN;
  2117. }
  2118. #-- iterate over timers to reset the "done" flag
  2119. for( my $i=0;$i<int( @{$hash->{DATA}{"WT"}} );$i++){
  2120. $hash->{DATA}{"WT"}[$i]{"done"} = 0;
  2121. }
  2122. my ($ret,$line,$fline,$date);
  2123. my (@lines,@chunks,@tday,@eday,@sday,@tmor,@ttwo);
  2124. my ($todaydesc,$todaytype,$tomdesc,$tomtype,$twodesc,$twotype);
  2125. my $stoday = strftime('%d.%m.%Y', localtime(time));
  2126. my $stom = strftime('%d.%m.%Y', localtime(time+86400));
  2127. my $stwo = strftime('%d.%m.%Y', localtime(time+2*86400));
  2128. #-- workday has lowest priority
  2129. $todaytype = "workday";
  2130. $hash->{DATA}{"DD"}[0]{"date"} = $stoday;
  2131. $hash->{DATA}{"DD"}[0]{"weekday"} = (strftime('%w', localtime(time))+6)%7;
  2132. $hash->{DATA}{"DD"}[0]{"daytype"} = "workday";
  2133. $hash->{DATA}{"DD"}[0]{"desc"} = $yaahm_tt->{"workday"};
  2134. $hash->{DATA}{"DD"}[0]{"vacflag"} = 0;
  2135. $tomtype = "workday";
  2136. $hash->{DATA}{"DD"}[1]{"date"} = $stom;
  2137. $hash->{DATA}{"DD"}[1]{"weekday"} = (strftime('%w', localtime(time+86400))+6)%7;
  2138. $hash->{DATA}{"DD"}[1]{"daytype"} = "workday";
  2139. $hash->{DATA}{"DD"}[1]{"desc"} = $yaahm_tt->{"workday"};
  2140. $hash->{DATA}{"DD"}[1]{"vacflag"} = 0;
  2141. $twotype = "workday";
  2142. $hash->{DATA}{"DD"}[2]{"date"} = $stwo;
  2143. $hash->{DATA}{"DD"}[2]{"weekday"} = (strftime('%w', localtime(time+2*86400))+6)%7;
  2144. $hash->{DATA}{"DD"}[2]{"daytype"} = "workday";
  2145. $hash->{DATA}{"DD"}[2]{"desc"} = $yaahm_tt->{"workday"};
  2146. $hash->{DATA}{"DD"}[2]{"vacflag"} = 0;
  2147. #-- vacation = vacdays has higher priority
  2148. my $vacdayDevs = AttrVal( $name, "vacationDevices", "" );
  2149. foreach my $vacdayDev ( split( /,/, $vacdayDevs ) ) {
  2150. #-- device of type holiday
  2151. if( IsDevice( $vacdayDev, "holiday" )){
  2152. $stoday = strftime('%m-%d', localtime(time));
  2153. $stom = strftime('%m-%d', localtime(time+86400));
  2154. $stwo = strftime('%m-%d', localtime(time+2*86400));
  2155. my $tod = holiday_refresh( $vacdayDev, $stoday );
  2156. if ( $tod ne "none" ) {
  2157. $todaydesc = $tod;
  2158. $todaytype = "vacday";
  2159. Log3 $name, 5,"[YAAHM] found today=vacation \"$todaydesc\" in holiday $vacdayDev";
  2160. }
  2161. $tod = holiday_refresh( $vacdayDev, $stom );
  2162. if ( $tod ne "none" ) {
  2163. $tomdesc = $tod;
  2164. $tomtype = "vacday";
  2165. Log3 $name, 5,"[YAAHM] found tomorrow=vacation \"$tomdesc\" in holiday $vacdayDev";
  2166. }
  2167. $tod = holiday_refresh( $vacdayDev, $stwo );
  2168. if ( $tod ne "none" ) {
  2169. $twodesc = $tod;
  2170. $twotype = "vacday";
  2171. Log3 $name, 5,"[YAAHM] found twodays=vacation \"$twodesc\" in holiday $vacdayDev";
  2172. }
  2173. #-- device of type calendar
  2174. }elsif( IsDevice($vacdayDev, "Calendar" )){
  2175. $stoday = strftime('%d.%m.%y', localtime(time));
  2176. $stom = strftime('%d.%m.%y', localtime(time+86400));
  2177. $stwo = strftime('%d.%m.%y', localtime(time+2*86400));
  2178. @tday = split('\.',$stoday);
  2179. @tmor = split('\.',$stom);
  2180. @ttwo = split('\.',$stwo);
  2181. #-- more complicated to check here
  2182. $fline=Calendar_Get($defs{$vacdayDev},"get","full","mode=alarm|start|upcoming");
  2183. if($fline){
  2184. #chomp($fline);
  2185. @lines = split('\n',$fline);
  2186. foreach $fline (@lines){
  2187. chomp($fline);
  2188. @chunks = split(' ',$fline);
  2189. @sday = split('\.',$chunks[2]);
  2190. @eday = split('\.',substr($chunks[3],9,10));
  2191. #-- today
  2192. my $rets = ($sday[2]-$tday[2])*365+($sday[1]-$tday[1])*31+($sday[0]-$tday[0]);
  2193. my $rete = ($eday[2]-$tday[2])*365+($eday[1]-$tday[1])*31+($eday[0]-$tday[0]);
  2194. if( ($rete>=0) && ($rets<=0) ){
  2195. $todaydesc = $chunks[5];
  2196. $todaytype = "vacation";
  2197. Log3 $name, 5,"[YAAHM] found today=vacation \"$todaydesc\" in calendar $vacdayDev";
  2198. }
  2199. $rets = ($sday[2]-$tmor[2])*365+($sday[1]-$tmor[1])*31+($sday[0]-$tmor[0]);
  2200. $rete = ($eday[2]-$tmor[2])*365+($eday[1]-$tmor[1])*31+($eday[0]-$tmor[0]);
  2201. if( ($rete>=0) && ($rets<=0) ){
  2202. $tomdesc = $chunks[5];
  2203. $tomtype = "vacation";
  2204. Log3 $name, 5,"[YAAHM] found tomorrow=vacation \"$tomdesc\" in calendar $vacdayDev";
  2205. }
  2206. $rets = ($sday[2]-$ttwo[2])*365+($sday[1]-$ttwo[1])*31+($sday[0]-$ttwo[0]);
  2207. $rete = ($eday[2]-$ttwo[2])*365+($eday[1]-$ttwo[1])*31+($eday[0]-$ttwo[0]);
  2208. if( ($rete>=0) && ($rets<=0) ){
  2209. $twodesc = $chunks[5];
  2210. $twotype = "vacation";
  2211. Log3 $name, 5,"[YAAHM] found twodays=vacation \"$twodesc\" in calendar $vacdayDev";
  2212. }
  2213. }
  2214. }
  2215. }else{
  2216. Log3 $name, 1,"[YAAHM] unknown vacation device $vacdayDev";
  2217. }
  2218. }
  2219. #-- put into readings
  2220. if( $todaytype eq "vacation" ){
  2221. $hash->{DATA}{"DD"}[0]{"daytype"} = "vacation";
  2222. $hash->{DATA}{"DD"}[0]{"desc"} = $todaydesc;
  2223. $hash->{DATA}{"DD"}[0]{"vacflag"} = 1;
  2224. }
  2225. if( $tomtype eq "vacation" ){
  2226. $hash->{DATA}{"DD"}[1]{"daytype"} = "vacation";
  2227. $hash->{DATA}{"DD"}[1]{"desc"} = $tomdesc;
  2228. $hash->{DATA}{"DD"}[1]{"vacflag"} = 1;
  2229. }
  2230. if( $twotype eq "vacation" ){
  2231. $hash->{DATA}{"DD"}[2]{"daytype"} = "vacation";
  2232. $hash->{DATA}{"DD"}[2]{"desc"} = $twodesc;
  2233. $hash->{DATA}{"DD"}[2]{"vacflag"} = 1;
  2234. }
  2235. #-- weekend has higher priority
  2236. if( strftime('%u', localtime(time)) > 5){
  2237. $todaytype = "weekend";
  2238. if( $hash->{DATA}{"DD"}[0]{"daytype"} ne "workday" ){
  2239. $hash->{DATA}{"DD"}[0]{"desc"} = $yaahm_tt->{"weekend"}.", ".$hash->{DATA}{"DD"}[0]{"desc"};
  2240. }else{
  2241. $hash->{DATA}{"DD"}[0]{"desc"} = $yaahm_tt->{"weekend"};
  2242. }
  2243. $hash->{DATA}{"DD"}[0]{"daytype"} = "weekend";
  2244. }
  2245. if( strftime('%u', localtime(time+86400)) > 5){
  2246. $tomtype = "weekend";
  2247. if( $hash->{DATA}{"DD"}[1]{"daytype"} ne "workday" ){
  2248. $hash->{DATA}{"DD"}[1]{"desc"} = $yaahm_tt->{"weekend"}.", ".$hash->{DATA}{"DD"}[1]{"desc"};
  2249. }else{
  2250. $hash->{DATA}{"DD"}[1]{"desc"} = $yaahm_tt->{"weekend"};
  2251. }
  2252. $hash->{DATA}{"DD"}[1]{"daytype"} = "weekend";
  2253. }
  2254. if( strftime('%u', localtime(time+2*86400)) > 5){
  2255. $twotype = "weekend";
  2256. if( $hash->{DATA}{"DD"}[2]{"daytype"} ne "workday" ){
  2257. $hash->{DATA}{"DD"}[2]{"desc"} = $yaahm_tt->{"weekend"}.", ".$hash->{DATA}{"DD"}[2]{"desc"};
  2258. }else{
  2259. $hash->{DATA}{"DD"}[2]{"desc"} = $yaahm_tt->{"weekend"};
  2260. }
  2261. $hash->{DATA}{"DD"}[2]{"daytype"} = "weekend";
  2262. }
  2263. #-- holidays have the highest priority
  2264. my $holidayDevs = AttrVal( $name, "holidayDevices", "" );
  2265. foreach my $holidayDev ( split( /,/, $holidayDevs ) ) {
  2266. #-- device of type holiday
  2267. if( IsDevice( $holidayDev, "holiday" )){
  2268. $stoday = strftime('%m-%d', localtime(time));
  2269. $stom = strftime('%m-%d', localtime(time+86400));
  2270. $stwo = strftime('%m-%d', localtime(time+2*86400));
  2271. my $tod = holiday_refresh( $holidayDev, $stoday );
  2272. if ( $tod ne "none" ) {
  2273. $todaydesc = $tod;
  2274. $todaytype = "holiday";
  2275. Log3 $name, 5,"[YAAHM] found today=holiday \"$todaydesc\" in holiday $holidayDev";
  2276. }
  2277. $tod = holiday_refresh( $holidayDev, $stom );
  2278. if ( $tod ne "none" ) {
  2279. $tomdesc = $tod;
  2280. $tomtype = "holiday";
  2281. Log3 $name, 5,"[YAAHM] found tomorrow=holiday \"$tomdesc\" in holiday $holidayDev";
  2282. }
  2283. $tod = holiday_refresh( $holidayDev, $stwo );
  2284. if ( $tod ne "none" ) {
  2285. $twodesc = $tod;
  2286. $twotype = "holiday";
  2287. Log3 $name, 5,"[YAAHM] found twodays=holiday \"$twodesc\" in holiday $holidayDev";
  2288. }
  2289. #-- device of type calendar
  2290. }elsif( IsDevice($holidayDev, "Calendar" )){
  2291. $stoday = strftime('%d.%m.%y', localtime(time));
  2292. $stom = strftime('%d.%m.%y', localtime(time+86400));
  2293. $stwo = strftime('%d.%m.%y', localtime(time+2*86400));
  2294. $line=Calendar_Get($defs{$holidayDev},"get","text","mode=alarm|start|upcoming");
  2295. if($line){
  2296. chomp($line);
  2297. @lines = split('\n',$line);
  2298. foreach $line (@lines){
  2299. chomp($line);
  2300. $date = substr($line,0,8);
  2301. if( $date eq $stoday ){
  2302. $todaydesc = substr($line,15);
  2303. $todaytype = "holiday";
  2304. Log3 $name, 5,"[YAAHM] found today=holiday \"$todaydesc\" in calendar $holidayDev";
  2305. }
  2306. if( $date eq $stom ){
  2307. $tomdesc = substr($line,15);
  2308. $tomtype = "holiday";
  2309. Log3 $name, 5,"[YAAHM] found tomorrow=holiday \"$tomdesc\" in calendar $holidayDev";
  2310. }
  2311. if( $date eq $stwo ){
  2312. $twodesc = substr($line,15);
  2313. $twotype = "holiday";
  2314. Log3 $name, 5,"[YAAHM] found twodays=holiday \"$twodesc\" in calendar $holidayDev";
  2315. }
  2316. }
  2317. }
  2318. }else{
  2319. Log3 $name, 1,"[YAAHM] unknown holiday device $holidayDev";
  2320. }
  2321. }
  2322. #-- put into store
  2323. if( $todaytype eq "holiday" ){
  2324. if( $hash->{DATA}{"DD"}[0]{"daytype"} ne "workday" ){
  2325. $hash->{DATA}{"DD"}[0]{"desc"} = $todaydesc.", ".$hash->{DATA}{"DD"}[0]{"desc"};
  2326. }else{
  2327. $hash->{DATA}{"DD"}[0]{"desc"} = $todaydesc;
  2328. }
  2329. $hash->{DATA}{"DD"}[0]{"daytype"} = "holiday";
  2330. }
  2331. if( $tomtype eq "holiday" ){
  2332. if( $hash->{DATA}{"DD"}[1]{"daytype"} ne "workday" ){
  2333. $hash->{DATA}{"DD"}[1]{"desc"} = $tomdesc.", ".$hash->{DATA}{"DD"}[1]{"desc"};
  2334. }else{
  2335. $hash->{DATA}{"DD"}[1]{"desc"} = $tomdesc;
  2336. }
  2337. $hash->{DATA}{"DD"}[1]{"daytype"} = "holiday";
  2338. }
  2339. if( $twotype eq "holiday" ){
  2340. if( $hash->{DATA}{"DD"}[2]{"daytype"} ne "workday" ){
  2341. $hash->{DATA}{"DD"}[2]{"desc"} = $twodesc.", ".$hash->{DATA}{"DD"}[2]{"desc"};
  2342. }else{
  2343. $hash->{DATA}{"DD"}[2]{"desc"} = $twodesc;
  2344. }
  2345. $hash->{DATA}{"DD"}[2]{"daytype"} = "holiday";
  2346. }
  2347. #-- sunrise, sunset and the offsets
  2348. YAAHM_sun($hash);
  2349. YAAHM_sunoffsets($hash);
  2350. readingsBeginUpdate($hash);
  2351. #-- and do not forget to put them into readings, because these are read by the timer
  2352. foreach my $key (sort YAAHM_dsort keys %defaultdailytable){
  2353. my $f1 = defined($defaultdailytable{$key}[0]);
  2354. my $f2 = defined($defaultdailytable{$key}[1]);
  2355. #-- entries in the default table with no entry are single-timers
  2356. if( !$f1 and !$f2 ){
  2357. readingsBulkUpdate( $hash, "s_".$key, $hash->{DATA}{"DT"}{$key}[0] );
  2358. #-- entries in the default table with only first time are single-timers
  2359. }elsif( $f1 and !$f2 ){
  2360. readingsBulkUpdate( $hash, "s_".$key, $hash->{DATA}{"DT"}{$key}[0] );
  2361. #-- entries in the default table with only second time are single-timer offsets
  2362. }elsif( !$f1 and $f2 ){
  2363. readingsBulkUpdate( $hash, "s_".$key, $hash->{DATA}{"DT"}{$key}[0] );
  2364. #-- entries in the default table with first and second time are two-timer periods
  2365. }elsif( $f1 and $f2 ){
  2366. readingsBulkUpdate( $hash, "s_".$key, $hash->{DATA}{"DT"}{$key}[0] );
  2367. readingsBulkUpdate( $hash, "t_".$key, $hash->{DATA}{"DT"}{$key}[1] );
  2368. #-- something wrong
  2369. }else{
  2370. my $msg = "Readings update failed, something wrong with entry ".$key;
  2371. Log 1,"[YAAHM_GetDayStatus] ".$msg;
  2372. return $msg;
  2373. }
  2374. }
  2375. readingsBulkUpdateIfChanged( $hash, "todayType",$todaytype );
  2376. readingsBulkUpdateIfChanged( $hash, "tr_todayType",$yaahm_tt->{$hash->{DATA}{"DD"}[0]{"daytype"}} );
  2377. if( $todaytype eq "workday"){
  2378. readingsBulkUpdateIfChanged( $hash, "todayDesc","--" )
  2379. }elsif( $todaytype eq "vacation"){
  2380. readingsBulkUpdateIfChanged( $hash, "todayDesc",$hash->{DATA}{"DD"}[0]{"desc"} )
  2381. }elsif( $todaytype eq "weekend"){
  2382. readingsBulkUpdateIfChanged( $hash, "todayDesc","--" )
  2383. }else{
  2384. readingsBulkUpdateIfChanged( $hash, "todayDesc",$hash->{DATA}{"DD"}[0]{"desc"} )
  2385. }
  2386. readingsBulkUpdateIfChanged( $hash, "tomorrowType",$tomtype );
  2387. readingsBulkUpdateIfChanged( $hash, "tr_tomorrowType",$yaahm_tt->{$hash->{DATA}{"DD"}[1]{"daytype"}} );
  2388. if( $tomtype eq "workday"){
  2389. readingsBulkUpdateIfChanged( $hash, "tomorrowDesc","--" )
  2390. }elsif( $tomtype eq "vacation"){
  2391. readingsBulkUpdateIfChanged( $hash, "tomorrowDesc",$hash->{DATA}{"DD"}[1]{"desc"} )
  2392. }elsif( $tomtype eq "weekend"){
  2393. readingsBulkUpdateIfChanged( $hash, "tomorrowDesc","--" )
  2394. }else{
  2395. readingsBulkUpdateIfChanged( $hash, "tomorrowDesc",$hash->{DATA}{"DD"}[1]{"desc"} )
  2396. }
  2397. readingsBulkUpdateIfChanged( $hash, "twodaysType",$tomtype );
  2398. readingsBulkUpdateIfChanged( $hash, "tr_twodaysType",$yaahm_tt->{$hash->{DATA}{"DD"}[2]{"daytype"}} );
  2399. if( $tomtype eq "workday"){
  2400. readingsBulkUpdateIfChanged( $hash, "twodaysDesc","--" )
  2401. }elsif( $tomtype eq "vacation"){
  2402. readingsBulkUpdateIfChanged( $hash, "twodaysDesc",$hash->{DATA}{"DD"}[2]{"desc"} )
  2403. }elsif( $tomtype eq "weekend"){
  2404. readingsBulkUpdateIfChanged( $hash, "twodaysDesc","--" )
  2405. }else{
  2406. readingsBulkUpdateIfChanged( $hash, "twodaysDesc",$hash->{DATA}{"DD"}[2]{"desc"} )
  2407. }
  2408. YAAHM_setWeeklyTime($hash);
  2409. readingsEndUpdate($hash,1);
  2410. return undef;
  2411. }
  2412. #########################################################################################
  2413. #
  2414. # YAAHM_sun - obtain time offsets for midnight etc. sunrise and sunset
  2415. #
  2416. # Parameter hash = hash of device addressed
  2417. #
  2418. #########################################################################################
  2419. sub YAAHM_sun($) {
  2420. my ($hash) = @_;
  2421. my $name = $hash->{NAME};
  2422. #-- sunrise and sunset today and tomorrow and in two days
  2423. my ($sttoday,$sttom,$sttwo);
  2424. my $count = 0;
  2425. my $strise0 = "";
  2426. my $strise1 = "";
  2427. my $strise2 = "";
  2428. my ($msg,$stset0,$stseas0,$stset1,$stseas1,$stset2,$stseas2);
  2429. $sttoday = strftime('%Y-%m-%d', localtime(time));
  2430. #-- since the Astro module sometimes gives us strange results, we need to do this more than once
  2431. while( $strise0 !~ /^\d\d:\d\d:\d\d/ && $count < 5){
  2432. $strise0 = Astro_Get($hash,"dummy","text", "SunRise",$sttoday).":00";
  2433. $count++;
  2434. select(undef,undef,undef,0.01);
  2435. }
  2436. if( $count == 5 ){
  2437. $msg = "Error, no proper sunrise today return from Astro module in 5 attempts";
  2438. Log3 $name,1,"[YAAHM_sun] ".$msg;
  2439. $strise0 = "06:00:00";
  2440. }
  2441. my ($hour,$min) = split(":",$strise0);
  2442. $hash->{DATA}{"DD"}[0]{"sunrise"} = sprintf("%02d:%02d",$hour,$min);
  2443. $stset0 = Astro_Get($hash,"dummy","text", "SunSet",$sttoday).":00";
  2444. ($hour,$min) = split(":",$stset0);
  2445. $hash->{DATA}{"DD"}[0]{"sunset"} = sprintf("%02d:%02d",$hour,$min);
  2446. $stseas0 = Astro_Get($hash,"dummy","text", "ObsSeasonN",$sttoday);
  2447. $hash->{DATA}{"DD"}[0]{"season"} = $seasons[$stseas0];
  2448. $sttom = strftime('%Y-%m-%d', localtime(time+86400));
  2449. $count = 0;
  2450. $msg = "";
  2451. #-- since the Astro module sometimes gives us strange results, we need to do this more than once
  2452. while( $strise1 !~ /^\d\d:\d\d:\d\d/ && $count < 5){
  2453. $strise1 = Astro_Get($hash,"dummy","text", "SunRise",$sttom).":00";
  2454. $count++;
  2455. select(undef,undef,undef,0.01);
  2456. }
  2457. if( $count == 5 ){
  2458. $msg = "Error, no proper sunrise tomorrow return from Astro module in 5 attempts";
  2459. Log3 $name,1,"[YAAHM_sun] ".$msg;
  2460. $strise1 = "06:00:00";
  2461. }
  2462. ($hour,$min) = split(":",$strise1);
  2463. $hash->{DATA}{"DD"}[1]{"sunrise"} = sprintf("%02d:%02d",$hour,$min);
  2464. $stset1 = Astro_Get($hash,"dummy","text", "SunSet",$sttom).":00";
  2465. ($hour,$min) = split(":",$stset1);
  2466. $hash->{DATA}{"DD"}[1]{"sunset"} = sprintf("%02d:%02d",$hour,$min);
  2467. $stseas1 = Astro_Get($hash,"dummy","text", "ObsSeasonN",$sttom);
  2468. $hash->{DATA}{"DD"}[1]{"season"} = $seasons[$stseas1];
  2469. $sttwo = strftime('%Y-%m-%d', localtime(time+2*86400));
  2470. $count = 0;
  2471. $msg = "";
  2472. #-- since the Astro module sometimes gives us strange results, we need to do this more than once
  2473. while( $strise2 !~ /^\d\d:\d\d:\d\d/ && $count < 5){
  2474. $strise2 = Astro_Get($hash,"dummy","text", "SunRise",$sttwo).":00";
  2475. $count++;
  2476. select(undef,undef,undef,0.01);
  2477. }
  2478. if( $count == 5 ){
  2479. $msg = "Error, no proper sunrise twodays return from Astro module in 5 attempts";
  2480. Log3 $name,1,"[YAAHM_sun] ".$msg;
  2481. $strise2 = "06:00:00";
  2482. }
  2483. ($hour,$min) = split(":",$strise2);
  2484. $hash->{DATA}{"DD"}[2]{"sunrise"} = sprintf("%02d:%02d",$hour,$min);
  2485. $stset2 = Astro_Get($hash,"dummy","text", "SunSet",$sttom).":00";
  2486. ($hour,$min) = split(":",$stset2);
  2487. $hash->{DATA}{"DD"}[2]{"sunset"} = sprintf("%02d:%02d",$hour,$min);
  2488. $stseas2 = Astro_Get($hash,"dummy","text", "ObsSeasonN",$sttwo);
  2489. $hash->{DATA}{"DD"}[2]{"season"} = $seasons[$stseas2];
  2490. }
  2491. #########################################################################################
  2492. #
  2493. # YAAHM_sunoffsets - obtain time offsets for midnight etc. sunrise and sunset
  2494. #
  2495. # Parameter hash = hash of device addressed
  2496. #
  2497. #########################################################################################
  2498. sub YAAHM_sunoffsets($) {
  2499. my ($hash) = @_;
  2500. #-- sunrise
  2501. my $st = $hash->{DATA}{"DD"}[0]{"sunrise"};
  2502. my ($hour,$min) = split(":",$st);
  2503. $hash->{DATA}{"DT"}{"sunrise"}[0] = sprintf("%02d:%02d",$hour,$min);
  2504. #-- before sunrise
  2505. my ($ofh,$ofm);
  2506. my $of = $hash->{DATA}{"DT"}{"beforesunrise"}[1];
  2507. if( $of !~ /\d\d:\d\d/){
  2508. Log 1,"[YAAHM] Offset before sunrise not in format hh:mm, using 00:00";
  2509. $ofh = 0;
  2510. $ofm = 0;
  2511. }else{
  2512. ($ofh,$ofm) = split(":",$of);
  2513. }
  2514. $ofm = $min-$ofm;
  2515. $ofh = $hour-$ofh;
  2516. if( $ofm < 0 ){
  2517. $ofh--;
  2518. $ofm +=60;
  2519. }
  2520. $hash->{DATA}{"DT"}{"beforesunrise"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2521. #-- after sunrise
  2522. $of = $hash->{DATA}{"DT"}{"aftersunrise"}[1];
  2523. if( $of !~ /\d\d:\d\d/){
  2524. Log 1,"[YAAHM] Offset after sunrise not in format hh:mm, using 00:00";
  2525. $ofh = 0;
  2526. $ofm = 0;
  2527. }else{
  2528. ($ofh,$ofm) = split(":",$of);
  2529. }
  2530. $ofm = $min+$ofm;
  2531. $ofh = $hour+$ofh;
  2532. if( $ofm > 59 ){
  2533. $ofh++;
  2534. $ofm -=60;
  2535. }
  2536. $hash->{DATA}{"DT"}{"aftersunrise"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2537. #-- sunset
  2538. $st = $hash->{DATA}{"DD"}[0]{"sunset"};
  2539. ($hour,$min) = split(":",$st);
  2540. $hash->{DATA}{"DT"}{"sunset"}[0] = sprintf("%02d:%02d",$hour,$min);
  2541. #-- before sunset
  2542. $of = $hash->{DATA}{"DT"}{"beforesunset"}[1];
  2543. if( $of !~ /\d\d:\d\d/){
  2544. Log 1,"[YAAHM] Offset before sunset not in format hh:mm, using 00:00";
  2545. $ofh = 0;
  2546. $ofm = 0;
  2547. }else{
  2548. ($ofh,$ofm) = split(":",$of);
  2549. }
  2550. $ofm = $min-$ofm;
  2551. $ofh = $hour-$ofh;
  2552. if( $ofm < 0 ){
  2553. $ofh--;
  2554. $ofm +=60;
  2555. }
  2556. $hash->{DATA}{"DT"}{"beforesunset"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2557. #-- after sunset
  2558. $of = $hash->{DATA}{"DT"}{"aftersunset"}[1];
  2559. if( $of !~ /\d\d:\d\d/){
  2560. Log 1,"[YAAHM] Offset after sunset not in format hh:mm, using 00:00";
  2561. $ofh = 0;
  2562. $ofm = 0;
  2563. }else{
  2564. ($ofh,$ofm) = split(":",$of);
  2565. }
  2566. $ofm = $min+$ofm;
  2567. $ofh = $hour+$ofh;
  2568. if( $ofm > 59 ){
  2569. $ofh++;
  2570. $ofm -=60;
  2571. }
  2572. $hash->{DATA}{"DT"}{"aftersunset"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2573. #-- before midnight
  2574. $hour = 24;
  2575. $min = 0;
  2576. $of = $hash->{DATA}{"DT"}{"beforemidnight"}[1];
  2577. if( $of !~ /\d\d:\d\d/){
  2578. Log 1,"[YAAHM] Offset before midnight not in format hh:mm, using 00:05";
  2579. $ofh = 0;
  2580. $ofm = 5;
  2581. }else{
  2582. ($ofh,$ofm) = split(":",$of);
  2583. }
  2584. $ofm = $min-$ofm;
  2585. $ofh = $hour-$ofh;
  2586. if( $ofm < 0 ){
  2587. $ofh--;
  2588. $ofm +=60;
  2589. }
  2590. $hash->{DATA}{"DT"}{"beforemidnight"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2591. #-- after midnight
  2592. $hour = 0;
  2593. $min = 0;
  2594. $of = $hash->{DATA}{"DT"}{"aftermidnight"}[1];
  2595. if( $of !~ /\d\d:\d\d/){
  2596. Log 1,"[YAAHM] Offset after midnight not in format hh:mm, using 00:05";
  2597. $ofh = 0;
  2598. $ofm = 5;
  2599. }else{
  2600. ($ofh,$ofm) = split(":",$of);
  2601. }
  2602. $ofm = $min+$ofm;
  2603. $ofh = $hour+$ofh;
  2604. if( $min > 59 ){
  2605. $ofh++;
  2606. $ofm -=60;
  2607. }
  2608. $hash->{DATA}{"DT"}{"aftermidnight"}[0] = sprintf("%02d:%02d",$ofh,$ofm);
  2609. }
  2610. #########################################################################################
  2611. #
  2612. # YAAHM_statewidget - returns SVG code for inclusion into page
  2613. #
  2614. # Parameter hash = hash of device addressed
  2615. #
  2616. #########################################################################################
  2617. sub YAAHM_statewidget($){
  2618. my ($hash) = @_;
  2619. my $name = $hash->{NAME};
  2620. return ""
  2621. if( AttrVal($name,"noicons",0) == 1);
  2622. my $state = $hash->{DATA}{"HSM"}{"state"};
  2623. my ($color,$locks,$unlocks,$bells,$eyes);
  2624. if( $state eq "unsecured" ){
  2625. $color=$csstate[0];
  2626. $locks="hidden";
  2627. $unlocks="visible";
  2628. $bells="hidden";
  2629. $eyes="hidden";
  2630. }elsif( $state eq "secured" ){
  2631. $color=$csstate[1];
  2632. $locks="visible";
  2633. $unlocks="hidden";
  2634. $bells="hidden";
  2635. $eyes="hidden";
  2636. }elsif( $state eq "protected" ){
  2637. $color=$csstate[2];
  2638. $locks="visible";
  2639. $unlocks="hidden";
  2640. $bells="visible";
  2641. $eyes="hidden";
  2642. }elsif( $state eq "guarded" ){
  2643. $color=$csstate[3];
  2644. $locks="visible";
  2645. $unlocks="hidden";
  2646. $bells="visible";
  2647. $eyes="visible";
  2648. }else{
  2649. Log 1,"[YAAHM_statewidget] Error, housestate $state not defined";
  2650. return;
  2651. }
  2652. my $ret = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40px" height="40px">';
  2653. $ret .= '<g transform="translate(5,0)">\n';
  2654. #-- shield
  2655. $ret .='<g transform="scale(0.1,0.1)">\n'.
  2656. '<path class="hs_is" fill="'.$color.'" d="M179.6,13c0,0-50.1,44.5-146.1,48.5c0,0,13.5,158,50.5,197s95.6,80,95.6,80s78.9-55.5,98.9-81.5 s37.5-75,47-196.5C325.5,60.5,251.8,64,179.6,13z"/>'.
  2657. '<path style="fill:none;stroke:black;stroke-width:10" d="M179.6,13c0,0-50.1,44.5-146.1,48.5c0,0,13.5,158,50.5,197s95.6,80,95.6,80s78.9-55.5,98.9-81.5 s37.5-75,47-196.5C325.5,60.5,251.8,64,179.6,13z"/></g>';
  2658. #-- small bell
  2659. $ret .= '<g class="hs_smb" transform="translate(0,23) scale(0.28,0.28)" visibility="'.$bells.'">\n';
  2660. $ret .= '<path d="M 25 6 C 23.354545 6 22 7.3545455 22 9 L 22 10.365234 C 17.172775 11.551105 14.001117 15.612755 14.001953 21.0625 '.
  2661. 'L 14.001953 28.863281 C 14.001953 31.035281 12.718469 33.494563 11.980469 34.726562 L 10.167969 37.445312 C 9.9629687 37.751312 9.9431875 '.
  2662. '38.147656 10.117188 38.472656 C 10.291188 38.797656 10.631 39 11 39 L 39 39 C 39.369 39 39.708813 38.797656 39.882812 38.472656 C 40.056813 '.
  2663. '38.147656 40.037031 37.752313 39.832031 37.445312 L 38.044922 34.767578 C 36.668922 32.473578 36 30.587 36 29 L 36 21.199219 C 36 15.68167 '.
  2664. '32.827303 11.569596 28 10.369141 L 28 9 C 28 7.3545455 26.645455 6 25 6 z M 25 8 C 25.554545 8 26 8.4454545 26 9 L 26 10.044922 C 25.671339 '.
  2665. '10.019952 25.339787 10 25 10 C 24.660213 10 24.328661 10.020256 24 10.044922 L 24 9 C 24 8.4454545 24.445455 8 25 8 z " fill="black" stroke="black"/>'.
  2666. '.<path d="M 20.423828 41 C 21.197828 42.763 22.955 44 25 44 C 27.045 44 28.802172 42.763 29.576172 41 L 20.423828 41 z" fill="black"/></g>\n';
  2667. #-- lock
  2668. $ret .= '<g transform="translate(1,-2) scale(0.1,0.1)" visibility="visible" style="fill:black">\n';
  2669. $ret .= '<path d="M 130.57422 130.58594 C 129.00209 130.6591 127.9626 130.81157 126.67383 131.14062 C 121.6497 132.42339 117.28863 136.02241 114.93164 140.83398 '.
  2670. 'C 114.02534 142.68411 113.52297 144.27216 113.15234 146.45508 C 113.00288 147.33557 112.9931 149.48213 112.9668 187.17578 C 112.9473 215.14019 '.
  2671. '112.96786 227.3203 113.03516 228.16016 C 113.33148 231.85916 114.6641 235.33647 116.91797 238.29492 C 117.57258 239.15416 119.29708 240.86998 '.
  2672. '120.20508 241.56445 C 122.81931 243.56391 125.66412 244.76433 128.79688 245.18945 C 130.22882 245.38381 208.07219 245.39507 209.54102 245.20117 '.
  2673. 'C 217.01971 244.21392 223.13119 238.65732 224.92969 231.21289 C 225.49437 228.87561 225.45703 232.00604 225.45703 187.94336 C 225.45703 161.07427 '.
  2674. '225.42603 147.57463 225.36523 147.07227 C 225.02812 144.2871 223.98281 141.29331 222.58203 139.10742 C 219.44374 134.21016 214.3933 131.11459 '.
  2675. '208.72656 130.61328 C 208.60764 130.60277 208.27712 130.59537 208.06445 130.58594 L 191.28711 130.58594 L 191.28711 130.60938 L 169.33203 130.60938 '.
  2676. 'L 147.37695 130.60938 L 147.37695 130.58594 L 130.57422 130.58594 z M 168.84766 141.69531 C 196.24408 141.67681 207.96833 141.69697 208.37305 141.76367 '.
  2677. 'C 209.88322 142.01259 211.23437 142.69523 212.37109 143.78125 C 213.56262 144.91963 214.28722 146.23107 214.60742 147.82812 C 214.73627 148.47097 '.
  2678. '214.74661 152.13826 214.72461 188.36914 L 214.70117 228.20312 L 214.5 228.85352 C 213.61281 231.71117 211.36106 233.77649 208.56055 234.30469 C '.
  2679. '207.89347 234.43005 204.00466 234.44545 169.125 234.43945 C 131.56507 234.4334 130.40611 234.42765 129.63281 234.26562 C 128.63219 234.05598 127.28912 '.
  2680. '233.40183 126.47852 232.73047 C 124.95438 231.46815 123.97606 229.64008 123.74609 227.625 C 123.67879 227.03543 123.65738 215.18032 123.67578 187.48438 '.
  2681. 'L 123.70312 148.18164 L 123.90234 147.47266 C 124.36618 145.82739 125.37129 144.27333 126.59375 143.31055 C 127.28146 142.76892 128.48475 142.14943 '.
  2682. '129.36719 141.88281 C 129.87599 141.72912 131.53564 141.72051 168.84766 141.69531 z "/>\n'.
  2683. '<path d="m 168.43291,207.79319 c -2.82175,-0.41393 -5.20902,-2.19673 -6.45085,-4.81746 -0.18456,-0.38949 -0.42714,-1.02683 -0.53907,-1.41632 -0.20211,-0.70332 '.
  2684. '-0.20369,-0.74985 -0.23134,-6.79219 l -0.0278,-6.08403 -0.4938,-0.46648 c -1.94776,-1.84005 -3.29729,-4.28865 -3.89177,-7.06126 -0.22834,-1.06501 '.
  2685. '-0.23065,-3.78941 -0.004,-4.90967 0.5504,-2.72228 1.69072,-4.87295 3.59555,-6.78128 1.86685,-1.87027 3.93834,-2.98308 6.53099,-3.50847 1.14974,-0.23298 '.
  2686. '3.66048,-0.21247 4.804,0.0393 5.18176,1.14065 9.02113,5.15685 9.96535,10.42433 0.21336,1.19023 0.21526,3.4008 0.004,4.5588 -0.4602,2.5216 -1.68503,4.94293 '.
  2687. '-3.41861,6.75821 -0.69412,0.72682 -0.76553,0.83257 -0.69333,1.02659 0.0464,0.1247 0.0688,2.7301 0.0528,6.15217 -0.027,5.78112 -0.0327,5.95363 -0.22,6.61733 '.
  2688. '-0.66956,2.3726 -2.29646,4.38412 -4.40661,5.44839 -0.87624,0.44194 -1.4689,0.63145 -2.43431,0.77844 -0.91656,0.13954 -1.37046,0.14668 -2.14096,0.0337 l 0,0 z"/></g>\n';
  2689. $ret .= '<g class="hs_unlocked" transform="translate(1,-2) scale(0.1,0.1)" visibility="'.$unlocks.'" style="fill:black">\n';
  2690. $ret .= '<path d="M 170.82812 73.273438 C 169.18654 73.276141 168.63971 73.313421 167.69531 73.492188 C 165.61131 73.886674 163.21555 74.79455 161.46875 75.849609 '.
  2691. 'C 159.08962 77.286589 156.84235 79.476504 155.35742 81.806641 C 154.71611 82.812977 143.83472 103.12552 143.69141 103.58398 C 143.32362 104.76045 143.9985 '.
  2692. '106.3 145.13867 106.88867 C 145.89649 107.27994 146.17527 107.28572 149.68359 106.98633 C 153.46023 106.66405 153.66989 106.60755 154.45508 105.72266 C '.
  2693. '154.74018 105.40138 156.38998 102.44246 159.67383 96.359375 C 164.73578 86.982504 164.89996 86.708871 166.08984 85.746094 C 168.20634 84.033559 171.35111 '.
  2694. '83.652548 173.67188 84.828125 C 175.01748 85.50974 200.19425 99.256274 200.56836 99.513672 C 201.52002 100.16843 202.5089 101.37978 203 102.49023 C 203.13162 '.
  2695. '102.78785 203.32969 103.40656 203.43945 103.86523 C 203.70946 104.99358 203.65191 106.59046 203.30273 107.66992 C 203.08072 108.35626 193.2629 127.83267 '.
  2696. '192.83203 128.44141 L 192.7168 128.60352 L 204.32227 128.60352 C 204.34489 128.54135 206.0865 125.32409 208.24219 121.36133 C 210.41184 117.37291 212.41514 '.
  2697. '113.62986 212.69531 113.04297 C 216.53579 104.99821 213.90717 95.127045 206.625 90.25 C 205.84937 89.730545 181.06362 76.260203 178.6582 75.050781 C 177.5495 '.
  2698. '74.493339 176.37806 74.054405 175.24219 73.771484 C 173.49309 73.335818 172.90843 73.270038 170.82812 73.273438 z"/></g>\n';
  2699. $ret .= '<g class="hs_locked" transform="translate(1,-2) scale(0.1,0.1)" visibility="'.$locks.'" style="fill:black">\n';
  2700. $ret .= '<path d="M 169.33398 90.152344 C 161.36399 90.152344 153.39548 90.185221 152.80078 90.25 C 145.74301 91.018795 139.82758 95.841827 137.60547 102.63867 C '.
  2701. '137.17355 103.9598 136.90608 105.2186 136.76367 106.60938 C 136.70267 107.20469 136.66992 111.55655 136.66992 119.02344 L 136.66992 130.49805 L 147.37695 '.
  2702. '130.49805 L 147.40234 118.94727 L 147.42773 107.28516 L 147.62109 106.60352 C 148.34477 104.05797 150.12767 102.25431 152.73633 101.42969 L 153.5332 101.17773 '.
  2703. 'L 169.24609 101.17773 L 184.95898 101.17773 L 185.70703 101.37695 C 186.11913 101.48679 186.71238 101.69727 187.02539 101.84375 C 189.21729 102.86946 190.84344 '.
  2704. '105.12709 191.19141 107.62891 C 191.25571 108.09078 191.28711 111.96821 191.28711 119.46289 L 191.28711 130.49805 L 201.99805 130.49805 L 201.99805 119.11719 C '.
  2705. '201.99805 111.72973 201.9653 107.37447 201.9043 106.74805 C 201.77551 105.42445 201.44824 103.83653 201.06445 102.67773 C 200.64348 101.40669 199.67547 99.408912 '.
  2706. '198.95703 98.326172 C 195.91418 93.74044 191.20002 90.830687 185.86914 90.25 C 185.27445 90.185221 177.30398 90.152344 169.33398 90.152344 z"/></g>\n';
  2707. #-- eye
  2708. $ret .= '<g class="hs_eye" transform="translate(18,24) scale(0.014,0.014)" visibility="'.$eyes.'" style="fill:black">\n';
  2709. $ret .= '<path d="M493.6,794C196.9,794,10,576.3,10,518.6C10,460.8,196.9,206,493.6,206C790.2,206,990,444,990,518.6C990,593.1,790.2,794,493.6,794z M480.2,368.2c-43.3,0-78.4,'.
  2710. '36.3-78.4,81c0,44.7,35.1,81,78.4,81c43.3,0,78.4-36.3,78.4-81C558.6,404.4,523.5,368.2,480.2,368.2z M715.6,327.7c28.6,39.3,48,79.2,48,132.1c0,129.9-102.1,235.2-228.1,'.
  2711. '235.2c-125.9,0-228-105.3-228-235.2c0-80.3,15.4-130.2,74.9-172.7C194.4,316,76,472.2,76,514.9c0,46.8,155.7,217.2,414.2,217.2c258.5,0,427.8-156.8,427.8-217.2C918,475.6,'.
  2712. '873.8,393.3,715.6,327.7z"/></g>\n';
  2713. $ret .= '</g></svg>';
  2714. return $ret
  2715. }
  2716. #########################################################################################
  2717. #
  2718. # YAAHM_modewidget - returns SVG code for inclusion into page
  2719. #
  2720. # Parameter hash = hash of device addressed
  2721. #
  2722. #########################################################################################
  2723. sub YAAHM_modewidget($){
  2724. my ($hash) = @_;
  2725. my $name = $hash->{NAME};
  2726. return ""
  2727. if( AttrVal($name,"noicons",0) == 1);
  2728. my $mode = $hash->{DATA}{"HSM"}{"mode"};
  2729. my ($color,$normals,$absents,$partys,$dnds);
  2730. if( $mode eq "normal" ){
  2731. $color=$csmode[0];
  2732. $normals="visible";
  2733. $partys="hidden";
  2734. $absents="hidden";
  2735. $dnds="hidden";
  2736. }elsif( $mode eq "party" ){
  2737. $color=$csmode[1];
  2738. $normals="hidden";
  2739. $partys="visible";
  2740. $absents="hidden";
  2741. $dnds="hidden";
  2742. }elsif( $mode eq "absence" ){
  2743. $color=$csmode[2];
  2744. $normals="hidden";
  2745. $partys="hidden";
  2746. $absents="visible";
  2747. $dnds="hidden";
  2748. }elsif( $mode eq "donotdisturb" ){
  2749. $color=$csmode[3];
  2750. $normals="hidden";
  2751. $partys="hidden";
  2752. $absents="hidden";
  2753. $dnds="visible";
  2754. }else{
  2755. Log 1,"[YAAHM_modewidget] Error, housemode $mode not defined";
  2756. return;
  2757. }
  2758. my $ret = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40px" height="40px">';
  2759. $ret .= ' <g transform="translate(6,5) scale(0.8,0.8)">';
  2760. $ret .= ' <circle class="hm_is" r="20" cx="20" cy="20" fill="'.$color.'" stroke="none"/>'.
  2761. ' <circle r="20" cx="20" cy="20" style="fill:none;stroke:black"/>';
  2762. $ret .= ' <g class="hm_n" transform="translate(0,-6) scale(0.08,0.08)" style="fill:black" visibility="'.$normals.'">'.
  2763. '<path d="m 313.166,240.194 c 16.213,0 29.35,-13.139 29.35,-29.351 0,-16.213 -13.137,-29.351 -29.35,-29.351 -16.214,0 -29.351,13.138 -29.351,29.351 0,16.212 13.137,29.351 29.351,29.351 z"/>'.
  2764. '<path d="m 346.165,252.835 c -1.794,-4.361 -6.008,-6.992 -10.449,-6.989 -0.034,-0.002 -0.069,-0.005 -0.103,-0.005 l -44.897,0 c -0.05,0 -0.099,0.005 -0.148,0.007 -4.462,-0.026 -8.704,2.607 '.
  2765. ' -10.506,6.987 l -34.027,82.713 -34.013,-82.677 c -1.156,-2.809 -4.349,-7.03 -10.858,-7.03 0,0 -33.673,0 -44.897,0 -0.041,0 -0.081,0.005 -0.121,0.006 -4.446,0.057 -8.666,2.623 -10.463,6.989 '.
  2766. ' l -41.854,101.739 c -2.371,5.764 0.379,12.359 6.143,14.73 1.405,0.578 2.859,0.852 4.289,0.852 4.438,0 8.647,-2.636 10.441,-6.995 l 18.433,-44.807 0,7.792 -21.053,65.773 c -3.541,11.061 '.
  2767. ' 4.711,22.368 16.326,22.368 l 4.727,0 0,52.187 c 0,8.382 6.944,15.141 15.393,14.841 8.047,-0.287 14.308,-7.145 14.308,-15.197 l 0,-51.831 11.758,0 0,52.187 c 0,8.381 6.943,15.141 '.
  2768. ' 15.392,14.841 8.048,-0.286 14.309,-7.145 14.309,-15.197 l 0,-51.83 4.727,0 c 11.614,0 19.866,-11.307 16.326,-22.368 l -21.053,-65.773 0,-8.423 18.707,45.473 c 1.793,4.359 '.
  2769. ' 6.003,6.995 10.441,6.995 0.887,0 1.782,-0.119 2.671,-0.337 0.84,0.194 1.687,0.301 2.525,0.301 4.438,0 8.647,-2.636 10.441,-6.995 l 18.504,-44.979 0,148.292 c 0,8.382 6.944,15.141 '.
  2770. ' 15.393,14.841 8.047,-0.287 14.308,-7.145 14.308,-15.197 l 0,-80.144 c 0,-3.132 2.363,-5.866 5.488,-6.068 3.424,-0.221 6.27,2.49 6.27,5.866 l 0,80.701 c 0,8.381 6.944,15.141 15.392,14.841 '.
  2771. ' 8.048,-0.286 14.309,-7.145 14.309,-15.197 l 0,-147.689 18.402,44.732 c 1.793,4.359 6.003,6.995 10.441,6.995 1.43,0 2.885,-0.274 4.29,-0.852 5.764,-2.371 8.514,-8.966 6.143,-14.73 L '.
  2772. ' 346.165,252.835 Z"/>'.
  2773. '<path d="m 178.716,240.194 c 16.213,0 29.35,-13.139 29.35,-29.351 0,-16.213 -13.137,-29.351 -29.35,-29.351 -16.214,0 -29.351,13.138 -29.351,29.351 0,16.212 13.138,29.351 29.351,29.351 z"/></g>';
  2774. $ret .= '<g class="hm_a" transform="translate(2.5,0) scale(0.125,0.125)" style="fill:black" visibility="'.$absents.'">'.
  2775. '<path d="M59.717,110.045L53.353,98.45c-6.704,3.681-13.257,7.81-19.482,12.274l7.711,10.746 C47.376,117.315,53.476,113.47,59.717,110.045z" />'.
  2776. '<path d="M99.411,94.105l-3.415-12.779c-7.389,1.975-14.738,4.424-21.841,7.277l4.929,12.274 C85.699,98.22,92.535,95.943,99.411,94.105z" />'.
  2777. '<path d="M230.536,95.09c-6.834-3.415-13.958-6.452-21.186-9.029l-4.44,12.459c6.726,2.396,13.356,5.222,19.714,8.401 L230.536,95.09z" />'.
  2778. '<path d="M285.464,136.504l-9.739,8.943c4.823,5.251,9.373,10.85,13.528,16.632L300,154.368 C295.538,148.152,290.649,142.14,285.464,136.504z" />'.
  2779. '<path d="M243.18,117.654c5.932,3.935,11.694,8.28,17.115,12.909l8.588-10.059c-5.826-4.977-12.016-9.646-18.398-13.874 L243.18,117.654z" />'.
  2780. '<path d="M0,141.823l10.054,8.593c4.629-5.416,9.64-10.605,14.888-15.422l-8.943-9.741C10.358,130.426,4.977,136.003,0,141.823z" />'.
  2781. '<path d="M106.286,100.191l6.644,0.004l8.061-12.223l25.91,0.181l-11.593,39.963c0,1.166,0.948,2.116,2.114,2.116h10.66 l22.266-41.295l20.437,0.679c5.817,0,10.524-4.455,'.
  2782. ' 10.524-9.951c0.004-5.491-4.711-9.946-10.519-9.946l-20.589,0.688 l-22.117-41.023l-10.665-0.002c-1.166,0.002-2.114,0.952-2.114,2.118l11.513,39.685l-25.97,'.
  2783. ' 0.225l-7.923-11.987l-6.644,0.002 c-0.884,0-1.598,0.712-1.598,1.594v37.582C104.688,99.479,105.404,100.196,106.286,100.191z" />'.
  2784. '<path d="M171.31,150.616c-8.657-1.973-17.503-2.974-26.307-2.974c-55.361,0-102.631,37.757-114.949,91.814 c-2.361,10.361-3.28,20.82-2.863,31.161h13.237v-0.003c-0.425-9.353,'.
  2785. ' 0.379-18.823,2.515-28.201 c4.329,1.122,23.682,6.492,23.067,12.719c-0.518,5.222-2.198,11.17-0.8,15.481h12.166c1.671-1.217,3.282-1.797,4.858-0.139 c0.042,0.046,0.097,'.
  2786. ' 0.093,0.141,0.139h96.071c-22.612-14.403-25.811-39.848-25.811-39.848c-1.596-0.694,2.969-18.768-4.14-20.939 c-7.12-2.169-11.608-0.43-21.691-4.929'.
  2787. ' c-10.096-4.499-6.316-10.786-4.658-25.789c0.708-6.402,2.337-12.133,4.413-16.636 c5.998-1.056,12.159-1.607,18.442-1.607c7.812,0,15.678,0.888,23.373,2.641c27.292,6.216,'.
  2788. ' 50.529,22.69,65.43,46.38 c11.233,17.864,16.705,38.217,16.059,58.848c-0.864,0.628-1.792,1.254-2.762,1.883h15.907 c1.049-23.743-5.088-47.224-18.01-67.771C228.221,'.
  2789. ' 176.164,202.05,157.617,171.31,150.616z" /></g>';
  2790. $ret .= '<g class="hm_p" transform="translate(7,4) scale(0.027,0.027)" style="fill:black" visibility="'.$partys.'">'.
  2791. '<path d="M64.6,951.8l114.8,36.8c16.1,5.1,33.2-3.7,38.5-19.8l0,0c5.1-16.1-3.7-33.2-19.8-38.5l-26.2-8.4l47.9-147.1c-10.3-1.8-20.5-4.3-30.5-7.5c-10.9-3.5-21.3-7.7-31.3-12.7l-48,'.
  2792. ' 147.4l-26.5-8.5c-16.1-5.1-33.2,3.7-38.5,19.8l0,0C39.7,929.4,48.5,946.6,64.6,951.8z" />'.
  2793. '<path d="M167.2,725.7c26.3,13.8,61,22.3,90.8,22.3c58.8,0,115.7-27.5,152.3-73.6c33.4-42,43.9-96,52.9-147.6c4.7-27.1,20.8-142.7,21.4-152.9c0.5-8.4,'.
  2794. ' 1.3-19.4-3-26.9c-3.7-6.7-10-11.8-17.3-14.1l-246.9-79c-3.1-1-6.2-1.5-9.3-1.5c-10.9,0-21.2,5.8-26.7,15.7c-31.4,56.6-62.7,113.8-88.9,173C78,473.2,64.9,506.3,63,'.
  2795. ' 541.9c-1.7,29.8,3.7,60,15.4,87.4C96,670.5,127.5,704.9,167.2,725.7z M222.6,319.7l199,63.7c-9.8,82.7-19.7,145.2-28.9,183.5l-95.5-30.5l-62-19.8l-95.5-30.5C154.4,449.3,'.
  2796. ' 182.6,392.8,222.6,319.7z" />'.
  2797. '<path d="M916.8,893.5l-26.5,8.5l-48-147.4c-10,4.9-20.4,9.2-31.3,12.7c-10,3.2-20.2,5.7-30.5,7.5l47.9,147l-26.2,8.4c-16.1,5.1-24.9,22.3-19.8,38.5l0,0c5.1,16.1,22.3,24.9,38.5,'.
  2798. ' 19.8l114.8-36.8c16.1-5.1,24.9-22.3,19.8-38.5l0,0C950.1,897.2,932.9,888.4,916.8,893.5z" />'.
  2799. '<path d="M515.2,355.5c-0.7,3.1-0.9,6.4-0.5,9.8c0.1,1.2,15.4,132,28.5,195.1c7,34,15.8,67.9,34.7,97.4c15.9,24.9,37.5,46.1,62.7,61.4c30.4,18.6,65.8,28.6,101.5,28.6c20.2,0,'.
  2800. ' 40.5-3.2,59.7-9.3c10.9-3.5,21.2-7.8,31-13c52.9-27.6,90.7-79.6,101.4-138.2c11.3-62.4-13.8-120.8-40.2-175.8c-23.4-48.8-49.2-96.3-75.4-143.6c-5.5-9.9-15.8-15.7-26.7-15.7c-3.1,'.
  2801. ' 0-6.2,0.5-9.3,1.5l-246.9,79C525.1,336.2,517.5,345,515.2,355.5z M777.4,319.7c40.1,73.1,68.3,129.6,83,166.1l-95.5,30.5l-62,19.8l-95.5,30.5c-9.2-38.3-19.1-100.6-28.9-183.5L777.4,319.7z" /></g>';
  2802. $ret .= '<g class="hm_dnd" transform="translate(8,8) scale(0.05,0.05)" style="fill:white" visibility="'.$dnds.'">'.
  2803. '<path d="M471,330v82h41V289H41V121c0-5.336-2-10.169-6-14.5c-4-4.333-8.833-6.5-14.5-6.5 S10,102.167,6,106.5s-6,9.167-6,14.5v291h41v-82H471z"/>'.
  2804. '<path d="M105.5,205c10.333,0,19.333-3.667,27-11c7.667-7.334,11.5-16.168,11.5-26.5 c0-10.335-3.833-19.168-11.5-26.5c-7.667-7.333-16.667-11-27-11s-19.167,3.667-26.5,11s-11,'.
  2805. ' 16.167-11,26.5s3.667,19.167,11,26.5 C86.333,201.333,95.167,205,105.5,205z"/>'.
  2806. '<path d="M512,258v-50c0-8-2.833-14.5-8.5-19.5c-5.67-5-12.837-8.167-21.5-9.5l-297-29h-2 c-5.333,0-9.833,1.833-13.5,5.5s-5.5,8.167-5.5,13.5v58H88c-12.667,0-19,5.167-19,'.
  2807. ' 15.5S75.333,258,88,258H512z"/></g></g>';
  2808. $ret .= '</svg>';
  2809. return $ret
  2810. }
  2811. #########################################################################################
  2812. #
  2813. # YAAHM_timewidget - returns SVG code for inclusion into any room page
  2814. #
  2815. # Parameter name = name of the YAAHM definition
  2816. #
  2817. #########################################################################################
  2818. sub YAAHM_timewidget($){
  2819. my ($arg) = @_;
  2820. my $name = $FW_webArgs{name};
  2821. $name =~ s/'//g;
  2822. my $sz = ($FW_webArgs{size} ? $FW_webArgs{size} : '400x400');
  2823. $sz =~ s/'//g;
  2824. my @size=split('x',$sz);
  2825. $FW_RETTYPE = "image/svg+xml";
  2826. $FW_RET="";
  2827. FW_pO '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800" width="'.$size[0].'px" height="'.$size[1].'px">';
  2828. my $hash = $defs{$name};
  2829. my $radius = 250;
  2830. my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time);
  2831. my $t_now = sprintf("%02d:%02d",$hour,$min);
  2832. my $a_now = (60*$hour + $min)/1440 * 2 * pi;
  2833. my $x_now = -int(sin($a_now)*$radius*100)/100;
  2834. my $y_now = int(cos($a_now)*$radius*100)/100;
  2835. my $t_sunrise = defined($hash->{DATA}{"DD"}[0]{"sunrise"}) ? $hash->{DATA}{"DD"}[0]{"sunrise"} : "06:00";
  2836. $t_sunrise =~ s/^0//;
  2837. ($hour,$min) = split(":",$t_sunrise);
  2838. my $sr = $hour + $min*60;
  2839. my $a_sunrise = (60*$hour + $min)/1440 * 2 * pi;
  2840. my $x_sunrise = -int(sin($a_sunrise)*$radius*100)/100;
  2841. my $y_sunrise = int(cos($a_sunrise)*$radius*100)/100;
  2842. my $t_morning = defined($hash->{DATA}{"DT"}{"morning"}[0]) ? $hash->{DATA}{"DT"}{"morning"}[0] : "08:00";
  2843. $t_morning =~ s/^0//;
  2844. ($hour,$min) = split(":",$t_morning);
  2845. my $mo = $hour + $min*60;
  2846. my $a_morning = (60*$hour + $min)/1440 * 2 * pi;
  2847. my $x_morning = -int(sin($a_morning)*$radius*100)/100;
  2848. my $y_morning = int(cos($a_morning)*$radius*100)/100;
  2849. my $t_noon = defined($hash->{DATA}{"DT"}{"noon"}[0]) ? $hash->{DATA}{"DT"}{"noon"}[0] : "12:00";
  2850. $t_noon =~ s/^0//;
  2851. ($hour,$min) = split(":",$t_noon);
  2852. my $a_noon = (60*$hour + $min)/1440 * 2 * pi;
  2853. my $x_noon = -int(sin($a_noon)*$radius*100)/100;
  2854. my $y_noon = int(cos($a_noon)*$radius*100)/100;
  2855. my $t_afternoon = defined($hash->{DATA}{"DT"}{"afternoon"}[0]) ? $hash->{DATA}{"DT"}{"afternoon"}[0] : "14:00";
  2856. $t_afternoon =~ s/^0//;
  2857. ($hour,$min) = split(":",$t_afternoon);
  2858. my $a_afternoon = (60*$hour + $min)/1440 * 2 * pi;
  2859. my $x_afternoon = -int(sin($a_afternoon)*$radius*100)/100;
  2860. my $y_afternoon = int(cos($a_afternoon)*$radius*100)/100;
  2861. my $t_sunset = defined($hash->{DATA}{"DD"}[0]{"sunset"}) ? $hash->{DATA}{"DD"}[0]{"sunset"} : "18:00";
  2862. $t_sunset =~ s/^0//;
  2863. ($hour,$min) = split(":",$t_sunset);
  2864. my $ss = $hour + $min*60;
  2865. my $a_sunset = (60*$hour + $min)/1440 * 2 * pi;
  2866. my $x_sunset = -int(sin($a_sunset)*$radius*100)/100;
  2867. my $y_sunset = int(cos($a_sunset)*$radius*100)/100;
  2868. my $t_evening = defined($hash->{DATA}{"DT"}{"evening"}[0]) ? $hash->{DATA}{"DT"}{"evening"}[0] : "19:00";
  2869. $t_evening =~ s/^0//;
  2870. ($hour,$min) = split(":",$t_evening);
  2871. my $ev = $hour + $min*60;
  2872. my $a_evening = (60*$hour + $min)/1440 * 2 * pi;
  2873. my $x_evening = -int(sin($a_evening)*$radius*100)/100;
  2874. my $y_evening = int(cos($a_evening)*$radius*100)/100;
  2875. my $t_night = defined($hash->{DATA}{"DT"}{"night"}[0]) ? $hash->{DATA}{"DT"}{"night"}[0] : "22:00";
  2876. $t_night =~ s/^0//;
  2877. ($hour,$min) = split(":",$t_night);
  2878. my $a_night = (60*$hour + $min)/1440 * 2 * pi;
  2879. my $x_night = -int(sin($a_night)*$radius*100)/100;
  2880. my $y_night = int(cos($a_night)*$radius*100)/100;
  2881. my $t_midnight = "0:00";
  2882. $t_midnight =~ s/^0//;
  2883. ($hour,$min) = split(":",$t_midnight);
  2884. my $a_midnight = 0.0;
  2885. my $x_midnight = 0.0;
  2886. my $y_midnight = $radius;
  2887. FW_pO '<defs>'.
  2888. sprintf('<linearGradient id="grad1" x1="0%%" y1="0%%" x2="%d%%" y2="%d%%">',int(-$x_noon/$radius*100),int(-$y_noon/$radius*100)).
  2889. '<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />'.
  2890. '<stop offset="100%" style="stop-color:rgb(255,100,0);stop-opacity:1" />'.
  2891. '</linearGradient>'.
  2892. sprintf('<linearGradient id="grad2" x1="%d%%" y1="%d%%" x2="0%%" y2="0%%">',int(-$x_noon/$radius*100),int(-$y_noon/$radius*100)).
  2893. '<stop offset="0%" style="stop-color:rgb(70,70,100);stop-opacity:1" />'.
  2894. '<stop offset="100%" style="stop-color:rgb(255,150,0);stop-opacity:1" />'.
  2895. '</linearGradient>'.
  2896. sprintf('<linearGradient id="grad3" x1="%d%%" y1="%d%%" x2="0%%" y2="0%%">',int(-$x_noon/$radius*100),int(-$y_noon/$radius*100)).
  2897. '<stop offset="0%" style="stop-color:rgb(80,80,80);stop-opacity:1" />'.
  2898. '<stop offset="100%" style="stop-color:rgb(120,120,100);stop-opacity:1" />'.
  2899. '</linearGradient>'.
  2900. '</defs>';
  2901. FW_pO '<g id="Ebene_1" transform="translate(400,400)">';
  2902. #-- daytime arc
  2903. FW_pO '<path d="M 0 0 '.($x_morning*1.1).' '.($y_morning*1.1). ' A '.($radius*1.1).' '.($radius*1.1).' 0 1 1 '.($x_night*1.1).' '.($y_night*1.1).' Z" fill="none" stroke="rgb(0,255,200)" stroke-width="15" />';
  2904. #-- sunset to sunrise sector. We need a workaround here for the broken SVG engine of Firefox, splitting this in two arcs
  2905. FW_pO '<path d="M 0 0 '.$x_sunset. ' '.$y_sunset. ' A '.$radius.' '.$radius.' 0 0 1 '.$x_midnight.' '.$y_midnight.' Z" fill="rgb(70,70,100)"/>';
  2906. FW_pO '<path d="M 0 0 '.$x_midnight.' '.$y_midnight. ' A '.$radius.' '.$radius.' 0 0 1 '.$x_sunrise.' '. $y_sunrise. ' Z" fill="rgb(70,70,100)"/>';
  2907. #-- sunrise to morning sector
  2908. my $dir = ( $sr < $mo ) ? 0 : 1;
  2909. FW_pO '<path d="M 0 0 '.$x_sunrise.' '.$y_sunrise.' A '.$radius.' '.$radius.' 0 0 '.$dir.' '.$x_morning.' '.$y_morning.' Z" fill="url(#grad2)"/>';
  2910. #-- morning to evening sector
  2911. FW_pO '<path d="M 0 0 '.$x_morning.' '.$y_morning.' A '.$radius.' '.$radius.' 0 0 1 '.$x_evening.' '.$y_evening.' Z" fill="url(#grad1)"/>';
  2912. #-- evening to sunset sector
  2913. $dir = ( $ss > $ev ) ? 1 : 0;
  2914. FW_pO '<path d="M 0 0 '.$x_evening.' '.$y_evening.' A '.$radius.' '.$radius.' 0 0 '.$dir.' '.$x_sunset.' '.$y_sunset.' Z" fill="url(#grad2)"/>';
  2915. #-- midnight line
  2916. FW_pO '<line x1="0" y1="0" x2="0" y2="'.($radius*1.2).'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2917. FW_pO '<text x="-30" y="'.($radius*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">0:00</text>';
  2918. #--sunrise line
  2919. FW_pO '<line x1="0" y1="0" x2="'.($x_sunrise*1.2).'" y2="'.($y_sunrise*1.2).'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2920. FW_pO '<text x="'.($x_sunrise*1.25-30).'" y="'.($y_sunrise*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_sunrise.'</text>';
  2921. #--morning line
  2922. FW_pO '<line x1="0" y1="0" x2="'.($x_morning*1.2).'" y2="'.($y_morning*1.2).'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2923. FW_pO '<text x="'.($x_morning*1.25-30).'" y="'.($y_morning*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_morning.'</text>';
  2924. #--noon line
  2925. FW_pO '<line x1="0" y1="0" x2="'.($x_noon*1.2) .'" y2="'.($y_noon*1.2) .'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2926. FW_pO '<text x="'.($x_noon*1.25).'" y="'.($y_noon*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_noon.'</text>';
  2927. #--afternoon line
  2928. FW_pO '<line x1="0" y1="0" x2="'.($x_afternoon*1.2) .'" y2="'.($y_afternoon*1.2) .'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2929. FW_pO '<text x="'.($x_afternoon*1.25).'" y="'.($y_afternoon*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_afternoon.'</text>';
  2930. #--sunset line
  2931. FW_pO '<line x1="0" y1="0" x2="'.($x_sunset*1.2) .'" y2="'.($y_sunset*1.2) .'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2932. FW_pO '<text x="'.($x_sunset*1.25).'" y="'.($y_sunset*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_sunset.'</text>';
  2933. #--evening line
  2934. FW_pO '<line x1="0" y1="0" x2="'.($x_evening*1.2) .'" y2="'.($y_evening*1.2) .'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2935. FW_pO '<text x="'.($x_evening*1.25).'" y="'.($y_evening*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_evening.'</text>';
  2936. #--night line
  2937. FW_pO '<line x1="0" y1="0" x2="'.($x_night*1.2) .'" y2="'.($y_night*1.2) .'" style="stroke:rgb(75, 75, 75);stroke-width:2" />';
  2938. FW_pO '<text x="'.($x_night*1.25).'" y="'.($y_night*1.25).'" fill="rgb(75, 75, 75)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_night.'</text>';
  2939. #--now line
  2940. FW_pO '<line x1="0" y1="0" x2="'.($x_now*1.2) .'" y2="'.($y_now*1.2) .'" style="stroke:rgb(255,0,0);stroke-width:4" />';
  2941. FW_pO '<text x="'.($x_now*1.25).'" y="'.($y_now*1.25).'" fill="rgb(255,0,0)" style="font-family:Helvetica;font-size:36px;font-weight:bold">'.$t_now.'</text>';
  2942. FW_pO '</g>';
  2943. FW_pO '</svg>';
  2944. return ($FW_RETTYPE, $FW_RET);
  2945. }
  2946. #########################################################################################
  2947. #
  2948. # YAAHM_toptable - returns incomplete HTML code for inclusion into any room page
  2949. # (action and overview fields)
  2950. #
  2951. # Parameter name = name of the YAAHM definition
  2952. #
  2953. #########################################################################################
  2954. sub YAAHM_toptable($){
  2955. my ($name) = @_;
  2956. my $hash = $defs{$name};
  2957. if( !defined($yaahm_tt) ){
  2958. #-- readjust language
  2959. my $lang = AttrVal("global","language","EN");
  2960. if( $lang eq "DE"){
  2961. $yaahm_tt = \%yaahm_transtable_DE;
  2962. }else{
  2963. $yaahm_tt = \%yaahm_transtable_EN;
  2964. }
  2965. }
  2966. #-- something's rotten in the state of denmark
  2967. my $st = $hash->{DATA}{"DD"}[0]{"sunrise"};
  2968. my $ts;
  2969. my ($styl,$stym,$styr);
  2970. my $ret = "";
  2971. YAAHM_GetDayStatus($hash);
  2972. #--
  2973. my $lockstate = ($hash->{READINGS}{lockstate}{VAL}) ? $hash->{READINGS}{lockstate}{VAL} : "unlocked";
  2974. my $showhelper = ($lockstate eq "unlocked") ? 1 : 0;
  2975. %dailytable = %{$hash->{DATA}{"DT"}};
  2976. my $dailyno = scalar keys %dailytable;
  2977. my $weeklyno = int( @{$hash->{DATA}{"WT"}} );
  2978. #--
  2979. $ret .= "<script type=\"text/javascript\" src=\"$FW_ME/pgm2/yaahm.js\"></script><script type=\"text/javascript\">\n";
  2980. $ret .= "var csmode = [\"".$csmode[0]."\",\"".$csmode[1]."\",\"".$csmode[2]."\",\"".$csmode[3]."\"];";
  2981. $ret .= "var csstate = [\"".$csstate[0]."\",\"".$csstate[1]."\",\"".$csstate[2]."\",\"".$csstate[3]."\"];";
  2982. $ret .= "var blinking = 0;\n";
  2983. $ret .= "var hscolor = \"".$csstate[0]."\";\n";
  2984. $ret .= "var dailyno = ".$dailyno.";\n";
  2985. $ret .= "var dailykeys = [\"".join("\",\"",(sort YAAHM_dsort keys %dailytable))."\"];\n";
  2986. $ret .= "var weeklyno = ".$weeklyno.";\n";
  2987. $ret .= "var weeklykeys = [\"".join("\",\"",@weeklytable)."\"];\n"; # Day names !!!
  2988. $ret .= "var weeklynames = [";
  2989. for( my $i=0;$i<$weeklyno;$i++){
  2990. $ret .= ","
  2991. if( $i!=0 );
  2992. $ret .= "\"".$hash->{DATA}{"WT"}[$i]{"name"}."\"";
  2993. }
  2994. $ret .= "];\n";
  2995. #-- watcher for next hidden divisions
  2996. for( my $i=2;$i<$weeklyno;$i++){
  2997. $ret .= "\$(\"body\").on('DOMSubtreeModified', \"#wt".$i."_o\",function () {nval = document.getElementById(\"wt".$i."_o\").innerHTML;document.getElementById(\"wt".$i."_n\").value = nval;})\n";
  2998. }
  2999. $ret .= "</script>\n";
  3000. $ret .= "<div informId=\"$name-housestate\" style=\"display:none\" id=\"hid_hs\">".ReadingsVal($name,"housestate",undef)."</div>".
  3001. "<div informId=\"$name-housemode\" style=\"display:none\" id=\"hid_hm\">".ReadingsVal($name,"housemode",undef)."</div>";
  3002. $ret .= "<table class=\"roomoverview\">\n";
  3003. $ret .= "<tr><td colspan=\"3\"><div class=\"devType\" style=\"font-weight:bold\">".$yaahm_tt->{"action"}.
  3004. "</div> <div informId=\"$name-tr_errmsg\" class=\"devType\" style=\"font-weight:normal\">".ReadingsVal($name,"tr_errmsg",undef)."</div></td></tr>\n";
  3005. ### action ################################################################################################
  3006. #-- determine columns
  3007. my $cols = max(max(int(@modes),int(@states)),$weeklyno);
  3008. $ret .= "<tr><td colspan=\"3\" style=\"align:left\"><table class=\"readings\" style=\"border:1px solid gray;border-radius:10px\">".
  3009. "<tr><td rowspan=\"2\" height=\"40\" valign=\"bottom\" id=\"wid_hm\">".YAAHM_modewidget($hash)."</td><td colspan=\"5\" height=\"20\"></td></tr>".
  3010. "<tr class=\"odd\"><td width=\"100px\" class=\"dname\" style=\"padding:5px;\">".$yaahm_tt->{"mode"}."</td>".
  3011. "<td width=\"120px\"><div class=\"dval\" informId=\"$name-tr_housemode\">".ReadingsVal($name,"tr_housemode",undef)."</div></td><td></td>";
  3012. for( my $i=0; $i<$cols; $i++){
  3013. if( $i < int(@modes)){
  3014. $ret .= "<td width=\"120px\"><input type=\"button\" id=\"b_".$modes[$i]."\" value=\"".$yaahm_tt->{$modes[$i]}.
  3015. "\" style=\"width:120px;\" onclick=\"javascript:yaahm_mode('$name','".$modes[$i]."')\"/></td>";
  3016. }else{
  3017. $ret .= "<td width=\"120px\"></td>";
  3018. }
  3019. }
  3020. $ret .= "</tr>";
  3021. $ret .= "<tr class=\"even\"><td rowspan=\"2\" height=\"40\" id=\"wid_hs\">".YAAHM_statewidget($hash)."</td>".
  3022. "<td class=\"dname\" style=\"padding:5px;\">".$yaahm_tt->{"state"}."</td>".
  3023. "<td><div informId=\"$name-tr_housestate\">".ReadingsVal($name,"tr_housestate",undef).
  3024. "</div></td><td style=\"width:20px\"><div id=\"sym_hs\" informId=\"$name-sym_housestate\" style=\"align:center\">".ReadingsVal($name,"sym_housestate",undef)."</div></td>";
  3025. for( my $i=0; $i<$cols; $i++){
  3026. if( $i < int(@states)){
  3027. $ret .= "<td width=\"120px\"><input type=\"button\" id=\"b_".$states[$i]."\" value=\"".$yaahm_tt->{$states[$i]}.
  3028. "\" style=\"width:120px;\" onclick=\"javascript:yaahm_state('$name','".$states[$i]."')\"/></td>";
  3029. }else{
  3030. $ret .= "<td width=\"120px\"></td>";
  3031. }
  3032. }
  3033. #style=\"height:20px;border-bottom: 10px solid #333333;background-image: linear-gradient(#e5e5e5,#ababab);\"
  3034. $ret .= "</tr><td colspan=\"5\" height=\"20\"></td></tr>";
  3035. $ret .= "</table><br/><table class=\"readings\">";
  3036. #-- repeat manual next for every weekly table
  3037. my $nval = "";
  3038. my $wupn;
  3039. $ret .= "<tr class=\"odd\"><td class=\"col1\" style=\"padding:5px; border-left: 1px solid gray; border-top:1px solid gray; border-bottom:1px solid gray; border-bottom-left-radius:10px; border-top-left-radius:10px;\">".$yaahm_tt->{"manual"}."</td>\n";
  3040. for (my $i=0;$i<$weeklyno;$i++){
  3041. if($i<$weeklyno-1){
  3042. $styl= "border-bottom:1px solid gray;border-top:1px solid gray";
  3043. }else{
  3044. $styl= "border-bottom:1px solid gray;border-top:1px solid gray;border-right:1px solid gray;border-bottom-right-radius:10px;border-top-right-radius:10px";
  3045. }
  3046. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  3047. $nval = ( defined($hash->{DATA}{"WT"}[$i]{"next"}) ) ? $hash->{DATA}{"WT"}[$i]{"next"} : "";
  3048. $ret .= sprintf("<td class=\"col2\" style=\"text-align:left;padding:5px;padding-left:10px;padding-right:10px;$styl\">$wupn<br/>".
  3049. "<div style=\"display:none\" id=\"wt%d_o\" informId=\"$name-next_$i\">$nval</div><input type=\"text\" id=\"wt%d_n\" size=\"4\" maxlength=\"120\" value=\"$nval\" onchange=\"javascript:yaahm_setnext('$name',%d)\"/></td>\n",$i,$i,$i);
  3050. }
  3051. $ret .= "</tr>\n";
  3052. $ret .= "</table><br/></td></tr>";
  3053. $ret .= "<tr><td colspan=\"3\"><div class=\"devType\" style=\"font-weight:bold\">".$yaahm_tt->{"overview"}."</div></td></tr>\n";
  3054. ### daily overview ################################################################################################
  3055. $styl="border-left:1px solid gray;border-top:1px solid gray;border-top-left-radius:10px;border-top-right-radius:0px;border-bottom-left-radius:0px;";
  3056. $stym="border-top:1px solid gray;border-radius:0px;";
  3057. $styr="border-right:1px solid gray;border-top:1px solid gray;border-top-right-radius:10px;border-top-left-radius:0px;border-bottom-right-radius:0px;";
  3058. $ret .= "<tr><td colspan=\"3\"><table>";
  3059. #-- time widget
  3060. $ret .= "<tr><td rowspan=\"8\" width=\"200\" style=\"padding-right:50px\"><img src=\"/fhem/YAAHM_timewidget?name='".$name."'&amp;size='200x200'\" type=\"image/svg+xml\" ></td>";
  3061. #-- continue summary with headers
  3062. $ret .= "<td colspan=\"2\" width=\"150\" style=\"$styl\"></td><td width=\"120\" style=\"$stym\">".$yaahm_tt->{"today"}."</td><td width=\"120\" style=\"$styr\">".$yaahm_tt->{"tomorrow"}."</td>";
  3063. #-- device states
  3064. $ret .= "<td rowspan=\"8\" style=\"padding:5px;vertical-align:top;border:1px solid gray;border-radius:10px\">".
  3065. "<div class=\"dval\" informId=\"$name-tr_housestate\">".ReadingsVal($name,"tr_housestate",undef)."</div>&#x2192;".
  3066. $yaahm_tt->{"secstate"}."<div class=\"dval\" informId=\"$name-sdev_housestate\">".ReadingsVal($name,"sdev_housestate","")."</div></td></tr>\n";
  3067. $styl="border-left:1px solid gray;border-radius:0px;";
  3068. $stym="border:none";
  3069. $styr="border-right:1px solid gray;border-radius:0px;";
  3070. $ret .= "<tr><td colspan=\"2\" style=\"$styl\"></td><td style=\"padding:5px;$stym\">".$yaahm_tt->{$weeklytable[$hash->{DATA}{"DD"}[0]{"weekday"}]}[0] .
  3071. "</td><td style=\"padding:5px;$styr\">".$yaahm_tt->{$weeklytable[$hash->{DATA}{"DD"}[1]{"weekday"}]}[0]."</td></tr>";
  3072. #-- continue summary with entries
  3073. $ret .= "<tr><td colspan=\"2\" style=\"$styl\"></td><td style=\"padding:5px;$stym\">".$hash->{DATA}{"DD"}[0]{"date"}.
  3074. "</td><td style=\"padding:5px;$styr\">".$hash->{DATA}{"DD"}[1]{"date"}."</td></tr>\n";
  3075. $ret .= "<tr><td colspan=\"2\" class=\"dname\" style=\"padding:5px;$styl\">".$yaahm_tt->{"daylight"}."</td><td style=\"padding:5px;$stym\">".$hash->{DATA}{"DD"}[0]{"sunrise"}."-".$hash->{DATA}{"DD"}[0]{"sunset"}.
  3076. "</td><td style=\"padding:5px;$styr\">".$hash->{DATA}{"DD"}[1]{"sunrise"}."-".$hash->{DATA}{"DD"}[1]{"sunset"}."</td></tr>\n";
  3077. $ret .= "<tr><td colspan=\"2\" class=\"dname\" style=\"padding:5px;$styl\">".$yaahm_tt->{"daytime"}."</td><td style=\"padding:5px;$stym\">".$hash->{DATA}{"DT"}{"morning"}[0]."-".
  3078. $hash->{DATA}{"DT"}{"night"}[0]."</td><td style=\"$styr\"></td></tr>\n";
  3079. $ret .= "<tr><td colspan=\"2\" class=\"dname\" style=\"padding:5px;$styl\">".$yaahm_tt->{"daytype"}."</td><td style=\"padding:5px;$stym\">".$yaahm_tt->{$hash->{DATA}{"DD"}[0]{"daytype"}}.
  3080. "</td><td style=\"padding:5px;$styr\">".$yaahm_tt->{$hash->{DATA}{"DD"}[1]{"daytype"}}."</td></tr>\n";
  3081. $ret .= "<tr><td colspan=\"2\" class=\"dname\" style=\"padding:5px;$styl\">".$yaahm_tt->{"description"}."</td><td style=\"padding:5px;$stym\">".$hash->{DATA}{"DD"}[0]{"desc"}.
  3082. "</td><td style=\"padding:5px;width:100px;$styr\">".$hash->{DATA}{"DD"}[1]{"desc"}."</td></tr>\n";
  3083. $styl="border-left:1px solid gray;border-bottom:1px solid gray;border-bottom-left-radius:10px;border-bottom-right-radius:0px;border-top-left-radius:0px;";
  3084. $stym="border-bottom:1px solid gray;border-radius:0px;";
  3085. $styr="border-right:1px solid gray;border-bottom:1px solid gray;border-bottom-right-radius:10px;border-top-right-radius:0px;border-bottom-left-radius:0px;";
  3086. $ret .= "<tr><td colspan=\"2\" class=\"dname\" style=\"padding:5px;$styl\">".$yaahm_tt->{"date"}."</td><td style=\"padding:5px;width:100px;$stym\">".$hash->{DATA}{"DD"}[0]{"special"}.
  3087. "</td><td style=\"padding:5px;width:100px;$styr\">".$hash->{DATA}{"DD"}[1]{"special"}."</td></tr>\n";
  3088. #-- housetime/phase
  3089. $ret .= "<tr><td rowspan=\"".$weeklyno."\" style=\"text-align:center; white-space:nowrap;max-height:20px\">".
  3090. "<label><div class=\"dval\" informId=\"$name-tr_housetime\">".ReadingsVal($name,"tr_housetime","").
  3091. "</div>&nbsp;<div class=\"dval\" informId=\"$name-tr_housephase\">".ReadingsVal($name,"tr_housephase","")."</div></label></td>";
  3092. #-- weekly timers
  3093. for( my $i=0;$i<$weeklyno;$i++ ){
  3094. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  3095. #-- timer status
  3096. if( defined($defs{$name.".wtimer_".$i.".IF"})){
  3097. if( ReadingsVal($name.".wtimer_".$i.".IF","mode","") ne "disabled" ){
  3098. $ts = "<div style=\"color:green\">&#x2713;</div>";
  3099. }else{
  3100. $ts = "<div style=\"color:red\">&#x274c;</div>";
  3101. }
  3102. }else{
  3103. $ts = "";
  3104. }
  3105. #-- ring times
  3106. my $ring_0x = $hash->{DATA}{"WT"}[$i]{"ring_0x"};
  3107. my $ring_1x = $hash->{DATA}{"WT"}[$i]{"ring_1x"};
  3108. my $wake = $hash->{DATA}{"WT"}[$i]{"wake"};
  3109. #-- border styles
  3110. if( $i==0 ){
  3111. $styl="border-left:1px solid gray;border-top:1px solid gray;border-top-left-radius:10px;border-top-right-radius:0px;border-bottom-left-radius:0px;";
  3112. $stym="border-top:1px solid gray;border-radius:0px;";
  3113. $styr="border-right:1px solid gray;border-top:1px solid gray;border-top-right-radius:10px;border-top-left-radius:0px;border-bottom-right-radius:0px;";
  3114. }elsif( $i == $weeklyno-1 ){
  3115. $styl="border-left:1px solid gray;border-bottom:1px solid gray;border-bottom-left-radius:10px;border-bottom-right-radius:0px;border-top-left-radius:0px;";
  3116. $stym="border-bottom:1px solid gray;border-radius:0px;";
  3117. $styr="border-right:1px solid gray;border-bottom:1px solid gray;border-bottom-right-radius:10px;border-top-right-radius:0px;border-bottom-left-radius:0px;";
  3118. }else{
  3119. $styl="border-left:1px solid gray;border-radius:0px;";
  3120. $stym="border:none";
  3121. $styr="border-right:1px solid gray;border-radius:0px;";
  3122. }
  3123. $ret.="<td style=\"padding:5px;$styl\">".$wupn.
  3124. "</td><td style=\"text-align:center;width:30px;padding:5px;$stym\">$ts</td>".
  3125. "<td style=\"padding:5px;$stym\"><div class=\"dval\" informId=\"$name-ring_".$i."x\">".$ring_0x."</div></td>".
  3126. "<td style=\"padding:5px;$stym\"><div class=\"dval\" informId=\"$name-ring_".$i."_1x\">".$ring_1x."</div></td>".
  3127. "<td style=\"padding:5px;$styr\"><div class=\"dval\" informId=\"$name-tr_wake_$i\">".$wake."</div></td></tr>\n";
  3128. }
  3129. $ret .= "</table></td></tr>\n";
  3130. return $ret;
  3131. }
  3132. #########################################################################################
  3133. #
  3134. # YAAHM_Shorttable - returns complete HTML code for inclusion into any room page
  3135. # (action and overview fields)
  3136. #
  3137. # Parameter name = name of the YAAHM definition
  3138. #
  3139. #########################################################################################
  3140. sub YAAHM_Shorttable($){
  3141. my ($name) = @_;
  3142. my $ret = YAAHM_toptable($name);
  3143. #-- complete the code of the page
  3144. $ret .= "</table>";
  3145. InternalTimer(gettimeofday()+ 1, "YAAHM_informer", $defs{$FW_cname},0);
  3146. return $ret;
  3147. }
  3148. #########################################################################################
  3149. #
  3150. # YAAHM_Longtable - returns complete HTML code for the full YAAHM page
  3151. # (action, overview, daily and weekly profile)
  3152. #
  3153. # Parameter name = name of the YAAHM definition
  3154. #
  3155. #########################################################################################
  3156. sub YAAHM_Longtable($){
  3157. my ($name) = @_;
  3158. my $ret = "";
  3159. my $hash = $defs{$name};
  3160. my $id = $defs{$name}{NR};
  3161. #--
  3162. my $lockstate = ($hash->{READINGS}{lockstate}{VAL}) ? $hash->{READINGS}{lockstate}{VAL} : "unlocked";
  3163. my $showhelper = ($lockstate eq "unlocked") ? 1 : 0;
  3164. %dailytable = %{$hash->{DATA}{"DT"}};
  3165. my $dailyno = scalar keys %dailytable;
  3166. my $weeklyno = int( @{$hash->{DATA}{"WT"}} );
  3167. #--
  3168. $ret = YAAHM_toptable($name);
  3169. ### daily profile table ################################################################################################
  3170. my $row = 1;
  3171. my $event = "";
  3172. my $sval = "";
  3173. my $eval = "";
  3174. my $xval = "";
  3175. my $dh = (defined($attr{$name}{"timeHelper"})) ? $attr{$name}{"timeHelper"} : undef;
  3176. #-- global status of timer
  3177. my ($tl,$ts);
  3178. if( defined($defs{$name.".dtimer.IF"})){
  3179. $tl = "<a href=\"/fhem?detail=$name.dtimer.IF\">$name.dtimer.IF</a>";
  3180. #-- green hook
  3181. if( ReadingsVal($name.".dtimer.IF","mode","") ne "disabled" ){
  3182. $ts = "<td style=\"color:green;padding-left:5px\">&#x2713;</td>\n";
  3183. #-- red cross
  3184. }else{
  3185. $ts = "<td style=\"color:red;padding-left:5px\">&#x274c;</td>\n";
  3186. }
  3187. }else{
  3188. $tl= $yaahm_tt->{"notstarted"};
  3189. $ts ="<td></td>";
  3190. }
  3191. #-- name link button status
  3192. $ret .= "<tr><td colspan=\"3\"><div class=\"devType\" style=\"font-weight:bold; white-space:nowrap;\">".$yaahm_tt->{"daily"}.$yaahm_tt->{"profile"}."\n".
  3193. "<table><tr><td style=\"text-align:center;vertical-align:middle;white-space:nowrap;padding-left:5px\"><div id=\"dtlink\" style=\"font-weight:normal\">$tl</div></td>$ts";
  3194. $ret .= "<td style=\"vertical-align:middle;;padding-left:5px\"><input type=\"button\" value=\"".$yaahm_tt->{"start"}." ".$yaahm_tt->{"daily"}.$yaahm_tt->{"timer"}."\" onclick=\"javascript:yaahm_startDayTimer('$name')\"/></td>\n";
  3195. $ret .= "</tr></table></div></td></tr>";
  3196. #-- header line
  3197. $ret .= "<tr><td colspan=\"3\"><table class=\"block wide\" id=\"dailytable\">\n";
  3198. $ret .= "<tr style=\"font-weight:bold\"><td rowspan=\"2\" class=\"devType col1\" style=\"min-width:120px;\">".$yaahm_tt->{"event"}."</td><td class=\"devType,col2\" style=\"text-align:right;min-width:180px;white-space:nowrap;\">".
  3199. $yaahm_tt->{"time"}." [hh:mm]&nbsp;&nbsp;&nbsp;</td>".
  3200. "<td rowspan=\"2\" class=\"devType col3\" style=\"min-width:200px;\">".$yaahm_tt->{"action"}."</td>".
  3201. "</tr>".
  3202. "<tr style=\"font-weight:bold\"><td class=\"devType col2\" style=\"text-align:right\">Start/Offset&nbsp;&nbsp;&nbsp;End&nbsp;&nbsp;&nbsp;</td></tr>\n";
  3203. #-- iterate through table
  3204. foreach my $key (sort YAAHM_dsort keys %dailytable){
  3205. $row++;
  3206. $event= $yaahm_tt->{$key};
  3207. $sval = $dailytable{$key}[0];
  3208. $eval = $dailytable{$key}[1];
  3209. $xval = $dailytable{$key}[2];
  3210. $xval = "" if( !defined($xval) );
  3211. #--
  3212. if( $dh ){
  3213. #-- timeHelper not in command list
  3214. if( $xval !~ /^{$dh/ ){
  3215. if( defined($xval) && length($xval)>0 ){
  3216. $xval ="{".$dh."('".$key."')},".$xval;
  3217. }else{
  3218. $xval ="{".$dh."('".$key."')}";
  3219. }
  3220. }
  3221. }
  3222. $ret .= sprintf("<tr class=\"%s\"><td class=\"col1\">$event</td>\n", ($row&1)?"odd":"even");
  3223. #-- First field
  3224. #-- Only reference for wakeup
  3225. if( $key =~ /^wakeup.*/ ){
  3226. $ret .= "<td class=\"col2\" style=\"text-align:left\">".$yaahm_tt->{"weekly"}.$yaahm_tt->{"profile"}."</td><td></td><td></td></tr>\n";
  3227. next;
  3228. #-- Only reference for sleep
  3229. }elsif( $key =~ /^sleep.*/ ){
  3230. $ret .= "<td class=\"col2\" style=\"text-align:left\">".$yaahm_tt->{"weekly"}.$yaahm_tt->{"profile"}."</td><td></td><td></td></tr>\n";
  3231. next;
  3232. #-- calculated value for sunrise/sunset
  3233. }elsif( $key =~ /.*((sunrise)|(sunset)|(midnight)).*/ ){
  3234. my $pre;
  3235. if( $key =~ /.*sunrise.*/ ){
  3236. $pre = $hash->{DATA}{"DD"}[0]{"sunrise"}
  3237. }elsif( $key =~ /.*sunset.*/ ){
  3238. $pre = $hash->{DATA}{"DD"}[0]{"sunset"}
  3239. }else{
  3240. $pre = "00:00";
  3241. }
  3242. $ret .= "<td class=\"col2\" style=\"text-align:right\">";
  3243. if( $key =~ /.*before.*/ ){
  3244. $ret .= "$pre - &nbsp;<input type=\"text\" id=\"dt".$key."_e\" size=\"4\" maxlength=\"120\" value=\"$eval\"/>&nbsp;=&nbsp;$sval&nbsp;&nbsp;&nbsp;&nbsp;</td>"
  3245. }elsif( $key =~ /.*after.*/ ){
  3246. $ret .= "$pre + &nbsp;<input type=\"text\" id=\"dt".$key."_e\" size=\"4\" maxlength=\"120\" value=\"$eval\"/>&nbsp;=&nbsp;$sval&nbsp;&nbsp;&nbsp;&nbsp;</td>"
  3247. }else{
  3248. $ret .= "$pre&nbsp;&nbsp;&nbsp;&nbsp;</td>"
  3249. }
  3250. #-- normal input of one time
  3251. }else{
  3252. $ret .= "<td class=\"col2\" style=\"text-align:right\"><input type=\"text\" id=\"dt".$key."_s\" size=\"4\" maxlength=\"120\" value=\"$sval\"/></td>";
  3253. }
  3254. #-- Second field
  3255. $ret .= "<td class=\"col3\"><input type=\"text\" id=\"dt".$key."_x\" size=\"28\" maxlength=\"512\" value=\"$xval\"/></td></tr>\n";
  3256. }
  3257. $ret .= "</table></td></tr></tr>";
  3258. ### weekly profile table ################################################################################################
  3259. $row = 1;
  3260. $event = "";
  3261. $sval = "";
  3262. my $wupn;
  3263. my $wt = ( $weeklyno == 1) ? $yaahm_tt->{"profile"} :$yaahm_tt->{"profiles"};
  3264. $ret .= "<tr><td colspan=\"3\"><div class=\"devType\" style=\"font-weight:bold; white-space: nowrap;\">".$yaahm_tt->{"weekly"}.$wt.
  3265. "&nbsp;&nbsp;<input type=\"button\" value=\"".$yaahm_tt->{"start"}." ".$yaahm_tt->{"weekly"}.$yaahm_tt->{"timer"}."\" onclick=\"javascript:yaahm_startWeeklyTimer('$name')\"/></div></td></tr>";
  3266. $ret .= "<tr><td><table class=\"readings\" id=\"weeklytable\">\n";
  3267. #-- repeat name for every weekly table
  3268. $ret .= "<tr class=\"odd\"><td class=\"col1\" style=\"font-weight:bold;text-align:left;padding:5px\">".$yaahm_tt->{"name"}."</td>";
  3269. for (my $i=0;$i<$weeklyno;$i++){
  3270. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  3271. $ret .= "<td class=\"col2\" style=\"text-align:left;padding:5px\">$wupn</td>";
  3272. }
  3273. $ret .= "</tr>\n";
  3274. #-- repeat link for every weekly table
  3275. $ret .= "<tr class=\"even\"><td class=\"col1\" style=\"font-weight:bold;text-align:left;padding:5px\">".$yaahm_tt->{"timer"}."</td>";
  3276. #-- array with activity status
  3277. my @tss;
  3278. for (my $i=0;$i<$weeklyno;$i++){
  3279. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  3280. #-- timer status
  3281. if( defined($defs{$name.".wtimer_".$i.".IF"})){
  3282. $tl = "<a href=\"/fhem?detail=".$name.".wtimer_".$i.".IF\">".$name.".wtimer_".$i.".IF</a>";
  3283. if( ReadingsVal($name.".wtimer_".$i.".IF","mode","") ne "disabled" ){
  3284. push(@tss,"<div style=\"color:green\">&#x2713;</div>");
  3285. }else{
  3286. push(@tss,"<div style=\"color:red\">&#x274c;</div>");
  3287. }
  3288. }else{
  3289. $tl = $yaahm_tt->{"notstarted"};
  3290. push(@tss,"");
  3291. }
  3292. $ret .= sprintf("<td class=\"col2\" style=\"text-align:left;padding:5px\"><div id=\"wt%dlink\">%s</div></td>",$i,$tl);
  3293. }
  3294. $ret .= "</tr>\n";
  3295. #-- repeat active status for every weekly table
  3296. my $asg = "";
  3297. my $ast = "";
  3298. my $ass;
  3299. my $acc;
  3300. #--header
  3301. for(my $i=0;$i<int(@profmode);$i++){
  3302. $asg .= substr($yaahm_tt->{$profmode[$i]},0,3)."&nbsp;";
  3303. $ast .= $yaahm_tt->{$profmode[$i]}." ";
  3304. }
  3305. for(my $i=0;$i<int(@profday);$i++){
  3306. $asg .= substr($yaahm_tt->{$profday[$i]},0,3)."&nbsp;";
  3307. $ast .= $yaahm_tt->{$profday[$i]}." ";
  3308. }
  3309. $ret .= "<tr class=\"odd\"><td class=\"col1\" style=\"font-weight:bold;text-align:left;padding:5px\">".$yaahm_tt->{"active"}."<br/><div title=\".$ast.\" style=\"font-weight:normal\">".$asg."</div></td>";
  3310. for (my $i=0;$i<$weeklyno;$i++){
  3311. $wupn = $hash->{DATA}{"WT"}[$i]{"name"};
  3312. $ret .= "<td class=\"col2\" style=\"text-align:center;padding:5px\">".$tss[$i]."</br>";
  3313. $asg = "";
  3314. $ass = ( defined($hash->{DATA}{"WT"}[$i]{"acti_m"}) ) ? $hash->{DATA}{"WT"}[$i]{"acti_m"} : "";
  3315. for( my $j=0;$j<int(@profmode);$j++ ){
  3316. $acc = $profmode[$j];
  3317. $acc = ( $ass =~ /.*$acc.*/ ) ? " checked=\"checked\"" : "";
  3318. $asg .= sprintf("<input type=\"checkbox\" name=\"acti_%d_m\" value=\"".$profmode[$j]."\" $acc/>&nbsp;",$i);
  3319. }
  3320. $ass = ( defined($hash->{DATA}{"WT"}[$i]{"acti_d"}) ) ? $hash->{DATA}{"WT"}[$i]{"acti_d"} : "";
  3321. for( my $j=0;$j<int(@profday);$j++ ){
  3322. $acc = $profday[$j];
  3323. $acc = ( $ass =~ /.*$acc.*/ ) ? " checked=\"checked\"" : "";
  3324. $asg .= sprintf("<input type=\"checkbox\" name=\"acti_%d_d\" value=\"".$profday[$j]."\" $acc/>&nbsp;",$i);
  3325. }
  3326. $ret .= "$asg</td>";
  3327. }
  3328. $ret .= "</tr>\n";
  3329. #-- repeat action for every weekly table
  3330. $ret .= "<tr class=\"odd\"><td class=\"col1\" style=\"font-weight:bold;text-align:left;padding:5px\">".$yaahm_tt->{"action"}."</td>";
  3331. for (my $i=0;$i<$weeklyno;$i++){
  3332. $xval = $hash->{DATA}{"WT"}[$i]{"action"};
  3333. #--
  3334. if( $dh && $i<2 ){
  3335. #-- timeHelper not in command list
  3336. $wupn = ($i==0) ? "wakeup" : "sleep";
  3337. if( $xval !~ /^{$dh/ ){
  3338. if( defined($xval) && length($xval)>0 ){
  3339. $xval ="{".$dh."('".$wupn."')},".$xval;
  3340. }else{
  3341. $xval ="{".$dh."('".$wupn."')}";
  3342. }
  3343. }
  3344. }
  3345. $ret .= sprintf("<td class=\"col2\" style=\"text-align:left;padding:5px\"><input class=\"expand\" type=\"text\" id=\"wt%d_x\" size=\"10\" maxlength=\"512\" value=\"$xval\"/></td>",$i);
  3346. }
  3347. $ret .= "</tr>\n";
  3348. #-- repeat unit for every weekly table
  3349. $ret .= "<tr class=\"even\"><td></td>";
  3350. for (my $i=0;$i<$weeklyno;$i++){
  3351. $ret .= "<td class=\"col2\" style=\"text-align:left;padding:5px\">".$yaahm_tt->{"time"}." [hh:mm]</td>";
  3352. }
  3353. $ret .= "</tr>\n";
  3354. #-- weekday header
  3355. $ret .= "<tr class=\"even\"><td class=\"col1\" style=\"font-weight:bold;text-align:left;padding:5px\">".$yaahm_tt->{"weekday"}."</td>";
  3356. for (my $i=0;$i<$weeklyno;$i++){
  3357. $ret .= "<td></td>";
  3358. }
  3359. $ret .= "</tr>\n";
  3360. for (my $j=0;$j<7;$j++){
  3361. my $key = $weeklytable[$j];
  3362. $row++;
  3363. $event = $yaahm_tt->{$key}[0];
  3364. $ret .= sprintf("<tr class=\"%s\"><td class=\"col1\" style=\"text-align:left;padding-left:5px\">$event</td>\n", ($row&1)?"odd":"even");
  3365. for (my $i=0;$i<$weeklyno;$i++){
  3366. $sval = $hash->{DATA}{"WT"}[$i]{$key};
  3367. $ret .= sprintf("<td class=\"col2\" style=\"text-align:left;padding-left:5px\"><input type=\"text\" id=\"wt%s%d_s\" size=\"4\" maxlength=\"120\" value=\"$sval\"/></td>",$key,$i);
  3368. }
  3369. $ret .= "</tr>\n";
  3370. }
  3371. $ret .= "</table></td></tr>";
  3372. #-- complete the code of the page
  3373. $ret .= "</table>";
  3374. #InternalTimer(gettimeofday()+ 3, "YAAHM_informer", $defs{$FW_cname},0);
  3375. return $ret;
  3376. }
  3377. 1;
  3378. =pod
  3379. =item helper
  3380. =item summary admimistration of profiles for daily, weekly and monthly processes
  3381. =item summary_DE Verwaltung von Profilen für tägliche, wöchentliche und monatliche Abläufe
  3382. =begin html
  3383. <a name="YAAHM"></a>
  3384. <h3>YAAHM</h3>
  3385. <ul>
  3386. <p> Yet Another Auto Home Module to set up a cyclic processing of commands (daily, weekly, monthly, yearly profile)</p>
  3387. <a name="YAAHMusage"></a>
  3388. <h4>Usage</h4>
  3389. See <a href="http://www.fhemwiki.de/wiki/Modul_YAAHM">German Wiki page</a>
  3390. <br/>
  3391. <a name="YAAHMdefine"></a>
  3392. <h4>Define</h4>
  3393. <p>
  3394. <code>define &lt;name&gt; YAAHM</code>
  3395. <br />Defines the YAAHM system. </p>
  3396. Notes: <ul>
  3397. <li>This module uses the global attribute <code>language</code> to determine its output data<br/>
  3398. (default: EN=english). For German output set <code>attr global language DE</code>.</li>
  3399. <li>This module needs the JSON package</li>
  3400. </ul>
  3401. <a name="YAAHMset"></a>
  3402. <h4>Set</h4>
  3403. <ul>
  3404. <li><a name="yaahm_time">
  3405. <code>set &lt;name&gt; time &lt;timeevent&gt;</code></a><br/>
  3406. Set the current house time (event), i.e. one of several values:
  3407. <ul>
  3408. <li>(after|before) midnight | [before|after] sunrise | [before|after] sunset are calculated from astronomical data (&pm;offset).
  3409. These values vary from day to day, only the offset can be specified in the daily profile. </li>
  3410. <li>morning | noon | afternoon | evening | night are fixed time events specified in the daily profile.
  3411. The time phase between events morning and night is called <i>daytime</i>, the
  3412. time phase between events night and morning is called <i>nighttime</i></li>
  3413. <li>wakeup|sleep are time events specified in the weekly default profiles <i>Wakeup</i> and <i>Sleep</i>, i.e. the value may change from day to day.</li>
  3414. </ul>
  3415. The actual changes to certain devices are made by the functions in the command field, or by an external <a href="#yaahm_timehelper">timeHelper function</a>.
  3416. </li>
  3417. <li><a name="yaahm_manualnext"></a>
  3418. <code>set &lt;name&gt; manualnext &lt;timernumber&gt; &lt;time&gt;</code><br/>
  3419. <code>set &lt;name&gt; manualnext &lt;timername&gt; &lt;time&gt;</code><br/>
  3420. For the weekly timer identified by its number (starting at 0) or its name, set the next ring time manually. The time specification &lt;time&gt;must be in the format hh:mm, or "off", or "default".
  3421. <ul>
  3422. <li>If the time specification &lt;time&gt; is later than the current time, it will be used for today. If it is earlier than the current time, it will be used tomorrow.</li>
  3423. <li>If the time specification is "off", the next pending waketime will be ignored.</li>
  3424. <li>If the time specification id "default", the manual waketime is removed and the value from the weekly schedule will be used.</li>
  3425. </ul>
  3426. </li>
  3427. <li><a name="yaahm_mode">
  3428. <code>set &lt;name&gt; mode normal | party | absence | donotdisturb</code>
  3429. </a>
  3430. <br />Set the current house mode, i.e. one of several values:
  3431. <ul>
  3432. <li>normal - normal daily and weekly time profiles apply</li>
  3433. <li>party - can be used in the timeHelper function to suppress certain actions, like e.g. those that set the house (security) state to <i>secured</i> or the house time event to <i>night</i>.</li>
  3434. <li>absence - can be used in the timeHelper function to suppress certain actions. Valid until manual mode change</li>
  3435. <li>donotodisturb - can be used in the timeHelper function to suppress certain actions. Valid until manual mode change</li>
  3436. </ul>
  3437. House modes are valid until manual mode change. If the attribute <i>modeAuto</i> is set (see below), mode will change automatically at certain time events.
  3438. The actual changes to certain devices are made by an external <a href="#yaahm_modehelper">modeHelper function</a>.
  3439. </li>
  3440. <li><a name="yaahm_state">
  3441. <code>set &lt;name&gt; state unsecured | secured | protected | guarded</code>
  3442. </a>
  3443. <br/>Set house (security) state, i.e. one of several values:
  3444. <ul>
  3445. <li> unsecured - Example: doors etc.
  3446. </li>
  3447. <li> secured - Example: doors etc. are locked, windows may not be open
  3448. </li>
  3449. <li> protected - Example: doors etc. are locked, windows may not be open, alarm system is armed
  3450. </li>
  3451. <li> guarded - Example: doors etc. are locked, windows may not be open, alarm is armed, a periodic house check is run and a simulation as well
  3452. </li>
  3453. </ul>
  3454. House (security) states are valid until manual change. If the attribute <i>stateAuto</i> is set (see below), state will change automatically at certain times.
  3455. The actual changes to certain devices are made by an external <a href="#yaahm_statehelper">stateHelper function</a>. If these external devices are in their proper state
  3456. for a particular house (security) state can be checked automatically, see the attribute <a href="#yaahm_statedevices">stateDevices</a>
  3457. </li>
  3458. <li><a name="yaahm_checkstate">
  3459. <code>set &lt;name&gt; checkstate 0|5|10</code>
  3460. </a>
  3461. <br/>Check the house (security) state for each of the devices defined in the stateDevices attribute in 0, 5 or 10 seconds from now</li>
  3462. <li><a name="yaahm_correctstate">
  3463. <code>set &lt;name&gt; correctstate</code>
  3464. </a>
  3465. <br/>Try to correct the house (security) state, by issueing a FHEM command <i>set &lt;device&gt; &lt;targetstate&gt;</i>
  3466. for each of the devices defined in the stateDevices attribute and not in the proper state for the given house (security) state</li>
  3467. <li><a name="yaahm_createweekly">
  3468. <code>set &lt;name&gt; createWeekly &lt;string&gt;</code>
  3469. </a>
  3470. <br/>Create a new weekly profile &lt;string&gt;</li>
  3471. <li><a name="yaahm_deleteweekly">
  3472. <code>set &lt;name&gt; deleteWeekly &lt;string&gt;</code>
  3473. </a>
  3474. <br/>Delete the weekly profile &lt;string&gt;</li>
  3475. <li><a name="yaahm_initialize">
  3476. <code>set &lt;name&gt; initialize</code>
  3477. </a>
  3478. <br/>Restart the internal timers</li>
  3479. <li><a name="yaahm_lock">
  3480. <code>set &lt;name&gt; locked|unlocked</code>
  3481. </a>
  3482. <br />Set the lockstate of the yaahm module to <i>locked</i> (i.e., yaahm setups
  3483. may not be changed) resp. <i>unlocked</i> (i.e., yaahm setups may be changed>)</li>
  3484. <li><a name="yaahm_save">
  3485. <code>set &lt;name&gt; save|restore</code>
  3486. </a>
  3487. <br />Manually save/restore the complete profile data to/from the external file YAAHMFILE (save done automatically at each timer start, restore at FHEM start)</li>
  3488. </ul>
  3489. <a name="YAAHMget"></a>
  3490. <h4>Get</h4>
  3491. <ul>
  3492. <li><a name="yaahm_next"></a>
  3493. <code>get &lt;name&gt; next &lt;timernumber&gt;</code><br/>
  3494. <code>get &lt;name&gt; next &lt;timername&gt;</code><br/>
  3495. For the weekly timer identified by its number (starting at 0) or its name, get the next ring time in a format suitable for text devices.</li>
  3496. <li><a name="yaahm_saynext"></a>
  3497. <code>get &lt;name&gt; sayNext &lt;timernumber&gt;</code><br/>
  3498. <code>get &lt;name&gt; sayNext &lt;timername&gt;</code><br/>
  3499. For the weekly timer identified by its number (starting at 0) or its name, get the next ring time in a format suitable for speech devices.</li>
  3500. <li><a name="yaahm_version"></a>
  3501. <code>get &lt;name&gt; version</code>
  3502. <br />Display the version of the module</li>
  3503. <li><a name="yaahm_template"></a>
  3504. <code>get &lt;name&gt; template</code>
  3505. <br />Return an (empty) perl subroutine for the helper functions</li>
  3506. </ul>
  3507. <a name="YAAHMattr"></a>
  3508. <h4>Attributes</h4>
  3509. <ul>
  3510. <li><a name="yaahm_linkname"><code>attr &lt;name&gt; linkname
  3511. &lt;string&gt;</code></a>
  3512. <br />Name for yaahm web link, default:
  3513. Profile</li>
  3514. <li><a name="yaahm_hiddenroom"><code>attr &lt;name&gt; hiddenroom
  3515. &lt;string&gt;</code></a>
  3516. <br />Room name for hidden yaahm room (containing only the YAAHM device), default:
  3517. ProfileRoom</li>
  3518. <li><a name="yaahm_lockstate"><code>attr &lt;name&gt; lockstate
  3519. locked|unlocked</code></a>
  3520. <br /><i>locked</i> means that yaahm setups may not be changed, <i>unlocked</i>
  3521. means that yaahm setups may be changed</li>
  3522. <li><a name="yaahm_simulation"><code>attr &lt;name&gt; simulation
  3523. 0|1</code></a>
  3524. <br />a value of 1 means that commands issued directly on the device as "set ... " will not be executed, but only simulated. Does <i>not</i> prevent the button
  3525. click commands from the interactive web page to be executed.</li>
  3526. <li><a name="yaahm_noicons"><code>attr &lt;name&gt; noicons
  3527. 0|1</code></a>
  3528. <br />when set to 1, animated icons are suppressed</li>
  3529. <li><a name="yaahm_modecolor"><code>attr &lt;name&gt; modecolor[0|1|2|3] <i>color</i></code></a>
  3530. <br />four color specifications to signal the modes normal (default <span style="color:#53f3c7">#53f3c7</span>),
  3531. party (default <span style="color:#8bfa56">#8bfa56</span>), absence (default <span style="color:#ff9458">#ff9458</span>),
  3532. donotodisturb (default <span style="color:#fd5777">#fd5777</span>), </li>
  3533. <li><a name="yaahm_statecolor"><code>attr &lt;name&gt; statecolor[0|1|2|3] <i>color</i></code></a>
  3534. <br />four color specifications to signal the states unsecured (default <span style="color:#53f3c7">#53f3c7</span>),
  3535. secured (default <span style="color:#ff9458">#ff9458</span>),
  3536. protected (default <span style="color:#f554e2">#f554e2</span>) and guarded (default <span style="color:#fd5777">#fd5777</span>)</li>
  3537. <li><a name="yaahm_timehelper"><code>attr &lt;name&gt; timeHelper &lt;name of perl program&gt;</code></a>
  3538. <br />name of a perl function that is called at each time step of the daily profile and for the two default weekly profiles</li>
  3539. <li><a name="yaahm_modehelper"><code>attr &lt;name&gt; modeHelper &lt;name of perl program&gt;</code></a>
  3540. <br />name of a perl function that is called at every change of the house mode</li>
  3541. <li><a name="yaahm_modeauto"><code>attr &lt;name&gt; modeAuto 0|1</code></a>
  3542. <br />If this attribute is set to 1, the house mode changes automatically at certain time events.
  3543. <ul>
  3544. <li>On time (event) <i>sleep</i> or <i>morning</i>, <i>party</i> mode will be reset to <i>normal</i> mode.</li>
  3545. <li>On time (event) <i>wakeup</i>, <i>absence</i> mode will be reset to <i>normal</i> mode.</li>
  3546. <li>On <i>any</i> time (event), <i>donotdisturb</i> mode will be reset to <i>normal</i> mode.</li>
  3547. </ul>
  3548. </li>
  3549. <li><<a name="yaahm_norepeat"><code>attr &lt;name&gt; norepeat 0|1</code></a>
  3550. <br/> if set to 1, repeated executions of wakeup, sleep and other timer events from the weekly programme will be suppressed.</li>
  3551. <li><a name="yaahm_statedevices"><code>attr &lt;name&gt; stateDevices (&lt;device&gt;:&lt;state-unsecured&gt;:&lt;state-secured&gt;:&lt;state-protected&gt;:&lt;state-guarded&gt;,)*</code></a>
  3552. <br />comma separated list of devices and their state in each of the house (security) states. Each of the listed devices will be checked in the interval given by the <i>stateInterval</i> attribute
  3553. for its proper state, and a <i>stateWarning</i> function will be called if it is not in the proper state.</li>
  3554. <li><a name="yaahm_stateinterval"><code>attr &lt;name&gt; stateInterval &lt;integer&gt;</code></a>
  3555. <br />interval in minutes for checking all <i>stateDevices</i> for their proper state according of the house (security) state. Default 60 minutes.</li>
  3556. <li><a name="yaahm_statewarning"><code>attr &lt;name&gt; stateWarning &lt;name of perl program&gt;</code></a>
  3557. <br />name of a perl function that is called as <i>stateWarning('device','desired state','actual state')</i>if a device is not in the desired state.</li>
  3558. <li><a name="yaahm_statehelper"><code>attr &lt;name&gt; stateHelper &lt;name of perl program&gt;</code></a>
  3559. <br />name of a perl function that is called as <i>stateHelper('event')</i> at every change of the house (security) state</li>
  3560. <li><a name="yaahm_stateauto"><code>attr &lt;name&gt; stateAuto 0|1</code></a>
  3561. <br />If this attribute is set to 1, the house state changes automatically if certain modes are set or at certain time events
  3562. <ul>
  3563. <li>If leaving <i>party</i> mode and time event <i>sleep</i>, and currently in (security) state <i>unsecured</i>, the state will change to <i>secured</i>.</li>
  3564. <li>If in <i>normal</i> mode and time event <i>sleep</i> or <i>night</i>, and currently in (security) state <i>unsecured</i>, the state will change to <i>secured</i>.</li>
  3565. </ul>
  3566. </li>
  3567. <li><a name="yaahm_holidaydevices"><code>attr &lt;name&gt; &lt;comma-separated list of devices&gt; </code></a>
  3568. <br />list of devices that provide holiday information. The devices may be
  3569. <a href="#holiday">holiday devices</a> or <a href="#Calendar">Calendar devices</a></li>
  3570. <li><a name="yaahm_vacationdevices"><code>attr &lt;comma-separated list of devices&gt; </code></a>
  3571. <br />list of devices that provide vacation information. The devices may be
  3572. <a href="#holiday">holiday devices</a> or <a href="#Calendar">Calendar devices</a></li>
  3573. <li><a name="yaahm_specialdevices"><code>attr &lt;comma-separated list of devices&gt; </code></a>
  3574. <br />list of devices that provide special date information (like e.g. garbage collection). The devices may be
  3575. <a href="#holiday">holiday devices</a> or <a href="#Calendar">Calendar devices</a></li>
  3576. </ul>
  3577. </ul>
  3578. =end html
  3579. =begin html_DE
  3580. <a name="YAAHM"></a>
  3581. <h3>YAAHM</h3>
  3582. <ul>
  3583. <a href="https://wiki.fhem.de/wiki/Modul_YAAHM">Deutsche Dokumentation im Wiki</a> vorhanden, die englische Version gibt es hier: <a href="/fhem/docs/commandref.html#YAAHM">YAAHM</a>
  3584. </ul>
  3585. =end html_DE
  3586. =cut