forked from florianbeer/grafana-netatmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netatmo_influx.py
executable file
·70 lines (62 loc) · 2.21 KB
/
netatmo_influx.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
#!/usr/bin/env python3
# encoding=utf-8
import lnetatmo
from influxdb import InfluxDBClient
CLIENT_ID = ''
CLIENT_SECRET = ''
USERNAME = ''
PASSWORD = ''
authorization = lnetatmo.ClientAuth(
clientId=CLIENT_ID,
clientSecret=CLIENT_SECRET,
username=USERNAME,
password=PASSWORD,
scope='read_station'
)
weatherData = lnetatmo.WeatherStationData(authorization)
client = InfluxDBClient()
if {'name': 'netatmo'} not in client.get_list_database():
client.create_database('netatmo')
for station in weatherData.stations:
station_data = []
module_data = []
station = weatherData.stationById(station)
station_name = station['station_name']
altitude = station['place']['altitude']
country= station['place']['country']
timezone = station['place']['timezone']
longitude = station['place']['location'][0]
latitude = station['place']['location'][1]
for module, moduleData in weatherData.lastData(exclude=3600).items():
for measurement in ['altitude', 'country', 'longitude', 'latitude', 'timezone']:
value = eval(measurement)
if type(value) == int:
value = float(value)
station_data.append({
"measurement": measurement,
"tags": {
"station": station_name,
"module": module
},
"time": moduleData['When'],
"fields": {
"value": value
}
})
for sensor, value in moduleData.items():
if sensor.lower() != 'when':
if type(value) == int:
value = float(value)
module_data.append({
"measurement": sensor.lower(),
"tags": {
"station": station_name,
"module": module
},
"time": moduleData['When'],
"fields": {
"value": value
}
})
client.write_points(station_data, time_precision='s', database='netatmo')
client.write_points(module_data, time_precision='s', database='netatmo')