Skip to content

Instantly share code, notes, and snippets.

@paultag
Created July 2, 2016 17:12
Show Gist options
  • Save paultag/7f0a649983b0fed070ea5939fb28bde9 to your computer and use it in GitHub Desktop.
Save paultag/7f0a649983b0fed070ea5939fb28bde9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from influxdb import InfluxDBClient
import json
import datetime as dt
from sense.service import Sense
api = Sense()
data = api.room_sensors(quantity=20)
def items(data):
for flavor, series in data.items():
for datum in reversed(series):
value = datum['value']
if value == -1:
continue
timezone = dt.timezone(dt.timedelta(
seconds=datum['offset_millis'] / 1000,
))
when = dt.datetime.fromtimestamp(
datum['datetime'] / 1000,
).replace(tzinfo=timezone)
yield flavor, when, value
client = InfluxDBClient(
'influx.pault.ag',
443,
'username',
'password',
'sense',
ssl=True,
)
def series(data):
for flavor, when, value in items(data):
yield {
"measurement": "{}".format(flavor),
"tags": {
"user": "paultag"
},
"time": when.isoformat(),
"fields": {
"value": value,
}
}
client.write_points(list(series(data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment