From 08a9721ab7a7e3d5799ccad5afc9b676273b83ae Mon Sep 17 00:00:00 2001
From: Florian Beer <florian.beer@nextlayer.at>
Date: Fri, 29 May 2020 12:15:24 +0200
Subject: [PATCH] Initial commit

---
 README.md         | 11 ++++++++
 netatmo_influx.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100755 netatmo_influx.py

diff --git a/README.md b/README.md
index e73d4a6..ade7f30 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,13 @@
 # grafana-netatmo
 Netatmo Weather Station dashboard for Grafana
+
+## Installation
+
+* Create a [Netatmo developer account](https://dev.netatmo.com/apidocumentation) and fill in your CLIENT_ID, CLIENT_SECRET, USERNAME and PASSWORD in the script.
+* This script assumes you have InfluxDB running on the same machine as this script and it uses no authentication.
+* Create a cron job to run the script periodically e.g.
+
+```
+# cat /etc/cron.d/netatmo
+*/5 * * * * root  /usr/local/bin/netatmo_influx.py > /dev/null 2>&1
+```
diff --git a/netatmo_influx.py b/netatmo_influx.py
new file mode 100755
index 0000000..4af9822
--- /dev/null
+++ b/netatmo_influx.py
@@ -0,0 +1,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')
-- 
GitLab