diff options
Diffstat (limited to 'scripts/mqtt_listener.py')
-rw-r--r-- | scripts/mqtt_listener.py | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py index 85d27a1..1920824 100644 --- a/scripts/mqtt_listener.py +++ b/scripts/mqtt_listener.py @@ -27,12 +27,60 @@ def on_message(client, userdata, msg): data = json.loads(msg.payload) if name.startswith('tmp') and 'temperature' in data and 'humidity' in data: - sql = "INSERT INTO mqtt_temps (name, temperature, humidity, battery, linkquality, voltage, time) VALUES(%s,%s,%s,%s,%s,%s,%s)" - values = (name, data['temperature'], data['humidity'], data['battery'], data['linkquality'], data['voltage'], datetime.utcnow()) + sql = """INSERT INTO mqtt_temps + (name, + temperature, + humidity, + battery, + linkquality, + voltage, + time, + room) + SELECT %s, %s, %s, %s, %s, %s, %s, sensors.room + FROM sensors + WHERE sensors.name = %s""" + values = (name, + data['temperature'], + data['humidity'], + data['battery'], + data['linkquality'], + data['voltage'], + datetime.utcnow(), + name) + elif name == 'HAN' and 'current' in data: - sql = "INSERT INTO mqtt_han (name, current, power, voltage, linkquality, time) VALUES(%s,%s,%s,%s,%s,%s)" - values = (name, data['current'], data['power'], data['voltage'], data['linkquality'], datetime.utcnow()) + sql = """INSERT INTO mqtt_han + (name, + current, + power, + voltage, + linkquality, + time) + VALUES(%s,%s,%s,%s,%s,%s)""" + values = (name, + data['current'], + data['power'], + data['voltage'], + data['linkquality'], + datetime.utcnow()) + + elif name == 'toiletmotion' and 'temperature' in data: + sql = """INSERT INTO mqtt_temps + (name, + temperature, + battery, + linkquality, + time, + room) + SELECT %s, %s, %s, %s, %s, sensors.room + FROM sensors + WHERE sensors.name = %s""" + values = (name, data['temperature'], + data['battery'], + data['linkquality'], + datetime.utcnow(), + name) else: return |