-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirQualityDriver.groovy
558 lines (485 loc) · 23.2 KB
/
AirQualityDriver.groovy
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* AirQuality Sensor
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*
* TODO
*
*/
metadata {
definition (name: "Air Quality Sensor", namespace: "hzindustries", author: "David McPaul") {
capability "AirQuality"
capability "Sensor"
capability "TemperatureMeasurement"
capability "RelativeHumidityMeasurement"
capability "PresenceSensor"
capability "Configuration"
capability "Refresh"
attribute "voltage", "number"
command "reconfigure"
command "refreshAll"
command "resetToDefaults"
command "identify"
// Home Automation Profile = 0104 (clusterId 0000 = base info, clusterId 0001 = battery (perc, volt), clusterId 0003 = identify, clusterId 0402 = temperature, clusterId 0403 = pressure, clusterId 0405 = humidity)
fingerprint profileId: "0104", endpointId: "01", inClusters: "0004,0005,EF00,0000", outClusters:"0019,000A", manufacturer:"_TZE200_dwcarsat", model:" TS0601", deviceJoinName: "Smart Air Quality Sensor"
}
preferences {
input name: "checkHealth", type: "bool", title: "Enable Health Check", description: "Track the health of the device by checking when the device reports and if no report occurs within the time defined then report as not present. Devices that fail to report will likely need to be rediscovered", defaultValue: true
input name: "healthTimeout", type: "enum", title: "Health Timeout", description: "The number of hours before a device is considered 'not present'.", defaultValue: "12", options: ["1","2","3","4","6","12","24"]
input name: "temperatureOffset", type: "number", title: "Temperature Offset", description: "This setting compensates for an inaccurate temperature sensor. For example, set to -7 if the temperature is 7 degress too warm.", defaultValue: "0"
input name: "humidityOffset", type: "number", title: "Humidity Offset", description: "This setting compensates for an inaccurate humidity sensor. For example, set to -7 if the humidity is 7% too high.", defaultValue: "0"
}
}
public static String version() { return "v1.0.0" }
import groovy.transform.Field
import java.util.concurrent.*
// Field annotation makes these variables global to the class
@Field static java.util.concurrent.Semaphore mutex = new java.util.concurrent.Semaphore(1)
@Field static def queueMap = [:]
// Callbacks
// Parse can be handling multiple event simultaneously (need to single thread the queue mechanism)
def parse(String description) {
deviceReported()
def descMap = zigbee.parseDescriptionAsMap(description)
def label = "Unknown"
if (descMap.cluster != null || descMap.clusterId != null) {
def lookup = descMap.cluster != null ? descMap.cluster : descMap.clusterId
def cluster = zigbee.clusterLookup(lookup)
label = cluster == null ? "Lookup failed for cluster ${lookup}" : cluster.clusterLabel == null ? "Unknown" : cluster.clusterLabel
}
//log.info "${device.displayName} ${device.getDataValue("manufacturer")} ${device.getDataValue("model")}"
if (description?.startsWith("read attr")) {
if (descMap.cluster == "0000" && descMap.attrId == "0001") {
log.info "${device.displayName} Application Version ${descMap.value}"
state.version = descMap.value
} else if (descMap.cluster == "0000" && descMap.attrId == "0004") {
log.info "${device.displayName} Manufacturer Name ${descMap.value}"
state.manufacturerName = descMap.value
identifyDevice()
} else if (descMap.cluster == "0000" && descMap.attrId == "0005") {
log.info "${device.displayName} Model ID ${descMap.value}"
state.modelId = descMap.value
} else if (descMap.cluster == "0000" && descMap.attrId == "0006") {
log.info "${device.displayName} Date Code ${descMap.value}"
} else if (descMap.cluster == "0000" && descMap.attrId == "FF01" && descMap.value.size() > 20) {
log.debug "${device.displayName} Xiaomi data ${descMap.raw[22..27]}"
if (descMap.raw[22..23] == "21") {
batteryVoltageEvent(Integer.parseInt(descMap.raw[26..27] + descMap.raw[24..25], 16) / 100)
}
} else if (descMap.cluster == "0001" && descMap.attrId == "0020") {
batteryVoltageEvent(Integer.parseInt(descMap.value, 16))
} else if (descMap.cluster == "0001" && descMap.attrId == "0021") {
batteryPercentageEvent(Integer.parseInt(descMap.value, 16))
} else if (descMap.cluster == "0402" && descMap.attrId == "0000") {
temperatureEvent(hexStrToSignedInt(descMap.value))
} else if (descMap.cluster == "0403" && descMap.attrId == "0000") {
pressureEvent(Integer.parseInt(descMap.value, 16))
} else if (descMap.cluster == "0405" && descMap.attrId == "0000") {
humidityEvent(Integer.parseInt(descMap.value, 16))
} else {
log.error "${device.displayName} unknown cluster and attribute ${descMap} with data size ${descMap.value.size()}"
}
} else if (description?.startsWith("catchall")) {
if (descMap.clusterId == "0013") {
log.info "${device.displayName} cluster ${descMap.clusterId} device announce? ${descMap.data}"
} else if (descMap.clusterId == "8021") {
log.info "${device.displayName} Bind Successfull with sequence ${descMap.data[0]} and command ${descMap.command}"
} else if (descMap.command == "01") { // Default response to a command - For us it indicates unsupported request
if (descMap.clusterId == "0403") {
log.info "${device.displayName} Pressure Refresh Ignored ${descMap.data}"
}
} else if (descMap.command == "07") { // Configuration responses
if (descMap.clusterId == "0001") {
logConfigureResponse(descMap.clusterId, "battery percentage or voltage", descMap.data[0]) // How to differentiate configure responses here for cluster 0001
} else if (descMap.clusterId == "0006") {
logConfigureResponse(descMap.clusterId, "", descMap.data[0])
} else if (descMap.clusterId == "0402") {
log.info "${device.displayName} Temperature configuration Successfull ${descMap.data}"
} else if (descMap.clusterId == "0403") {
log.info "${device.displayName} Pressure configuration Successfull ${descMap.data}"
} else if (descMap.clusterId == "0405") {
log.info "${device.displayName} Humidity configuration Successfull ${descMap.data}"
}
} else if (descMap.command == "0B") { // Default responses
if (descMap.clusterId == "0003") {
if (descMap.data[0] == "00") {
log.info "${device.displayName} Identify Successfull ${descMap.data}"
} else if (descMap.data[0] == "40") {
log.info "${device.displayName} Trigger Effect Successfull ${descMap.data}"
} else {
log.info "${device.displayName} Unknown Response from Identify cluster ${descMap.data}"
}
} else if (descMap.clusterId == "0402") {
log.info "${device.displayName} Temperature configuration Successfull ${descMap.data}"
} else if (descMap.clusterId == "0403") {
log.info "${device.displayName} Pressure configuration Successfull ${descMap.data}"
} else if (descMap.clusterId == "0405") {
log.info "${device.displayName} Humidity configuration Successfull ${descMap.data}"
}
} else {
log.warn "${device.displayName} unsupported catchall with map ${descMap}"
}
} else {
log.error "${device.displayName} unsupported description ${description} with map ${descMap}"
}
return [:]
}
void identifyDevice() {
log.info "${device.displayName} trying to identify device using manufacturer ${device.getDataValue("manufacturer")} & model ${device.getDataValue("model")}"
state.manufacturerName = device.getDataValue("manufacturer")
state.deviceName = device.getDataValue("model")
}
void installed() {
log.info "${device.displayName} installed() called"
state.clear()
device.updateSetting("checkHealth",[value:"true",type:"bool"])
device.updateSetting("healthTimeout",[value:"12",type:"enum"])
device.updateSetting("temperatureOffset",[value:"0",type:"number"])
device.updateSetting("humidityOffset",[value:"0",type:"number"])
updateDataValue("calcBattery", "true") // Calculate Battery Perc until an Battery Perc event is sent
identifyDevice()
}
void uninstalled() {
log.info "${device.displayName} uninstalled() called"
state.clear()
unschedule()
}
// Called when preferences saved
void updated() {
log.info "${device.displayName} updated() called"
state.clear()
identifyDevice()
resetHealthCheck()
}
def refresh() {
log.info "${device.displayName} refresh() requested"
state.clear()
identifyDevice()
refreshAll()
return getRefreshCmds()
}
// This only seems to work when called as part of device join.
def configure() {
log.info "${device.displayName} configure() requested"
state.clear()
updateDataValue("calcBattery", "true") // Calculate Battery Perc until an Battery Perc event is sent
resetHealthCheck()
identifyDevice()
return getConfigureCmds()
}
// Internal Functions
void resetHealthCheck() {
unschedule()
if (checkHealth != false) {
startHealthCheck()
}
}
void logConfigureResponse(cluster, attribute, code) {
if (code == "00") {
log.info "${device.displayName} cluster ${cluster} successful configure reporting response for ${attribute}"
} else if (code == "86") {
log.error "${device.displayName} cluster ${cluster} UNSUPPORTED_ATTRIBUTE passed to configure ${attribute}"
} else if (code == "8D") {
log.error "${device.displayName} cluster ${cluster} INVALID_DATA_TYPE passed to configure ${attribute}"
}
}
void deviceReported() {
try {
// synchronize this method
mutex.acquire()
sendDelayedCmds()
if (checkHealth != false) { // Null or True
unschedule(reportHealthCheckFail)
if (device.currentValue("presence") != "present") {
present()
}
startHealthCheck()
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mutex.release()
}
}
void sendZigbeeCommands(List<String> cmds) {
log.debug "${device.displayName} sendZigbeeCommands received : ${cmds}"
sendHubCommand(new hubitat.device.HubMultiAction(cmds, hubitat.device.Protocol.ZIGBEE))
}
void sendZigbeeCommands(List<String> cmds, Long delay) {
sendZigbeeCommands(delayBetween(cmds, delay))
}
void resetToDefaults() {
state.clear()
updateDataValue("calcBattery", "true") // Calculate Battery Perc until an Battery Perc event is sent
addToQueue("resetToDefaults")
}
void refreshAll() {
addToQueue("refreshAll")
}
void reconfigure() {
state.clear()
updateDataValue("calcBattery", "true") // Calculate Battery Perc until an Battery Perc event is sent
addToQueue("reconfigure")
}
void identify() {
// Try sending immediately then queue it
sendZigbeeCommands(getIdentifyCmds(), 500)
addToQueue("identify")
log.info "${device.displayName} queued command identify"
}
List<String> getRefreshCmds() {
List<String> cmds = []
cmds += zigbee.readAttribute(0x0000, 0x0004) // Manufacturer Name
cmds += zigbee.readAttribute(0x0000, 0x0005) // Model ID
cmds += zigbee.readAttribute(0x0000, 0x0001) // App Version
cmds += zigbee.readAttribute(0x0000, 0x0006) // Date Code
cmds += zigbee.readAttribute(0x0000, 0xFF01) // Xiaomi Voltage
cmds += zigbee.readAttribute(0x0001, 0x0020) // Battery Voltage
cmds += zigbee.readAttribute(0x0001, 0x0021) // Battery % remaining
cmds += zigbee.readAttribute(0x0402, 0x0000) // Temperature
cmds += zigbee.readAttribute(0x0403, 0x0000) // Pressure
cmds += zigbee.readAttribute(0x0405, 0x0000) // Humidity
return cmds
}
List<String> getConfigureCmds() {
List<String> cmds = []
//List configureReporting(Integer clusterId, Integer attributeId, Integer dataType, Integer minReportTime, Integer maxReportTime, Integer reportableChange = null, Map additionalParams=[:], int delay = STANDARD_DELAY_INT)
cmds += zigbee.configureReporting(0x0402, 0x0000, DataType.INT16, 300, 3600, 100, [:], 500) // Configure temperature - Report once per hour
cmds += zigbee.configureReporting(0x0403, 0x0000, DataType.INT16, 300, 3600, 100, [:], 500) // Configure Pressure - Report once per hour
cmds += zigbee.configureReporting(0x0405, 0x0000, DataType.INT16, 300, 3600, 100, [:], 500) // Configure Humidity - Report once per hour
// Xiaomi does not report battery using 0x0001
if (state.manufacturerName == "Xiaomi") {
cmds += zigbee.configureReporting(0x0000, 0xFF01, DataType.UINT8, 0, 21600, 1, [:], 500) // Configure Voltage - Report once per 6hrs or if a change of 100mV detected
} else {
cmds += zigbee.configureReporting(0x0001, 0x0020, DataType.UINT8, 0, 21600, 1, [:], 500) // Configure Voltage - Report once per 6hrs or if a change of 100mV detected
cmds += zigbee.configureReporting(0x0001, 0x0021, DataType.UINT8, 0, 21600, 1, [:], 500) // Configure Battery % - Report once per 6hrs or if a change of 1% detected
}
return cmds
}
List<String> getResetToDefaultsCmds() {
List<String> cmds = []
if (state.manufacturerName == "Xiaomi") {
cmds += zigbee.configureReporting(0x0000, 0xFF01, DataType.UINT8, 0, 0xFFFF, null, [:], 500) // Reset Battery Voltage reporting to default
} else {
cmds += zigbee.configureReporting(0x0001, 0x0020, DataType.UINT8, 0, 0xFFFF, null, [:], 500) // Reset Battery Voltage reporting to default
cmds += zigbee.configureReporting(0x0001, 0x0021, DataType.UINT8, 0, 0xFFFF, null, [:], 500) // Reset Battery % reporting to default
}
cmds += zigbee.configureReporting(0x0402, 0x0000, DataType.INT16, 0, 0xFFFF, null, [:], 500) // Reset Temperature reporting to default (looks to be 1/2 hr reporting)
cmds += zigbee.configureReporting(0x0403, 0x0000, DataType.INT16, 0, 0xFFFF, null, [:], 500) // Reset Pressure reporting to default (looks to be 1/2 hr reporting)
cmds += zigbee.configureReporting(0x0405, 0x0000, DataType.UINT16, 0, 0xFFFF, null, [:], 500) // Reset Humidity reporting to default (looks to be 1/2 hr reporting)
//cmds += "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0001 {${device.zigbeeId}} {}"
//cmds += "he cr 0x${device.deviceNetworkId} 0x01 0x0001 0x0020 0x20 0xFFFF 0x0000 {0000}"
//cmds += "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0001 {${device.zigbeeId}} {}"
//cmds += "he cr 0x${device.deviceNetworkId} 0x01 0x0001 0x0021 0x20 0xFFFF 0x0000 {0000}"
//cmds += "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0402 {${device.zigbeeId}} {}"
//cmds += "he cr 0x${device.deviceNetworkId} 0x01 0x0402 0x0000 0x29 0xFFFF 0x0000 {0000}"
//cmds += "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0405 {${device.zigbeeId}} {}"
//cmds += "he cr 0x${device.deviceNetworkId} 0x01 0x0405 0x0000 0x21 0xFFFF 0x0000 {0000}"
return cmds
}
def intTo16bitUnsignedHex(value) {
def hexStr = zigbee.convertToHexString(value.toInteger(),4)
return new String(hexStr.substring(2, 4) + hexStr.substring(0, 2))
}
def intTo8bitUnsignedHex(value) {
return zigbee.convertToHexString(value.toInteger(), 2)
}
void addToQueue(String command) {
queueMap.put(device.displayName, command)
log.info "${device.displayName} queued command " + queueMap.get(device.displayName)
}
String removeFromQueue() {
String command = queueMap.get(device.displayName)
if (command != null) {
log.info "${device.displayName} reading command " + command
queueMap.put(device.displayName, null)
}
return command
}
private getEFFECT_BLINK() { 0 }
private getEFFECT_BREATHE() { 1 }
private getEFFECT_OK() { 2 }
private getEFFECT_CHANNEL_CHANGE() { 11 }
private getEFFECT_FINISH() { 254 }
private getEFFECT_STOP() { 255 }
private getIDENTIFY_CMD_IDENTIFY() { 0x00 }
private getIDENTIFY_CMD_QUERY() { 0x01 }
private getIDENTIFY_CMD_TRIGGER() { 0x40 }
List<String> getIdentifyCmds() {
List<String> cmds = []
// Identify for 60 seconds
cmds += "he cmd 0x${device.deviceNetworkId} 0x${device.endpointId} 0x0003 ${IDENTIFY_CMD_IDENTIFY} { 0x${intTo16bitUnsignedHex(60)} }"
// Trigger Effect
//cmds += "he cmd 0x${device.deviceNetworkId} 0x${device.endpointId} 0x0003 ${IDENTIFY_CMD_TRIGGER} { 0x${intTo8bitUnsignedHex(EFFECT_BREATHE)} 0x${intTo8bitUnsignedHex(0)} }"
return cmds;
}
void sendDelayedCmds() {
String command = removeFromQueue()
if (command != null) {
log.debug "${device.displayName} sending delayed command ${command}"
if (command == "resetToDefaults") {
sendZigbeeCommands(getResetToDefaultsCmds())
} else if (command == "refreshAll") {
sendZigbeeCommands(getRefreshCmds(), 500)
} else if (command == "reconfigure") {
sendZigbeeCommands(getConfigureCmds())
} else if (command == "identify") {
sendZigbeeCommands(getIdentifyCmds(), 500)
}
}
}
def startHealthCheck() {
if (healthTimeout == null) healthTimeout = "12"
def timeoutAsHours = healthTimeout.toInteger() * 60 * 60
runIn(timeoutAsHours, "reportHealthCheckFail")
}
def reportHealthCheckFail() {
notPresent()
}
// Events generated
def temperatureEvent(rawValue) {
// rawValue represents the temperature in degrees Celsius as follows:
// Value = 100 x temperature in degrees Celsius. Where -273.15°C <= temperature <= 327.67 ºC, corresponding to a Value in the range 0x954d to 0x7fff.
// The maximum resolution this format allows is 0.01 ºC.
// A Value of 0x8000 indicates that the temperature measurement is invalid
if (rawValue != 32768) {
BigDecimal offset = temperatureOffset ? new BigDecimal(temperatureOffset).setScale(2, BigDecimal.ROUND_HALF_UP) : 0
BigDecimal temp = new BigDecimal(rawValue).setScale(2, BigDecimal.ROUND_HALF_UP) / 100
// Apply offset and convert to F if location scale set to F
temp = (location.temperatureScale == "F") ? ((temp * 1.8) + 32) + offset : temp + offset
sendEvent("name": "temperature", "value": temp, "unit": "\u00B0" + location.temperatureScale)
log.info "${device.displayName} temperature changed to ${temp}\u00B0 ${location.temperatureScale} calculated from raw value ${rawValue} and offset ${offset}"
} else {
log.error "${device.displayName} temperature read failed"
}
}
def humidityEvent(rawValue) {
// Value represents the relative humidity in % as follows:
// Value = 100 x Relative humidity Where 0% <= Relative humidity <= 100%, corresponding to a value in the range 0 to 0x2710.
// The maximum resolution this format allows is 0.01%.
// A value of 0xffff indicates that the measurement is invalid.
if (rawValue != 65535 && rawValue <= 10000) {
BigDecimal offset = humidityOffset ? new BigDecimal(humidityOffset).setScale(2, BigDecimal.ROUND_HALF_UP) : 0
BigDecimal humidity = new BigDecimal(rawValue).setScale(2, BigDecimal.ROUND_HALF_UP) / 100 + offset
sendEvent("name": "humidity", "value": humidity, "unit": "%")
log.info "${device.displayName} humidity changed to ${humidity}% calculated from raw value ${rawValue} and offset ${offset}"
} else {
log.error "${device.displayName} humidity read failed"
}
}
def pressureEvent(rawValue) {
// Value represents the pressure in kPa as follows:
// Value = 10 x Pressure where -3276.7 kPa <= Pressure <= 3276.7 kPa, corresponding to a value in the range 0x8001 to 0x7fff.
// A Valueof 0x8000 indicates that the pressure measurement is invalid.
if (rawValue != 32768) {
Integer pressure = rawValue // Divide by 10 for kPa or leave for hPa
sendEvent("name": "pressure", "value": pressure, "unit": "hPa")
log.info "${device.displayName} pressure changed to ${pressure} hPa calculated from raw value ${rawValue}"
} else {
log.error "${device.displayName} pressure read failed"
}
}
def batteryVoltageEvent(rawValue) {
// The BatteryVoltage attribute is 8 bits in length and specifies the current actual (measured) battery voltage, in units of 100mV
BigDecimal batteryVolts = new BigDecimal(rawValue).setScale(2, BigDecimal.ROUND_HALF_UP) / 10
if (batteryVolts > 0){
sendEvent("name": "voltage", "value": batteryVolts, "unit": "volts")
log.info "${device.displayName} voltage changed to ${batteryVolts}V calculated from raw value ${rawValue}"
if (getDataValue("calcBattery") == null || getDataValue("calcBattery") == "true") {
updateDataValue("calcBattery", "true") // We will calculate until a battery perc event occurs
// Guess at percentage remaining
// Battery percantage is not a linear relationship to voltage
// Should try to do this as a table with more ranges
def batteryValue = 100.0
if (state.manufacturerName == "TUYATEC") {
// Battery used is a 3V Battery
if (rawValue < 20.01) {
batteryValue = 0.0
} else if (rawValue < 24.01) {
batteryValue = 10.0
} else if (rawValue < 25.01) {
batteryValue = 20.0
} else if (rawValue < 26.01) {
batteryValue = 30.0
} else if (rawValue < 27.01) {
batteryValue = 40.0
} else if (rawValue < 27.51) {
batteryValue = 50.0
} else if (rawValue < 28.01) {
batteryValue = 60.0
} else if (rawValue < 28.51) {
batteryValue = 75.0
} else if (rawValue < 29.01) {
batteryValue = 95.0
} else if (rawValue <= 29.99) {
batteryValue = 99.0
}
} else {
// Battery used is a 3.2V Battery
if (rawValue < 20.01) {
batteryValue = 0.0
} else if (rawValue < 24.01) {
batteryValue = 10.0
} else if (rawValue < 25.01) {
batteryValue = 20.0
} else if (rawValue < 26.01) {
batteryValue = 30.0
} else if (rawValue < 27.01) {
batteryValue = 40.0
} else if (rawValue < 27.51) {
batteryValue = 50.0
} else if (rawValue < 28.01) {
batteryValue = 60.0
} else if (rawValue < 28.51) {
batteryValue = 70.0
} else if (rawValue < 29.01) {
batteryValue = 80.0
} else if (rawValue < 29.51) {
batteryValue = 90.0
} else if (rawValue < 30.01) {
batteryValue = 92.0
} else if (rawValue < 30.51) {
batteryValue = 95.0
} else if (rawValue < 31.01) {
batteryValue = 97.0
} else if (rawValue < 31.51) {
batteryValue = 99.0
}
}
sendEvent("name": "battery", "value": batteryValue, "unit": "%")
log.info "${device.displayName} battery % remaining changed to ${batteryValue}% calculated from voltage ${batteryVolts}"
}
}
}
def batteryPercentageEvent(rawValue) {
// The BatteryPercentageRemaining attribute specifies the remaining battery life as a half integer percentage of the full battery capacity
// (e.g., 34.5%, 45%, 68.5%, 90%) with a range between zero and 100%, with 0x00 = 0%, 0x64 = 50%, and 0xC8 = 100%
// A value of 0xff indicates that the measurement is invalid.
if (rawValue != 255) {
Float pct = rawValue / 2
def batteryValue = Math.min(100, pct)
sendEvent("name": "battery", "value": batteryValue, "unit": "%")
log.info "${device.displayName} battery % remaining changed to ${batteryValue}% calculated from raw value ${rawValue}"
updateDataValue("calcBattery", "false") // Battery events are generated so no need to calc
} else {
log.error "${device.displayName} battery % remaining read failed"
}
}
// Not sure if using the presence sensor is good for this?
def present() {
sendEvent("name": "presence", "value": "present", isStateChange: true)
log.info "${device.displayName} contact changed to present"
}
def notPresent() {
sendEvent("name": "presence", "value": "not present", isStateChange: true)
log.warn "${device.displayName} contact changed to not present"
}