-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathow-server-child-humidity.groovy
122 lines (91 loc) · 3.7 KB
/
ow-server-child-humidity.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
def version() {'v0.2.0'}
metadata {
definition (name: 'OW-Server 1-Wire - Child - Humidity',
namespace: 'ckamps',
author: 'Christopher Kampmeier',
importUrl: 'https://raw.githubusercontent.com/ckamps/hubitat-drivers-ow-server/master/ow-server-child-humidity.groovy') {
capability 'RelativeHumidityMeasurement'
capability 'TemperatureMeasurement'
capability 'Refresh'
}
preferences {
input name: 'humidityOffset', type: 'decimal', title: 'Humidity Offset', description: '-n, +n or n to adjust sensor reading', range:'*..*'
input name: 'tempOffset', type: 'decimal', title: 'Temperature Offset', description: '-n, +n or n to adjust sensor reading', range:'*..*'
input name: 'logEnable', type: 'bool', title: 'Enable debug logging', defaultValue: false
}
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def initialize() {
state.version = version()
}
def poll() {
refresh()
}
def refresh() {
sensorId = device.deviceNetworkId
if (logEnable) log.debug("Getting humidity and temperature for sensor: ${sensorId}")
try {
humidity = getHumidity(sensorId)
}
catch (Exception e) {
log.warn("Can't obtain humidity for sensor ${sensorId}: ${e}")
return
}
if (logEnable) log.debug("Humidity: ${humidity}")
humidity = humidityOffset ? (humidity + humidityOffset) : humidity
humidity = humidity.round(1)
sendEvent(
name: 'humidity',
value: humidity,
unit: "%RH",
descriptionText: "Humidity is ${humidity}%",
)
try {
temp = getTemperature(sensorId)
}
catch (Exception e) {
log.warn("Can't obtain temperature for sensor ${sensorId}: ${e}")
return
}
if (logEnable) log.debug("Temperature - C: ${temp}")
temp = (location.temperatureScale == "F") ? ((temp * 1.8) + 32) : temp
temp = tempOffset ? (temp + tempOffset) : temp
temp = temp.round(2)
sendEvent(
name: 'temperature',
value: temp,
unit: "°${location.temperatureScale}",
descriptionText: "Temperature is ${temp}°${location.temperatureScale}",
translatable: true
)
def nowDay = new Date().format('MMM dd', location.timeZone)
def nowTime = new Date().format('h:mm a', location.timeZone)
sendEvent(
name: 'lastUpdated',
value: nowDay + " at " + nowTime,
displayed: false
)
}
private float getHumidity(sensorId) {
def uri = "http://${parent.getOwServerAddress()}/details.xml"
response = parent.doHttpGet(uri)
if (!response) throw new Exception("doHttpGet to get humidity returned empty response ${sensorId}")
element = response.'**'.find{ it.ROMId == sensorId }
if (!element) throw new Exception("Can't find matching ROMId element in response from OW-Server for sensor ${sensorId}")
if (!element.Humidity) throw new Exception("Humidity element does not exist in response from OW-Server for sensor ${sensorId}")
return(element.Humidity.toFloat())
}
private float getTemperature(sensorId) {
def uri = "http://${parent.getOwServerAddress()}/details.xml"
response = parent.doHttpGet(uri)
if (!response) throw new Exception("doHttpGet to get temperature returned empty response ${sensorId}")
element = response.'**'.find{ it.ROMId == sensorId }
if (!element) throw new Exception("Can't find matching ROMId element in response from OW-Server for sensor ${sensorId}")
if (!element.Temperature) throw new Exception("Temperature element does not exist in response from OW-Server for sensor ${sensorId}")
return(element.Temperature.toFloat())
}