RESIDENTStk.pm 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787
  1. ###############################################################################
  2. # $Id: RESIDENTStk.pm 17593 2018-10-22 15:35:04Z loredo $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Data::Dumper;
  7. use Unit;
  8. our (@RESIDENTStk_attr);
  9. # module variables ############################################################
  10. @RESIDENTStk_attr = (
  11. "autoGoneAfter:0,12,16,24,26,28,30,36,48,60",
  12. "geofenceUUIDs",
  13. "lang:EN,DE",
  14. "locationHome",
  15. "locations",
  16. "locationUnderway",
  17. "locationWayhome",
  18. "moodDefault",
  19. "moods",
  20. "moodSleepy",
  21. "noDuration:0,1",
  22. "passPresenceTo",
  23. "presenceDevices",
  24. "realname:group,alias",
  25. "showAllStates:0,1",
  26. "wakeupDevice",
  27. );
  28. ## initialize #################################################################
  29. sub RESIDENTStk_Initialize() { }
  30. # regular Fn ##################################################################
  31. sub RESIDENTStk_InitializeDev($) {
  32. my ($hash) = @_;
  33. $hash = $defs{$hash} unless ( ref($hash) );
  34. my $NOTIFYDEV = "global";
  35. if ( $hash->{TYPE} eq "RESIDENTS" ) {
  36. $NOTIFYDEV .= "," . RESIDENTStk_findResidentSlaves($hash);
  37. }
  38. else {
  39. $NOTIFYDEV .= "," . RESIDENTStk_findDummySlaves($hash);
  40. }
  41. return 0
  42. if ( defined( $hash->{NOTIFYDEV} ) && $hash->{NOTIFYDEV} eq $NOTIFYDEV );
  43. $hash->{NOTIFYDEV} = $NOTIFYDEV;
  44. return undef;
  45. }
  46. sub RESIDENTStk_Define($$) {
  47. my ( $hash, $def ) = @_;
  48. my @a = split( "[ \t][ \t]*", $def );
  49. my $name = $hash->{NAME};
  50. my $name_attr;
  51. my $TYPE = GetType($name);
  52. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  53. if ( $TYPE ne "RESIDENTS" && int(@a) < 2 ) {
  54. my $msg = "Wrong syntax: define <name> $TYPE [RESIDENTS-DEVICE-NAMES]";
  55. Log3 $name, 4, $msg;
  56. return $msg;
  57. }
  58. return "Invalid parameter for RESIDENTS-DEVICE-NAMES"
  59. unless ( $TYPE ne "RESIDENTS"
  60. || !defined( $a[2] )
  61. || $a[2] =~ /^[A-Za-z\d._]+(?:,[A-Za-z\d._]*)*$/ );
  62. $hash->{MOD_INIT} = 1;
  63. $hash->{NOTIFYDEV} = "global";
  64. delete $hash->{RESIDENTGROUPS} if ( $hash->{RESIDENTGROUPS} );
  65. $hash->{RESIDENTGROUPS} = $a[2] if ( defined( $a[2] ) );
  66. # set default settings on first define
  67. if ( $init_done && !defined( $hash->{OLDDEF} ) ) {
  68. RESIDENTStk_Attr( "init", $name, $prefix . "lang" );
  69. $attr{$name}{webCmd} = "state";
  70. if ( $TYPE eq "RESIDENTS" ) {
  71. $attr{$name}{icon} = "control_building_filled";
  72. $attr{$name}{room} = "Residents";
  73. }
  74. elsif ( $TYPE ne "RESIDENTS" ) {
  75. my $fname = $name;
  76. $fname =~ s/^$prefix//;
  77. $attr{$name}{group} = $fname if ( $prefix eq "rr_" );
  78. $attr{$name}{group} = "Guests" if ( $prefix eq "rg_" );
  79. $attr{$name}{alias} = "Status" if ( $prefix eq "rr_" );
  80. $attr{$name}{alias} = $fname if ( $prefix eq "rg_" );
  81. $attr{$name}{icon} = "people_sensor" if ( $prefix eq "rr_" );
  82. $attr{$name}{icon} = "scene_visit_guests" if ( $prefix eq "rg_" );
  83. $attr{$name}{rr_realname} = "group" if ( $prefix eq "rr_" );
  84. $attr{$name}{rg_realname} = "alias" if ( $prefix eq "rg_" );
  85. $attr{$name}{sortby} = "1";
  86. if ( $hash->{RESIDENTGROUPS} ) {
  87. my $firstRgr = $hash->{RESIDENTGROUPS};
  88. $firstRgr =~ s/,[\s\S]*$//gmi;
  89. $attr{$name}{room} = $attr{$firstRgr}{room}
  90. if ( $attr{$firstRgr} && $attr{$firstRgr}{room} );
  91. }
  92. }
  93. }
  94. # trigger for modified objects
  95. if ( $TYPE ne "RESIDENTS" ) {
  96. readingsBeginUpdate($hash);
  97. readingsBulkUpdateIfChanged( $hash, "state",
  98. ReadingsVal( $name, "state", "" ) );
  99. readingsEndUpdate( $hash, 1 );
  100. }
  101. # run timers
  102. InternalTimer(
  103. gettimeofday() + 15,
  104. "RESIDENTStk_StartInternalTimers",
  105. $hash, 0
  106. );
  107. return undef;
  108. }
  109. sub RESIDENTStk_Undefine($$) {
  110. my ( $hash, $name ) = @_;
  111. RESIDENTStk_StopInternalTimers($hash);
  112. return undef unless ($init_done);
  113. # delete child roommates
  114. if ( defined( $hash->{ROOMMATES} )
  115. && $hash->{ROOMMATES} ne "" )
  116. {
  117. my @registeredRoommates =
  118. split( /,/, $hash->{ROOMMATES} );
  119. foreach my $child (@registeredRoommates) {
  120. fhem( "delete " . $child );
  121. Log3 $name, 3, "RESIDENTS $name: deleted device $child";
  122. }
  123. }
  124. # delete child guests
  125. if ( defined( $hash->{GUESTS} )
  126. && $hash->{GUESTS} ne "" )
  127. {
  128. my @registeredGuests =
  129. split( /,/, $hash->{GUESTS} );
  130. foreach my $child (@registeredGuests) {
  131. fhem( "delete " . $child );
  132. Log3 $name, 3, "RESIDENTS $name: deleted device $child";
  133. }
  134. }
  135. return undef;
  136. }
  137. sub RESIDENTStk_Set($@);
  138. sub RESIDENTStk_Set($@) {
  139. my ( $hash, @a ) = @_;
  140. my $name = $hash->{NAME};
  141. my $TYPE = GetType($name);
  142. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  143. my $state = ReadingsVal( $name, "state", "initialized" );
  144. my $presence = ReadingsVal( $name, "presence", "undefined" );
  145. my $mood = ReadingsVal( $name, "mood", "-" );
  146. my $location = ReadingsVal( $name, "location", "undefined" );
  147. my $roommates = ( $hash->{ROOMMATES} ? $hash->{ROOMMATES} : undef );
  148. my $guests = ( $hash->{GUESTS} ? $hash->{GUESTS} : undef );
  149. my $silent = 0;
  150. return undef if ( IsDisabled($name) );
  151. return "No Argument given" unless ( defined( $a[1] ) );
  152. # depending on current FHEMWEB instance's allowedCommands,
  153. # restrict set commands if there is "set-user" in it
  154. my $adminMode = 1;
  155. my $FWallowedCommands = 0;
  156. $FWallowedCommands = AttrVal( $FW_wname, "allowedCommands", 0 )
  157. if ( defined($FW_wname) );
  158. if ( $FWallowedCommands && $FWallowedCommands =~ m/\bset-user\b/ ) {
  159. $adminMode = 0;
  160. return "Forbidden command: set " . $a[1]
  161. if ( $TYPE ne "RESIDENTS" && lc( $a[1] ) eq "create" );
  162. return "Forbidden command: set "
  163. . $a[1]
  164. if (
  165. $TYPE eq "RESIDENTS"
  166. && ( lc( $a[1] ) eq "addroommate"
  167. || lc( $a[1] ) eq "addguest"
  168. || lc( $a[1] ) eq "removeroommate"
  169. || lc( $a[1] ) eq "removeguest"
  170. || lc( $a[1] ) eq "create" )
  171. );
  172. }
  173. # states
  174. my $states = (
  175. defined( $attr{$name}{ $prefix . 'states' } )
  176. ? $attr{$name}{ $prefix . 'states' }
  177. : (
  178. defined( $attr{$name}{ $prefix . 'showAllStates' } )
  179. && $attr{$name}{ $prefix . 'showAllStates' } == 1
  180. ? "home,gotosleep,asleep,awoken,absent"
  181. : "home,gotosleep,absent"
  182. )
  183. );
  184. $states .= ",gone" if ( $TYPE ne "GUEST" );
  185. $states .= ",none" if ( $TYPE eq "GUEST" );
  186. $states = $state . "," . $states
  187. if ( $state ne "initialized" && $states !~ /$state/ );
  188. $states =~ s/ /,/g;
  189. # moods
  190. my $moods = (
  191. defined( $attr{$name}{ $prefix . 'moods' } )
  192. ? $attr{$name}{ $prefix . 'moods' } . ",toggle"
  193. : "calm,relaxed,happy,excited,lonely,sad,bored,stressed,uncomfortable,sleepy,angry,toggle"
  194. );
  195. $moods = $mood . "," . $moods if ( $moods !~ /$mood/ );
  196. $moods =~ s/ /,/g;
  197. # locations
  198. my $locations = (
  199. defined( $attr{$name}{ $prefix . 'locations' } )
  200. ? $attr{$name}{ $prefix . 'locations' }
  201. : ""
  202. );
  203. if ( $locations !~ /$location/
  204. && $locations ne "" )
  205. {
  206. $locations = ":" . $location . "," . $locations;
  207. }
  208. elsif ( $locations ne "" ) {
  209. $locations = ":" . $locations;
  210. }
  211. $locations =~ s/ /,/g;
  212. my $usage = "Unknown argument " . $a[1] . ", choose one of state:$states";
  213. if ( $TYPE ne "RESIDENTS" ) {
  214. $usage .= " mood:$moods";
  215. $usage .= " location$locations";
  216. if ($adminMode) {
  217. $usage .= " create:wakeuptimer";
  218. $usage .= ",locationMap"
  219. if ( ReadingsVal( $name, "locationLat", "-" ) ne "-"
  220. && ReadingsVal( $name, "locationLong", "-" ) ne "-" );
  221. }
  222. }
  223. else {
  224. if ($adminMode) {
  225. $usage .= " addRoommate addGuest";
  226. $usage .= " removeRoommate:" . $roommates if ($roommates);
  227. $usage .= " removeGuest:" . $guests if ($guests);
  228. $usage .= " create:wakeuptimer";
  229. }
  230. }
  231. # silentSet
  232. if ( $a[1] eq "silentSet" ) {
  233. $silent = 1;
  234. my $first = shift @a;
  235. $a[0] = $first;
  236. }
  237. # states
  238. if ( $a[1] eq "state"
  239. || $a[1] eq "home"
  240. || $a[1] eq "gotosleep"
  241. || $a[1] eq "asleep"
  242. || $a[1] eq "awoken"
  243. || $a[1] eq "absent"
  244. || $a[1] eq "none"
  245. || $a[1] eq "gone" )
  246. {
  247. my $newstate;
  248. # if not direct
  249. if (
  250. $a[1] eq "state"
  251. && defined( $a[2] )
  252. && ( $a[2] eq "home"
  253. || $a[2] eq "gotosleep"
  254. || $a[2] eq "asleep"
  255. || $a[2] eq "awoken"
  256. || $a[2] eq "absent"
  257. || $a[2] eq "none"
  258. || $a[2] eq "gone" )
  259. )
  260. {
  261. $newstate = $a[2];
  262. }
  263. elsif ( defined( $a[2] ) ) {
  264. return
  265. "Invalid 2nd argument, choose one of home gotosleep asleep awoken absent gone ";
  266. }
  267. else {
  268. $newstate = $a[1];
  269. }
  270. $newstate = "none" if ( $newstate eq "gone" && $TYPE eq "GUEST" );
  271. $newstate = "gone" if ( $newstate eq "none" && $TYPE ne "GUEST" );
  272. Log3 $name, 2, "$TYPE set $name " . $newstate if ( !$silent );
  273. # RESIDENTS only
  274. if ( $TYPE eq "RESIDENTS" ) {
  275. $presence = "absent";
  276. # loop through every roommate
  277. if ($roommates) {
  278. my @registeredRoommates =
  279. split( /,/, $roommates );
  280. foreach my $roommate (@registeredRoommates) {
  281. fhem "set $roommate silentSet state $newstate"
  282. if ( ReadingsVal( $roommate, "state", "initialized" ) ne
  283. $newstate );
  284. }
  285. }
  286. # loop through every guest
  287. if ($guests) {
  288. $newstate = "none" if ( $newstate eq "gone" );
  289. my @registeredGuests =
  290. split( /,/, $guests );
  291. foreach my $guest (@registeredGuests) {
  292. fhem "set $guest silentSet state $newstate"
  293. if ( ReadingsVal( $guest, "state", "initialized" ) ne
  294. $newstate );
  295. }
  296. }
  297. }
  298. # ROOMMATE+GUEST: if state changed
  299. elsif ( $state ne $newstate ) {
  300. readingsBeginUpdate($hash);
  301. readingsBulkUpdate( $hash, "lastState", $state );
  302. readingsBulkUpdate( $hash, "state", $newstate );
  303. my $datetime = TimeNow();
  304. # reset mood
  305. my $mood_default =
  306. ( defined( $attr{$name}{ $prefix . 'moodDefault' } ) )
  307. ? $attr{$name}{ $prefix . 'moodDefault' }
  308. : "calm";
  309. my $mood_sleepy =
  310. ( defined( $attr{$name}{ $prefix . 'moodSleepy' } ) )
  311. ? $attr{$name}{ $prefix . 'moodSleepy' }
  312. : "sleepy";
  313. if (
  314. $mood ne "-"
  315. && ( $newstate eq "gone"
  316. || $newstate eq "none"
  317. || $newstate eq "absent"
  318. || $newstate eq "asleep" )
  319. )
  320. {
  321. Log3 $name, 4,
  322. "$TYPE $name: implicit mood change caused by state "
  323. . $newstate;
  324. RESIDENTStk_Set( $hash, $name, "silentSet", "mood", "-" );
  325. }
  326. elsif ( $mood ne $mood_sleepy
  327. && ( $newstate eq "gotosleep" || $newstate eq "awoken" ) )
  328. {
  329. Log3 $name, 4,
  330. "$TYPE $name: implicit mood change caused by state "
  331. . $newstate;
  332. RESIDENTStk_Set( $hash, $name, "silentSet", "mood",
  333. $mood_sleepy );
  334. }
  335. elsif ( ( $mood eq "-" || $mood eq $mood_sleepy )
  336. && $newstate eq "home" )
  337. {
  338. Log3 $name, 4,
  339. "$TYPE $name: implicit mood change caused by state "
  340. . $newstate;
  341. RESIDENTStk_Set( $hash, $name, "silentSet", "mood",
  342. $mood_default );
  343. }
  344. # if state is asleep, start sleep timer
  345. readingsBulkUpdate( $hash, "lastSleep", $datetime )
  346. if ( $newstate eq "asleep" );
  347. # if prior state was asleep, update sleep statistics
  348. if ( $state eq "asleep"
  349. && ReadingsVal( $name, "lastSleep", "" ) ne "" )
  350. {
  351. readingsBulkUpdate( $hash, "lastAwake", $datetime );
  352. readingsBulkUpdate(
  353. $hash,
  354. "lastDurSleep",
  355. UConv::duration(
  356. $datetime, ReadingsVal( $name, "lastSleep", "" )
  357. )
  358. );
  359. readingsBulkUpdate(
  360. $hash,
  361. "lastDurSleep_cr",
  362. UConv::duration(
  363. $datetime, ReadingsVal( $name, "lastSleep", "" ),
  364. "min"
  365. )
  366. );
  367. }
  368. # calculate presence state
  369. my $newpresence =
  370. ( $newstate ne "none"
  371. && $newstate ne "gone"
  372. && $newstate ne "absent" )
  373. ? "present"
  374. : "absent";
  375. # stop any running wakeup-timers in case state changed
  376. my $wakeupState = ReadingsVal( $name, "wakeup", 0 );
  377. if ( $wakeupState > 0 ) {
  378. my $wakeupDeviceList =
  379. AttrVal( $name, $prefix . 'wakeupDevice', 0 );
  380. for my $wakeupDevice ( split /,/, $wakeupDeviceList ) {
  381. next if !$wakeupDevice;
  382. if ( IsDevice( $wakeupDevice, "dummy" ) ) {
  383. # forced-stop only if resident is not present anymore
  384. if ( $newpresence eq "present" ) {
  385. Log3 $name, 4,
  386. "$TYPE $name: ending wakeup-timer $wakeupDevice";
  387. fhem "set $wakeupDevice:FILTER=running!=0 end";
  388. }
  389. else {
  390. Log3 $name, 4,
  391. "$TYPE $name: stopping wakeup-timer $wakeupDevice";
  392. fhem "set $wakeupDevice:FILTER=running!=0 stop";
  393. }
  394. }
  395. }
  396. }
  397. # if presence changed
  398. if ( $newpresence ne $presence ) {
  399. readingsBulkUpdate( $hash, "presence", $newpresence );
  400. # update location
  401. my @location_home =
  402. split( ' ',
  403. AttrVal( $name, $prefix . 'locationHome', "home" ) );
  404. my @location_underway =
  405. split(
  406. ' ',
  407. AttrVal( $name, $prefix . 'locationUnderway', "underway" )
  408. );
  409. my @location_wayhome =
  410. split( ' ',
  411. AttrVal( $name, $prefix . 'locationWayhome', "wayhome" ) );
  412. my $searchstring = quotemeta($location);
  413. if ( !$silent && $newpresence eq "present" ) {
  414. if ( !grep( m/^$searchstring$/, @location_home )
  415. && $location ne $location_home[0] )
  416. {
  417. Log3 $name, 4,
  418. "$TYPE $name: implicit location change caused by state "
  419. . $newstate;
  420. RESIDENTStk_Set( $hash, $name, "silentSet", "location",
  421. $location_home[0] );
  422. }
  423. }
  424. else {
  425. if ( !$silent
  426. && !grep( m/^$searchstring$/, @location_underway )
  427. && $location ne $location_underway[0] )
  428. {
  429. Log3 $name, 4,
  430. "$TYPE $name: implicit location change caused by state "
  431. . $newstate;
  432. RESIDENTStk_Set( $hash, $name, "silentSet", "location",
  433. $location_underway[0] );
  434. }
  435. }
  436. # reset wayhome
  437. readingsBulkUpdateIfChanged( $hash, "wayhome", "0" );
  438. # update statistics
  439. if ( $newpresence eq "present" ) {
  440. readingsBulkUpdate( $hash, "lastArrival", $datetime );
  441. # absence duration
  442. if ( ReadingsVal( $name, "lastDeparture", "-" ) ne "-" ) {
  443. readingsBulkUpdate(
  444. $hash,
  445. "lastDurAbsence",
  446. UConv::duration(
  447. $datetime,
  448. ReadingsVal( $name, "lastDeparture", "-" )
  449. )
  450. );
  451. readingsBulkUpdate(
  452. $hash,
  453. "lastDurAbsence_cr",
  454. UConv::duration(
  455. $datetime,
  456. ReadingsVal( $name, "lastDeparture", "-" ),
  457. "min"
  458. )
  459. );
  460. }
  461. }
  462. else {
  463. readingsBulkUpdate( $hash, "lastDeparture", $datetime );
  464. # presence duration
  465. if ( ReadingsVal( $name, "lastArrival", "-" ) ne "-" ) {
  466. readingsBulkUpdate(
  467. $hash,
  468. "lastDurPresence",
  469. UConv::duration(
  470. $datetime,
  471. ReadingsVal( $name, "lastArrival", "-" )
  472. )
  473. );
  474. readingsBulkUpdate(
  475. $hash,
  476. "lastDurPresence_cr",
  477. UConv::duration(
  478. $datetime,
  479. ReadingsVal( $name, "lastArrival", "-" ), "min"
  480. )
  481. );
  482. }
  483. }
  484. # adjust linked objects
  485. if ( defined( $attr{$name}{ $prefix . 'passPresenceTo' } )
  486. && $attr{$name}{ $prefix . 'passPresenceTo' } ne "" )
  487. {
  488. my @linkedObjects =
  489. split( ' ', $attr{$name}{ $prefix . 'passPresenceTo' } );
  490. foreach my $object (@linkedObjects) {
  491. if ( IsDevice( $object, "ROOMMATE|GUEST" )
  492. && $defs{$object} ne $name
  493. && ReadingsVal( $object, "state", "" ) ne "gone"
  494. && ReadingsVal( $object, "state", "" ) ne "none" )
  495. {
  496. fhem("set $object $newstate");
  497. }
  498. }
  499. }
  500. }
  501. # clear readings if guest is gone
  502. if ( $newstate eq "none" ) {
  503. readingsBulkUpdate( $hash, "lastArrival", "-" )
  504. if ( ReadingsVal( $name, "lastArrival", "-" ) ne "-" );
  505. readingsBulkUpdate( $hash, "lastAwake", "-" )
  506. if ( ReadingsVal( $name, "lastAwake", "-" ) ne "-" );
  507. readingsBulkUpdate( $hash, "lastDurAbsence", "-" )
  508. if ( ReadingsVal( $name, "lastDurAbsence", "-" ) ne "-" );
  509. readingsBulkUpdate( $hash, "lastDurSleep", "-" )
  510. if ( ReadingsVal( $name, "lastDurSleep", "-" ) ne "-" );
  511. readingsBulkUpdate( $hash, "lastLocation", "-" )
  512. if ( ReadingsVal( $name, "lastLocation", "-" ) ne "-" );
  513. readingsBulkUpdate( $hash, "lastSleep", "-" )
  514. if ( ReadingsVal( $name, "lastSleep", "-" ) ne "-" );
  515. readingsBulkUpdate( $hash, "lastMood", "-" )
  516. if ( ReadingsVal( $name, "lastMood", "-" ) ne "-" );
  517. readingsBulkUpdate( $hash, "location", "-" )
  518. if ( ReadingsVal( $name, "location", "-" ) ne "-" );
  519. readingsBulkUpdate( $hash, "mood", "-" )
  520. if ( ReadingsVal( $name, "mood", "-" ) ne "-" );
  521. }
  522. # calculate duration timers
  523. RESIDENTStk_DurationTimer( $hash, $silent );
  524. readingsEndUpdate( $hash, 1 );
  525. # enable or disable AutoGone timer
  526. if ( $newstate eq "absent" ) {
  527. RESIDENTStk_AutoGone($hash);
  528. }
  529. elsif ( $state eq "absent" ) {
  530. delete $hash->{AUTOGONE} if ( $hash->{AUTOGONE} );
  531. RESIDENTStk_RemoveInternalTimer( "AutoGone", $hash );
  532. }
  533. }
  534. }
  535. # RESIDENTS only: addRoommate
  536. elsif ( $TYPE eq "RESIDENTS" && lc( $a[1] ) eq "addroommate" ) {
  537. Log3 $name, 2, "$TYPE set $name " . $a[1] . " " . $a[2]
  538. if ( defined( $a[2] ) );
  539. my $rr_name;
  540. my $rr_name_attr;
  541. if ( $a[2] ne "" ) {
  542. $rr_name = "rr_" unless ( $a[2] =~ /^rr_/ );
  543. $rr_name .= $a[2];
  544. # define roommate
  545. fhem( "define " . $rr_name . " ROOMMATE " . $name )
  546. unless ( IsDevice($rr_name) );
  547. if ( IsDevice($rr_name) ) {
  548. return "Can't create, device $rr_name already existing."
  549. unless ( IsDevice( $rr_name, "ROOMMATE" ) );
  550. my $lang =
  551. $a[3]
  552. ? uc( $a[3] )
  553. : AttrVal( $rr_name, "rr_lang",
  554. AttrVal( $name, "rgr_lang", undef ) );
  555. fhem( "attr " . $rr_name . " rr_lang " . $lang )
  556. if ($lang);
  557. $attr{$rr_name}{comment} = "Auto-created by $name"
  558. unless ( defined( $attr{$rr_name}{comment} )
  559. && $attr{$rr_name}{comment} eq "Auto-created by $name" );
  560. fhem "sleep 1;set $rr_name silentSet state home";
  561. Log3 $name, 3, "$TYPE $name: created new device $rr_name";
  562. }
  563. }
  564. else {
  565. return "No Argument given, choose one of name ";
  566. }
  567. }
  568. # RESIDENTS only: removeRoommate
  569. elsif ( $TYPE eq "RESIDENTS" && lc( $a[1] ) eq "removeroommate" ) {
  570. Log3 $name, 2, "$TYPE set $name " . $a[1] . " " . $a[2]
  571. if ( defined( $a[2] ) );
  572. if ( $a[2] ne "" ) {
  573. my $rr_name = $a[2];
  574. # delete roommate
  575. if ( IsDevice($rr_name) ) {
  576. Log3 $name, 3, "$TYPE $name: deleted device $rr_name"
  577. if fhem( "delete " . $rr_name );
  578. }
  579. }
  580. else {
  581. return "No Argument given, choose one of name ";
  582. }
  583. }
  584. # RESIDENTS only: addGuest
  585. elsif ( $TYPE eq "RESIDENTS" && lc( $a[1] ) eq "addguest" ) {
  586. Log3 $name, 2, "$TYPE set $name " . $a[1] . " " . $a[2]
  587. if ( defined( $a[2] ) );
  588. my $rg_name;
  589. my $rg_name_attr;
  590. if ( $a[2] ne "" ) {
  591. $rg_name = "rg_" unless ( $a[2] =~ /^rg_/ );
  592. $rg_name .= $a[2];
  593. # define guest
  594. fhem( "define " . $rg_name . " GUEST " . $name )
  595. unless ( IsDevice($rg_name) );
  596. if ( IsDevice($rg_name) ) {
  597. return "Can't create, device $rg_name already existing."
  598. unless ( IsDevice( $rg_name, "GUEST" ) );
  599. my $lang =
  600. $a[3]
  601. ? uc( $a[3] )
  602. : AttrVal( $rg_name, "rg_lang",
  603. AttrVal( $name, "rgr_lang", undef ) );
  604. fhem( "attr " . $rg_name . " rg_lang " . $lang )
  605. if ($lang);
  606. $attr{$rg_name}{comment} = "Auto-created by $name"
  607. unless ( defined( $attr{$rg_name}{comment} )
  608. && $attr{$rg_name}{comment} eq "Auto-created by $name" );
  609. fhem "sleep 1;set $rg_name silentSet state home";
  610. Log3 $name, 3, "$TYPE $name: created new device $rg_name";
  611. }
  612. }
  613. else {
  614. return "No Argument given, choose one of name ";
  615. }
  616. }
  617. # RESIDENTS only: removeGuest
  618. elsif ( $TYPE eq "RESIDENTS" && lc( $a[1] ) eq "removeguest" ) {
  619. Log3 $name, 2, "$TYPE set $name " . $a[1] . " " . $a[2]
  620. if ( defined( $a[2] ) );
  621. if ( $a[2] ne "" ) {
  622. my $rg_name = $a[2];
  623. # delete guest
  624. if ( IsDevice($rg_name) ) {
  625. Log3 $name, 3, "$TYPE $name: deleted device $rg_name"
  626. if fhem( "delete " . $rg_name );
  627. }
  628. }
  629. else {
  630. return "No Argument given, choose one of name ";
  631. }
  632. }
  633. # mood
  634. elsif ( $TYPE ne "RESIDENTS" && $a[1] eq "mood" ) {
  635. if ( defined( $a[2] ) && $a[2] ne "" ) {
  636. Log3 $name, 2, "$TYPE set $name mood " . $a[2] if ( !$silent );
  637. readingsBeginUpdate($hash) if ( !$silent );
  638. if ( $a[2] eq "toggle"
  639. && ReadingsVal( $name, "lastMood", "" ) ne "" )
  640. {
  641. readingsBulkUpdate( $hash, "mood",
  642. ReadingsVal( $name, "lastMood", "" ) );
  643. readingsBulkUpdate( $hash, "lastMood", $mood );
  644. }
  645. elsif ( $mood ne $a[2] ) {
  646. readingsBulkUpdate( $hash, "lastMood", $mood )
  647. if ( $mood ne "-" );
  648. readingsBulkUpdate( $hash, "mood", $a[2] );
  649. }
  650. readingsEndUpdate( $hash, 1 ) if ( !$silent );
  651. }
  652. else {
  653. return "Invalid 2nd argument, choose one of mood toggle";
  654. }
  655. }
  656. # location
  657. elsif ( $TYPE ne "RESIDENTS" && $a[1] eq "location" ) {
  658. if ( defined( $a[2] ) && $a[2] ne "" ) {
  659. Log3 $name, 2, "$TYPE set $name location " . $a[2]
  660. if ( !$silent );
  661. if ( $location ne $a[2] ) {
  662. my $searchstring;
  663. readingsBeginUpdate($hash) if ( !$silent );
  664. # read attributes
  665. my @location_home =
  666. split( ' ',
  667. AttrVal( $name, $prefix . 'locationHome', "home" ) );
  668. my @location_underway =
  669. split(
  670. ' ',
  671. AttrVal( $name, $prefix . 'locationUnderway', "underway" )
  672. );
  673. my @location_wayhome =
  674. split( ' ',
  675. AttrVal( $name, $prefix . 'locationWayhome', "wayhome" ) );
  676. $searchstring = quotemeta($location);
  677. readingsBulkUpdate( $hash, "lastLocation", $location )
  678. if ( $location ne "wayhome"
  679. && !grep( m/^$searchstring$/, @location_underway ) );
  680. readingsBulkUpdate( $hash, "location", $a[2] )
  681. if ( $a[2] ne "wayhome" );
  682. # wayhome detection
  683. $searchstring = quotemeta($location);
  684. if (
  685. (
  686. $a[2] eq "wayhome"
  687. || grep( m/^$searchstring$/, @location_wayhome )
  688. )
  689. && ( $presence eq "absent" )
  690. )
  691. {
  692. Log3 $name, 3,
  693. "$TYPE $name: on way back home from $location";
  694. readingsBulkUpdateIfChanged( $hash, "wayhome", "1" );
  695. }
  696. readingsEndUpdate( $hash, 1 ) if ( !$silent );
  697. # auto-updates
  698. $searchstring = quotemeta( $a[2] );
  699. if (
  700. (
  701. $a[2] eq "home"
  702. || grep( m/^$searchstring$/, @location_home )
  703. )
  704. && $state ne "home"
  705. && $state ne "gotosleep"
  706. && $state ne "asleep"
  707. && $state ne "awoken"
  708. && $state ne "initialized"
  709. )
  710. {
  711. Log3 $name, 4,
  712. "$TYPE $name: implicit state change caused by location "
  713. . $a[2];
  714. RESIDENTStk_Set( $hash, $name, "silentSet", "state",
  715. "home" );
  716. }
  717. elsif (
  718. (
  719. $a[2] eq "underway"
  720. || grep( m/^$searchstring$/, @location_underway )
  721. )
  722. && $state ne "gone"
  723. && $state ne "none"
  724. && $state ne "absent"
  725. && $state ne "initialized"
  726. )
  727. {
  728. Log3 $name, 4,
  729. "$TYPE $name: implicit state change caused by location "
  730. . $a[2];
  731. RESIDENTStk_Set( $hash, $name, "silentSet", "state",
  732. "absent" );
  733. }
  734. }
  735. }
  736. else {
  737. return "Invalid 2nd argument, choose one of location ";
  738. }
  739. }
  740. # create
  741. elsif ( lc( $a[1] ) eq "create" ) {
  742. if ( $TYPE eq "RESIDENTS"
  743. && ( !defined( $a[2] ) || lc( $a[2] ) ne "wakeuptimer" ) )
  744. {
  745. return "Invalid 2nd argument, choose one of wakeuptimer ";
  746. }
  747. elsif ( !defined( $a[2] ) || $a[2] !~ /^(wakeuptimer|locationMap)$/i ) {
  748. return
  749. "Invalid 2nd argument, choose one of wakeuptimer locationMap ";
  750. }
  751. elsif ( lc( $a[2] ) eq "wakeuptimer" ) {
  752. my $i = "1";
  753. my $wakeuptimerName = $name . "_wakeuptimer" . $i;
  754. my $created = 0;
  755. until ($created) {
  756. if ( IsDevice($wakeuptimerName) ) {
  757. $i++;
  758. $wakeuptimerName = $name . "_wakeuptimer" . $i;
  759. }
  760. else {
  761. my $sortby = AttrVal( $name, "sortby", -1 );
  762. $sortby++;
  763. # create new dummy device
  764. fhem "define $wakeuptimerName dummy";
  765. fhem "attr $wakeuptimerName alias Wake-up Timer $i";
  766. fhem
  767. "attr $wakeuptimerName comment Auto-created by $TYPE module for use with RESIDENTS Toolkit";
  768. fhem
  769. "attr $wakeuptimerName devStateIcon OFF:general_aus\@red:reset running:general_an\@green:stop .*:general_an\@orange:nextRun%20OFF";
  770. fhem "attr $wakeuptimerName group " . $attr{$name}{group}
  771. if ( defined( $attr{$name}{group} ) );
  772. fhem "attr $wakeuptimerName icon time_timer";
  773. fhem "attr $wakeuptimerName room " . $attr{$name}{room}
  774. if ( defined( $attr{$name}{room} ) );
  775. fhem
  776. "attr $wakeuptimerName setList nextRun:OFF,00:00,00:15,00:30,00:45,01:00,01:15,01:30,01:45,02:00,02:15,02:30,02:45,03:00,03:15,03:30,03:45,04:00,04:15,04:30,04:45,05:00,05:15,05:30,05:45,06:00,06:15,06:30,06:45,07:00,07:15,07:30,07:45,08:00,08:15,08:30,08:45,09:00,09:15,09:30,09:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45 reset:noArg trigger:noArg start:noArg stop:noArg end:noArg wakeupOffset:slider,0,1,120 wakeupDefaultTime:OFF,00:00,00:15,00:30,00:45,01:00,01:15,01:30,01:45,02:00,02:15,02:30,02:45,03:00,03:15,03:30,03:45,04:00,04:15,04:30,04:45,05:00,05:15,05:30,05:45,06:00,06:15,06:30,06:45,07:00,07:15,07:30,07:45,08:00,08:15,08:30,08:45,09:00,09:15,09:30,09:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45 wakeupResetdays:multiple-strict,0,1,2,3,4,5,6 wakeupDays:multiple-strict,0,1,2,3,4,5,6 wakeupHolidays:,andHoliday,orHoliday,andNoHoliday,orNoHoliday wakeupEnforced:0,1,2,3";
  777. fhem "attr $wakeuptimerName userattr wakeupUserdevice";
  778. fhem "attr $wakeuptimerName sortby " . $sortby
  779. if ($sortby);
  780. fhem "attr $wakeuptimerName wakeupUserdevice $name";
  781. fhem "attr $wakeuptimerName webCmd nextRun";
  782. # register slave device
  783. my $wakeupDevice =
  784. AttrVal( $name, $prefix . 'wakeupDevice', 0 );
  785. if ( !$wakeupDevice ) {
  786. fhem "attr $name $prefix"
  787. . "wakeupDevice $wakeuptimerName";
  788. }
  789. elsif ( $wakeupDevice !~ /(.*,?)($wakeuptimerName)(.*,?)/ )
  790. {
  791. fhem "attr $name $prefix"
  792. . "wakeupDevice "
  793. . $wakeupDevice
  794. . ",$wakeuptimerName";
  795. }
  796. # trigger first update
  797. fhem "sleep 1;set $wakeuptimerName nextRun OFF";
  798. $created = 1;
  799. }
  800. }
  801. return
  802. "Dummy $wakeuptimerName and other pending devices created and pre-configured.\nYou may edit Macro_$wakeuptimerName to define your wake-up actions\nand at_$wakeuptimerName for optional at-device adjustments.";
  803. }
  804. elsif ( $TYPE ne "RESIDENTS" && lc( $a[2] ) eq "locationmap" ) {
  805. my $locationmapName = $name . "_map";
  806. if ( IsDevice($locationmapName) ) {
  807. return
  808. "Device $locationmapName existing already, delete it first to have it re-created.";
  809. }
  810. else {
  811. my $sortby = AttrVal( $name, "sortby", -1 );
  812. $sortby++;
  813. # create new weblink device
  814. fhem "define $locationmapName weblink htmlCode {
  815. '<ul style=\"width: 400px;; overflow: hidden;; height: 300px;;\">
  816. <iframe name=\"$locationmapName\" src=\"https://www.google.com/maps/embed/v1/place?key=AIzaSyB66DvcpbXJ5eWgIkzxpUN2s_9l3_6fegM&q='
  817. .ReadingsVal('$name','locationLat','')
  818. .','
  819. .ReadingsVal('$name','locationLong','')
  820. .'&zoom=13\" width=\"480\" height=\"480\" frameborder=\"0\" style=\"border:0;; margin-top: -165px;; margin-left: -135px;;\">
  821. </iframe>
  822. </ul>'
  823. }";
  824. fhem "attr $locationmapName alias Current Location";
  825. fhem
  826. "attr $locationmapName comment Auto-created by $TYPE module";
  827. fhem "attr $locationmapName group " . $attr{$name}{group}
  828. if ( defined( $attr{$name}{group} ) );
  829. fhem "attr $locationmapName room " . $attr{$name}{room}
  830. if ( defined( $attr{$name}{room} ) );
  831. }
  832. return "Weblink device $locationmapName was created.";
  833. }
  834. }
  835. # return usage hint
  836. else {
  837. return $usage;
  838. }
  839. return undef;
  840. }
  841. sub RESIDENTStk_Attr(@) {
  842. my ( $cmd, $name, $attribute, $value ) = @_;
  843. my $hash = $defs{$name};
  844. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  845. if ( $attribute eq $prefix . "wakeupDevice"
  846. || $attribute eq $prefix . "presenceDevices" )
  847. {
  848. return "Value for $attribute has invalid format"
  849. unless ( $cmd eq "del"
  850. || $value =~
  851. m/^([a-zA-Z\d._]+(:[A-Za-z\d_\.\-\/]+)?,?)([a-zA-Z\d._]+(:[A-Za-z\d_\.\-\/]+)?,?)*$/
  852. );
  853. }
  854. elsif ( !$init_done ) {
  855. return undef;
  856. }
  857. elsif ( $attribute eq "disable" ) {
  858. if ( $value and $value == 1 ) {
  859. $hash->{STATE} = "disabled";
  860. RESIDENTStk_StopInternalTimers($hash);
  861. }
  862. elsif ( $cmd eq "del" or !$value ) {
  863. evalStateFormat($hash);
  864. RESIDENTStk_StartInternalTimers( $hash, 1 );
  865. }
  866. }
  867. elsif ( $prefix ne "rgr_" && $attribute eq $prefix . "autoGoneAfter" ) {
  868. if ($value) {
  869. RESIDENTStk_AutoGone($hash);
  870. }
  871. elsif ( !$value ) {
  872. delete $hash->{AUTOGONE} if ( $hash->{AUTOGONE} );
  873. RESIDENTStk_RemoveInternalTimer( "AutoGone", $hash );
  874. }
  875. }
  876. elsif ( $attribute eq $prefix . "noDuration" ) {
  877. if ($value) {
  878. delete $hash->{DURATIONTIMER} if ( $hash->{DURATIONTIMER} );
  879. RESIDENTStk_RemoveInternalTimer( "DurationTimer", $hash );
  880. }
  881. elsif ( !$value ) {
  882. RESIDENTStk_DurationTimer($hash);
  883. }
  884. }
  885. elsif ( $attribute eq $prefix . "lang" ) {
  886. my $lang =
  887. $cmd eq "set" ? uc($value) : AttrVal( "global", "language", "EN" );
  888. # for initial define, ensure fallback to EN
  889. $lang = "EN"
  890. if ( $cmd eq "init" && $lang !~ /^EN|DE$/i );
  891. if ( $lang eq "DE" ) {
  892. if ( $prefix eq "rgr_" ) {
  893. $attr{$name}{alias} = "Bewohner"
  894. if ( !defined( $attr{$name}{alias} )
  895. || $attr{$name}{alias} eq "Residents" );
  896. $attr{$name}{group} = "Zuhause Status"
  897. if ( !defined( $attr{$name}{group} )
  898. || $attr{$name}{group} eq "Home State" );
  899. }
  900. $attr{$name}{devStateIcon} =
  901. '.*zuhause:user_available:absent '
  902. . '.*anwesend:user_available:absent .*abwesend:user_away:home .*verreist:user_ext_away:home .*bettfertig:scene_toilet:asleep .*schlaeft:scene_sleeping:awoken .*schläft:scene_sleeping:awoken .*aufgestanden:scene_sleeping_alternat:home .*:user_unknown:home';
  903. $attr{$name}{eventMap} =
  904. "home:zuhause absent:abwesend gone:verreist "
  905. . "gotosleep:bettfertig asleep:schläft awoken:aufgestanden";
  906. $attr{$name}{widgetOverride} =
  907. "state:zuhause,bettfertig,schläft,"
  908. . "aufgestanden,abwesend,verreist";
  909. }
  910. elsif ( $lang eq "EN" ) {
  911. if ( $prefix eq "rgr_" ) {
  912. $attr{$name}{alias} = "Residents"
  913. if ( !defined( $attr{$name}{alias} )
  914. || $attr{$name}{alias} eq "Bewohner" );
  915. $attr{$name}{group} = "Home State"
  916. if ( !defined( $attr{$name}{group} )
  917. || $attr{$name}{group} eq "Zuhause Status" );
  918. }
  919. $attr{$name}{devStateIcon} =
  920. '.*home:user_available:absent .*absent:user_away:home '
  921. . '.*gone:user_ext_away:home .*gotosleep:scene_toilet:asleep .*asleep:scene_sleeping:awoken .*awoken:scene_sleeping_alternat:home .*:user_unknown:home';
  922. delete $attr{$name}{eventMap}
  923. if ( defined( $attr{$name}{eventMap} ) );
  924. delete $attr{$name}{widgetOverride}
  925. if ( defined( $attr{$name}{widgetOverride} ) );
  926. }
  927. else {
  928. return "Unsupported language $lang";
  929. }
  930. evalStateFormat($hash);
  931. }
  932. return undef;
  933. }
  934. sub RESIDENTStk_Notify($$) {
  935. my ( $hash, $dev ) = @_;
  936. my $name = $hash->{NAME};
  937. my $TYPE = GetType($name);
  938. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  939. my $devName = $dev->{NAME};
  940. my $devPrefix = RESIDENTStk_GetPrefixFromType($devName);
  941. my $devType = GetType($devName);
  942. if ( $devName eq "global" ) {
  943. my $events = deviceEvents( $dev, 1 );
  944. return "" unless ($events);
  945. foreach ( @{$events} ) {
  946. next if ( $_ =~ m/^[A-Za-z\d_-]+:/ );
  947. # module and device initialization
  948. if ( $_ =~ m/^INITIALIZED|REREADCFG$/ ) {
  949. if ( !defined( &{'DoInitDev'} ) ) {
  950. if ( $_ eq "REREADCFG" ) {
  951. delete $modules{$TYPE}{READY};
  952. delete $modules{$TYPE}{INIT};
  953. }
  954. RESIDENTStk_DoInitDev(
  955. devspec2array("TYPE=$TYPE:FILTER=MOD_INIT=.+") );
  956. }
  957. }
  958. # if any of our monitored devices was modified,
  959. # recalculate monitoring status
  960. elsif ( $_ =~
  961. m/^(DEFINED|MODIFIED|RENAMED|DELETED)\s+([A-Za-z\d_-]+)$/ )
  962. {
  963. if ( defined( &{'DoInitDev'} ) ) {
  964. # DELETED would normally be handled by fhem.pl and imply
  965. # DoModuleTrigger instead of DoInitDev to update module
  966. # init state
  967. next if ( $_ =~ /^DELETED/ );
  968. DoInitDev($name);
  969. }
  970. else {
  971. # for DELETED, we normally would want to use
  972. # DoModuleTrigger() but we miss the deleted
  973. # device's TYPE at this state :-(
  974. RESIDENTStk_DoInitDev($name);
  975. }
  976. }
  977. # only process attribute events
  978. next
  979. unless ( $_ =~
  980. m/^((?:DELETE)?ATTR)\s+([A-Za-z\d._]+)\s+([A-Za-z\d_\.\-\/]+)(?:\s+(.*)\s*)?$/
  981. );
  982. my $cmd = $1;
  983. my $d = $2;
  984. my $attr = $3;
  985. my $val = $4;
  986. my $type = GetType($d);
  987. # filter attributes to be processed
  988. next
  989. unless ( $attr eq $prefix . "wakeupDevice"
  990. || $attr eq $prefix . "presenceDevices"
  991. || $attr eq $prefix . "wakeupResetSwitcher" );
  992. # when attributes of RESIDENTS, ROOMMATE or GUEST were changed
  993. if ( $d eq $name ) {
  994. if ( defined( &{'DoInitDev'} ) ) {
  995. DoInitDev($name)
  996. if ( $TYPE ne "dummy" );
  997. }
  998. else {
  999. RESIDENTStk_DoInitDev($name)
  1000. if ( $TYPE ne "dummy" );
  1001. }
  1002. return "";
  1003. }
  1004. # when slave attributes where changed
  1005. elsif ( $hash->{NOTIFYDEV} ) {
  1006. my @ndlarr = devspec2array( $hash->{NOTIFYDEV} );
  1007. return "" unless ( grep( $_ eq $d, @ndlarr ) );
  1008. # wakeupResetSwitcher
  1009. if ( $attr eq "wakeupResetSwitcher" ) {
  1010. if ( $cmd eq "ATTR" ) {
  1011. if ( !IsDevice($val) ) {
  1012. my $alias = AttrVal( $d, "alias", undef );
  1013. my $group = AttrVal( $d, "group", undef );
  1014. my $room = AttrVal( $d, "room", undef );
  1015. fhem "define $val dummy";
  1016. $attr{$val}{comment} =
  1017. "Auto-created by RESIDENTS Toolkit:"
  1018. . " easy switch between on/off for auto time reset of wake-up timer $d";
  1019. if ($alias) {
  1020. $attr{$val}{alias} = "$alias Reset";
  1021. }
  1022. else {
  1023. $attr{$val}{alias} = "Wake-up Timer Reset";
  1024. }
  1025. $attr{$val}{devStateIcon} =
  1026. "auto:time_automatic:off "
  1027. . "off:time_manual_mode:auto";
  1028. $attr{$val}{group} = "$group"
  1029. if ($group);
  1030. $attr{$val}{icon} = "refresh";
  1031. $attr{$val}{room} = "$room"
  1032. if ($room);
  1033. $attr{$val}{setList} = "state:auto,off";
  1034. $attr{$val}{webCmd} = "state";
  1035. fhem "set $val auto";
  1036. Log3 $d, 3,
  1037. "RESIDENTStk $d: "
  1038. . "new slave dummy device $val created";
  1039. }
  1040. }
  1041. if ( defined( &{'DoInitDev'} ) ) {
  1042. DoInitDev($name);
  1043. }
  1044. else {
  1045. RESIDENTStk_DoInitDev($name);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. return "";
  1051. }
  1052. return "" if ( IsDisabled($name) or IsDisabled($devName) );
  1053. # process events from ROOMMATE or GUEST devices
  1054. # only when they hit RESIDENTS devices
  1055. if ( $TYPE eq "RESIDENTS" && $devType =~ /^ROOMMATE|GUEST$/ ) {
  1056. my $events = deviceEvents( $dev, 1 );
  1057. return "" unless ($events);
  1058. readingsBeginUpdate($hash);
  1059. foreach my $event ( @{$events} ) {
  1060. next unless ( defined($event) );
  1061. # state changed
  1062. if ( $event !~ /^[a-zA-Z\d._]+:/
  1063. || $event =~ /^state:/
  1064. || $event =~ /^wayhome:/
  1065. || $event =~ /^wakeup:/ )
  1066. {
  1067. RESIDENTS_UpdateReadings($hash);
  1068. }
  1069. # activity
  1070. if ( $event !~ /^[a-zA-Z\d._]+:/ || $event =~ /^state:/ ) {
  1071. # get user realname
  1072. my $realname =
  1073. AttrVal( $devName,
  1074. AttrVal( $devName, $devPrefix . "realname", "group" ),
  1075. $devName );
  1076. # update statistics
  1077. readingsBulkUpdate( $hash, "lastActivity",
  1078. ReadingsVal( $devName, "state", $event ) );
  1079. readingsBulkUpdate( $hash, "lastActivityBy", $realname );
  1080. readingsBulkUpdate( $hash, "lastActivityByDev", $devName );
  1081. }
  1082. }
  1083. readingsEndUpdate( $hash, 1 );
  1084. return "";
  1085. }
  1086. delete $dev->{CHANGEDWITHSTATE};
  1087. my $events = deviceEvents( $dev, 1 );
  1088. return "" unless ($events);
  1089. my @registeredWakeupdevs =
  1090. split( ',', AttrVal( $name, $prefix . "wakeupDevice", "" ) );
  1091. my @presenceDevices =
  1092. split( ',', AttrVal( $name, $prefix . "presenceDevices", "" ) );
  1093. foreach my $event ( @{$events} ) {
  1094. next unless ( defined($event) );
  1095. my $found = 0;
  1096. # process wakeup devices
  1097. if (@registeredWakeupdevs) {
  1098. # if this is an event of a registered wakeup device
  1099. if ( grep { m/^$devName$/ } @registeredWakeupdevs ) {
  1100. RESIDENTStk_wakeupSet( $devName, $event );
  1101. next;
  1102. }
  1103. # process sub-child events: *_wakeupDevice
  1104. foreach my $wakeupDev (@registeredWakeupdevs) {
  1105. # if this is an event of a registered sub dummy device
  1106. # of one of our wakeup devices
  1107. if ( AttrVal( $wakeupDev, "wakeupResetSwitcher", "" ) eq
  1108. $devName
  1109. && IsDevice( $devName, "dummy" ) )
  1110. {
  1111. RESIDENTStk_wakeupSet( $wakeupDev, $event )
  1112. unless ( $event =~ /^(?:state:\s*)?off$/i );
  1113. $found = 1;
  1114. last;
  1115. }
  1116. }
  1117. next if ($found);
  1118. }
  1119. # process PRESENCE
  1120. if ( @presenceDevices
  1121. && ( grep { /^$devName(:[A-Za-z\d_\.\-\/]+)?$/ } @presenceDevices )
  1122. && $event =~ /^(?:([A-Za-z\d_\.\-\/]+): )?(.+)$/ )
  1123. {
  1124. my $reading = $1;
  1125. my $value = $2;
  1126. # early exit if unexpected event value
  1127. next
  1128. unless ( $value =~
  1129. m/^0|false|absent|disappeared|unavailable|unreachable|disconnected|1|true|present|appeared|available|reachable|connected$/i
  1130. );
  1131. my $counter = {
  1132. absent => 0,
  1133. present => 0,
  1134. };
  1135. for (@presenceDevices) {
  1136. my $r = "presence";
  1137. my $d = $_;
  1138. if ( $d =~ m/^([a-zA-Z\d._]+):(?:([A-Za-z\d_\.\-\/]*))?$/ ) {
  1139. $d = $1;
  1140. $r = $2 if ( $2 && $2 ne "" );
  1141. }
  1142. my $presenceState =
  1143. ReadingsVal( $d, $r, ReadingsVal( $d, "state", "" ) );
  1144. # ignore device if it has unexpected state
  1145. next
  1146. unless ( $presenceState =~
  1147. m/^(0|false|absent|disappeared|unavailable|unreachable|disconnected)|(1|true|present|appeared|available|reachable|connected|)$/i
  1148. );
  1149. $counter->{absent}++ if ( defined($1) );
  1150. $counter->{present}++ if ( defined($2) );
  1151. }
  1152. if ( $counter->{absent} && !$counter->{present} ) {
  1153. Log3 $name, 4,
  1154. "$TYPE $name: " . "Syncing status with $devName = absent";
  1155. fhem "set $name:FILTER=presence=present absent";
  1156. }
  1157. elsif ( $counter->{present} ) {
  1158. Log3 $name, 4,
  1159. "$TYPE $name: " . "Syncing status with $devName = present";
  1160. fhem "set $name:FILTER=presence=absent home";
  1161. }
  1162. }
  1163. }
  1164. return "";
  1165. }
  1166. # module Fn ####################################################################
  1167. sub RESIDENTStk_AutoGone($;$) {
  1168. my ( $mHash, @a ) = @_;
  1169. my $hash = ( $mHash->{HASH} ) ? $mHash->{HASH} : $mHash;
  1170. my $name = $hash->{NAME};
  1171. my $TYPE = GetType($name);
  1172. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  1173. my $autoGoneAfter = AttrVal(
  1174. $hash->{NAME},
  1175. $prefix . "autoGoneAfter",
  1176. ( $prefix eq "rr_" ? 36 : 16 )
  1177. );
  1178. delete $hash->{AUTOGONE} if ( $hash->{AUTOGONE} );
  1179. RESIDENTStk_RemoveInternalTimer( "AutoGone", $hash );
  1180. return if ( IsDisabled($name) || !$autoGoneAfter );
  1181. if ( ReadingsVal( $name, "state", "home" ) eq "absent" ) {
  1182. my ( $date, $time, $y, $m, $d,
  1183. $hour, $min, $sec, $timestamp, $timeDiff );
  1184. my $timestampNow = gettimeofday();
  1185. ( $date, $time ) = split( ' ', $hash->{READINGS}{state}{TIME} );
  1186. ( $y, $m, $d ) = split( '-', $date );
  1187. ( $hour, $min, $sec ) = split( ':', $time );
  1188. $m -= 01;
  1189. $timestamp = timelocal( $sec, $min, $hour, $d, $m, $y );
  1190. $timeDiff = $timestampNow - $timestamp;
  1191. if ( $timeDiff >= $autoGoneAfter * 3600 ) {
  1192. Log3 $name, 3,
  1193. "$TYPE $name: AutoGone timer changed state to 'gone'";
  1194. RESIDENTStk_Set( $hash, $name, "silentSet", "state", "gone" );
  1195. }
  1196. else {
  1197. my $runtime = $timestamp + $autoGoneAfter * 3600;
  1198. $hash->{AUTOGONE} = $runtime;
  1199. Log3 $name, 4, "$TYPE $name: AutoGone timer scheduled: $runtime";
  1200. RESIDENTStk_InternalTimer( "AutoGone", $runtime,
  1201. "RESIDENTStk_AutoGone", $hash, 1 );
  1202. }
  1203. }
  1204. return undef;
  1205. }
  1206. sub RESIDENTStk_DurationTimer($;$) {
  1207. my ( $mHash, @a ) = @_;
  1208. my $hash = ( $mHash->{HASH} ) ? $mHash->{HASH} : $mHash;
  1209. my $name = $hash->{NAME};
  1210. my $TYPE = GetType($name);
  1211. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  1212. my $silent = ( defined( $a[0] ) && $a[0] eq "1" ) ? 1 : 0;
  1213. my $timestampNow = gettimeofday();
  1214. my $diff;
  1215. my $durPresence = "0";
  1216. my $durAbsence = "0";
  1217. my $durSleep = "0";
  1218. my $noDuration = AttrVal( $name, $prefix . "noDuration", 0 );
  1219. delete $hash->{DURATIONTIMER} if ( $hash->{DURATIONTIMER} );
  1220. RESIDENTStk_RemoveInternalTimer( "DurationTimer", $hash );
  1221. return if ( IsDisabled($name) || $noDuration );
  1222. # presence timer
  1223. if ( ReadingsVal( $name, "presence", "absent" ) eq "present"
  1224. && ReadingsVal( $name, "lastArrival", "-" ) ne "-" )
  1225. {
  1226. $durPresence =
  1227. $timestampNow -
  1228. time_str2num( ReadingsVal( $name, "lastArrival", "" ) );
  1229. }
  1230. # absence timer
  1231. if ( ReadingsVal( $name, "presence", "present" ) eq "absent"
  1232. && ReadingsVal( $name, "lastDeparture", "-" ) ne "-" )
  1233. {
  1234. $durAbsence =
  1235. $timestampNow -
  1236. time_str2num( ReadingsVal( $name, "lastDeparture", "" ) );
  1237. }
  1238. # sleep timer
  1239. if ( ReadingsVal( $name, "state", "home" ) eq "asleep"
  1240. && ReadingsVal( $name, "lastSleep", "-" ) ne "-" )
  1241. {
  1242. $durSleep =
  1243. $timestampNow - time_str2num( ReadingsVal( $name, "lastSleep", "" ) );
  1244. }
  1245. my $durPresence_hr =
  1246. ( $durPresence > 0 )
  1247. ? UConv::s2hms($durPresence)
  1248. : "00:00:00";
  1249. my $durPresence_cr =
  1250. ( $durPresence > 60 ) ? int( $durPresence / 60 + 0.5 ) : 0;
  1251. my $durAbsence_hr =
  1252. ( $durAbsence > 0 ) ? UConv::s2hms($durAbsence) : "00:00:00";
  1253. my $durAbsence_cr =
  1254. ( $durAbsence > 60 ) ? int( $durAbsence / 60 + 0.5 ) : 0;
  1255. my $durSleep_hr = ( $durSleep > 0 ) ? UConv::s2hms($durSleep) : "00:00:00";
  1256. my $durSleep_cr = ( $durSleep > 60 ) ? int( $durSleep / 60 + 0.5 ) : 0;
  1257. readingsBeginUpdate($hash) if ( !$silent );
  1258. readingsBulkUpdateIfChanged( $hash, "durTimerPresence_cr",
  1259. $durPresence_cr );
  1260. readingsBulkUpdateIfChanged( $hash, "durTimerPresence", $durPresence_hr );
  1261. readingsBulkUpdateIfChanged( $hash, "durTimerAbsence_cr", $durAbsence_cr );
  1262. readingsBulkUpdateIfChanged( $hash, "durTimerAbsence", $durAbsence_hr );
  1263. readingsBulkUpdateIfChanged( $hash, "durTimerSleep_cr", $durSleep_cr );
  1264. readingsBulkUpdateIfChanged( $hash, "durTimerSleep", $durSleep_hr );
  1265. readingsEndUpdate( $hash, 1 ) if ( !$silent );
  1266. $hash->{DURATIONTIMER} = $timestampNow + 60;
  1267. RESIDENTStk_InternalTimer( "DurationTimer", $hash->{DURATIONTIMER},
  1268. "RESIDENTStk_DurationTimer", $hash, 1 );
  1269. return undef;
  1270. }
  1271. sub RESIDENTStk_SetLocation(@) {
  1272. my (
  1273. $name, $location, $trigger, $id,
  1274. $time, $lat, $long, $address,
  1275. $device, $radius, $posLat, $posLong,
  1276. $posAddress, $posBeaconUUID, $posDistHome, $posDistLoc,
  1277. $motion, $wifiSSID, $wifiBSSID
  1278. ) = @_;
  1279. my $hash = $defs{$name};
  1280. my $TYPE = GetType($name);
  1281. my $prefix = RESIDENTStk_GetPrefixFromType($name);
  1282. my $state = ReadingsVal( $name, "state", "initialized" );
  1283. my $presence = ReadingsVal( $name, "presence", "present" );
  1284. my $currLocation = ReadingsVal( $name, "location", "-" );
  1285. my $currWayhome = ReadingsVal( $name, "wayhome", "0" );
  1286. my $currLat = ReadingsVal( $name, "locationLat", "-" );
  1287. my $currLong = ReadingsVal( $name, "locationLong", "-" );
  1288. my $currRadius = ReadingsVal( $name, "locationRadius", "" );
  1289. my $currAddr = ReadingsVal( $name, "locationAddr", "" );
  1290. my $currPosLat = ReadingsVal( $name, "positionLat", "" );
  1291. my $currPosLong = ReadingsVal( $name, "positionLong", "" );
  1292. my $currPosAddr = ReadingsVal( $name, "positionAddr", "" );
  1293. my $currPosBeaconUUID = ReadingsVal( $name, "positionBeaconUUID", "" );
  1294. my $currPosDistHome = ReadingsVal( $name, "positionDistHome", "" );
  1295. my $currPosDistLoc = ReadingsVal( $name, "positionDistLocation", "" );
  1296. my $currPosMotion = ReadingsVal( $name, "positionMotion", "" );
  1297. my $currPosSSID = ReadingsVal( $name, "positionSSID", "" );
  1298. my $currPosBSSID = ReadingsVal( $name, "positionBSSID", "" );
  1299. $id = "-" if ( !$id || $id eq "" );
  1300. $lat = "-" if ( !$lat || $lat eq "" );
  1301. $long = "-" if ( !$long || $long eq "" );
  1302. $address = "" if ( !$address );
  1303. $time = "" if ( !$time );
  1304. $device = "" if ( !$device );
  1305. $posLat = "" if ( !$posLat || $posLat eq "-" );
  1306. $posLong = "" if ( !$posLong || $posLong eq "-" );
  1307. $posAddress = "-" if ( !$posAddress || $posAddress eq "" );
  1308. $posBeaconUUID = "" if ( !$posBeaconUUID || $posBeaconUUID eq "-" );
  1309. Log3 $name, 5,
  1310. "$TYPE $name: received location information: id=$id name=$location trig=$trigger date=$time lat=$lat long=$long posLat=$posLat posLong=$posLong address=$address device=$device";
  1311. my $searchstring;
  1312. readingsBeginUpdate($hash);
  1313. # read attributes
  1314. my @location_home =
  1315. split( ' ', AttrVal( $name, $prefix . "locationHome", "home" ) );
  1316. my @location_underway =
  1317. split( ' ', AttrVal( $name, $prefix . "locationUnderway", "underway" ) );
  1318. my @location_wayhome =
  1319. split( ' ', AttrVal( $name, $prefix . "locationWayhome", "wayhome" ) );
  1320. $searchstring = quotemeta($location);
  1321. # update locationPresence
  1322. # if ( $posBeaconUUID eq "" ) {
  1323. readingsBulkUpdate( $hash, "locationPresence", "present" )
  1324. if ( $trigger == 1 );
  1325. readingsBulkUpdate( $hash, "locationPresence", "absent" )
  1326. if ( $trigger == 0 );
  1327. # }
  1328. # # update positionPresence
  1329. # readingsBulkUpdate( $hash, "positionPresence", "present" )
  1330. # if ( $trigger == 1 );
  1331. # readingsBulkUpdate( $hash, "positionPresence", "absent" )
  1332. # if ( $trigger == 0 );
  1333. # travelled distance for location
  1334. my $locTravDist = "";
  1335. if ( $lat ne "" && $long ne "" ) {
  1336. my $locLatVal = ReadingsVal( $name, "locationLat", "-" );
  1337. $locLatVal = ReadingsVal( $name, "lastLocationLat", "-" )
  1338. if ( $locLatVal eq "-" );
  1339. my $locLongVal = ReadingsVal( $name, "locationLong", "-" );
  1340. $locLongVal = ReadingsVal( $name, "lastLocationLong", "-" )
  1341. if ( $locLongVal eq "-" );
  1342. if ( $locLatVal ne "-" && $locLongVal ne "-" ) {
  1343. $locTravDist =
  1344. UConv::distance( $lat, $long, $locLatVal, $locLongVal, 2 );
  1345. }
  1346. }
  1347. # travelled distance for position
  1348. my $posTravDist = "";
  1349. if ( $posLat ne "" && $posLong ne "" ) {
  1350. my $currPosLatVal = ReadingsVal( $name, "positionLat", "" );
  1351. my $currPosLongVal = ReadingsVal( $name, "positionLong", "" );
  1352. if ( $currPosLatVal ne "" && $currPosLongVal ne "" ) {
  1353. $posTravDist = UConv::distance( $posLat, $posLong, $currPosLatVal,
  1354. $currPosLongVal, 2 );
  1355. }
  1356. }
  1357. # backup last known position
  1358. foreach (
  1359. 'positionLat', 'positionLong',
  1360. 'positionAddr', 'positionBeaconUUID',
  1361. 'positionDistHome', 'positionDistLocation',
  1362. 'positionMotion', 'positionSSID',
  1363. 'positionBSSID', 'positionTravDistance',
  1364. 'locationTravDistance'
  1365. )
  1366. {
  1367. my $currReading = $_;
  1368. my $lastReading = "last" . ucfirst($_);
  1369. my $currVal = ReadingsVal( $name, $currReading, undef );
  1370. readingsBulkUpdate( $hash, $lastReading, $currVal )
  1371. if ( defined($currVal) );
  1372. }
  1373. # update position based readings
  1374. readingsBulkUpdate( $hash, "positionLat", $posLat );
  1375. readingsBulkUpdate( $hash, "positionLong", $posLong );
  1376. readingsBulkUpdate( $hash, "positionAddr", $posAddress );
  1377. readingsBulkUpdate( $hash, "positionBeaconUUID", $posBeaconUUID );
  1378. readingsBulkUpdate( $hash, "positionDistHome", $posDistHome );
  1379. readingsBulkUpdate( $hash, "positionDistLocation", $posDistLoc );
  1380. readingsBulkUpdate( $hash, "positionMotion", $motion );
  1381. readingsBulkUpdate( $hash, "positionSSID", $wifiSSID );
  1382. readingsBulkUpdate( $hash, "positionBSSID", $wifiBSSID );
  1383. readingsBulkUpdate( $hash, "positionTravDistance", $posTravDist );
  1384. readingsBulkUpdate( $hash, "locationTravDistance", $locTravDist );
  1385. # check for implicit state change
  1386. #
  1387. my $stateChange = 0;
  1388. # home
  1389. if ( $location eq "home" || grep( m/^$searchstring$/, @location_home ) ) {
  1390. Log3 $name, 5, "$TYPE $name: received signal from home location";
  1391. # home
  1392. if ( $state ne "home"
  1393. && $state ne "gotosleep"
  1394. && $state ne "asleep"
  1395. && $state ne "awoken"
  1396. && $trigger eq "1" )
  1397. {
  1398. $stateChange = 1;
  1399. }
  1400. # absent
  1401. elsif ($state ne "gone"
  1402. && $state ne "none"
  1403. && $state ne "absent"
  1404. && $trigger eq "0" )
  1405. {
  1406. $stateChange = 2;
  1407. }
  1408. }
  1409. # underway
  1410. elsif ($location eq "underway"
  1411. || $location eq "wayhome"
  1412. || grep( m/^$searchstring$/, @location_underway )
  1413. || grep( m/^$searchstring$/, @location_wayhome ) )
  1414. {
  1415. Log3 $name, 5, "$TYPE $name: received signal from underway location";
  1416. # absent
  1417. $stateChange = 2
  1418. if ( $state ne "gone"
  1419. && $state ne "none"
  1420. && $state ne "absent" );
  1421. }
  1422. # wayhome
  1423. if (
  1424. $location eq "wayhome"
  1425. || ( grep( m/^$searchstring$/, @location_wayhome )
  1426. && $trigger eq "0" )
  1427. )
  1428. {
  1429. Log3 $name, 5, "$TYPE $name: wayhome signal received";
  1430. # wayhome=true
  1431. if (
  1432. (
  1433. ( $location eq "wayhome" && $trigger eq "1" )
  1434. || ( $location ne "wayhome" && $trigger eq "0" )
  1435. )
  1436. && ReadingsVal( $name, "wayhome", "0" ) ne "1"
  1437. )
  1438. {
  1439. Log3 $name, 3, "$TYPE $name: on way back home from $location";
  1440. readingsBulkUpdate( $hash, "wayhome", "1" );
  1441. }
  1442. # wayhome=false
  1443. elsif ($location eq "wayhome"
  1444. && $trigger eq "0"
  1445. && ReadingsVal( $name, "wayhome", "0" ) ne "0" )
  1446. {
  1447. Log3 $name, 3,
  1448. "$TYPE $name: seems not to be on way back home anymore";
  1449. readingsBulkUpdate( $hash, "wayhome", "0" );
  1450. }
  1451. }
  1452. # activate wayhome tracing when reaching another location while wayhome=1
  1453. elsif ( $stateChange == 0 && $trigger == 1 && $currWayhome == 1 ) {
  1454. Log3 $name, 3,
  1455. "$TYPE $name: seems to stay at $location before coming home";
  1456. readingsBulkUpdate( $hash, "wayhome", "2" );
  1457. }
  1458. # revert wayhome during active wayhome tracing
  1459. elsif ( $stateChange == 0 && $trigger == 0 && $currWayhome == 2 ) {
  1460. Log3 $name, 3, "$TYPE $name: finally on way back home from $location";
  1461. readingsBulkUpdate( $hash, "wayhome", "1" );
  1462. }
  1463. my $currLongDiff = 0;
  1464. my $currLatDiff = 0;
  1465. $currLongDiff =
  1466. maxNum( ReadingsVal( $name, "lastLocationLong", 0 ), $currLong ) -
  1467. minNum( ReadingsVal( $name, "lastLocationLong", 0 ), $currLong )
  1468. if ( $currLong ne "-" );
  1469. $currLatDiff =
  1470. maxNum( ReadingsVal( $name, "lastLocationLat", 0 ), $currLat ) -
  1471. minNum( ReadingsVal( $name, "lastLocationLat", 0 ), $currLat )
  1472. if ( $currLat ne "-" );
  1473. if (
  1474. $trigger == 1
  1475. && ( $stateChange > 0
  1476. || ReadingsVal( $name, "lastLocation", "-" ) ne $currLocation
  1477. || $currLongDiff > 0.00002
  1478. || $currLatDiff > 0.00002 )
  1479. )
  1480. {
  1481. Log3 $name, 5, "$TYPE $name: archiving last known location";
  1482. readingsBulkUpdate( $hash, "lastLocationLat", $currLat );
  1483. readingsBulkUpdate( $hash, "lastLocationLong", $currLong );
  1484. readingsBulkUpdate( $hash, "lastLocationRadius", $currRadius );
  1485. readingsBulkUpdate( $hash, "lastLocationAddr", $currAddr )
  1486. if ( $currAddr ne "" );
  1487. readingsBulkUpdate( $hash, "lastLocation", $currLocation );
  1488. }
  1489. readingsBulkUpdate( $hash, "locationLat", $lat );
  1490. readingsBulkUpdate( $hash, "locationLong", $long );
  1491. readingsBulkUpdate( $hash, "locationRadius", $radius );
  1492. if ( $address ne "" ) {
  1493. readingsBulkUpdate( $hash, "locationAddr", $address );
  1494. }
  1495. elsif ( $currAddr ne "" ) {
  1496. readingsBulkUpdate( $hash, "locationAddr", "-" );
  1497. }
  1498. readingsBulkUpdate( $hash, "location", $location );
  1499. readingsEndUpdate( $hash, 1 );
  1500. # trigger state change
  1501. if ( $stateChange > 0 ) {
  1502. Log3 $name, 4,
  1503. "$TYPE $name: implicit state change caused by location " . $location;
  1504. RESIDENTStk_Set( $hash, $name, "silentSet", "state", "home" )
  1505. if $stateChange == 1;
  1506. RESIDENTStk_Set( $hash, $name, "silentSet", "state", "absent" )
  1507. if $stateChange == 2;
  1508. }
  1509. }
  1510. sub RESIDENTStk_wakeupSet($$) {
  1511. my ( $NAME, $n ) = @_;
  1512. my ( $a, $h ) = parseParams($n);
  1513. my $cmd = shift @$a;
  1514. my $VALUE = join( " ", @$a );
  1515. $cmd =~ s/^state:\s*(.*)$/$1/;
  1516. return if ( $cmd =~ /^[A-Za-z]+:/ );
  1517. # filter non-registered events
  1518. if ( $cmd !~
  1519. m/^((?:next[rR]un)?\s*(off|OFF|([\+\-])?(([0-9]{2}):([0-9]{2})|([1-9]+[0-9]*)))?|trigger|start|stop|end|reset|auto|wakeupResetdays|wakeupDays|wakeupHolidays|wakeupEnforced|wakeupDefaultTime|wakeupOffset)$/i
  1520. )
  1521. {
  1522. Log3 $NAME, 6,
  1523. "RESIDENTStk $NAME: "
  1524. . "received unspecified event '$cmd' - nothing to do";
  1525. return;
  1526. }
  1527. my $wakeupMacro = AttrVal( $NAME, "wakeupMacro", 0 );
  1528. my $wakeupDefaultTime =
  1529. ReadingsVal( $NAME, "wakeupDefaultTime",
  1530. AttrVal( $NAME, "wakeupDefaultTime", 0 ) );
  1531. my $wakeupAtdevice = AttrVal( $NAME, "wakeupAtdevice", 0 );
  1532. my $wakeupUserdevice = AttrVal( $NAME, "wakeupUserdevice", 0 );
  1533. my $wakeupDays =
  1534. ReadingsVal( $NAME, "wakeupDays", AttrVal( $NAME, "wakeupDays", "" ) );
  1535. my $wakeupHolidays =
  1536. ReadingsVal( $NAME, "wakeupHolidays",
  1537. AttrVal( $NAME, "wakeupHolidays", "" ) );
  1538. my $wakeupResetdays =
  1539. ReadingsVal( $NAME, "wakeupResetdays",
  1540. AttrVal( $NAME, "wakeupResetdays", "" ) );
  1541. my $wakeupOffset =
  1542. ReadingsVal( $NAME, "wakeupOffset", AttrVal( $NAME, "wakeupOffset", 0 ) );
  1543. my $wakeupEnforced =
  1544. ReadingsVal( $NAME, "wakeupEnforced",
  1545. AttrVal( $NAME, "wakeupEnforced", 0 ) );
  1546. my $wakeupResetSwitcher = AttrVal( $NAME, "wakeupResetSwitcher", 0 );
  1547. my $holidayDevice = AttrVal( "global", "holiday2we", 0 );
  1548. my $room = AttrVal( $NAME, "room", 0 );
  1549. my $userattr = AttrVal( $NAME, "userattr", 0 );
  1550. my $lastRun = ReadingsVal( $NAME, "lastRun", "07:00" );
  1551. my $nextRun = ReadingsVal( $NAME, "nextRun", "07:00" );
  1552. my $running = ReadingsVal( $NAME, "running", 0 );
  1553. my $wakeupUserdeviceState = ReadingsVal( $wakeupUserdevice, "state", 0 );
  1554. my $atName = "at_" . $NAME;
  1555. my $wdNameGotosleep = "wd_" . $wakeupUserdevice . "_gotosleep";
  1556. my $wdNameAsleep = "wd_" . $wakeupUserdevice . "_asleep";
  1557. my $wdNameAwoken = "wd_" . $wakeupUserdevice . "_awoken";
  1558. my $macroName = "Macro_" . $NAME;
  1559. my $macroNameGotosleep = "Macro_" . $wakeupUserdevice . "_gotosleep";
  1560. my $macroNameAsleep = "Macro_" . $wakeupUserdevice . "_asleep";
  1561. my $macroNameAwoken = "Macro_" . $wakeupUserdevice . "_awoken";
  1562. my $TYPE = GetType($wakeupUserdevice);
  1563. my $prefix = RESIDENTStk_GetPrefixFromType($wakeupUserdevice);
  1564. my $wakeupUserdeviceRealname = "Bewohner";
  1565. if ( $TYPE eq "RESIDENTS" ) {
  1566. $wakeupUserdeviceRealname = AttrVal(
  1567. AttrVal( $NAME, "wakeupUserdevice", "" ),
  1568. AttrVal(
  1569. AttrVal( $NAME, "wakeupUserdevice", "" ),
  1570. $prefix . "realname", "alias"
  1571. ),
  1572. $wakeupUserdeviceRealname
  1573. );
  1574. }
  1575. else {
  1576. $wakeupUserdeviceRealname = AttrVal(
  1577. AttrVal( $NAME, "wakeupUserdevice", "" ),
  1578. AttrVal(
  1579. AttrVal( $NAME, "wakeupUserdevice", "" ),
  1580. $prefix . "realname", "group"
  1581. ),
  1582. $wakeupUserdeviceRealname
  1583. );
  1584. }
  1585. # check for required userattr attribute
  1586. my $userattributes =
  1587. "wakeupOffset:slider,0,1,120 wakeupDefaultTime:OFF,00:00,00:15,00:30,00:45,01:00,01:15,01:30,01:45,02:00,02:15,02:30,02:45,03:00,03:15,03:30,03:45,04:00,04:15,04:30,04:45,05:00,05:15,05:30,05:45,06:00,06:15,06:30,06:45,07:00,07:15,07:30,07:45,08:00,08:15,08:30,08:45,09:00,09:15,09:30,09:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45 wakeupMacro wakeupUserdevice wakeupAtdevice wakeupResetSwitcher wakeupResetdays:multiple-strict,0,1,2,3,4,5,6 wakeupDays:multiple-strict,0,1,2,3,4,5,6 wakeupHolidays:andHoliday,orHoliday,andNoHoliday,orNoHoliday wakeupEnforced:0,1,2,3 wakeupWaitPeriod:slider,0,1,360";
  1588. if ( !$userattr || $userattr ne $userattributes ) {
  1589. Log3 $NAME, 4,
  1590. "RESIDENTStk $NAME: "
  1591. . "adjusting dummy device for required attribute userattr";
  1592. fhem "attr $NAME userattr $userattributes";
  1593. }
  1594. # check for required userdevice attribute
  1595. if ( !$wakeupUserdevice ) {
  1596. Log3 $NAME, 3,
  1597. "RESIDENTStk $NAME: "
  1598. . "WARNING - set attribute wakeupUserdevice before running wakeup function!";
  1599. }
  1600. elsif ( !IsDevice($wakeupUserdevice) ) {
  1601. Log3 $NAME, 3,
  1602. "RESIDENTStk $NAME: "
  1603. . "WARNING - user device $wakeupUserdevice does not exist!";
  1604. }
  1605. elsif ( !IsDevice( $wakeupUserdevice, "RESIDENTS|ROOMMATE|GUEST" ) ) {
  1606. Log3 $NAME, 3,
  1607. "RESIDENTStk $NAME: "
  1608. . "WARNING - defined user device '$wakeupUserdevice' is not a RESIDENTS, ROOMMATE or GUEST device!";
  1609. }
  1610. # check for required wakeupMacro attribute
  1611. if ( !$wakeupMacro ) {
  1612. Log3 $NAME, 4,
  1613. "RESIDENTStk $NAME: "
  1614. . "adjusting dummy device for required attribute wakeupMacro";
  1615. fhem "attr $NAME wakeupMacro $macroName";
  1616. $wakeupMacro = $macroName;
  1617. }
  1618. if ( !IsDevice($wakeupMacro) ) {
  1619. my $wakeUpMacroTemplate = "{\
  1620. ##=============================================================================\
  1621. ## This is an example wake-up program running within a period of 30 minutes:\
  1622. ## - drive shutters upwards slowly\
  1623. ## - light up a HUE bulb from 2000K to 5600K\
  1624. ## - have some voice notifications via SONOS\
  1625. ## - have some wake-up chill music via SONOS during program run\
  1626. ##\
  1627. ## Actual FHEM commands are commented out by default as they would need\
  1628. ## to be adapted to your configuration.\
  1629. ##\
  1630. ## Available wake-up variables:\
  1631. ## 1. \$EVTPART0 -> start or stop\
  1632. ## 2. \$EVTPART1 -> target wake-up time\
  1633. ## 3. \$EVTPART2 -> wake-up begin time considering wakeupOffset attribute\
  1634. ## 4. \$EVTPART3 -> enforced wakeup yes=1,no=0 from wakeupEnforced attribute\
  1635. ## 5. \$EVTPART4 -> device name of the user which called this macro\
  1636. ## 6. \$EVTPART5 -> current state of user\
  1637. ##=============================================================================\
  1638. \
  1639. ##-----------------------------------------------------------------------------\
  1640. ## DELETE TEMP. AT-COMMANDS POTENTIALLY CREATED EARLIER BY THIS SCRIPT\
  1641. ## Executed for start to cleanup in case this wake-up automation is re-started.\
  1642. ## Executed for stop to cleanup in case the user ends this automation earlier.\
  1643. ##\
  1644. fhem \"delete atTmp_.*_\".\$NAME.\":FILTER=TYPE=at\";;\
  1645. \
  1646. ##-----------------------------------------------------------------------------\
  1647. ## BEGIN WAKE-UP PROGRAM\
  1648. ## Run first automation commands and create temp. at-devices for lagging actions.\
  1649. ##\
  1650. if (\$EVTPART0 eq \"start\") {\
  1651. Log3 \$NAME, 3, \"\$NAME: Wake-up program started for \$EVTPART4 with target time \$EVTPART1. Current state: \$EVTPART5\";;\
  1652. \
  1653. # fhem \"set BR_FloorLamp:FILTER=onoff=0 pct 1 : ct 2000 : transitiontime 0;; set BR_FloorLamp:FILTER=pct=1 pct 90 : ct 5600 : transitiontime 17700\";;\
  1654. \
  1655. # fhem \"define atTmp_1_\$NAME at +00:10:00 set BR_Shutter:FILTER=pct<20 pct 20\";;\
  1656. # fhem \"define atTmp_2_\$NAME at +00:20:00 set BR_Shutter:FILTER=pct<40 pct 40\";;\
  1657. # fhem \"define atTmp_4_\$NAME at +00:30:00 msg audio \\\@Sonos_Bedroom |Hint| Es ist \".\$EVTPART1.\" Uhr, Zeit zum aufstehen!;;;; set BR_FloorLamp:FILTER=pct<100 pct 100 60;;;; sleep 10;;;; set BR_Shutter:FILTER=pct<60 pct 60;;;; set Sonos_Bedroom:FILTER=Volume<10 Volume 10 10\";;\
  1658. \
  1659. # if wake-up should be enforced\
  1660. if (\$EVTPART3) {\
  1661. Log3 \$NAME, 3, \"\$NAME: planning enforced wake-up\";;\
  1662. # fhem \"define atTmp_3_\$NAME at +00:25:00 set Sonos_Bedroom:FILTER=Volume>4 Volume 4;;;; sleep 0.5;;;; set Sonos_Bedroom:FILTER=Shuffle=0 Shuffle 1;;;; sleep 0.5;;;; set Sonos_Bedroom StartFavourite Morning%20Sounds\";;\
  1663. # fhem \"define atTmp_4_\$NAME at +00:26:00 set Sonos_Bedroom:FILTER=Volume<5 Volume 5\";;\
  1664. # fhem \"define atTmp_5_\$NAME at +00:27:00 set Sonos_Bedroom:FILTER=Volume<6 Volume 6\";;\
  1665. # fhem \"define atTmp_6_\$NAME at +00:28:00 set Sonos_Bedroom:FILTER=Volume<7 Volume 7\";;\
  1666. # fhem \"define atTmp_7_\$NAME at +00:29:00 set Sonos_Bedroom:FILTER=Volume<8 Volume 8\";;\
  1667. }\
  1668. }\
  1669. \
  1670. ##-----------------------------------------------------------------------------\
  1671. ## END WAKE-UP PROGRAM (OPTIONAL)\
  1672. ## Put some post wake-up tasks here like reminders after the actual wake-up period.\
  1673. ##\
  1674. ## Note: Will only be run when program ends normally after minutes specified in wakeupOffset.\
  1675. ## If stop was user-forced by sending explicit set-command 'stop', this is not executed\
  1676. ## assuming the user does not want any further automation activities.\
  1677. ##\
  1678. if (\$EVTPART0 eq \"stop\") {\
  1679. Log3 \$NAME, 3, \"\$NAME: Wake-up program ended for \$EVTPART4 with target time \$EVTPART1. Current state: \$EVTPART5\";;\
  1680. \
  1681. # if wake-up should be enforced, auto-change user state from 'asleep' to 'awoken'\
  1682. # after a small additional nap to kick you out of bed if user did not confirm to be awake :-)\
  1683. # An additional notify for user state 'awoken' may take further actions\
  1684. # and change to state 'home' afterwards.\
  1685. if (\$EVTPART3) {\
  1686. fhem \"define atTmp_9_\$NAME at +00:05:00 set \$EVTPART4:FILTER=state=asleep awoken\";;\
  1687. \
  1688. # Without enforced wake-up, be jentle and just set user state to 'home' after some\
  1689. # additional long nap time\
  1690. } else {\
  1691. fhem \"define atTmp_9_\$NAME at +01:30:00 set \$EVTPART4:FILTER=state=asleep home\";;\
  1692. }\
  1693. }\
  1694. \
  1695. }\
  1696. ";
  1697. Log3 $NAME, 3,
  1698. "RESIDENTStk $NAME: "
  1699. . "new notify macro device $wakeupMacro created";
  1700. fhem "define $wakeupMacro notify $wakeupMacro $wakeUpMacroTemplate";
  1701. fhem
  1702. "attr $wakeupMacro comment Macro auto-created by RESIDENTS Toolkit";
  1703. fhem "attr $wakeupMacro room $room"
  1704. if ($room);
  1705. }
  1706. elsif ( !IsDevice( $wakeupMacro, "notify" ) ) {
  1707. Log3 $NAME, 3,
  1708. "RESIDENTStk $NAME: "
  1709. . "WARNING - defined macro device '$wakeupMacro' is not a notify device!";
  1710. }
  1711. # check for required wakeupAtdevice attribute
  1712. if ( !$wakeupAtdevice ) {
  1713. Log3 $NAME, 4,
  1714. "RESIDENTStk $NAME: "
  1715. . "adjusting dummy device for required attribute wakeupAtdevice";
  1716. fhem "attr $NAME wakeupAtdevice $atName";
  1717. $wakeupAtdevice = $atName;
  1718. }
  1719. if ( !IsDevice($wakeupAtdevice) ) {
  1720. Log3 $NAME, 3,
  1721. "RESIDENTStk $NAME: " . "new at-device $wakeupAtdevice created";
  1722. fhem "define $wakeupAtdevice "
  1723. . "at *{RESIDENTStk_wakeupGetBegin(\"$NAME\",\"$wakeupAtdevice\")} set $NAME trigger";
  1724. fhem "attr $wakeupAtdevice "
  1725. . "comment Auto-created by RESIDENTS Toolkit: trigger wake-up timer at specific time";
  1726. fhem "attr $wakeupAtdevice computeAfterInit 1";
  1727. fhem "attr $wakeupAtdevice room $room"
  1728. if ($room);
  1729. ########
  1730. # (re)create other notify and watchdog templates
  1731. # for ROOMMATE or GUEST devices
  1732. # macro: gotosleep
  1733. if ( !IsDevice( $wakeupUserdevice, "RESIDENTS" )
  1734. && !IsDevice($macroNameGotosleep) )
  1735. {
  1736. my $templateGotosleep = "{\
  1737. ##=============================================================================\
  1738. ## This is an example macro when gettin' ready for bed.\
  1739. ##\
  1740. ## Actual FHEM commands are commented out by default as they would need\
  1741. ## to be adapted to your configuration.\
  1742. ##=============================================================================\
  1743. \
  1744. ##-----------------------------------------------------------------------------\
  1745. ## LIGHT SCENE\
  1746. ##\
  1747. \
  1748. ## Dim up floor light\
  1749. #fhem \"set FL_Light:FILTER=pct=0 pct 20\";;\
  1750. \
  1751. ## Dim down bright ceilling light in bedroom\
  1752. #fhem \"set BR_Light:FILTER=pct!=0 pct 0 5\";;\
  1753. \
  1754. ## Dim up HUE floor lamp with very low color temperature\
  1755. #fhem \"set BR_FloorLamp ct 2000 : pct 80 : transitiontime 30\";;\
  1756. \
  1757. \
  1758. ##-----------------------------------------------------------------------------\
  1759. ## ENVIRONMENT SCENE\
  1760. ##\
  1761. \
  1762. ## Turn down shutter to 28%\
  1763. #fhem \"set BR_Shutter:FILTER=pct>28 pct 28\";;\
  1764. \
  1765. \
  1766. ##-----------------------------------------------------------------------------\
  1767. ## PLAY CHILLOUT MUSIC\
  1768. ## via SONOS at Bedroom and Bathroom\
  1769. ##\
  1770. \
  1771. ## Stop playback bedroom's Sonos device might be involved in\
  1772. #fhem \"set Sonos_Bedroom:transportState=PLAYING stop;;\";;\
  1773. \
  1774. ## Make Bedroom's and Bathroom's Sonos devices a single device\
  1775. ## and do not touch other Sonos devices (this is why we use RemoveMember!)\
  1776. #fhem \"sleep 0.5;; set Sonos_Bedroom RemoveMember Sonos_Bedroom\";;\
  1777. #fhem \"sleep 1.0;; set Sonos_Bathroom RemoveMember Sonos_Bathroom\";;\
  1778. \
  1779. ## Group Bedroom's and Bathroom's Sonos devices with Bedroom as master\
  1780. #fhem \"sleep 2.0;; set Sonos_Bedroom AddMember Sonos_Bathroom;; set Sonos_Bedroom:FILTER=Shuffle!=1 Shuffle 1;; set Sonos_Bedroom:FILTER=Volume!=12,Sonos_Bathroom:FILTER=Volume!=12 Volume 12\";;\
  1781. \
  1782. ## Start music from playlist\
  1783. #fhem \"sleep 3.0;; set Sonos_Bedroom StartFavourite Evening%%20Chill\";;\
  1784. \
  1785. return;;\
  1786. }";
  1787. Log3 $NAME, 3,
  1788. "RESIDENTStk $NAME: "
  1789. . "new notify macro device $macroNameGotosleep created";
  1790. fhem "define $macroNameGotosleep "
  1791. . "notify $macroNameGotosleep $templateGotosleep";
  1792. fhem "attr $macroNameGotosleep "
  1793. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run when gettin' ready for bed";
  1794. fhem "attr $macroNameGotosleep room $room"
  1795. if ($room);
  1796. }
  1797. # wd: gotosleep
  1798. if ( !IsDevice($wdNameGotosleep) ) {
  1799. Log3 $NAME, 3,
  1800. "RESIDENTStk $NAME: "
  1801. . "new watchdog device $wdNameGotosleep created";
  1802. fhem "define $wdNameGotosleep "
  1803. . "watchdog $wakeupUserdevice:(gotosleep|bettfertig) 00:00:04 $wakeupUserdevice:(home|anwesend|zuhause|absent|abwesend|gone|verreist|asleep|schlaeft|schläft|awoken|aufgestanden) trigger $macroNameGotosleep";
  1804. fhem "attr $wdNameGotosleep autoRestart 1";
  1805. fhem "attr $wdNameGotosleep "
  1806. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state gotosleep";
  1807. fhem "attr $wdNameGotosleep room $room"
  1808. if ($room);
  1809. }
  1810. # macro: asleep
  1811. if ( !IsDevice( $wakeupUserdevice, "RESIDENTS" )
  1812. && !IsDevice($macroNameAsleep) )
  1813. {
  1814. my $templateAsleep = "{\
  1815. ##=============================================================================\
  1816. ## This is an example macro when jumpin' into bed and start to sleep.\
  1817. ##\
  1818. ## Actual FHEM commands are commented out by default as they would need\
  1819. ## to be adapted to your configuration.\
  1820. ##=============================================================================\
  1821. \
  1822. ##-----------------------------------------------------------------------------\
  1823. ## LIGHT SCENE\
  1824. ##\
  1825. \
  1826. ## In 15 seconds, turn off all lights in Bedroom using a structure\
  1827. #fhem \"sleep 15;; set g_BR_Lights [FILTER=state!=off] off\";;\
  1828. \
  1829. \
  1830. ##-----------------------------------------------------------------------------\
  1831. ## ENVIRONMENT SCENE\
  1832. ##\
  1833. \
  1834. ## In 12 seconds, close shutter if window is closed\
  1835. #if (ReadingsVal(\"BR_Window\",\"state\",0) eq \"closed\") {\
  1836. # fhem \"sleep 12;; set BR_Shutter:FILTER=pct>0 close\";;\
  1837. \
  1838. ## In 12 seconds, if window is not closed just make sure shutter is at least\
  1839. ## at 28% to allow some ventilation\
  1840. #} else {\
  1841. # fhem \"sleep 12;; set BR_Shutter:FILTER=pct>28 pct 28\";;\
  1842. #}\
  1843. \
  1844. \
  1845. ##-----------------------------------------------------------------------------\
  1846. ## PLAY WAKE-UP ANNOUNCEMENT\
  1847. ## via SONOS at Bedroom and stop playback elsewhere\
  1848. ##\
  1849. \
  1850. #my \$nextWakeup = ReadingsVal(\"$wakeupUserdevice\",\"nextWakeup\",\"none\");;
  1851. #my \$text = \"|Hint| $wakeupUserdeviceRealname, es ist kein Wecker gestellt. Du könntest verschlafen! Trotzdem eine gute Nacht.\";;
  1852. #if (\$nextWakeup ne \"OFF\") {
  1853. # \$text = \"|Hint| $wakeupUserdeviceRealname, dein Wecker ist auf \$nextWakeup Uhr gestellt. Gute Nacht und schlaf gut.\";;
  1854. #}
  1855. #if (\$nextWakeup ne \"none\") {
  1856. # fhem \"set Sonos_Bedroom RemoveMember Sonos_Bedroom;; sleep 0.5;; msg audio \\\@Sonos_Bedroom \$text\";;\
  1857. #}
  1858. \
  1859. return;;\
  1860. }";
  1861. Log3 $NAME, 3,
  1862. "RESIDENTStk $NAME: "
  1863. . "new notify macro device $macroNameAsleep created";
  1864. fhem
  1865. "define $macroNameAsleep notify $macroNameAsleep $templateAsleep";
  1866. fhem "attr $macroNameAsleep "
  1867. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run when jumpin' into bed and start to sleep";
  1868. fhem "attr $macroNameAsleep room $room"
  1869. if ($room);
  1870. }
  1871. # wd: asleep
  1872. if ( !IsDevice($wdNameAsleep) ) {
  1873. Log3 $NAME, 3,
  1874. "RESIDENTStk $NAME: "
  1875. . "new watchdog device $wdNameAsleep created";
  1876. fhem "define $wdNameAsleep "
  1877. . "watchdog $wakeupUserdevice:(asleep|schlaeft|schläft) 00:00:04 $wakeupUserdevice:(home|anwesend|zuhause|absent|abwesend|gone|verreist|gotosleep|bettfertig|awoken|aufgestanden) trigger $macroNameAsleep";
  1878. fhem "attr $wdNameAsleep autoRestart 1";
  1879. fhem "attr $wdNameAsleep "
  1880. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state asleep";
  1881. fhem "attr $wdNameAsleep room $room"
  1882. if ($room);
  1883. }
  1884. # macro: awoken
  1885. if ( !IsDevice( $wakeupUserdevice, "RESIDENTS" )
  1886. && !IsDevice($macroNameAwoken) )
  1887. {
  1888. my $templateAwoken = "{\
  1889. ##=============================================================================\
  1890. ## This is an example macro after confirming to be awake.\
  1891. ##\
  1892. ## Actual FHEM commands are commented out by default as they would need\
  1893. ## to be adapted to your configuration.\
  1894. ##=============================================================================\
  1895. \
  1896. ##-----------------------------------------------------------------------------\
  1897. ## LIGHT SCENE\
  1898. ##\
  1899. \
  1900. ## Dim up HUE floor lamp to maximum with cold color temperature\
  1901. #fhem \"set BR_FloorLamp:FILTER=pct<100 pct 100 : ct 6500 : transitiontime 30\";;\
  1902. \
  1903. \
  1904. ##-----------------------------------------------------------------------------\
  1905. ## ENVIRONMENT SCENE\
  1906. ##\
  1907. \
  1908. ## In 22 seconds, turn up shutter at least until 60%\
  1909. #fhem \"sleep 22;; set BR_Shutter:FILTER=pct<60 60\";;\
  1910. \
  1911. \
  1912. ##-----------------------------------------------------------------------------\
  1913. ## RAMP-UP ALL MORNING STUFF\
  1914. ##\
  1915. \
  1916. ## Play morning announcement via SONOS at Bedroom\
  1917. #fhem \"set Sonos_Bedroom Stop;; msg audio \\\@Sonos_Bedroom |Hint| Guten Morgen, $wakeupUserdeviceRealname.\";;\
  1918. \
  1919. ## In 10 seconds, start webradio playback in Bedroom\
  1920. #fhem \"sleep 10;; set Sonos_Bedroom StartRadio /Charivari/;; sleep 2;; set Sonos_Bedroom Volume 15\";;\
  1921. \
  1922. ## Make webradio stream available at Bathroom and\
  1923. ## Kitchen 5 seonds after it started\
  1924. #fhem \"set Sonos_Bathroom,Sonos_Kitchen Volume 15;; sleep 15;; set Sonos_Bedroom AddMember Sonos_Bathroom;; set Sonos_Bedroom AddMember Sonos_Kitchen\";;\
  1925. \
  1926. ## change user state to home after 60 seconds\
  1927. fhem \"sleep 60;; set $wakeupUserdevice:FILTER=state!=home home\";;\
  1928. \
  1929. return;;\
  1930. }";
  1931. Log3 $NAME, 3,
  1932. "RESIDENTStk $NAME: "
  1933. . "new notify macro device $macroNameAwoken created";
  1934. fhem
  1935. "define $macroNameAwoken notify $macroNameAwoken $templateAwoken";
  1936. fhem "attr $macroNameAwoken "
  1937. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run after confirming to be awake";
  1938. fhem "attr $macroNameAwoken room $room"
  1939. if ($room);
  1940. }
  1941. # wd: awoken
  1942. if ( !IsDevice($wdNameAwoken) ) {
  1943. Log3 $NAME, 3,
  1944. "RESIDENTStk $NAME: "
  1945. . "new watchdog device $wdNameAwoken created";
  1946. fhem "define $wdNameAwoken "
  1947. . "watchdog $wakeupUserdevice:(awoken|aufgestanden) 00:00:04 $wakeupUserdevice:(home|anwesend|zuhause|absent|abwesend|gone|verreist|gotosleep|bettfertig|asleep|schlaeft|schläft) trigger $macroNameAwoken";
  1948. fhem "attr $wdNameAwoken autoRestart 1";
  1949. fhem "attr $wdNameAwoken "
  1950. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state awoken";
  1951. fhem "attr $wdNameAwoken room $room"
  1952. if ($room);
  1953. }
  1954. ########
  1955. # (re)create other notify and watchdog templates
  1956. # for RESIDENT devices
  1957. #
  1958. my $RESIDENTGROUPS;
  1959. if ( IsDevice( $wakeupUserdevice, "RESIDENTS" ) ) {
  1960. $RESIDENTGROUPS = $wakeupUserdevice;
  1961. }
  1962. elsif ( IsDevice($wakeupUserdevice)
  1963. && defined( $defs{$wakeupUserdevice}{RESIDENTGROUPS} ) )
  1964. {
  1965. $RESIDENTGROUPS = $defs{$wakeupUserdevice}{RESIDENTGROUPS};
  1966. }
  1967. for my $deviceName ( split /,/, $RESIDENTGROUPS ) {
  1968. my $macroRNameGotosleep = "Macro_" . $deviceName . "_gotosleep";
  1969. my $macroRNameAsleep = "Macro_" . $deviceName . "_asleep";
  1970. my $macroRNameAwoken = "Macro_" . $deviceName . "_awoken";
  1971. my $wdRNameGotosleep = "wd_" . $deviceName . "_gotosleep";
  1972. my $wdRNameAsleep = "wd_" . $deviceName . "_asleep";
  1973. my $wdRNameAwoken = "wd_" . $deviceName . "_awoken";
  1974. # macro: gotosleep
  1975. if ( !IsDevice($macroRNameGotosleep) ) {
  1976. my $templateGotosleep = "{\
  1977. ##=============================================================================\
  1978. ## This is an example macro when all residents are gettin' ready for bed.\
  1979. ##\
  1980. ## Actual FHEM commands are commented out by default as they would need\
  1981. ## to be adapted to your configuration.\
  1982. ##=============================================================================\
  1983. \
  1984. ##-----------------------------------------------------------------------------\
  1985. ## HOUSE MODE\
  1986. ## Enforce evening mode if we are still in day mode\
  1987. ##\
  1988. \
  1989. #fhem \"set HouseMode:FILTER=state=day evening\";;\
  1990. \
  1991. \
  1992. ##-----------------------------------------------------------------------------\
  1993. ## LIGHT SCENE\
  1994. ##\
  1995. \
  1996. ## In 10 seconds, turn off lights in unused rooms using structures\
  1997. #fhem \"sleep 10;; set g_LR_Lights,g_KT_Lights [FILTER=state!=off] off\";;\
  1998. \
  1999. \
  2000. ##-----------------------------------------------------------------------------\
  2001. ## ENVIRONMENT SCENE\
  2002. ##\
  2003. \
  2004. ## Turn off all media devices in the Living Room\
  2005. #fhem \"set g_HSE_Media [FILTER=state!=off] off\";;\
  2006. \
  2007. return;;\
  2008. }";
  2009. Log3 $NAME, 3,
  2010. "RESIDENTStk $NAME: "
  2011. . "new notify macro device $macroRNameGotosleep created";
  2012. fhem "define $macroRNameGotosleep "
  2013. . "notify $macroRNameGotosleep $templateGotosleep";
  2014. fhem "attr $macroRNameGotosleep "
  2015. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run when all residents are gettin' ready for bed";
  2016. fhem "attr $macroRNameGotosleep room $room"
  2017. if ($room);
  2018. }
  2019. # wd: gotosleep
  2020. if ( !IsDevice($wdRNameGotosleep) ) {
  2021. Log3 $NAME, 3,
  2022. "RESIDENTStk $NAME: "
  2023. . "new watchdog device $wdRNameGotosleep created";
  2024. fhem "define $wdRNameGotosleep "
  2025. . "watchdog $deviceName:(gotosleep|bettfertig) 00:00:03 $deviceName:(home|anwesend|zuhause|absent|abwesend|gone|verreist|asleep|schlaeft|schläft|awoken|aufgestanden) trigger $macroRNameGotosleep";
  2026. fhem "attr $wdRNameGotosleep autoRestart 1";
  2027. fhem "attr $wdRNameGotosleep "
  2028. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state gotosleep";
  2029. fhem "attr $wdRNameGotosleep room $room"
  2030. if ($room);
  2031. }
  2032. # macro: asleep
  2033. if ( !IsDevice($macroRNameAsleep) ) {
  2034. my $templateAsleep = "{\
  2035. ##=============================================================================\
  2036. ## This is an example macro when all residents are in their beds.\
  2037. ##\
  2038. ## Actual FHEM commands are commented out by default as they would need\
  2039. ## to be adapted to your configuration.\
  2040. ##=============================================================================\
  2041. \
  2042. ##-----------------------------------------------------------------------------\
  2043. ## HOUSE MODE\
  2044. ## Enforce night mode if we are still in evening mode\
  2045. ##\
  2046. \
  2047. #fhem \"set HouseMode:FILTER=state=evening night\";;\
  2048. \
  2049. \
  2050. ##-----------------------------------------------------------------------------\
  2051. ## LIGHT SCENE\
  2052. ##\
  2053. \
  2054. ## In 20 seconds, turn off all lights in the house using structures\
  2055. #fhem \"sleep 20;; set g_HSE_Lights [FILTER=state!=off] off\";;\
  2056. \
  2057. \
  2058. ##-----------------------------------------------------------------------------\
  2059. ## ENVIRONMENT SCENE\
  2060. ##\
  2061. \
  2062. ## Stop playback at SONOS devices in shared rooms, e.g. Bathroom\
  2063. #fhem \"set Sonos_Bathroom:FILTER=transportState=PLAYING Stop\";;\
  2064. \
  2065. return;;\
  2066. }";
  2067. Log3 $NAME, 3,
  2068. "RESIDENTStk $NAME: "
  2069. . "new notify macro device $macroRNameAsleep created";
  2070. fhem "define $macroRNameAsleep "
  2071. . "notify $macroRNameAsleep $templateAsleep";
  2072. fhem "attr $macroRNameAsleep "
  2073. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run when all residents are in their beds";
  2074. fhem "attr $macroRNameAsleep room $room"
  2075. if ($room);
  2076. }
  2077. # wd: asleep
  2078. if ( !IsDevice($wdRNameAsleep) ) {
  2079. Log3 $NAME, 3,
  2080. "RESIDENTStk $NAME: "
  2081. . "new watchdog device $wdNameAsleep created";
  2082. fhem "define $wdRNameAsleep "
  2083. . "watchdog $deviceName:(asleep|schlaeft|schläft) 00:00:03 $deviceName:(home|anwesend|zuhause|absent|abwesend|gone|verreist|gotosleep|bettfertig|awoken|aufgestanden) trigger $macroRNameAsleep";
  2084. fhem "attr $wdRNameAsleep autoRestart 1";
  2085. fhem "attr $wdRNameAsleep "
  2086. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state asleep";
  2087. fhem "attr $wdRNameAsleep room $room"
  2088. if ($room);
  2089. }
  2090. # macro: awoken
  2091. if ( !IsDevice($macroRNameAwoken) ) {
  2092. my $templateAwoken = "{\
  2093. ##=============================================================================\
  2094. ## This is an example macro when the first resident has confirmed to be awake\
  2095. ##\
  2096. ## Actual FHEM commands are commented out by default as they would need\
  2097. ## to be adapted to your configuration.\
  2098. ##=============================================================================\
  2099. \
  2100. ##-----------------------------------------------------------------------------\
  2101. ## HOUSE MODE\
  2102. ## Enforce morning mode if we are still in night mode\
  2103. ##\
  2104. \
  2105. #fhem \"set HouseMode:FILTER=state=night morning\";;\
  2106. \
  2107. \
  2108. ##-----------------------------------------------------------------------------\
  2109. ## LIGHT SCENE\
  2110. ##\
  2111. \
  2112. ## Turn on lights in the Kitchen already but set a timer to turn it off again\
  2113. #fhem \"set KT_CounterLight on-for-timer 6300\";;\
  2114. \
  2115. \
  2116. ##-----------------------------------------------------------------------------\
  2117. ## PREPARATIONS\
  2118. ##\
  2119. \
  2120. ## In 90 minutes, switch House Mode to 'day' and\
  2121. ## play voice announcement via SONOS\
  2122. #unless (IsDevice(\"atTmpHouseMode_day\")) {\
  2123. # fhem \"define atTmpHouseMode_day at +01:30:00 {if (ReadingsVal(\\\"HouseMode\\\", \\\"state\\\", 0) ne \\\"day\\\") {fhem \\\"msg audio \\\@Sonos_Kitchen Tagesmodus wird etabliert.;;;; sleep 10;;;; set HouseMode day\\\"}}\";;\
  2124. #}\
  2125. \
  2126. return;;\
  2127. }";
  2128. Log3 $NAME, 3,
  2129. "RESIDENTStk $NAME: "
  2130. . "new notify macro device $macroRNameAwoken created";
  2131. fhem "define $macroRNameAwoken "
  2132. . "notify $macroRNameAwoken $templateAwoken";
  2133. fhem "attr $macroRNameAwoken "
  2134. . "comment Auto-created by RESIDENTS Toolkit: FHEM commands to run after first resident confirmed to be awake";
  2135. fhem "attr $macroRNameAwoken room $room"
  2136. if ($room);
  2137. }
  2138. # wd: awoken
  2139. if ( !IsDevice($wdRNameAwoken) ) {
  2140. Log3 $NAME, 3,
  2141. "RESIDENTStk $NAME: "
  2142. . "new watchdog device $wdNameAwoken created";
  2143. fhem "define $wdRNameAwoken "
  2144. . "watchdog $deviceName:(awoken|aufgestanden) 00:00:04 $deviceName:(home|anwesend|zuhause|absent|abwesend|gone|verreist|gotosleep|bettfertig|asleep|schlaeft|schläft) trigger $macroRNameAwoken";
  2145. fhem "attr $wdRNameAwoken autoRestart 1";
  2146. fhem "attr $wdRNameAwoken "
  2147. . "comment Auto-created by RESIDENTS Toolkit: trigger macro after going to state awoken";
  2148. fhem "attr $wdRNameAwoken room $room"
  2149. if ($room);
  2150. }
  2151. }
  2152. }
  2153. elsif ( !IsDevice( $wakeupAtdevice, "at" ) ) {
  2154. Log3 $NAME, 3,
  2155. "RESIDENTStk $NAME: "
  2156. . "WARNING - defined at-device '$wakeupAtdevice' is not an at-device!";
  2157. }
  2158. elsif ( AttrVal( $wakeupAtdevice, "computeAfterInit", 0 ) ne "1" ) {
  2159. Log3 $NAME, 3,
  2160. "RESIDENTStk $NAME: "
  2161. . "Correcting '$wakeupAtdevice' attribute computeAfterInit required for correct recalculation after reboot";
  2162. fhem "attr $wakeupAtdevice computeAfterInit 1";
  2163. }
  2164. # verify holiday2we attribute
  2165. if ( $wakeupHolidays ne "" ) {
  2166. if ( !$holidayDevice ) {
  2167. Log3 $NAME, 3,
  2168. "RESIDENTStk $NAME: "
  2169. . "ERROR - wakeupHolidays set in this alarm clock but global attribute holiday2we not set!";
  2170. return "ERROR: "
  2171. . "wakeupHolidays set in this alarm clock but global attribute holiday2we not set!";
  2172. }
  2173. elsif ( !IsDevice($holidayDevice) ) {
  2174. Log3 $NAME, 3,
  2175. "RESIDENTStk $NAME: "
  2176. . "ERROR - global attribute holiday2we has reference to non-existing device $holidayDevice";
  2177. return "ERROR: "
  2178. . "global attribute holiday2we has reference to non-existing device $holidayDevice";
  2179. }
  2180. elsif ( !IsDevice( $holidayDevice, "holiday" ) ) {
  2181. Log3 $NAME, 3,
  2182. "RESIDENTStk $NAME: "
  2183. . "ERROR - global attribute holiday2we seems to have invalid device reference - $holidayDevice is not of type 'holiday'";
  2184. return "ERROR: "
  2185. . "global attribute holiday2we seems to have invalid device reference - $holidayDevice is not of type 'holiday'";
  2186. }
  2187. }
  2188. # start
  2189. #
  2190. if ( $cmd eq "start" ) {
  2191. RESIDENTStk_wakeupRun( $NAME, 1 );
  2192. }
  2193. # trigger
  2194. #
  2195. elsif ( $cmd eq "trigger" ) {
  2196. RESIDENTStk_wakeupRun($NAME);
  2197. }
  2198. # stop | end
  2199. #
  2200. elsif ( $cmd eq "stop" || $cmd eq "end" ) {
  2201. if ($running) {
  2202. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "stopping wake-up program";
  2203. readingsSingleUpdate( $defs{$NAME}, "running", "0", 1 );
  2204. }
  2205. fhem "set $NAME nextRun $nextRun";
  2206. return unless ($running);
  2207. # trigger macro again so it may clean up it's stuff.
  2208. # use $EVTPART1 to check
  2209. if ( !$wakeupMacro ) {
  2210. Log3 $NAME, 2,
  2211. "RESIDENTStk $NAME: " . "missing attribute wakeupMacro";
  2212. }
  2213. elsif ( !IsDevice($wakeupMacro) ) {
  2214. Log3 $NAME, 2,
  2215. "RESIDENTStk $NAME: "
  2216. . "notify macro $wakeupMacro not found - no wakeup actions defined!";
  2217. }
  2218. elsif ( !IsDevice( $wakeupMacro, "notify" ) ) {
  2219. Log3 $NAME, 2,
  2220. "RESIDENTStk $NAME: "
  2221. . "device $wakeupMacro is not of type notify";
  2222. }
  2223. else {
  2224. # conditional enforced wake-up:
  2225. # only if actual wake-up time is
  2226. # earlier than wakeupDefaultTime
  2227. if ( $wakeupEnforced == 3
  2228. && $wakeupDefaultTime
  2229. && UConv::hms2s($wakeupDefaultTime) > UConv::hms2s($lastRun) )
  2230. {
  2231. Log3 $NAME, 4,
  2232. "RESIDENTStk $NAME: "
  2233. . "Enforcing wake-up because wake-up time is earlier than normal (wakeupDefaultTime=$wakeupDefaultTime > lastRun=$lastRun)";
  2234. $wakeupEnforced = 1;
  2235. }
  2236. # conditional enforced wake-up:
  2237. # only if actual wake-up time is not wakeupDefaultTime
  2238. elsif ($wakeupEnforced == 2
  2239. && $wakeupDefaultTime
  2240. && $wakeupDefaultTime ne $lastRun )
  2241. {
  2242. Log3 $NAME, 4,
  2243. "RESIDENTStk $NAME: "
  2244. . "Enforcing wake-up because wake-up is different from normal (wakeupDefaultTime=$wakeupDefaultTime =! lastRun=$lastRun)";
  2245. $wakeupEnforced = 1;
  2246. }
  2247. elsif ( $wakeupEnforced > 1 ) {
  2248. $wakeupEnforced = 0;
  2249. }
  2250. if ( $VALUE ne "" || $cmd eq "end" ) {
  2251. Log3 $NAME, 4,
  2252. "RESIDENTStk $NAME: "
  2253. . "trigger $wakeupMacro stop $lastRun $wakeupOffset $wakeupEnforced $wakeupUserdevice $wakeupUserdeviceState";
  2254. fhem "trigger $wakeupMacro "
  2255. . "stop $lastRun $wakeupOffset $wakeupEnforced $wakeupUserdevice $wakeupUserdeviceState";
  2256. }
  2257. else {
  2258. Log3 $NAME, 4,
  2259. "RESIDENTStk $NAME: "
  2260. . "trigger $wakeupMacro forced-stop $lastRun $wakeupOffset $wakeupEnforced $wakeupUserdevice $wakeupUserdeviceState";
  2261. fhem "trigger $wakeupMacro "
  2262. . "forced-stop $lastRun $wakeupOffset $wakeupEnforced $wakeupUserdevice $wakeupUserdeviceState";
  2263. fhem "set $wakeupUserdevice:FILTER=state=asleep awoken";
  2264. }
  2265. my $wakeupStopAtdevice = $wakeupAtdevice . "_stop";
  2266. fhem "delete $wakeupStopAtdevice"
  2267. if ( IsDevice($wakeupStopAtdevice) );
  2268. }
  2269. readingsSingleUpdate( $defs{$wakeupUserdevice}, "wakeup", "0", 1 );
  2270. return;
  2271. }
  2272. # auto or reset
  2273. #
  2274. elsif ( $cmd eq "auto" || $cmd eq "reset" ) {
  2275. my $resetTime = ReadingsVal( $NAME, "lastRun", 0 );
  2276. if ($wakeupDefaultTime) {
  2277. $resetTime = $wakeupDefaultTime;
  2278. }
  2279. if ( $resetTime
  2280. && !( $cmd eq "auto" && lc($resetTime) eq "off" ) )
  2281. {
  2282. fhem "set $NAME:FILTER=state!=$resetTime nextRun $resetTime";
  2283. }
  2284. elsif ( $cmd eq "reset" ) {
  2285. Log3 $NAME, 4,
  2286. "RESIDENTStk $NAME: "
  2287. . "no default value specified in attribute wakeupDefaultTime, just keeping setting OFF";
  2288. fhem "set $NAME:FILTER=state!=OFF nextRun OFF";
  2289. }
  2290. return;
  2291. }
  2292. # wakeup attributes
  2293. #
  2294. elsif ( $cmd =~
  2295. m/^(wakeupResetdays|wakeupDays|wakeupHolidays|wakeupEnforced|wakeupDefaultTime|wakeupOffset)$/
  2296. )
  2297. {
  2298. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "setting $1 to '$VALUE'";
  2299. readingsBeginUpdate( $defs{$NAME} );
  2300. readingsBulkUpdate( $defs{$NAME}, $1, $VALUE );
  2301. readingsEndUpdate( $defs{$NAME}, 1 );
  2302. fhem "set $NAME nextRun $nextRun";
  2303. return;
  2304. }
  2305. # set new wakeup value
  2306. elsif ( IsDevice( $wakeupAtdevice, "at" )
  2307. && $cmd =~
  2308. m/^(?:nextRun)?\s*(OFF|([\+\-])?(([0-9]{2}):([0-9]{2})|([1-9]+[0-9]*)))?$/i
  2309. )
  2310. {
  2311. $VALUE = $1 if ( $1 && $1 ne "" );
  2312. $VALUE =
  2313. RESIDENTStk_TimeSum( ReadingsVal( $NAME, "nextRun", 0 ), $VALUE )
  2314. if ($2);
  2315. # Update wakeuptimer device
  2316. #
  2317. readingsBeginUpdate( $defs{$NAME} );
  2318. if ( ReadingsVal( $NAME, "nextRun", 0 ) ne $VALUE ) {
  2319. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "New wake-up time: $VALUE";
  2320. readingsBulkUpdate( $defs{$NAME}, "nextRun", $VALUE );
  2321. # Update at-device
  2322. fhem "set $wakeupAtdevice "
  2323. . "modifyTimeSpec {RESIDENTStk_wakeupGetBegin(\"$NAME\",\"$wakeupAtdevice\")}";
  2324. }
  2325. if ( ReadingsVal( $NAME, "state", 0 ) ne $VALUE
  2326. && !$running )
  2327. {
  2328. readingsBulkUpdate( $defs{$NAME}, "state", $VALUE );
  2329. }
  2330. elsif ( ReadingsVal( $NAME, "state", 0 ) ne "running"
  2331. && $running )
  2332. {
  2333. readingsBulkUpdate( $defs{$NAME}, "state", "running" );
  2334. }
  2335. readingsEndUpdate( $defs{$NAME}, 1 );
  2336. # Update user device
  2337. #
  2338. readingsBeginUpdate( $defs{$wakeupUserdevice} );
  2339. my ( $nextWakeupDev, $nextWakeup ) =
  2340. RESIDENTStk_wakeupGetNext($wakeupUserdevice);
  2341. if ( !$nextWakeupDev || !$nextWakeup ) {
  2342. $nextWakeupDev = "";
  2343. $nextWakeup = "OFF";
  2344. }
  2345. readingsBulkUpdateIfChanged( $defs{$wakeupUserdevice},
  2346. "nextWakeupDev", $nextWakeupDev );
  2347. readingsBulkUpdateIfChanged( $defs{$wakeupUserdevice},
  2348. "nextWakeup", $nextWakeup );
  2349. readingsEndUpdate( $defs{$wakeupUserdevice}, 1 );
  2350. }
  2351. return undef;
  2352. }
  2353. sub RESIDENTStk_wakeupGetBegin($;$) {
  2354. my ( $NAME, $wakeupAtdevice ) = @_;
  2355. unless ( IsDevice($NAME) ) {
  2356. Log3 $NAME, 3,
  2357. "RESIDENTStk $NAME: "
  2358. . "Run function RESIDENTStk_wakeupGetBegin() for non-existing device!";
  2359. return "$NAME: Non-existing device";
  2360. }
  2361. my $nextRun = ReadingsVal( $NAME, "nextRun", 0 );
  2362. my $wakeupDefaultTime =
  2363. ReadingsVal( $NAME, "wakeupDefaultTime",
  2364. AttrVal( $NAME, "wakeupDefaultTime", 0 ) );
  2365. my $wakeupOffset =
  2366. ReadingsVal( $NAME, "wakeupOffset", AttrVal( $NAME, "wakeupOffset", 0 ) );
  2367. my $wakeupInitTime = (
  2368. $wakeupDefaultTime && lc($wakeupDefaultTime) ne "off"
  2369. ? $wakeupDefaultTime
  2370. : "05:00"
  2371. );
  2372. my $wakeupTime;
  2373. if ($wakeupAtdevice) {
  2374. Log3 $NAME, 4,
  2375. "RESIDENTStk $NAME: "
  2376. . "Wakeuptime recalculation triggered by at-device $wakeupAtdevice";
  2377. }
  2378. # just give any valuable return to at-device
  2379. # if wakeuptimer device does not exit anymore
  2380. # and run self-destruction to clean up
  2381. if ( !IsDevice($NAME) ) {
  2382. Log3 $NAME, 3,
  2383. "RESIDENTStk $NAME: "
  2384. . "this wake-up timer device does not exist anymore";
  2385. my $atName = "at_" . $NAME;
  2386. if ( IsDevice( $wakeupAtdevice, "at" ) ) {
  2387. Log3 $NAME, 3,
  2388. "RESIDENTStk $NAME: "
  2389. . "Cleaning up at-device $wakeupAtdevice (self-destruction)";
  2390. fhem "sleep 1; delete $wakeupAtdevice";
  2391. }
  2392. elsif ( IsDevice( $atName, "at" ) ) {
  2393. Log3 $NAME, 3,
  2394. "RESIDENTStk $NAME: " . "Cleaning up at-device $atName";
  2395. fhem "sleep 1; delete $atName";
  2396. }
  2397. else {
  2398. Log3 $NAME, 3,
  2399. "RESIDENTStk $NAME: "
  2400. . "Could not automatically clean up at-device, please perform manual cleanup.";
  2401. }
  2402. return $wakeupInitTime;
  2403. }
  2404. # use nextRun value if not OFF
  2405. if ( $nextRun && lc($nextRun) ne "off" ) {
  2406. $wakeupTime = $nextRun;
  2407. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "wakeupGetBegin source: nextRun";
  2408. }
  2409. # use wakeupDefaultTime if present and not OFF
  2410. elsif ( $wakeupDefaultTime
  2411. && lc($wakeupDefaultTime) ne "off" )
  2412. {
  2413. $wakeupTime = $wakeupDefaultTime;
  2414. Log3 $NAME, 4,
  2415. "RESIDENTStk $NAME: " . "wakeupGetBegin source: wakeupDefaultTime";
  2416. }
  2417. # Use a default value to ensure auto-reset at least once a day
  2418. else {
  2419. $wakeupTime = $wakeupInitTime;
  2420. Log3 $NAME, 4,
  2421. "RESIDENTStk $NAME: " . "wakeupGetBegin source: defaultValue";
  2422. }
  2423. # Recalculate new wake-up value
  2424. my $seconds = UConv::hms2s($wakeupTime) - $wakeupOffset * 60;
  2425. if ( $seconds < 0 ) { $seconds = 86400 + $seconds }
  2426. Log3 $NAME, 4,
  2427. "RESIDENTStk $NAME: "
  2428. . "wakeupGetBegin result: $wakeupTime = $seconds s - $wakeupOffset m = "
  2429. . UConv::s2hms($seconds);
  2430. return UConv::s2hms($seconds);
  2431. }
  2432. sub RESIDENTStk_wakeupRun($;$) {
  2433. my ( $NAME, $forceRun ) = @_;
  2434. unless ( IsDevice($NAME) ) {
  2435. Log3 $NAME, 3,
  2436. "RESIDENTStk $NAME: "
  2437. . "Run function RESIDENTStk_wakeupRun() for non-existing device!";
  2438. return "$NAME: Non-existing device";
  2439. }
  2440. my $wakeupMacro = AttrVal( $NAME, "wakeupMacro", 0 );
  2441. my $wakeupDefaultTime =
  2442. ReadingsVal( $NAME, "wakeupDefaultTime",
  2443. AttrVal( $NAME, "wakeupDefaultTime", 0 ) );
  2444. my $wakeupAtdevice = AttrVal( $NAME, "wakeupAtdevice", 0 );
  2445. my $wakeupUserdevice = AttrVal( $NAME, "wakeupUserdevice", 0 );
  2446. my $wakeupDays =
  2447. ReadingsVal( $NAME, "wakeupDays", AttrVal( $NAME, "wakeupDays", "" ) );
  2448. my $wakeupHolidays =
  2449. ReadingsVal( $NAME, "wakeupHolidays",
  2450. AttrVal( $NAME, "wakeupHolidays", "" ) );
  2451. my $wakeupResetdays =
  2452. ReadingsVal( $NAME, "wakeupResetdays",
  2453. AttrVal( $NAME, "wakeupResetdays", "" ) );
  2454. my $wakeupOffset =
  2455. ReadingsVal( $NAME, "wakeupOffset", AttrVal( $NAME, "wakeupOffset", 0 ) );
  2456. my $wakeupEnforced =
  2457. ReadingsVal( $NAME, "wakeupEnforced",
  2458. AttrVal( $NAME, "wakeupEnforced", 0 ) );
  2459. my $wakeupResetSwitcher = AttrVal( $NAME, "wakeupResetSwitcher", 0 );
  2460. my $wakeupWaitPeriod = AttrVal( $NAME, "wakeupWaitPeriod", 360 );
  2461. my $holidayDevice = AttrVal( "global", "holiday2we", 0 );
  2462. my $lastRun = ReadingsVal( $NAME, "lastRun", "06:00" );
  2463. my $nextRun = ReadingsVal( $NAME, "nextRun", "06:00" );
  2464. my $wakeupUserdeviceState = ReadingsVal( $wakeupUserdevice, "state", 0 );
  2465. my $wakeupUserdeviceWakeup = ReadingsVal( $wakeupUserdevice, "wakeup", 0 );
  2466. my $room = AttrVal( $NAME, "room", 0 );
  2467. my $running = 0;
  2468. my $preventRun = 0;
  2469. my $holidayToday = 0;
  2470. if ( $wakeupHolidays ne ""
  2471. && IsDevice( $holidayDevice, "holiday" ) )
  2472. {
  2473. $holidayToday = 1
  2474. unless ( ReadingsVal( $holidayDevice, "state", "none" ) eq "none" );
  2475. }
  2476. else {
  2477. $wakeupHolidays = "";
  2478. }
  2479. my ( $sec, $min, $hour, $mday, $mon, $year, $today, $yday, $isdst ) =
  2480. localtime( time() + $wakeupOffset * 60 );
  2481. $year += 1900;
  2482. $mon++;
  2483. $mon = "0" . $mon if ( $mon < 10 );
  2484. $mday = "0" . $mday if ( $mday < 10 );
  2485. $hour = "0" . $hour if ( $hour < 10 );
  2486. $min = "0" . $min if ( $min < 10 );
  2487. $sec = "0" . $sec if ( $sec < 10 );
  2488. my $nowRun = $hour . ":" . $min;
  2489. my $nowRunSec =
  2490. time_str2num( $year . "-"
  2491. . $mon . "-"
  2492. . $mday . " "
  2493. . $hour . ":"
  2494. . $min . ":"
  2495. . $sec );
  2496. if ( $nextRun ne $nowRun ) {
  2497. $lastRun = $nowRun;
  2498. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "lastRun != nextRun = $lastRun";
  2499. }
  2500. else {
  2501. $lastRun = $nextRun;
  2502. Log3 $NAME, 4, "RESIDENTStk $NAME: " . "lastRun = nextRun = $lastRun";
  2503. }
  2504. my @days = ($today);
  2505. @days = split /,/, $wakeupDays
  2506. if ( $wakeupDays ne "" );
  2507. my %days = map { $_ => 1 } @days;
  2508. my @rdays = ($today);
  2509. @rdays = split /,/, $wakeupResetdays
  2510. if ( $wakeupResetdays ne "" );
  2511. my %rdays = map { $_ => 1 } @rdays;
  2512. if ( IsDisabled($NAME) ) {
  2513. Log3 $NAME, 4,
  2514. "RESIDENTStk $NAME: "
  2515. . "wakeupDevice disabled - not triggering wake-up program";
  2516. }
  2517. elsif ( IsDisabled($wakeupUserdevice) ) {
  2518. Log3 $NAME, 4,
  2519. "RESIDENTStk $NAME: "
  2520. . "wakeupUserdevice disabled - not triggering wake-up program";
  2521. }
  2522. elsif ( !$wakeupUserdevice ) {
  2523. return "$NAME: missing attribute wakeupUserdevice";
  2524. }
  2525. elsif ( !IsDevice($wakeupUserdevice) ) {
  2526. return "$NAME: Non existing wakeupUserdevice $wakeupUserdevice";
  2527. }
  2528. elsif ( !IsDevice( $wakeupUserdevice, "RESIDENTS|ROOMMATE|GUEST" ) ) {
  2529. return "$NAME: "
  2530. . "wakeupUserdevice $wakeupUserdevice is not of type RESIDENTS, ROOMMATE or GUEST";
  2531. }
  2532. elsif ( IsDevice( $wakeupUserdevice, "GUEST" )
  2533. && $wakeupUserdeviceState eq "none" )
  2534. {
  2535. Log3 $NAME, 4,
  2536. "RESIDENTStk $NAME: "
  2537. . "GUEST device $wakeupUserdevice has status value 'none' so let's disable this wake-up timer";
  2538. fhem "set $NAME nextRun OFF";
  2539. return;
  2540. }
  2541. elsif ( IsDisabled($wakeupUserdevice) ) {
  2542. Log3 $NAME, 4,
  2543. "RESIDENTStk $NAME: "
  2544. . "wakeupUserdevice disabled - not triggering wake-up program";
  2545. }
  2546. elsif ( lc($nextRun) eq "off" && !$forceRun ) {
  2547. Log3 $NAME, 4,
  2548. "RESIDENTStk $NAME: "
  2549. . "wakeup timer set to OFF - not triggering wake-up program";
  2550. }
  2551. elsif (
  2552. !$forceRun
  2553. && !$days{$today}
  2554. && ( $wakeupHolidays eq ""
  2555. || $wakeupHolidays eq "andHoliday"
  2556. || $wakeupHolidays eq "andNoHoliday" )
  2557. )
  2558. {
  2559. Log3 $NAME, 4,
  2560. "RESIDENTStk $NAME: "
  2561. . "weekday restriction in use - not triggering wake-up program this time";
  2562. }
  2563. elsif (
  2564. !$forceRun
  2565. # && !$days{$today}
  2566. # && (
  2567. # ( $wakeupHolidays eq "andHoliday" && !$holidayToday )
  2568. # || ( $wakeupHolidays eq "andNoHoliday"
  2569. # && $holidayToday )
  2570. # || ( $wakeupHolidays eq "orHoliday" && !$holidayToday )
  2571. # || ( $wakeupHolidays eq "orNoHoliday"
  2572. # && $holidayToday )
  2573. # )
  2574. && (
  2575. (
  2576. $days{$today}
  2577. && ( ( $wakeupHolidays eq "andHoliday" && !$holidayToday )
  2578. || ( $wakeupHolidays eq "andNoHoliday" && $holidayToday ) )
  2579. )
  2580. || (
  2581. !$days{$today}
  2582. && ( ( $wakeupHolidays eq "orHoliday" && !$holidayToday )
  2583. || ( $wakeupHolidays eq "orNoHoliday" && $holidayToday ) )
  2584. )
  2585. )
  2586. )
  2587. {
  2588. Log3 $NAME, 4,
  2589. "RESIDENTStk $NAME: "
  2590. . "holiday restriction $wakeupHolidays in use - not triggering wake-up program this time";
  2591. }
  2592. elsif ($wakeupUserdeviceState eq "absent"
  2593. || $wakeupUserdeviceState eq "gone"
  2594. || $wakeupUserdeviceState eq "gotosleep"
  2595. || $wakeupUserdeviceState eq "awoken" )
  2596. {
  2597. Log3 $NAME, 4,
  2598. "RESIDENTStk $NAME: "
  2599. . "we should not start any wake-up program for resident device $wakeupUserdevice being in state '"
  2600. . $wakeupUserdeviceState
  2601. . "' - not triggering wake-up program this time";
  2602. }
  2603. # general conditions to trigger program fulfilled
  2604. else {
  2605. my $expLastWakeup = time_str2num(
  2606. ReadingsTimestamp(
  2607. $wakeupUserdevice, "lastWakeup", "1970-01-01 00:00:00"
  2608. )
  2609. ) - 1 +
  2610. $wakeupOffset * 60 +
  2611. $wakeupWaitPeriod * 60;
  2612. my $expLastAwake = time_str2num(
  2613. ReadingsTimestamp(
  2614. $wakeupUserdevice, "lastAwake", "1970-01-01 00:00:00"
  2615. )
  2616. ) - 1 +
  2617. $wakeupWaitPeriod * 60;
  2618. if ( !$wakeupMacro ) {
  2619. return "$NAME: missing attribute wakeupMacro";
  2620. }
  2621. elsif ( !IsDevice($wakeupMacro) ) {
  2622. return "$NAME: "
  2623. . "notify macro $wakeupMacro not found - no wakeup actions defined!";
  2624. }
  2625. elsif ( !IsDevice( $wakeupMacro, "notify" ) ) {
  2626. return "$NAME: device $wakeupMacro is not of type notify";
  2627. }
  2628. elsif ($wakeupUserdeviceWakeup) {
  2629. Log3 $NAME, 3,
  2630. "RESIDENTStk $NAME: "
  2631. . "Another wake-up program is already being executed for device $wakeupUserdevice, won't trigger $wakeupMacro";
  2632. }
  2633. elsif ( $expLastWakeup > $nowRunSec && !$forceRun ) {
  2634. Log3 $NAME, 3,
  2635. "RESIDENTStk $NAME: "
  2636. . "won't trigger wake-up program due to non-expired wakeupWaitPeriod threshold since lastWakeup (expLastWakeup=$expLastWakeup > nowRunSec=$nowRunSec)";
  2637. }
  2638. elsif ( $expLastAwake > $nowRunSec && !$forceRun ) {
  2639. Log3 $NAME, 3,
  2640. "RESIDENTStk $NAME: "
  2641. . "won't trigger wake-up program due to non-expired wakeupWaitPeriod threshold since lastAwake (expLastAwake=$expLastAwake > nowRunSec=$nowRunSec)";
  2642. }
  2643. else {
  2644. # conditional enforced wake-up:
  2645. # only if actual wake-up time is
  2646. # earlier than wakeupDefaultTime
  2647. if ( $wakeupEnforced == 3
  2648. && $wakeupDefaultTime
  2649. && UConv::hms2s($wakeupDefaultTime) > UConv::hms2s($lastRun) )
  2650. {
  2651. Log3 $NAME, 4,
  2652. "RESIDENTStk $NAME: "
  2653. . "Enforcing wake-up because wake-up time is earlier than normal (wakeupDefaultTime=$wakeupDefaultTime > lastRun=$lastRun)";
  2654. $wakeupEnforced = 1;
  2655. }
  2656. # conditional enforced wake-up:
  2657. # only if actual wake-up time is not wakeupDefaultTime
  2658. elsif ($wakeupEnforced == 2
  2659. && $wakeupDefaultTime
  2660. && $wakeupDefaultTime ne $lastRun )
  2661. {
  2662. Log3 $NAME, 4,
  2663. "RESIDENTStk $NAME: "
  2664. . "Enforcing wake-up because wake-up is different from normal (wakeupDefaultTime=$wakeupDefaultTime =! lastRun=$lastRun)";
  2665. $wakeupEnforced = 1;
  2666. }
  2667. elsif ( $wakeupEnforced > 1 ) {
  2668. $wakeupEnforced = 0;
  2669. }
  2670. Log3 $NAME, 4,
  2671. "RESIDENTStk $NAME: " . "trigger $wakeupMacro (running=1)";
  2672. fhem "trigger $wakeupMacro "
  2673. . "start $lastRun $wakeupOffset $wakeupEnforced $wakeupUserdevice $wakeupUserdeviceState";
  2674. # Update user device with last wakeup details
  2675. #
  2676. readingsBeginUpdate( $defs{$wakeupUserdevice} );
  2677. readingsBulkUpdate( $defs{$wakeupUserdevice},
  2678. "lastWakeup", $lastRun );
  2679. readingsBulkUpdate( $defs{$wakeupUserdevice},
  2680. "lastWakeupDev", $NAME );
  2681. readingsBulkUpdate( $defs{$wakeupUserdevice}, "wakeup", "1" );
  2682. readingsEndUpdate( $defs{$wakeupUserdevice}, 1 );
  2683. readingsSingleUpdate( $defs{$wakeupUserdevice}, "wakeup", "0", 1 )
  2684. unless ($wakeupOffset);
  2685. readingsSingleUpdate( $defs{$NAME}, "lastRun", $lastRun, 1 );
  2686. if ($wakeupOffset) {
  2687. my $wakeupStopAtdevice = $wakeupAtdevice . "_stop";
  2688. fhem "delete $wakeupStopAtdevice"
  2689. if ( IsDevice($wakeupStopAtdevice) );
  2690. Log3 $NAME, 4,
  2691. "RESIDENTStk $NAME: "
  2692. . "created at-device $wakeupStopAtdevice to stop wake-up program in $wakeupOffset minutes";
  2693. fhem "define $wakeupStopAtdevice at +"
  2694. . UConv::s2hms( $wakeupOffset * 60 + 1 )
  2695. . " set $NAME:FILTER=running=1 stop triggerpost";
  2696. fhem "attr $wakeupStopAtdevice "
  2697. . "comment Auto-created by RESIDENTS Toolkit: temp. at-device to stop wake-up program of timer $NAME when wake-up time is reached";
  2698. $running = 1;
  2699. }
  2700. }
  2701. }
  2702. if ( $running && $wakeupOffset ) {
  2703. readingsBeginUpdate( $defs{$NAME} );
  2704. readingsBulkUpdate( $defs{$NAME}, "running", "1" );
  2705. readingsBulkUpdate( $defs{$NAME}, "state", "running" );
  2706. readingsEndUpdate( $defs{$NAME}, 1 );
  2707. }
  2708. # Update user device with next wakeup details
  2709. #
  2710. readingsBeginUpdate( $defs{$wakeupUserdevice} );
  2711. my ( $nextWakeupDev, $nextWakeup ) =
  2712. RESIDENTStk_wakeupGetNext( $wakeupUserdevice, $NAME );
  2713. if ( !$nextWakeupDev || !$nextWakeup ) {
  2714. $nextWakeupDev = "";
  2715. $nextWakeup = "OFF";
  2716. }
  2717. readingsBulkUpdateIfChanged( $defs{$wakeupUserdevice},
  2718. "nextWakeupDev", $nextWakeupDev );
  2719. readingsBulkUpdateIfChanged( $defs{$wakeupUserdevice},
  2720. "nextWakeup", $nextWakeup );
  2721. readingsEndUpdate( $defs{$wakeupUserdevice}, 1 );
  2722. my $doReset = 1;
  2723. if ( IsDevice( $wakeupResetSwitcher, "dummy" )
  2724. && ReadingsVal( $wakeupResetSwitcher, "state", 0 ) eq "off" )
  2725. {
  2726. $doReset = 0;
  2727. }
  2728. if ( $wakeupDefaultTime && $rdays{$today} && $doReset ) {
  2729. Log3 $NAME, 4,
  2730. "RESIDENTStk $NAME: " . "Resetting based on wakeupDefaultTime";
  2731. fhem "set $NAME:FILTER=state!=$wakeupDefaultTime "
  2732. . "nextRun $wakeupDefaultTime";
  2733. }
  2734. elsif ( !$running ) {
  2735. readingsBeginUpdate( $defs{$NAME} );
  2736. readingsBulkUpdateIfChanged( $defs{$NAME}, "state", $nextRun );
  2737. readingsEndUpdate( $defs{$NAME}, 1 );
  2738. }
  2739. return undef;
  2740. }
  2741. sub RESIDENTStk_wakeupGetNext($;$) {
  2742. my ( $name, $wakeupDeviceRunning ) = @_;
  2743. my $wakeupDeviceAttrName = "";
  2744. $wakeupDeviceAttrName = "rgr_wakeupDevice"
  2745. if ( defined( $attr{$name}{"rgr_wakeupDevice"} ) );
  2746. $wakeupDeviceAttrName = "rr_wakeupDevice"
  2747. if ( defined( $attr{$name}{"rr_wakeupDevice"} ) );
  2748. $wakeupDeviceAttrName = "rg_wakeupDevice"
  2749. if ( defined( $attr{$name}{"rg_wakeupDevice"} ) );
  2750. my $wakeupDeviceList = AttrVal( $name, $wakeupDeviceAttrName, 0 );
  2751. my ( $sec, $min, $hour, $mday, $mon, $year, $today, $yday, $isdst ) =
  2752. localtime(time);
  2753. $hour = "0" . $hour if ( $hour < 10 );
  2754. $min = "0" . $min if ( $min < 10 );
  2755. my $tomorrow = $today + 1;
  2756. $tomorrow = 0 if ( $tomorrow == 7 );
  2757. my $secNow = UConv::hms2s( $hour . ":" . $min ) + $sec;
  2758. my $definitiveNextToday;
  2759. my $definitiveNextTomorrow;
  2760. my $definitiveNextTodayDev;
  2761. my $definitiveNextTomorrowDev;
  2762. my $holidayDevice = AttrVal( "global", "holiday2we", 0 );
  2763. # check for each registered wake-up device
  2764. for my $wakeupDevice ( split /,/, $wakeupDeviceList ) {
  2765. if ( !IsDevice($wakeupDevice) ) {
  2766. Log3 $name, 4,
  2767. "RESIDENTStk $name: "
  2768. . "00 - ignoring reference to non-existing wakeupDevice $wakeupDevice";
  2769. my $wakeupDeviceListNew = $wakeupDeviceList;
  2770. $wakeupDeviceListNew =~ s/,$wakeupDevice,/,/g;
  2771. $wakeupDeviceListNew =~ s/$wakeupDevice,//g;
  2772. $wakeupDeviceListNew =~ s/,$wakeupDevice//g;
  2773. if ( $wakeupDeviceListNew ne $wakeupDeviceList ) {
  2774. Log3 $name, 3,
  2775. "RESIDENTStk $name: "
  2776. . "reference to non-existing wakeupDevice '$wakeupDevice' was removed";
  2777. fhem "attr $name $wakeupDeviceAttrName $wakeupDeviceListNew";
  2778. }
  2779. next;
  2780. }
  2781. my $wakeupAtdevice = AttrVal( $wakeupDevice, "wakeupAtdevice", undef );
  2782. my $wakeupOffset =
  2783. ReadingsVal( $wakeupDevice, "wakeupOffset",
  2784. AttrVal( $wakeupDevice, "wakeupOffset", 0 ) );
  2785. my $wakeupAtNTM = (
  2786. IsDevice($wakeupAtdevice)
  2787. && defined( $defs{$wakeupAtdevice}{NTM} )
  2788. ? substr( $defs{$wakeupAtdevice}{NTM}, 0, -3 )
  2789. : undef
  2790. );
  2791. my $wakeupDays =
  2792. ReadingsVal( $wakeupDevice, "wakeupDays",
  2793. AttrVal( $wakeupDevice, "wakeupDays", "" ) );
  2794. my $wakeupHolidays =
  2795. ReadingsVal( $wakeupDevice, "wakeupHolidays",
  2796. AttrVal( $wakeupDevice, "wakeupHolidays", "" ) );
  2797. my $holidayToday = 0;
  2798. my $holidayTomorrow = 0;
  2799. my $nextRunSrc;
  2800. my $nextRun = ReadingsVal( $wakeupDevice, "nextRun", undef );
  2801. my $ltoday = $today;
  2802. my $ltomorrow = $tomorrow;
  2803. if ( IsDisabled($wakeupDevice)
  2804. || !$nextRun
  2805. || lc($nextRun) eq "off"
  2806. || $nextRun !~ /^([0-9]{2}:[0-9]{2})$/ )
  2807. {
  2808. Log3 $name, 4,
  2809. "RESIDENTStk $name: "
  2810. . "00 - ignoring disabled wakeupDevice $wakeupDevice";
  2811. next;
  2812. }
  2813. Log3 $name, 4,
  2814. "RESIDENTStk $name: "
  2815. . "00 - checking for next wake-up candidate $wakeupDevice";
  2816. # get holiday status for today and tomorrow
  2817. if ( $wakeupHolidays eq "" ) {
  2818. Log3 $name, 4,
  2819. "RESIDENTStk $wakeupDevice: 01 - Not considering any holidays";
  2820. }
  2821. elsif ( IsDevice( $holidayDevice, "holiday" ) ) {
  2822. $holidayToday = 1
  2823. unless (
  2824. ReadingsVal( $holidayDevice, "state", "none" ) eq "none" );
  2825. $holidayTomorrow = 1
  2826. unless (
  2827. ReadingsVal( $holidayDevice, "tomorrow", "none" ) eq "none" );
  2828. Log3 $name, 4,
  2829. "RESIDENTStk $wakeupDevice: "
  2830. . "01 - Holidays to be considered ($wakeupHolidays) - holidayToday=$holidayToday holidayTomorrow=$holidayTomorrow";
  2831. }
  2832. # set day scope for today
  2833. my @days = ($ltoday);
  2834. @days = split /,/, $wakeupDays
  2835. if ( $wakeupDays ne "" );
  2836. my %days = map { $_ => 1 } @days;
  2837. # set day scope for tomorrow
  2838. my @daysTomorrow = ($ltomorrow);
  2839. @daysTomorrow = split /,/, $wakeupDays
  2840. if ( $wakeupDays ne "" );
  2841. my %daysTomorrow = map { $_ => 1 } @daysTomorrow;
  2842. Log3 $name, 4,
  2843. "RESIDENTStk $wakeupDevice: "
  2844. . "02 - possible candidate found - weekdayToday=$ltoday weekdayTomorrow=$ltomorrow";
  2845. my $nextRunSec;
  2846. my $nextRunSecTarget;
  2847. # Use direct information from at-device if possible
  2848. if ( $wakeupAtNTM
  2849. && $wakeupAtNTM =~ /^([0-9]{2}:[0-9]{2})$/ )
  2850. {
  2851. $nextRunSrc = "at";
  2852. $nextRunSec = UConv::hms2s($wakeupAtNTM);
  2853. $nextRunSecTarget = $nextRunSec + $wakeupOffset * 60;
  2854. if ( $wakeupOffset && $nextRunSecTarget >= 86400 ) {
  2855. $nextRunSecTarget -= 86400;
  2856. $ltoday++;
  2857. $ltoday = $ltoday - 7
  2858. if ( $ltoday > 6 );
  2859. $ltomorrow++;
  2860. $ltomorrow = $ltomorrow - 7
  2861. if ( $ltomorrow > 6 );
  2862. }
  2863. Log3 $name, 4,
  2864. "RESIDENTStk $wakeupDevice: "
  2865. . "03 - considering at-device value wakeupAtNTM=$wakeupAtNTM wakeupOffset=$wakeupOffset nextRunSec=$nextRunSec nextRunSecTarget=$nextRunSecTarget";
  2866. }
  2867. else {
  2868. $nextRunSrc = "dummy";
  2869. $nextRunSecTarget = UConv::hms2s($nextRun);
  2870. $nextRunSec = $nextRunSecTarget - $wakeupOffset * 60;
  2871. if ( $wakeupOffset && $nextRunSec < 0 ) {
  2872. $nextRunSec += 86400;
  2873. $ltoday--;
  2874. $ltoday = $ltoday + 7
  2875. if ( $ltoday < 0 );
  2876. $ltomorrow--;
  2877. $ltomorrow = $ltomorrow + 7
  2878. if ( $ltomorrow < 0 );
  2879. }
  2880. Log3 $name, 4,
  2881. "RESIDENTStk $wakeupDevice: "
  2882. . "03 - considering dummy-device value nextRun=$nextRun wakeupOffset=$wakeupOffset nextRunSec=$nextRunSec nextRunSecTarget=$nextRunSecTarget (wakeupAtNTM=$wakeupAtNTM)";
  2883. }
  2884. # still running today
  2885. if ( $nextRunSec > $secNow ) {
  2886. Log3 $name, 4,
  2887. "RESIDENTStk $wakeupDevice: "
  2888. . "04 - this is a candidate for today - weekdayToday=$ltoday";
  2889. # if today is in scope
  2890. if ( $days{$ltoday} ) {
  2891. # if we need to consider holidays in addition
  2892. if (
  2893. ( $wakeupHolidays eq "andHoliday" && !$holidayToday )
  2894. || ( $wakeupHolidays eq "andNoHoliday"
  2895. && $holidayToday )
  2896. )
  2897. {
  2898. Log3 $name, 4,
  2899. "RESIDENTStk $wakeupDevice: "
  2900. . "05 - no run today due to holiday based on combined weekday and holiday decision";
  2901. next;
  2902. }
  2903. # easy if there is no holiday dependency
  2904. elsif ( !$definitiveNextToday
  2905. || $nextRunSec < $definitiveNextToday )
  2906. {
  2907. Log3 $name, 4,
  2908. "RESIDENTStk $wakeupDevice: "
  2909. . "05 - until now, will be NEXT WAKE-UP RUN today based on weekday decision";
  2910. $definitiveNextToday = $nextRunSec;
  2911. $definitiveNextTodayDev = $wakeupDevice;
  2912. }
  2913. }
  2914. elsif ($wakeupHolidays eq ""
  2915. || $wakeupHolidays eq "andHoliday"
  2916. || $wakeupHolidays eq "andNoHoliday" )
  2917. {
  2918. Log3 $name, 4,
  2919. "RESIDENTStk $wakeupDevice: "
  2920. . "05 - won't be running today anymore based on weekday decision";
  2921. next;
  2922. }
  2923. # if we need to consider holidays in parallel to weekdays
  2924. elsif (( $wakeupHolidays eq "orHoliday" && !$holidayToday )
  2925. || ( $wakeupHolidays eq "orNoHoliday" && $holidayToday ) )
  2926. {
  2927. Log3 $name, 4,
  2928. "RESIDENTStk $wakeupDevice: "
  2929. . "06 - won't be running today based on holiday decision";
  2930. next;
  2931. }
  2932. # easy if there is no holiday dependency
  2933. elsif ( !$definitiveNextToday
  2934. || $nextRunSec < $definitiveNextToday )
  2935. {
  2936. Log3 $name, 4,
  2937. "RESIDENTStk $wakeupDevice: "
  2938. . "06 - until now, will be NEXT WAKE-UP RUN today based on holiday decision";
  2939. $definitiveNextToday = $nextRunSec;
  2940. $definitiveNextTodayDev = $wakeupDevice;
  2941. }
  2942. }
  2943. # running later
  2944. else {
  2945. Log3 $name, 4,
  2946. "RESIDENTStk $wakeupDevice: "
  2947. . "04 - this is a candidate for tomorrow or later - weekdayTomorrow=$ltomorrow";
  2948. # if tomorrow is in scope
  2949. if ( $daysTomorrow{$ltomorrow} ) {
  2950. # if we need to consider holidays in addition
  2951. if (
  2952. ( $wakeupHolidays eq "andHoliday" && !$holidayTomorrow )
  2953. || ( $wakeupHolidays eq "andNoHoliday"
  2954. && $holidayTomorrow )
  2955. )
  2956. {
  2957. Log3 $name, 4,
  2958. "RESIDENTStk $wakeupDevice: "
  2959. . "05 - no run tomorrow due to holiday based on combined weekday and holiday decision";
  2960. next;
  2961. }
  2962. # easy if there is no holiday dependency
  2963. elsif ( !$definitiveNextTomorrow
  2964. || $nextRunSec < $definitiveNextTomorrow )
  2965. {
  2966. Log3 $name, 4,
  2967. "RESIDENTStk $wakeupDevice: "
  2968. . "05 - until now, will be NEXT WAKE-UP RUN tomorrow based on weekday decision";
  2969. $definitiveNextTomorrow = $nextRunSec;
  2970. $definitiveNextTomorrowDev = $wakeupDevice;
  2971. }
  2972. }
  2973. elsif ($wakeupHolidays eq ""
  2974. || $wakeupHolidays eq "andHoliday"
  2975. || $wakeupHolidays eq "andNoHoliday" )
  2976. {
  2977. Log3 $name, 4,
  2978. "RESIDENTStk $wakeupDevice: "
  2979. . "05 - won't be running tomorrow based on weekday decision";
  2980. next;
  2981. }
  2982. # if we need to consider holidays in parallel to weekdays
  2983. elsif (
  2984. ( $wakeupHolidays eq "orHoliday" && !$holidayTomorrow )
  2985. || ( $wakeupHolidays eq "orNoHoliday"
  2986. && $holidayTomorrow )
  2987. )
  2988. {
  2989. Log3 $name, 4,
  2990. "RESIDENTStk $wakeupDevice: "
  2991. . "06 - won't be running tomorrow based on holiday decision";
  2992. next;
  2993. }
  2994. # easy if there is no holiday dependency
  2995. elsif ( !$definitiveNextTomorrow
  2996. || $nextRunSec < $definitiveNextTomorrow )
  2997. {
  2998. Log3 $name, 4,
  2999. "RESIDENTStk $wakeupDevice: "
  3000. . "06 - until now, will be NEXT WAKE-UP RUN tomorrow based on holiday decision";
  3001. $definitiveNextTomorrow = $nextRunSec;
  3002. $definitiveNextTomorrowDev = $wakeupDevice;
  3003. }
  3004. }
  3005. if ($wakeupOffset) {
  3006. # add Offset
  3007. $definitiveNextToday += $wakeupOffset * 60
  3008. if ( defined($definitiveNextToday) );
  3009. $definitiveNextTomorrow += $wakeupOffset * 60
  3010. if ( defined($definitiveNextTomorrow) );
  3011. # correct change over midnight
  3012. if ( defined($definitiveNextToday) ) {
  3013. if ( $definitiveNextToday >= 86400 ) {
  3014. $definitiveNextToday -= 86400;
  3015. }
  3016. elsif ( $definitiveNextToday < 0 ) {
  3017. $definitiveNextToday += 86400;
  3018. }
  3019. }
  3020. if ( defined($definitiveNextTomorrow) ) {
  3021. if ( $definitiveNextTomorrow >= 86400 ) {
  3022. $definitiveNextTomorrow -= 86400;
  3023. }
  3024. elsif ( $definitiveNextTomorrow < 0 ) {
  3025. $definitiveNextTomorrow += 86400;
  3026. }
  3027. }
  3028. }
  3029. }
  3030. if ( defined($definitiveNextTodayDev)
  3031. && defined($definitiveNextToday) )
  3032. {
  3033. Log3 $name, 4,
  3034. "RESIDENTStk $name: 07 - next wake-up result: today at "
  3035. . UConv::s2hms($definitiveNextToday)
  3036. . ", wakeupDevice="
  3037. . $definitiveNextTodayDev;
  3038. return ( $definitiveNextTodayDev,
  3039. substr( UConv::s2hms($definitiveNextToday), 0, -3 ) );
  3040. }
  3041. elsif (defined($definitiveNextTomorrowDev)
  3042. && defined($definitiveNextTomorrow) )
  3043. {
  3044. Log3 $name, 4,
  3045. "RESIDENTStk $name: 07 - next wake-up result: tomorrow at "
  3046. . UConv::s2hms($definitiveNextTomorrow)
  3047. . ", wakeupDevice="
  3048. . $definitiveNextTomorrowDev;
  3049. return ( $definitiveNextTomorrowDev,
  3050. substr( UConv::s2hms($definitiveNextTomorrow), 0, -3 ) );
  3051. }
  3052. return ( undef, undef );
  3053. }
  3054. sub RESIDENTStk_TimeSum($$) {
  3055. my ( $val1, $val2 ) = @_;
  3056. my ( $timestamp1, $timestamp2, $math );
  3057. if ( $val1 !~ /^([0-9]{2}):([0-9]{2})$/ ) {
  3058. return $val1;
  3059. }
  3060. else {
  3061. $timestamp1 = UConv::hms2s($val1);
  3062. }
  3063. if ( $val2 =~ /^([\+\-])([0-9]{2}):([0-9]{2})$/ ) {
  3064. $math = $1;
  3065. $timestamp2 = UConv::hms2s("$2:$3");
  3066. }
  3067. elsif ( $val2 =~ /^([\+\-])([0-9]*)$/ ) {
  3068. $math = $1;
  3069. $timestamp2 = $2 * 60;
  3070. }
  3071. else {
  3072. return $val1;
  3073. }
  3074. if ( $math eq "-" ) {
  3075. return substr( UConv::s2hms( $timestamp1 - $timestamp2 ), 0, -3 );
  3076. }
  3077. else {
  3078. return substr( UConv::s2hms( $timestamp1 + $timestamp2 ), 0, -3 );
  3079. }
  3080. }
  3081. sub RESIDENTStk_InternalTimer($$$$$) {
  3082. my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_;
  3083. my $mHash;
  3084. if ( $modifier eq "" ) {
  3085. $mHash = $hash;
  3086. }
  3087. else {
  3088. my $timerName = $hash->{NAME} . "_" . $modifier;
  3089. if ( exists( $hash->{TIMER}{$timerName} ) ) {
  3090. $mHash = $hash->{TIMER}{$timerName};
  3091. }
  3092. else {
  3093. $mHash = {
  3094. HASH => $hash,
  3095. NAME => $hash->{NAME} . "_" . $modifier,
  3096. MODIFIER => $modifier
  3097. };
  3098. $hash->{TIMER}{$timerName} = $mHash;
  3099. }
  3100. }
  3101. InternalTimer( $tim, $callback, $mHash, $waitIfInitNotDone );
  3102. }
  3103. sub RESIDENTStk_RemoveInternalTimer($$) {
  3104. my ( $modifier, $hash ) = @_;
  3105. my $timerName = $hash->{NAME} . "_" . $modifier;
  3106. if ( $modifier eq "" ) {
  3107. RemoveInternalTimer($hash);
  3108. }
  3109. else {
  3110. my $mHash = $hash->{TIMER}{$timerName};
  3111. if ( defined($mHash) ) {
  3112. delete $hash->{TIMER}{$timerName};
  3113. RemoveInternalTimer($mHash);
  3114. }
  3115. }
  3116. }
  3117. sub RESIDENTStk_StartInternalTimers($$) {
  3118. my ($hash) = @_;
  3119. RESIDENTStk_AutoGone($hash);
  3120. RESIDENTStk_DurationTimer($hash);
  3121. }
  3122. sub RESIDENTStk_StopInternalTimers($) {
  3123. my ($hash) = @_;
  3124. delete $hash->{AUTOGONE} if ( $hash->{AUTOGONE} );
  3125. delete $hash->{DURATIONTIMER} if ( $hash->{DURATIONTIMER} );
  3126. RESIDENTStk_RemoveInternalTimer( "AutoGone", $hash );
  3127. RESIDENTStk_RemoveInternalTimer( "DurationTimer", $hash );
  3128. }
  3129. sub RESIDENTStk_findResidentSlaves($;$) {
  3130. my ( $hash, $ret ) = @_;
  3131. my @slaves;
  3132. my @ROOMMATES;
  3133. foreach ( devspec2array("TYPE=ROOMMATE") ) {
  3134. next
  3135. unless (
  3136. defined( $defs{$_}{RESIDENTGROUPS} )
  3137. && grep { $hash->{NAME} eq $_ }
  3138. split( /,/, $defs{$_}{RESIDENTGROUPS} )
  3139. );
  3140. push @ROOMMATES, $_;
  3141. }
  3142. my @GUESTS;
  3143. foreach ( devspec2array("TYPE=GUEST") ) {
  3144. next
  3145. unless (
  3146. defined( $defs{$_}{RESIDENTGROUPS} )
  3147. && grep { $hash->{NAME} eq $_ }
  3148. split( /,/, $defs{$_}{RESIDENTGROUPS} )
  3149. );
  3150. push @GUESTS, $_;
  3151. }
  3152. if ( scalar @ROOMMATES ) {
  3153. $hash->{ROOMMATES} = join( ",", @ROOMMATES );
  3154. }
  3155. elsif ( $hash->{ROOMMATES} ) {
  3156. delete $hash->{ROOMMATES};
  3157. }
  3158. if ( scalar @GUESTS ) {
  3159. $hash->{GUESTS} = join( ",", @GUESTS );
  3160. }
  3161. elsif ( $hash->{GUESTS} ) {
  3162. delete $hash->{GUESTS};
  3163. }
  3164. if ( $hash->{ROOMMATES} ) {
  3165. $ret .= "," if ($ret);
  3166. $ret .= $hash->{ROOMMATES};
  3167. }
  3168. if ( $hash->{GUESTS} ) {
  3169. $ret .= "," if ($ret);
  3170. $ret .= $hash->{GUESTS};
  3171. }
  3172. return RESIDENTStk_findDummySlaves( $hash, $ret );
  3173. }
  3174. sub RESIDENTStk_findDummySlaves($;$);
  3175. sub RESIDENTStk_findDummySlaves($;$) {
  3176. my ( $hash, $ret ) = @_;
  3177. my $prefix = RESIDENTStk_GetPrefixFromType( $hash->{NAME} );
  3178. my $wakeupDevice =
  3179. AttrVal( $hash->{NAME}, $prefix . "wakeupDevice", undef );
  3180. my $presenceDevices =
  3181. AttrVal( $hash->{NAME}, $prefix . "presenceDevices", undef );
  3182. $ret = "" unless ($ret);
  3183. # add r_*wakeupDevice
  3184. if ($wakeupDevice) {
  3185. $ret .= "," if ( $ret ne "" );
  3186. $ret .= $wakeupDevice;
  3187. # wakeupResetSwitcher
  3188. foreach ( split( ',', $wakeupDevice ) ) {
  3189. my $rsw = AttrVal( $_, "wakeupResetSwitcher", "" );
  3190. if ( $rsw =~ /^[a-zA-Z\d._]+$/ ) {
  3191. $ret .= "," if ( $ret ne "" );
  3192. $ret .= $rsw;
  3193. }
  3194. }
  3195. }
  3196. # add r_*presenceDevices
  3197. if ($presenceDevices) {
  3198. foreach ($presenceDevices) {
  3199. my $d = $_;
  3200. $d =~ s/:.*$//g;
  3201. $ret .= "," if ( $ret ne "" );
  3202. $ret .= $d;
  3203. }
  3204. }
  3205. return $ret;
  3206. }
  3207. sub RESIDENTStk_GetPrefixFromType($) {
  3208. my ($name) = @_;
  3209. return $modules{ $defs{$name}{TYPE} }{AttrPrefix}
  3210. if ( $defs{$name}
  3211. && $defs{$name}{TYPE}
  3212. && $modules{ $defs{$name}{TYPE} }
  3213. && $modules{ $defs{$name}{TYPE} }{AttrPrefix} );
  3214. return "";
  3215. }
  3216. sub RESIDENTStk_DoModuleTrigger($$@) {
  3217. my ( $hash, $newState, $noreplace, $TYPE ) = @_;
  3218. $hash = $defs{$hash} unless ( ref($hash) );
  3219. $TYPE = $hash->{TYPE} unless ( defined($TYPE) );
  3220. return ""
  3221. unless ( defined($TYPE)
  3222. && defined( $modules{$TYPE} )
  3223. && defined($newState)
  3224. && $newState =~
  3225. m/^([A-Za-z\d._]+)(?:\s+([A-Za-z\d._]+)(?:\s+(.*))?)?$/ );
  3226. $noreplace = 1 unless ( defined($noreplace) );
  3227. my $e = $1;
  3228. my $d = $2;
  3229. return "DoModuleTrigger() can only handle module related events"
  3230. if ( ( $hash->{NAME} && $hash->{NAME} eq "global" )
  3231. || $d eq "global" );
  3232. return DoTrigger( "global", "$TYPE:$newState", $noreplace )
  3233. unless ( $e =~ /^INITIALIZED|INITIALIZING|MODIFIED|DELETED$/ );
  3234. return "$e: missing device name"
  3235. if ( !defined($d) || $d eq "" );
  3236. my $dret;
  3237. my @rets;
  3238. $hash = $defs{$d};
  3239. if ( !$hash || !ref($hash) ) {
  3240. $dret = "device was deleted"
  3241. unless ( $e eq "DELETED" );
  3242. $e = "DELETED";
  3243. }
  3244. elsif ($e eq "INITIALIZING"
  3245. || ( $hash->{READY} && $hash->{MOD_INIT} )
  3246. || ( $modules{$TYPE}{READY} && $hash->{MOD_INIT} ) )
  3247. {
  3248. $dret = "device initialization started";
  3249. Log 4, "$TYPE $d: $dret";
  3250. $hash->{MOD_INIT} = 1;
  3251. $e = "INITIALIZING";
  3252. }
  3253. elsif ( $hash->{MOD_INIT} ) {
  3254. $dret = "pending device initialization";
  3255. Log 4, "$TYPE $d: $dret";
  3256. }
  3257. elsif ( !$hash->{READY} ) {
  3258. $e = "INITIALIZED";
  3259. $hash->{READY} = 1;
  3260. Log 4, "$TYPE $d: device initialization completed";
  3261. }
  3262. else {
  3263. $e = "MODIFIED";
  3264. }
  3265. my $ret = DoTrigger( "global", "$TYPE:$e $d", $noreplace )
  3266. if ( $e ne "DELETED"
  3267. && ( !$hash->{MOD_INIT} || $e eq "INITIALIZING" ) );
  3268. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3269. ####
  3270. ## track module readiness
  3271. my $initcnt = scalar devspec2array("TYPE=$TYPE:FILTER=MOD_INIT=.+");
  3272. # no more devices of that module
  3273. if ( scalar devspec2array("TYPE=$TYPE") == 0 ) {
  3274. delete $modules{$TYPE}{READY};
  3275. delete $modules{$TYPE}{INIT};
  3276. Log 4, "$TYPE: module is no more in use";
  3277. my $ret = DoTrigger( "global", "$TYPE:DELETED", $noreplace );
  3278. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3279. }
  3280. # all devices completed initialization after bootup
  3281. elsif ( !$modules{$TYPE}{READY} && !$initcnt ) {
  3282. $modules{$TYPE}{READY} = 1;
  3283. delete $modules{$TYPE}{INIT};
  3284. Log 4, "$TYPE: module initialization completed";
  3285. my $ret = DoTrigger( "global", "$TYPE:INITIALIZED", $noreplace );
  3286. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3287. }
  3288. # pending devices during bootup
  3289. elsif ( !$modules{$TYPE}{READY} && !$modules{$TYPE}{INIT} && $initcnt ) {
  3290. $modules{$TYPE}{INIT} = 1;
  3291. }
  3292. # pending devices during runtime
  3293. elsif ( !$modules{$TYPE}{INIT} && $initcnt ) {
  3294. $modules{$TYPE}{INIT} = 1;
  3295. Log 4, "$TYPE: module re-initialization in progress";
  3296. my $ret = DoTrigger( "global", "$TYPE:INITIALIZING", $noreplace );
  3297. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3298. }
  3299. # device constellation changed during runtime
  3300. # with pending devices
  3301. elsif ($modules{$TYPE}{READY}
  3302. && $modules{$TYPE}{INIT}
  3303. && ( !$initcnt || $e eq "DELETED" ) )
  3304. {
  3305. delete $modules{$TYPE}{INIT};
  3306. Log 4, "$TYPE: module re-initialization completed";
  3307. my $ret = DoTrigger( "global", "$TYPE:INITIALIZED", $noreplace );
  3308. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3309. }
  3310. # device constellation changed during runtime
  3311. elsif ( $modules{$TYPE}{READY} && ( !$initcnt || $e eq "DELETED" ) ) {
  3312. Log 4, "$TYPE: module constellation updated";
  3313. my $ret = DoTrigger( "global", "$TYPE:MODIFIED", $noreplace );
  3314. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3315. }
  3316. unshift @rets, " $d: $dret" if ($dret);
  3317. return join( "\n", @rets );
  3318. }
  3319. sub RESIDENTStk_DoInitDev(@) {
  3320. my (@devices) = @_;
  3321. my @rets;
  3322. foreach my $d (@devices) {
  3323. $d = $d->{NAME} if ( ref($d) eq "HASH" );
  3324. next unless ( $defs{$d} );
  3325. $defs{$d}{MOD_INIT} = 1;
  3326. my $ret = CallFn( $d, "InitDevFn", $defs{$d} );
  3327. if ( defined($ret) && $ret eq "0" ) {
  3328. delete $defs{$d}{MOD_INIT};
  3329. next;
  3330. }
  3331. if ( defined($ret) && $ret ne "" ) {
  3332. $defs{$d}{MOD_INIT} = 2;
  3333. $modules{ $defs{$d}{TYPE} }{INIT} = 2;
  3334. push @rets, $ret;
  3335. }
  3336. else {
  3337. delete $defs{$d}{MOD_INIT};
  3338. }
  3339. $ret = RESIDENTStk_DoModuleTrigger( $defs{$d}, "INITIALIZED $d" );
  3340. push @rets, $ret if ( defined($ret) && $ret ne "" );
  3341. }
  3342. return join( "\n", @rets );
  3343. }
  3344. 1;