-
Notifications
You must be signed in to change notification settings - Fork 10
/
presenceVariableAggregation.hms
131 lines (115 loc) · 5.35 KB
/
presenceVariableAggregation.hms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
! https://github.com/jollyjinx/homematic/blob/master/presenceVariableAggregation.hms
!
! Aggregates presence variables to ones that are used in scripts.
!
! Not all persons have a WLAN device to track their presence in the house.
! So I have variables that can be manually set, or set via a calendar/timetable.
!
! Presence variables have the form: presense.[presencetype].[personname]
! Aggregated variables have the form: presense.[personname] and presence.any
!
! Lets assume the following variables are set:
!
! presence.wlan.guests: away
! presence.wlan.patrick: PRESENT My phone is logged to the wlan
! presence.wlan.isabel: away
!
! presence.manual.guests: away
! presence.manual.patrick: away
! presence.manual.isabel: PRESENT manually set via a virtual ccu2 switch
!
! presence.calender.guests: away presence set via a timetable
! presence.calender.patrick: away
! presence.calender.isabel: away
! .
! .
! .
! Those variables are aggregated with this through variables named presence.type.personname
! This script creates then aggregated presence variables:
!
! presence.any: PRESENT
! presence.guests: away
! presence.patrick: PRESENT
! presence.isabel: PRESENT
! .
! .
! .
!
! As I have a timetable for my daughter isabel and she has no presence device
! she is set at home via a timetable or manually. As she is not at home alone
! her aggregated presence is only set when someone else is at home.
!
! Those persons, that are depending on the presence of a real person are called
! kids here, even though I use it for a my daughter and my office.
! When I'm at home and my office computer has a lit screen, then my office is
! getting warm.
! This can also be used for shift workers to have three depending users that have
! differrent work times an those are present depending on a calender and the presence
! of the real user.
!
!----- User setable variables
string presenceprefix = "presence";
string anyperson = "any";
string kids = "isabel,officeuser"; ! comma seperated list
boolean debug = false;
!
!------- nothing needs to be changed below.
!
string allpersons = ","#anyperson#",";
string allkids = ","#kids#",";
string presentpersons = ",";
boolean adultispresent = false;
string variableID;
foreach(variableID, dom.GetObject(ID_SYSTEM_VARIABLES).EnumUsedIDs())
{
string variablename = dom.GetObject(variableID).Name(); if(debug){WriteLine("Variable:"#variablename);}
if( presenceprefix == variablename.StrValueByIndex(".",0) )
{ if(debug){WriteLine("\t is presence variable");}
string personname = variablename.StrValueByIndex(".",2);
if( (personname.Length() > 0) && (personname != anyperson) )
{ if(debug){WriteLine("\t personname:"#personname);}
if( -1 == allpersons.Find(","#personname#",") )
{
allpersons = allpersons#personname#","; if(debug){WriteLine("\tAdding to all:"#personname);}
}
if( -1 == presentpersons.Find(","#personname#",") )
{
boolean personispresent = dom.GetObject(variablename).Value(); if(debug){WriteLine("\t"#personname#" is present:"#personispresent);}
if( personispresent )
{
if( (!adultispresent) && (-1 == allkids.Find(","#personname#",")) )
{
adultispresent = true;
presentpersons = ","#anyperson#presentpersons; if(debug){WriteLine("\tadultispresent is present.");}
}
presentpersons = presentpersons#personname#","; if(debug){WriteLine("\tpresentpersons:"#presentpersons);}
}
}
else
{
if(debug){WriteLine("\t"#personname#" already present");}
}
}
}
}
if(debug) { WriteLine("\nallpersons:"#allpersons#"\n\tpresentpersons:"#presentpersons#"\n\tadultispresent:"#adultispresent); }
string personname;
foreach(personname,allpersons.Split(","))
{
if( personname.Length() > 0 )
{
boolean ispresent = false;
if( (adultispresent) && (-1 != presentpersons.Find(","#personname#",")) )
{ if(debug){WriteLine("\tpersonname found:"#personname);}
ispresent = true;
}
var presencevariable = dom.GetObject(presenceprefix#"."#personname);
if( ispresent != presencevariable.Value() )
{ if(debug){WriteLine("\t"#presenceprefix#"."#personname#" has changed:"#ispresent);}
if(!debug){presencevariable.State(ispresent);}
}
else
{ if(debug){WriteLine("\t"#presenceprefix#"."#personname#" has not changed:"#ispresent);}
}
}
}