diff --git a/battery2mqtt.py b/battery2mqtt.py index 61507d4..12b8a80 100644 --- a/battery2mqtt.py +++ b/battery2mqtt.py @@ -26,8 +26,6 @@ if LOG_LEVEL.lower() not in ['debug', 'info', 'warning', 'error']: else: logging.basicConfig(level=LOG_LEVEL, format='%(asctime)s %(levelname)s: %(message)s') -client = mqtt.Client(MQTT_CLIENT) - monitored_conditions = MONITORED_CONDITIONS.split(',') path = "/sys/class/power_supply/" dirs = os.listdir(path) @@ -41,6 +39,7 @@ def mqtt_connect(): # Connect to MQTT broker, set LWT, and start loop global mqtt_connected try: + client = mqtt.Client(MQTT_CLIENT) client.username_pw_set(MQTT_USER, MQTT_PASSWORD) client.will_set("battery2mqtt/" + MQTT_TOPIC + '/status', 'offline', 0, True) client.connect(MQTT_HOST, MQTT_PORT) @@ -68,7 +67,6 @@ def check_conditions(): def get_info(): # Get requested conditions and generate/send MQTT payload global payload - global prev_payload for dir in dirs: if AC_ADAPTER: if dir.startswith('AC'): @@ -139,4 +137,4 @@ mqtt_connect() while mqtt_connected: get_info() - sleep(INTERVAL) \ No newline at end of file + sleep(INTERVAL)