This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue-sensors.py
90 lines (80 loc) · 2.48 KB
/
hue-sensors.py
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
#/usr/bin/env python3
#
# hue-sensors.py
#
# store HUE sensors data in influxdb
#
# author: Henrik Pingel
# email: [email protected]
#
# license: MIT
import json
import requests
import time
from influxdb import InfluxDBClient
from datetime import datetime
influxdb_dbname = 'test'
influxdb_user = 'admin'
influxdb_password = 'admin'
influxdb_host = 'influxdb host'
influxdb_port = 'influxdb port'
hue_host = "hue bridge ip"
hue_user = "hue bridge user api key"
update_interval = 1
client = InfluxDBClient(influxdb_host, influxdb_port, influxdb_user, influxdb_password, influxdb_dbname)
def update_data():
r = requests.get(url="http://" + hue_host + "/api/" + hue_user + "/sensors")
j = r.json()
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
for key in j.items():
if key[1]['type'] == 'ZLLLightLevel':
json_body = [
{
"measurement": "lightlevel",
"tags": {
"sensor": key[1]['name']
},
"time": current_time,
"fields": {
"value": key[1]['state']['lightlevel'],
}
}
]
client.write_points(json_body)
if key[1]['type'] == 'ZLLTemperature':
value_string = str(key[1]['state']['temperature'])
json_body = [
{
"measurement": "temperature",
"tags": {
"sensor": key[1]['name']
},
"time": current_time,
"fields": {
"value": value_string[:2] + '.' + value_string[-2:]
}
}
]
client.write_points(json_body)
if key[1]['type'] == 'ZLLPresence':
presence_state = key[1]['state']['presence']
if presence_state == False:
presence_value = 0
else:
presence_value = 1
json_body = [
{
"measurement": "status",
"tags": {
"sensor": key[1]['name']
},
"time": current_time,
"fields": {
"value": presence_value
}
}
]
client.write_points(json_body)
time.sleep(update_interval)
while True:
update_data()