-
Notifications
You must be signed in to change notification settings - Fork 10
/
ThermostatModeSwitch.hms
148 lines (132 loc) · 8.93 KB
/
ThermostatModeSwitch.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
! https://github.com/jollyjinx/homematic/blob/master/ThermostatModeSwitch.hms
!
! Script changes all Thermostats to either auto or manual mode depending on the automode variable
!
! If in automode the script also resets the thermostats to automode after some time (resettoautomodedelay)
!
! As the Thermostats do not rember the time they have switched their mode this script uses a
! variable to store the first time the thermostat had changed mode, this variable is created if not exists.
!
!----- User setable variables
boolean automode = true; !.Set to true to set all Thermostats to automode, if false all thermostats
!.will be set to manu mode and off.
!.I actually use the state of a hardware switch to set automode like
!. dom.GetObject("BidCos-RF.KEQ0768205:4.STATE").Value();
integer resettoautomodedelay = 180; !.time in minutes after which to reset the thermostats to the given mode - 0 for off
string systemvariablename = "thermostat.automode.store"; !.systemvariable that stores the state when to switch thermostats back to auto
!.will be created if needed
boolean debug = false; !.if set, output what is done
!
!------ Execution nothing needs to be changed below this line -----------
!
if(debug){WriteLine("Automode:"#automode);}
object systemvariable = dom.GetObject(systemvariablename);
if( !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("stores thermostat modeswitches");
systemvariable.State("");
dom.RTUpdate(0);
}
integer timenow = system.Date("%F %X").ToTime().ToInteger(); if(debug){WriteLine("Timenow:"#timenow);}
integer timetoswitchback = (timenow + (resettoautomodedelay*60 )); if(debug){WriteLine("timetoswitchback:"#timetoswitchback);}
string timetoswitchbackstring = timetoswitchback #":"#(timetoswitchback.ToTime().ToString().StrValueByIndex(" ",1)); if(debug){WriteLine("timetoswitchbackstring:"#timetoswitchbackstring);}
string systemvariablestring = systemvariable.Value(); if(debug){WriteLine("systemvariablestring:"#systemvariablestring);}
string objectsandtimes = systemvariablestring.StrValueByIndex("#",1); if(debug){WriteLine("objectsandtimes:"#objectsandtimes);}
string newobjectsandtimes = "automode:"#automode.ToString()#"#";
string lasttimerunstring = (systemvariablestring.StrValueByIndex("#",0)).StrValueByIndex(":",1); if(debug){WriteLine("lasttimerunstring:"#lasttimerunstring);}
boolean lasttimerunmode = !automode;
if( "true" == lasttimerunstring )
{
lasttimerunmode = true;
}
else
{
if( "false" == lasttimerunstring )
{
lasttimerunmode = false;
}
} if(debug){WriteLine("lasttimerunmode:"#lasttimerunmode);}
if(lasttimerunmode != automode)
{
resettoautomodedelay = 0;
}
string deviceid;
foreach(deviceid, dom.GetObject(ID_DEVICES).EnumUsedIDs())
{
var device = dom.GetObject(deviceid); if(debug){WriteLine("Device:"#device#" (id:"#deviceid#")");}
if( "HM-CC-RT-DN" == device.HssType() )
{
string channelid;
foreach(channelid,device.Channels().EnumUsedIDs())
{
var channel = dom.GetObject(channelid); if(debug){WriteLine("\t Channel:"#channel#" (id:"#channelid#")");}
if( "CLIMATECONTROL_RT_TRANSCEIVER" == channel.HssType() )
{
var interface = dom.GetObject(channel.Interface());
var datapoint = interface#"."#channel.Address(); if(debug){WriteLine("\t Datapoint:"#datapoint);}
integer currentstate= dom.GetObject(datapoint#".CONTROL_MODE").Value(); if(debug){WriteLine("\t State before:"#currentstate);}
!.modes: 0 = auto, 1 = manu, 2 = party, 3 = boost
boolean thermostatinautomode = false;
if( 0==currentstate)
{
thermostatinautomode = true;
}
if( automode != thermostatinautomode )
{ if(debug){WriteLine("\t Device not in suggested mode");}
if( automode && (resettoautomodedelay>0) )
{
boolean hasbeenfound = false;
string objectandtime;
foreach( objectandtime, objectsandtimes.Split(",") )
{ if(debug){WriteLine("\t found in systemvariable:"#objectandtime);}
if( (!hasbeenfound) && (objectandtime.Length()>0) )
{
string storeddevice = objectandtime.StrValueByIndex(":",0); if(debug){WriteLine("\t storeddevice in systemvariable:"#storeddevice#"\t device:"#device);}
if( device.Name() == storeddevice )
{ if(debug){WriteLine("\t Device found equals one in systemvariable:"#objectandtime);}
integer timechanged = objectandtime.StrValueByIndex(":",1).ToInteger();
if( timechanged < timenow )
{ if(debug){WriteLine("\t Setting back to auto mode.");}
dom.GetObject(datapoint#".AUTO_MODE").State(1);
}
else
{
newobjectsandtimes = newobjectsandtimes # "," # objectandtime;
}
hasbeenfound = true;
}
}
}
if( !hasbeenfound )
{
newobjectsandtimes = newobjectsandtimes # "," # device # ":" # timetoswitchbackstring; if(debug){WriteLine("newobjectsandtimes now:"#newobjectsandtimes);}
}
}
else
{
if( 0!=currentstate)
{
dom.GetObject(datapoint#".AUTO_MODE").State(1); if(debug){WriteLine("\t Setting to auto mode");}
}
if( 1!=currentstate )
{
dom.GetObject(datapoint#".MANU_MODE").State(0.0); if(debug){WriteLine("\t Setting to manual mode");}
}
}
if(debug){WriteLine("\t State after:"#dom.GetObject(datapoint#".CONTROL_MODE").Value());}
}
}
}
}
}
if(debug){WriteLine("newobjectsandtimes now:"#newobjectsandtimes);}
if(systemvariablestring!=newobjectsandtimes)
{
systemvariable.State(newobjectsandtimes);
}