diff options
author | Dennis Eriksen <d@ennis.no> | 2023-03-29 21:20:40 +0200 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-03-29 21:20:44 +0200 |
commit | b561663aba8c69b30ff14fb985df4cbf28ef0e6a (patch) | |
tree | 67e7e50f7310e27114e03af08f15a40e89409e73 | |
parent | entsoe has new api-url (diff) | |
download | energyscripts-b561663aba8c69b30ff14fb985df4cbf28ef0e6a.tar.gz |
adding user/pass for mqtt, made a service for neohub
-rw-r--r-- | neohub_listener.service | 17 | ||||
-rw-r--r-- | scripts/mqtt_listener.py | 60 | ||||
-rw-r--r-- | scripts/mqtt_watch.py | 4 | ||||
-rw-r--r-- | scripts/neohub.py | 25 |
4 files changed, 64 insertions, 42 deletions
diff --git a/neohub_listener.service b/neohub_listener.service new file mode 100644 index 0000000..880d08f --- /dev/null +++ b/neohub_listener.service @@ -0,0 +1,17 @@ +[Unit] +Description=Simple service to start neohub listener +StartLimitIntervalSec=100 +StartLimitBurst=5 + +[Service] +Type=simple +EnvironmentFile=%h/energyscripts/.env +WorkingDirectory=%h/energyscripts +ExecStart=%h/energyscripts/venv/bin/python3 -u scripts/neohub.py +Restart=on-failure +TimeoutStopSec=70 +RestartSec=30 +SyslogIdentifier=%N + +[Install] +WantedBy=default.target diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py index 1920824..6c617a5 100644 --- a/scripts/mqtt_listener.py +++ b/scripts/mqtt_listener.py @@ -12,6 +12,22 @@ mqtt_server = os.environ['el_mqtt_server'] mqtt_port = int(os.environ['el_mqtt_port']) keepalive = int(os.environ['el_mqtt_keepalive']) mqtt_topic = os.environ['el_mqtt_topic'] +mqtt_user = os.environ['el_mqtt_user'] +mqtt_pass = os.environ['el_mqtt_pass'] + +tempsensors = [ 'Bad Temp', + 'Barnerom Temp', + 'Isobod Temp', + 'Kjøkken Temp Matskap', + 'Kontor Temp', + 'Loft Temp', + 'Soverom Temp', + 'Stue Temp Display', + 'Stue Temp Stuebord', + 'Stue Temp Teleskap', + 'Toalett motion', + 'Utebod Temp', + 'Vaskerom Temp'] # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): @@ -26,30 +42,7 @@ def on_message(client, userdata, msg): name = msg.topic.split('/')[1] 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, - 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: + if name == 'HAN' and 'current' in data: sql = """INSERT INTO mqtt_han (name, current, @@ -65,20 +58,25 @@ def on_message(client, userdata, msg): data['linkquality'], datetime.utcnow()) - elif name == 'toiletmotion' and 'temperature' in data: + elif name in tempsensors and 'temperature' in data: + data['humidity'] = data['humidity'] if 'humidity' in data else None + data['voltage'] = data['voltage'] if 'voltage' in data else None sql = """INSERT INTO mqtt_temps - (name, + (sensor_id, temperature, + humidity, battery, linkquality, - time, - room) - SELECT %s, %s, %s, %s, %s, sensors.room + voltage, + time) + SELECT sensors.id, %s, %s, %s, %s, %s, %s FROM sensors WHERE sensors.name = %s""" - values = (name, data['temperature'], + values = (data['temperature'], + data['humidity'], data['battery'], data['linkquality'], + data['voltage'], datetime.utcnow(), name) @@ -93,6 +91,8 @@ client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message +client.username_pw_set(mqtt_user, password=mqtt_pass) + client.connect(mqtt_server, mqtt_port, keepalive) # Blocking call that processes network traffic, dispatches callbacks and diff --git a/scripts/mqtt_watch.py b/scripts/mqtt_watch.py index 371debd..d61ecc0 100644 --- a/scripts/mqtt_watch.py +++ b/scripts/mqtt_watch.py @@ -11,6 +11,8 @@ mqtt_server = os.environ['el_mqtt_server'] mqtt_port = int(os.environ['el_mqtt_port']) keepalive = int(os.environ['el_mqtt_keepalive']) mqtt_topic = os.environ['el_mqtt_topic'] +mqtt_user = os.environ['el_mqtt_user'] +mqtt_pass = os.environ['el_mqtt_pass'] # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): @@ -45,6 +47,8 @@ client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message +client.username_pw_set(mqtt_user, password=mqtt_pass) + client.connect(mqtt_server, mqtt_port, keepalive) # Blocking call that processes network traffic, dispatches callbacks and diff --git a/scripts/neohub.py b/scripts/neohub.py index 4fd3a16..7ef8b77 100644 --- a/scripts/neohub.py +++ b/scripts/neohub.py @@ -2,6 +2,7 @@ ''' Get stuff from neohub! This is mostly the usage-example from https://gitlab.com/neohubapi/neohubapi/ ''' import os +import time import asyncio from datetime import datetime import neohubapi.neohub as neohub @@ -10,8 +11,9 @@ import common neohub_ip = os.environ['el_neohub_ip'] neohub_port = os.environ['el_neohub_port'] +SLEEP = 120 # Sleep between runs -async def run(): +async def call_neohub(): ''' async runner! w00p ''' # Legacy connection hub = neohub.NeoHub(neohub_ip, int(neohub_port)) @@ -20,7 +22,7 @@ async def run(): system = await hub.get_system() hub_data, devices = await hub.get_live_data() for device in devices['thermostats']: - print(f"Temperature in zone {device.name}: {device}") + #print(f"Temperature in zone {device.name}: {device}") values = ( datetime.utcnow(), device.time, @@ -44,21 +46,20 @@ async def run(): temperature) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)""" - common.dbi(sql, values) - + common.dbi(sql, values, verbose=True) sql = """INSERT INTO mqtt_temps - (name, + (sensor_id, temperature, - time, - room) - SELECT 'neohub', %s, %s, sensors.room + time) + SELECT sensors.id, %s, %s FROM sensors WHERE sensors.name = 'neohub'""" values = (device.temperature, datetime.utcnow()) - common.dbi(sql, values) - - + common.dbi(sql, values, verbose=True) -asyncio.run(run()) +# Loop it forever +while True: + asyncio.run(call_neohub()) + time.sleep(SLEEP) |