mirror of
https://github.com/Tediore/battery2mqtt.git
synced 2025-04-12 03:13:43 +02:00
Only send MQTT message when state changes occur
This commit is contained in:
parent
6fcece939f
commit
576a17922f
@ -25,6 +25,7 @@ path = "/sys/class/power_supply/"
|
|||||||
dirs = os.listdir(path)
|
dirs = os.listdir(path)
|
||||||
|
|
||||||
payload = {}
|
payload = {}
|
||||||
|
last_payload = {}
|
||||||
health_calc = {}
|
health_calc = {}
|
||||||
time_remaining = {}
|
time_remaining = {}
|
||||||
|
|
||||||
@ -78,10 +79,14 @@ while True:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
if payload != last_payload:
|
||||||
client.connect(MQTT_HOST)
|
try:
|
||||||
client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), qos=MQTT_QOS, retain=False)
|
client.connect(MQTT_HOST)
|
||||||
except:
|
client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), qos=MQTT_QOS, retain=False)
|
||||||
print('Message send failure.')
|
last_payload = payload
|
||||||
|
except:
|
||||||
|
print('Message send failed.')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
sleep(INTERVAL)
|
sleep(INTERVAL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user