-
Notifications
You must be signed in to change notification settings - Fork 2
/
netatmo2influxdb.py
91 lines (83 loc) · 2.57 KB
/
netatmo2influxdb.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
91
#!/usr/bin/python3
# encoding=utf-8
from pytz import timezone
import datetime
from influxdb import InfluxDBClient
import json
import lnetatmo
import os
import sys
#
debug_str=os.getenv("DEBUG", "False")
debug = debug_str.lower() == "true"
# settings from EnvionmentValue
netatmo_clientId=os.getenv('NETATMO_CLIENT_ID', "")
netatmo_clientSecret=os.getenv('NETATMO_CLIENT_SECRET', "")
netatmo_username=os.getenv('NETATMO_USERNAME')
netatmo_password=os.getenv('NETATMO_PASSWORD')
#
influxdb_host=os.getenv('INFLUXDB_HOST', "")
influxdb_port=int(os.getenv('INFLUXDB_PORT', "8086"))
influxdb_username=os.getenv('INFLUXDB_USERNAME', "")
influxdb_password=os.getenv('INFLUXDB_PASSWORD', "")
influxdb_database=os.getenv('INFLUXDB_DATABASE', "")
# netatmo
authorization = lnetatmo.ClientAuth(clientId=netatmo_clientId,
clientSecret=netatmo_clientSecret,
username=netatmo_username,
password=netatmo_password)
devList = lnetatmo.WeatherStationData(authorization)
# influxdb
client = InfluxDBClient(influxdb_host,
influxdb_port,
influxdb_username,
influxdb_password,
influxdb_database)
# these keys are float
keylist=['Temperature', 'min_temp', 'max_temp', 'Pressure','AbsolutePressure']
def send_data(ds):
#
senddata={}
dd=ds['dashboard_data']
for key in dd:
senddata["measurement"]=key
senddata["time"]=datetime.datetime.fromtimestamp(dd['time_utc']).strftime("%Y-%m-%dT%H:%M:%S")
if debug:
print senddata["time"]
senddata["tags"]={}
senddata["tags"]["host"]=ds['_id']
if key in keylist:
dd[key]=float(dd[key])
senddata["fields"]={}
senddata["fields"]["value"]=dd[key]
if debug:
print json.dumps(senddata,indent=4)
client.write_points([senddata])
for name in devList.modulesNamesList():
if debug:
print "--- module"
print name
ds=devList.moduleByName(name)
if ds is None:
continue
if not 'dashboard_data' in ds:
continue
if debug:
print ds['_id']
send_data(ds)
for station_id in devList.stations:
if debug:
print "--- station"
print station_id
ds=devList.stationById(station_id)
if ds is None:
continue
if not 'dashboard_data' in ds:
continue
if debug:
if 'station_name' in ds:
print ds['station_name']
else:
print station_id
print ds['_id']
send_data(ds)