-
Notifications
You must be signed in to change notification settings - Fork 10
/
PresenceTriggersTemperature.hms
458 lines (391 loc) · 24.2 KB
/
PresenceTriggersTemperature.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
!https://github.com/jollyjinx/homematic/blob/master/PresenceTriggersTemperature.hms
!
! This script changes the SET_TEMPERATURE of thermostats depending if a person is present
! in a room.
! If there are persons present in the house hand have a location/time/temperature table
! It uses the function/Gewerk 'Heating' to find out switched in that room to deduce presence of someone
! If there are openings in the 'Heating' function/Gewerk so not to heat
!.
real miniumroomtemp = 14.0;
real openwindowtemp = 5.0; !. needs to be the same as set on the device. That way this script
!. can recgonize when the thermostat itself recognises a open window
!. even though no windowsensors are present.
string heatingfunctionname = "Heating"; !. name of the function/"Gewerk" heating objects are put in
!. open doors and windows in a room will switch heating off
!. light,plugs and other switches in a room will signal present persons
!. and will change to comfort temperature if lower
boolean letuserschangeinbetween = false; !. If the set temperature changes due to user interaction or other
!. programming, it will be set the thermostat temperature only when
!. switches/doors or the temperature table will change.
integer resetfrommanumodeafter = 3*3600; !. resets manual mode thermostats after n-seconds. 0 means keep in manual mode
string comfortname = "comforttemperatures"; !. name of the temperature table person that should act as default comfort temperature
string defaultroomname = "anyroom"; !. name of a default room for the person
string systemvariablename = "thermostatrun"; !. name of a system variable (string type) used to store the last used thermostat
!. set temperature. if set to "" it's not used . If the variable does not exist
!. it is created.
integer howoftendowerun = 5*60; !. How often this script runs automatically in seconds
boolean debug = false; !. debugging of script - if set no changes will be made
string temperaturetable = "
comforttemperatures: anyroom 0:00,19C
Wohnzimmer 8:00,18.5C 16:00,19.0C 22:00,18C
Büro 0:00,20C
presence.any: Flur 0:00,16C 7:30,17C 19:30,18C 22:00,17C
Wohnzimmer 0:00,16C 7:30,17C 20:00,18C 22:00,17C
Küche 0:00,16C 7:30,17C 20:00,17C
Wohnzimmer 0:00,16C
presence.dayguests: Flur 6:00,18.0C 7:30,19.5C 11:30,20C 22:30,17C
Wohnzimmer 7:30,19C 11:30,20C 22:30,17C
Küche 6:00,18.0C 7:30,20C 21:30,17C
presence.nightguests: Flur 6:00,18.0C 7:30,19.5C 15:00,20C 22:30,17C
Wohnzimmer 7:30,19C 19:30,20C 22:30,17C
Küche 6:00,18.0C 7:30,20.5C 21:30,17C
Gästezimmer 0:00,17C 9:00,16C 19:00,17C 21:00,19.0C
presence.patrick: Schlafzimmer 0:00,18C
presence.isabel: Flur 7:00,18.5C 7:00,19.5C 20:00,17C
Wohnzimmer 7:00,18.5C 20:00,17C
Küche 7:00,20.5C 20:00,17C
Isabel 7:00,20.5C 20:30,17.5C
presence.officeuser: Büro 0:00,20C
";
!.
!. Temperature table format: [presencevariablename]:\s+(\s*[roomname]\s+(HH?:MM,\d+(.\d)?C)+\n)+
!.
!. Isabel and officeuser are defined as kids. Those persons, that are depending on the presence of a real
!. person are called kids in the presenceaggregation script, even though I use it for a my daughter and my officeuser.
!. 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.
!--- no user setable parts below this point
!.
string currentyearmonthday = system.Date("%F ");
integer timenow = (currentyearmonthday#system.Date("%X")).ToTime().ToInteger();
integer timedaystart = (currentyearmonthday#"00:00:00").ToTime().ToInteger();
integer secondsthisday = timenow-timedaystart;
integer lasttimerun = timenow-howoftendowerun;
!.
!. In case you have the system variable
object systemvariable = dom.GetObject(systemvariablename);
if( (systemvariablename.Length()>0) && (!systemvariable) )
{ if(debug){WriteLine("Variable:"#systemvariablename#" does not exist - creating");}
object systemVariables =dom.GetObject(ID_SYSTEM_VARIABLES);
systemvariable=dom.CreateObject(OT_VARDP);
systemvariable.Name(systemvariablename);
systemVariables.Add(systemvariable.ID());
systemvariable.ValueType(ivtString); !. dokumentation says: boolean = 1; integer 2; real 3 ;time 5; 4 string;
systemvariable.ValueSubType(istChar8859);
systemvariable.DPInfo("current thermostat settings");
systemvariable.State("");
dom.RTUpdate(0);
}
string oldsystemvariablestring = "";
string newsystemvariablestring = "";
if( systemvariable )
{
string laststring = systemvariable.Variable().ToString();
integer offset = laststring.Find("#");
if( -1 != offset )
{
lasttimerun = laststring.StrValueByIndex("#",0).ToTime().ToInteger();
oldsystemvariablestring = laststring.Substr(offset+1,laststring.Length()-(offset+1));
}
}
if(debug){WriteLine("timenow:"#timenow#"\t secondsthisday:"#secondsthisday#"\tlasttimerun"#lasttimerun.ToTime());}
string ascii_space = " ";
string ascii_tabular = "\t";
string ascii_newline = "\n";
string ascii_return = "\r";
string table_delimiter = "#";
string roomtabledelimiter = ascii_newline;
!.
!. create a tabulator seperated temperaturetable
!.
if(true)
{
string substring;
string temporarytemperaturetable1;
string temporarytemperaturetable2;
foreach(substring, temperaturetable.Split(ascii_space) )
{
if( substring.Length() >0 ) { temporarytemperaturetable1 = temporarytemperaturetable1 # table_delimiter # substring; }
}
foreach(substring, temporarytemperaturetable1.Split(ascii_return) )
{
if( substring.Length() >0 ) { temporarytemperaturetable2 = temporarytemperaturetable2 # table_delimiter # substring; }
}
temperaturetable = "";
foreach(substring, temporarytemperaturetable2 )
{
if( substring.Length() >0 ) { temperaturetable = temperaturetable # table_delimiter # substring; }
}
temperaturetable = temperaturetable # ascii_newline;
if(debug) {WriteLine("temperaturetable:"#temperaturetable);}
}
!.
!. go through every person that is present and add figure out hte room preferences
!.
string roomtemperatures;
if(true)
{
string currentperson;
boolean personispresent;
integer personchangedtime;
string comforttemperatures;
string temperaturetableline;
foreach( temperaturetableline, temperaturetable.Split(ascii_newline) )
{ !. if(debug){WriteLine("Line:"#temperaturetableline);}
string currentroom;
integer currentchangetime;
string currentchangetemp;
string temperaturetablepart;
foreach(temperaturetablepart,temperaturetableline.Split(table_delimiter))
{
integer partlength = temperaturetablepart.Length();
if( partlength > 0 )
{ !. if(debug){WriteLine("\nPart:"#temperaturetablepart#" length:"#partlength);}
integer colonposition = temperaturetablepart.Find(":");
if( -1 == colonposition )
{
currentroom = temperaturetablepart; if(debug){WriteLine("\tcurrentroom:"#temperaturetablepart);}
currentchangetime = -1;
currentchangetemp = "-1";
}
else
{
if( colonposition == (partlength-1) )
{
currentperson = temperaturetablepart.Substr(0,partlength-1); if(debug){WriteLine("\nPerson:"#currentperson);}
object personvariable = dom.GetObject(currentperson);
if( personvariable )
{
personispresent = personvariable.Value();
boolean personwaspresent = personvariable.LastValue();
if( personispresent != personwaspresent )
{
personchangedtime = personvariable.Timestamp().ToInteger();
}
else
{
personchangedtime = timedaystart-86400;
} if(debug){WriteLine("\tpresence:"#personispresent#" since:"#personchangedtime.ToTime()#" waspresent:"#personwaspresent);}
}
}
else
{
integer timeseconds = (currentyearmonthday#temperaturetablepart.StrValueByIndex(",",0)#":00").ToTime().ToInteger();
string temperature = temperaturetablepart.StrValueByIndex(",",1);
if( timeseconds > timenow )
{
timeseconds = timeseconds - 86400;
} !.if(debug){WriteLine(" timeseconds:"#timeseconds#" temperature:"#temperature);}
!.if(debug){WriteLine("\troom:"#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp);}
if( -1 == currentchangetime )
{
currentchangetime = timeseconds;
currentchangetemp = temperature;
}
else
{ !.if(debug){WriteLine("\troom:"#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp);}
if( timeseconds > currentchangetime )
{
currentchangetime = timeseconds;
currentchangetemp = temperature;
}
} !.if(debug){WriteLine("\troom:"#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp);}
}
}
}
}
if( currentroom.Length() > 0)
{ if(debug){WriteLine("\tFinal:"#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp);}
if( comfortname == currentperson )
{
comforttemperatures = comforttemperatures#roomtabledelimiter#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp#","#comfortname;
}
else
{
if( !personispresent )
{
currentchangetemp = miniumroomtemp;
}
if( personchangedtime > currentchangetime )
{
currentchangetime = personchangedtime;
}
roomtemperatures = roomtemperatures#roomtabledelimiter#currentroom#","#currentchangetime.ToTime()#","#currentchangetemp#","#currentperson;
}
}
}
roomtemperatures = roomtemperatures # comforttemperatures;
if(debug){WriteLine("\nRoomtemperatures:"#roomtemperatures);}
}
string roomid;
foreach( roomid, dom.GetObject(ID_ROOMS).EnumUsedIDs() )
{
object room = dom.GetObject(roomid); if(debug){WriteLine("\n\nroom:"#room);}
boolean windowordoorsensorpresent = false;
boolean openwindowordoor = false;
boolean somebodyispresent = false;
integer somebodychangedtime = -1;
string thermostatdevices;
string channelid;
foreach(channelid,dom.GetObject(room).EnumUsedIDs())
{ !.if(debug){WriteLine("\t channelid:"#channelid);}
object channel = dom.GetObject(channelid); if(debug){WriteLine("\t channel:"#channel);}
string functionid;
foreach(functionid,channel.ChnFunction())
{ if(debug){WriteLine("\t functionid:"#functionid); }
string functionname = dom.GetObject(functionid); if(debug){WriteLine("\t functionname:"#functionname); }
if( heatingfunctionname == functionname )
{
object device = dom.GetObject(channel.Device()); if(debug){WriteLine("\t device:"#device);}
string devicehssid = device.HssType(); if(debug){WriteLine("\t devicehssid:"#devicehssid);}
if( "HM-CC-RT-DN" == devicehssid )
{
var interface=dom.GetObject(channel.Interface());
thermostatdevices = thermostatdevices # "\t" # interface # "." # channel.Address();
}
else
{
if( !openwindowordoor )
{
string datapointid;
foreach(datapointid,channel.DPs().EnumUsedIDs())
{
var datapoint = dom.GetObject(datapointid);
if(OPERATION_READ & datapoint.Operations())
{
string dpname = datapoint.Name();
if( (-1 != dpname.Find(".STATE")) || ( "HM-RCV-50" == devicehssid ) )
{ if(debug){WriteLine("\t Datapoint:"#dpname);}
if( 0 == devicehssid.Find("HM-Sec-") )
{ if(debug){WriteLine("\t WindowOrDoor: state:"#datapoint.Value());}
windowordoorsensorpresent = true;
if( datapoint.Value() )
{
openwindowordoor = true;
}
}
else
{
if( datapoint.Value() )
{ if(debug){WriteLine("\t Somebody is present in room:"#datapoint.Value());}
somebodyispresent = true;
}
}
integer lastactiontime = channel.LastDPActionTime().ToInteger(); if(debug){WriteLine("\t lastactiontime:"#lastactiontime.ToTime());}
if( somebodychangedtime < lastactiontime )
{
somebodychangedtime = lastactiontime;
}
}
}
}
}
} if(debug){WriteLine("");}
}
}
}
if(debug){ WriteLine("\tThermostats:"#thermostatdevices#"\tSomebodyispresent in room:"#somebodyispresent#" since:"#somebodychangedtime.ToTime()#"\twindow or door open:"#openwindowordoor);}
if( thermostatdevices.Length() )
{
real newtemp = miniumroomtemp;
integer newtime = -1;
if( openwindowordoor )
{
newtemp = openwindowtemp;
newtime = timenow;
}
else
{
string roomtemperatureset;
foreach(roomtemperatureset,roomtemperatures.Split(roomtabledelimiter) )
{
if( roomtemperatureset.Length() > 0 )
{ !.if(debug){WriteLine("\t testing room:"#roomtemperatureset);}
string roomnameset = roomtemperatureset.StrValueByIndex(",",0).ToString();
if( (roomnameset == room) || (roomnameset==defaultroomname) )
{ !.if(debug){WriteLine("\t found room:"#roomtemperatureset);}
integer newtemptime;
real newtempset;
string newtempperson = roomtemperatureset.StrValueByIndex(",",3).ToString();
if(newtempperson == comfortname)
{
if( !somebodyispresent )
{
newtempset = miniumroomtemp;
}
else
{
newtempset = roomtemperatureset.StrValueByIndex(",",2).ToFloat();
}
newtemptime = somebodychangedtime;
}
else
{
newtemptime = roomtemperatureset.StrValueByIndex(",",1).ToTime().ToInteger();
newtempset = roomtemperatureset.StrValueByIndex(",",2).ToFloat();
}
if( newtempset > newtemp)
{
newtemp = newtempset;
}
if( newtemptime > newtime)
{
newtime = newtemptime;
} if(debug){WriteLine("\t newtemp:"#newtemp#"\tTime:"#newtime.ToTime()#"\tperson:"#newtempperson);}
}
}
}
} !.if(debug){WriteLine("\n"#room#"\n\t newtemp:"#newtemp#"\tnewtime:"#newtime.ToTime());}
if( (!letuserschangeinbetween)
|| (newtime > (lasttimerun-howoftendowerun)) )
{ if(debug){WriteLine("WILL SET as it has changed. lastchange:"#newtime.ToTime()#" lasttime:"#lasttimerun.ToTime());};
newsystemvariablestring = newsystemvariablestring #room #":"# newtemp # ",";
string thermostatchanneladdress;
foreach(thermostatchanneladdress,thermostatdevices)
{
if( thermostatchanneladdress.Length() )
{ if(debug){WriteLine("\t thermostatchanneladdress: "#thermostatchanneladdress);}
object thermostatchannel = dom.GetObject(thermostatchanneladdress#".CONTROL_MODE");
integer currentmode = dom.GetObject(thermostatchannel).Value(); if(debug){WriteLine("\t Current mode: "#currentmode);}
!.modes: 0 = auto, 1 = manu, 2 = party, 3 = boost
if( 1 == currentmode )
{
string thermostatdeviceid = thermostatchannel.Device();
integer thermostattimechanged = dom.GetObject(thermostatdeviceid).LastDPActionTime().ToInteger(); if(debug){WriteLine("\t manu mode:thermostattimechanged: "#thermostattimechanged.ToTime());}
if( thermostattimechanged < (timenow-resetfrommanumodeafter) )
{ if(debug){WriteLine("\t Would change Mode to Automatic");}
if(!debug) { dom.GetObject(thermostatchanneladdress#".AUTO_MODE").State(1);}
currentmode = 0;
}
}
if( 0 == currentmode )
{
real termostattempcurrent = dom.GetObject(thermostatchanneladdress#".SET_TEMPERATURE").Value(); if(debug){WriteLine("\t settemp: "#termostattempcurrent);}
if( (!windowordoorsensorpresent) && (termostattempcurrent == openwindowtemp) )
{
if(debug){WriteLine("\t no window or door present, but thermostat is set to openwindowtemp. Ignoring set temperature");}
}
else
{
if( newtemp != termostattempcurrent )
{ if(debug){WriteLine("Would change Temperature to: "#newtemp);}
if(!debug) { dom.GetObject(thermostatchanneladdress#".SET_TEMPERATURE").State(newtemp);}
}
}
}
}
}
}
else
{
if(debug){WriteLine("NOTHING TO set as it has not changed. lastchange:"#newtime.ToTime()#" lasttime:"#lasttimerun.ToTime());};
}
}
}
if( systemvariable && (oldsystemvariablestring != newsystemvariablestring) )
{ if(debug){WriteLine("systemvariable changed\nfrom:\t" # oldsystemvariablestring # "\nto:\t:" # newsystemvariablestring);}
if(!debug){systemvariable.State(system.Date("%F %X") #"#"# newsystemvariablestring);}
}