Add sensors

This commit is contained in:
Tanner Collin 2022-02-13 08:28:47 +00:00
parent fcb860af29
commit 6e4b1b062b

68
main.py
View File

@ -24,7 +24,8 @@ app = web.Application()
http_session = None
from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, database='sensors1' if PROD else 'sensors1dev')
sensors_client = InfluxDBClient('localhost', 8086, database='sensors1' if PROD else 'sensors1dev')
solar_client = InfluxDBClient('localhost', 8086, database='solar2')
PORT = 6903 if PROD else 6904
class Sensors():
@ -109,14 +110,14 @@ class Sensor():
'tags': {'id': self.id_, 'name': self.name},
'fields': data,
}
client.write_points([point])
sensors_client.write_points([point])
logging.info('Wrote %s data to InfluxDB.', self)
def check_update(self):
if self.update_period:
if time.time() - self.last_update > self.update_period:
logging.error('Missed expected update from %s.', self)
logging.warning('Missed expected update from %s.', self)
self.last_update = time.time()
def update(self, data):
@ -182,6 +183,18 @@ class DustSensor(Sensor):
except TypeError:
pass
class SleepSensor(Sensor):
type_ = 'sleep'
update_period = 90
def transform(self, data):
for key, value in data.items():
# what happens if you do this to a timestamp?
try:
data[key] = float(round(value, 1))
except TypeError:
pass
class Acurite606TX(Sensor):
type_ = 'temperature'
bad_keys = [
@ -224,7 +237,7 @@ async def process_mqtt(message):
except json.JSONDecodeError:
return
if 'id' not in data:
if type(data) != dict or 'id' not in data:
return
await process_data(data)
@ -257,38 +270,62 @@ async def history(request):
end_unix = request.rel_url.query.get('end', None)
if end_unix:
end_unix = int(end_unix)
end = datetime.fromtimestamp(end_unix)
else:
end = datetime.now(tz=pytz.UTC)
duration = request.rel_url.query.get('duration', 'today')
duration = request.rel_url.query.get('duration', 'day')
if duration == 'today':
now_tz = datetime.now(tz=TIMEZONE)
start = now_tz.replace(hour=0, minute=0, second=0, microsecond=0)
window = '5m'
window = '10m'
elif duration == 'day':
start = end - timedelta(days=1)
window = '5m'
window = '10m'
elif duration == 'week':
start = end - timedelta(days=7)
window = '30m'
window = '1h'
elif duration == 'month':
start = end - timedelta(days=30)
window = '2h'
window = '1d'
elif duration == 'year':
start = end - timedelta(days=365)
window = '24h'
window = '1d'
window = request.rel_url.query.get('window', window)
if name == 'Water':
scale = 10
elif name == 'Gas':
scale = 0.001055*1000
else:
scale = 1
start = int(start.timestamp())
end = int(end.timestamp())
if measurement == 'temperature':
q = 'select mean("temperature_C") as temperature_C from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
client = sensors_client
q = 'select mean("temperature_C") as temperature_C from temperature where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
elif measurement == 'ertscm':
q = 'select max("consumption_data") as consumption_data from ertscm where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
client = sensors_client
q = 'select derivative(max("consumption_data"))*{} as delta, max("consumption_data")*{} as max from ertscm where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(scale, scale, name, start, end, window)
elif measurement == 'thermostat':
client = sensors_client
q = 'select first("spacetemp") as spacetemp, first("heattemp") as heattemp, first("state") as state from thermostat where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(previous)'.format(name, start, end, window)
elif measurement == 'dust':
q = 'select max("max_p10") as max_p10, max("max_p25") as max_p25 from dust where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(none)'.format(name, start, end, window)
client = sensors_client
q = 'select max("avg_p10") as max_p10, max("avg_p25") as max_p25 from dust where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
elif measurement == 'sleep':
client = sensors_client
q = 'select max("max_mag") as max_mag from sleep where "name" = \'{}\' and time >= {}s and time < {}s group by time({}) fill(linear)'.format(name, start, end, window)
elif measurement == 'solar':
client = solar_client
q = 'select max("actual_total") as actual_total from ecu where time >= {}s and time < {}s group by time({}) fill(linear)'.format(start, end, window)
q += ' tz(\'America/Edmonton\')'
#if window and moving_average:
# q = 'select moving_average(mean("value"),{}) as value from {} where "name" = \'{}\' and time >= {}s and time < {}s group by time({}m) fill(none)'.format(moving_average, measurement, name, start, end, window)
@ -316,8 +353,11 @@ if __name__ == '__main__':
sensors.add(ERTSCMSensor('31005493', 'Water'))
sensors.add(ERTSCMSensor('41249312', 'Gas'))
sensors.add(OwnTracksSensor('owntracks1', 'OwnTracks'))
sensors.add(DustSensor('dust1', 'Nook'))
sensors.add(DustSensor('dust1', 'Living Room'))
sensors.add(Acurite606TX('231', 'Outside'))
sensors.add(Acurite606TX('226', 'Bedroom'))
sensors.add(Acurite606TX('132', 'Nook'))
sensors.add(SleepSensor('sleep1', 'Bedroom'))
loop = asyncio.get_event_loop()
loop.create_task(poll_sensors())