-
Notifications
You must be signed in to change notification settings - Fork 5
/
netatmo2influxdb.py
132 lines (119 loc) · 3.63 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
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
#!/usr/bin/python3
# encoding=utf-8
from pytz import timezone
import datetime
from influxdb import InfluxDBClient
import json
import lnetatmo
import os
import sys
import requests
#
debug_str=os.getenv("DEBUG", None)
if debug_str is not None:
debug = debug_str.lower() == "true"
else:
debug = False
# 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')
# influx env variables
influxdb_host=os.getenv('INFLUXDB_HOST', "localhost")
influxdb_port=int(os.getenv('INFLUXDB_PORT', "8086"))
influxdb_username=os.getenv('INFLUXDB_USERNAME', "root")
influxdb_password=os.getenv('INFLUXDB_PASSWORD', "root")
influxdb_database=os.getenv('INFLUXDB_DATABASE', "netatmo")
# Luftkvalitet (Norwegian Air Quality
airLat=os.getenv('AIRQUALITY_LATITUDE', None)
airLon=os.getenv('AIRQUALITY_LONGITUDE', None)
# 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', 'Rain', 'sum_rain_24', 'sum_rain_1']
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']
senddata["tags"]["module"]=ds['module_name']
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)
#
# Air Quality (Norway)
#
if airLat is not None and airLon is not None:
airUri = f"https://api.nilu.no/aq/utd/{airLat}/{airLon}/3"
response = requests.get(airUri)
if response.status_code == 200:
for airdata in response.json():
station = airdata['station']
component = airdata['component']
airvalue = airdata['value']
output = [
{
"measurement": "airquality",
"tags": {
"station": station,
"component": component
},
"fields": {
"value": airvalue
}
}
]
if debug:
print(output)
client.write_points(output)
else:
print("error getting air quality")