Add translation table to convert yes/no/on/off strings to numbers
parent
6ead6853df
commit
adf4e564d9
17
mqtt-collect
17
mqtt-collect
|
@ -36,11 +36,22 @@ def main():
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
table = msg.topic.replace("/", "_")
|
table = msg.topic.replace("/", "_")
|
||||||
ts = datetime.datetime.now()
|
ts = datetime.datetime.now()
|
||||||
|
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)
|
value = float(msg.payload)
|
||||||
c.execute(f'CREATE TABLE IF NOT EXISTS {table} '
|
print(f"{table}: {value} ")
|
||||||
|
c.execute(f'CREATE TABLE IF NOT EXISTS "{table}" '
|
||||||
'(timestamp timestamp, value real)')
|
'(timestamp timestamp, value real)')
|
||||||
c.execute(f'INSERT INTO {table} VALUES (?, ?)', (ts, value))
|
c.execute(f'INSERT INTO "{table}" VALUES (?, ?)', (ts, value))
|
||||||
c.execute(f'SELECT * from {table}')
|
#c.execute(f'SELECT * from "{table}"')
|
||||||
db.commit()
|
db.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error writing to database: {e}')
|
print(f'Error writing to database: {e}')
|
||||||
|
|
Loading…
Reference in New Issue