forked from MrRonfo/openhab-smarther
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smarther.rules
189 lines (165 loc) · 6.99 KB
/
smarther.rules
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
//===================================================
// SET MANUAL TIMER VISIBILITY FLAG
//
rule "Smarther - Set manual timer visibility flag"
when
Item SMA_Thermo_Set_Mode received update
or Item SMA_Thermo_Set_Date received update
then
if (SMA_Thermo_Set_Mode.state == "MANUAL" && (SMA_Thermo_Set_Date.state == 1 || SMA_Thermo_Set_Date.state == 2)) {
SMA_Thermo_Set_ShowTime.sendCommand(ON)
}
else {
SMA_Thermo_Set_ShowTime.sendCommand(OFF)
}
end
//===================================================
// SET APPLY BUTTON VISIBILITY FLAG
//
rule "Smarther - Set Apply button visibility flag"
when
Item SMA_Thermo_Set_Mode received update
or Item SMA_Thermo_Set_Program received update
or Item SMA_Thermo_Set_Date received update
or Item SMA_Thermo_Set_Boost received update
then
if ((SMA_Thermo_Set_Mode.state == "AUTOMATIC" && SMA_Thermo_Set_Program.state == NULL)
|| (SMA_Thermo_Set_Mode.state == "MANUAL" && (SMA_Thermo_Set_Date.state == NULL || SMA_Thermo_Set_Date.state > 2))
|| (SMA_Thermo_Set_Mode.state == "BOOST" && SMA_Thermo_Set_Boost.state == NULL)) {
SMA_Thermo_Set_ShowApply.sendCommand(OFF)
}
else {
SMA_Thermo_Set_ShowApply.sendCommand(ON)
}
end
//===================================================
// RECOVER STATUS AT STARTUP
//
rule "Smarther - Refresh status at startup"
when
System started
then
val String rnd = ((Math::random * 1000.0).intValue + 1).toString
SMA_Thermo_Send_Args.sendCommand("get_status " + rnd)
Thread::sleep(1000)
// Set default command attributes
SMA_Thermo_Set_Function.postUpdate("HEATING")
SMA_Thermo_Set_Mode.postUpdate("MANUAL")
SMA_Thermo_Set_Point.postUpdate(20)
SMA_Thermo_Set_Date.postUpdate(1) // =Today
SMA_Thermo_Set_Hour.postUpdate(23)
SMA_Thermo_Set_Minute.postUpdate(30)
end
//===================================================
// QUICK START HEATING
//
rule "Smarther - Quick start heating"
when
Item SMA_Thermo_Quick_Start received command ON
then
// Set default command attributes
SMA_Thermo_Set_Function.postUpdate("HEATING")
SMA_Thermo_Set_Mode.postUpdate("MANUAL")
SMA_Thermo_Set_Point.postUpdate(20)
SMA_Thermo_Set_Date.postUpdate(1) // =Today
SMA_Thermo_Set_Hour.postUpdate(23)
SMA_Thermo_Set_Minute.postUpdate(30)
// Run thermo set switch command
SMA_Thermo_Set_Switch.sendCommand(ON)
SMA_Thermo_Quick_Start.postUpdate(OFF)
end
//===================================================
// APPLY NEW SETTINGS
//
rule "Smarther - Apply new settings"
when
Item SMA_Thermo_Set_Switch received command ON
then
var Integer setProgram = 0
var Integer setPoint = 0
var String setTimeStr = "forever"
val String setMode = SMA_Thermo_Set_Mode.state.toString.toLowerCase
if (setMode == "automatic") {
setProgram = Integer::parseInt(SMA_Thermo_Set_Program.state.toString)
}
else if (setMode == "manual") {
setPoint = Integer::parseInt(SMA_Thermo_Set_Point.state.toString)
val Integer setDate = Integer::parseInt(SMA_Thermo_Set_Date.state.toString)
val Integer setHour = Integer::parseInt(SMA_Thermo_Set_Hour.state.toString)
val Integer setMinute = Integer::parseInt(SMA_Thermo_Set_Minute.state.toString)
if (setDate == 1) { //Today
setTimeStr = (new DateTime(now.getYear, now.getMonthOfYear, now.getDayOfMonth, setHour, setMinute)).toString
}
else if (setDate == 2) { //Tomorrow
val DateTime tomorrow = now.plusDays(1)
setTimeStr = (new DateTime(tomorrow.getYear, tomorrow.getMonthOfYear, tomorrow.getDayOfMonth, setHour, setMinute)).toString
}
}
else if (setMode == "boost") {
val Integer setBoost = Integer::parseInt(SMA_Thermo_Set_Boost.state.toString)
val DateTime boostStart = now
setTimeStr = boostStart.toString + "/" + (boostStart.plusMinutes(setBoost)).toString
}
logInfo("SMA_Thermo_Send", "Arguments {mode:" + setMode + ", program:" + setProgram + ", setpoint:" + setPoint + ", timer:" + setTimeStr + "}")
val String rnd = ((Math::random * 1000.0).intValue + 1).toString
SMA_Thermo_Send_Args.sendCommand("set_thermo " + setMode + " " + setProgram + " " + setPoint + " " + setTimeStr + " " + rnd)
end
//===================================================
// RECEIVE DATA
//
rule "Smarther - Receive thermo data"
when
Item SMA_Thermo_Send_Exit received update
then
try {
Thread::sleep(500)
val String rspJson = SMA_Thermo_Send_Data.state.toString
logInfo("SMA_Thermo_Send", "Received " + rspJson)
val String rspType = transform("JSONPATH", "$.rsptype", rspJson).toString
val Integer rspCode = Integer::parseInt(transform("JSONPATH", "$.rspcode", rspJson).toString)
if ((rspType == "get_status" || rspType == "set_thermo") && rspCode == 200) {
//=========================
// Refresh thermo measures
//=========================
val String curTemp = transform("JSONPATH", "$.temperature", rspJson).toString
val String curHumi = transform("JSONPATH", "$.humidity", rspJson).toString
SMA_Thermo_Measure_Temperature.postUpdate(curTemp)
SMA_Thermo_Measure_Humidity.postUpdate(curHumi)
//=======================
// Refresh thermo status
//=======================
val String setMode = transform("JSONPATH", "$.mode", rspJson).toString
val String setPoint = transform("JSONPATH", "$.setpoint", rspJson).toString
val String setTime = transform("JSONPATH", "$.time", rspJson).toString
val String setStatus = transform("JSONPATH", "$.status", rspJson).toString
SMA_Thermo_Status_SetMode.postUpdate(setMode)
SMA_Thermo_Status_SetPoint.postUpdate(setPoint)
var String setTimeStr = "Forever"
if (setTime != "forever") {
val DateTime dateSetTime = new DateTime(setTime)
val DateTime dateTomorrow = now.plusDays(1).withTimeAtStartOfDay()
val DateTime dateDayAfter = dateTomorrow.plusDays(1)
if (dateSetTime.isBefore(dateTomorrow)) {
setTimeStr = "Today at " + dateSetTime.toString("HH:mm")
}
else if (dateSetTime.isBefore(dateDayAfter)) {
setTimeStr = "Tomorrow at " + dateSetTime.toString("HH:mm")
}
else {
setTimeStr = dateSetTime.toString("dd/MM/yyyy") + " at " + dateSetTime.toString("HH:mm")
}
}
SMA_Thermo_Status_SetTime.postUpdate(setTimeStr)
//Determine if heating is on
if (setStatus == "ACTIVE") {
SMA_Thermo_Status_Heating.postUpdate(ON)
}
else {
SMA_Thermo_Status_Heating.postUpdate(OFF)
}
}
}
finally {
SMA_Thermo_Set_Switch.postUpdate(OFF)
}
end