Add translation table to convert yes/no/on/off strings to numbers

master
0xee 2020-12-13 19:11:28 +01:00
parent 6ead6853df
commit adf4e564d9
1 changed files with 15 additions and 4 deletions

View File

@ -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}')