diff --git a/mqtt-collect b/mqtt-collect index da88495..c47145d 100755 --- a/mqtt-collect +++ b/mqtt-collect @@ -36,11 +36,22 @@ def main(): c = db.cursor() table = msg.topic.replace("/", "_") ts = datetime.datetime.now() - value = float(msg.payload) - c.execute(f'CREATE TABLE IF NOT EXISTS {table} ' + textual = { + b"on": 1., + b"off": 0., + b"yes": 1., + b"no": 0. + } + + if msg.payload.lower() in textual: + value = textual[msg.payload.lower()] + else: + value = float(msg.payload) + print(f"{table}: {value} ") + c.execute(f'CREATE TABLE IF NOT EXISTS "{table}" ' '(timestamp timestamp, value real)') - c.execute(f'INSERT INTO {table} VALUES (?, ?)', (ts, value)) - c.execute(f'SELECT * from {table}') + c.execute(f'INSERT INTO "{table}" VALUES (?, ?)', (ts, value)) + #c.execute(f'SELECT * from "{table}"') db.commit() except Exception as e: print(f'Error writing to database: {e}')