forked from sthope/battery2mqtt
Minor fixes and add AC adapter monitoring
This commit is contained in:
parent
4c321c66a7
commit
61109f7887
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
battery2mqttv2.py
|
||||||
|
battery2mqttAC.py
|
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
from sys import int_info
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
@ -11,9 +12,10 @@ MQTT_QOS = int(os.getenv('MQTT_QOS', 1))
|
|||||||
MQTT_TOPIC = os.getenv('MQTT_TOPIC', 'server')
|
MQTT_TOPIC = os.getenv('MQTT_TOPIC', 'server')
|
||||||
INTERVAL = int(os.getenv('INTERVAL', 60))
|
INTERVAL = int(os.getenv('INTERVAL', 60))
|
||||||
MONITORED_CONDITIONS = os.environ.get('MONITORED_CONDITIONS','status,capacity,energy_now,energy_full,energy_full_design,power_now,voltage_now')
|
MONITORED_CONDITIONS = os.environ.get('MONITORED_CONDITIONS','status,capacity,energy_now,energy_full,energy_full_design,power_now,voltage_now')
|
||||||
SHOW_UNITS = os.getenv('SHOW_UNITS', 1)
|
SHOW_UNITS = int(os.getenv('SHOW_UNITS', 1))
|
||||||
BATTERY_HEALTH = os.getenv('BATTERY_HEALTH', 1)
|
BATTERY_HEALTH = int(os.getenv('BATTERY_HEALTH', 1))
|
||||||
TIME_REMAINING = os.getenv('TIME_REMAINING', 1)
|
TIME_REMAINING = int(os.getenv('TIME_REMAINING', 1))
|
||||||
|
AC_ADAPTER = int(os.getenv('AC_ADAPTER', 1))
|
||||||
|
|
||||||
client = mqtt.Client("battery2mqtt")
|
client = mqtt.Client("battery2mqtt")
|
||||||
client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
||||||
@ -30,6 +32,14 @@ time_remaining = {}
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
for dir in dirs:
|
for dir in dirs:
|
||||||
|
if AC_ADAPTER == 1:
|
||||||
|
if dir.startswith('AC'):
|
||||||
|
try:
|
||||||
|
with open(path + dir + '/online', 'r') as file:
|
||||||
|
payload['ac_adapter'] = 'online' if int(file.read()) == 1 else 'offline'
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
for name in monitored_conditions:
|
for name in monitored_conditions:
|
||||||
try:
|
try:
|
||||||
with open(path + dir + '/' + name, 'r') as file:
|
with open(path + dir + '/' + name, 'r') as file:
|
||||||
@ -37,13 +47,13 @@ while True:
|
|||||||
payload[name] = int(file.read().replace('\n',''))
|
payload[name] = int(file.read().replace('\n',''))
|
||||||
elif name.startswith('voltage') or name.startswith('energy') or name.startswith('power'):
|
elif name.startswith('voltage') or name.startswith('energy') or name.startswith('power'):
|
||||||
if name.startswith('voltage'):
|
if name.startswith('voltage'):
|
||||||
unit = ' V' if SHOW_UNITS == '1' else ''
|
unit = ' V' if SHOW_UNITS == 1 else ''
|
||||||
elif name.startswith('energy'):
|
elif name.startswith('energy'):
|
||||||
unit = ' Wh' if SHOW_UNITS == '1' else ''
|
unit = ' Wh' if SHOW_UNITS == 1 else ''
|
||||||
else:
|
else:
|
||||||
unit = ' W' if SHOW_UNITS == '1' else ''
|
unit = ' W' if SHOW_UNITS == 1 else ''
|
||||||
|
|
||||||
if SHOW_UNITS == '1':
|
if SHOW_UNITS == 1:
|
||||||
payload[name] = str(round(float(file.read().replace('\n','')) / 1000000,2)) + unit
|
payload[name] = str(round(float(file.read().replace('\n','')) / 1000000,2)) + unit
|
||||||
else:
|
else:
|
||||||
payload[name] = round(float(file.read().replace('\n','')) / 1000000,2)
|
payload[name] = round(float(file.read().replace('\n','')) / 1000000,2)
|
||||||
@ -52,26 +62,26 @@ while True:
|
|||||||
except:
|
except:
|
||||||
payload[name] = "condition not found"
|
payload[name] = "condition not found"
|
||||||
|
|
||||||
if BATTERY_HEALTH == '1':
|
if BATTERY_HEALTH == 1:
|
||||||
unit = ' %' if SHOW_UNITS == '1' else ''
|
unit = ' %' if SHOW_UNITS == 1 else ''
|
||||||
try:
|
try:
|
||||||
for name in ['energy_full_design', 'energy_full']:
|
for name in ['energy_full_design', 'energy_full']:
|
||||||
with open(path + dir + '/' + name, 'r') as file:
|
with open(path + dir + '/' + name, 'r') as file:
|
||||||
health_calc[name] = int(file.read().replace('\n',''))
|
health_calc[name] = int(file.read().replace('\n',''))
|
||||||
if SHOW_UNITS == '1':
|
if SHOW_UNITS == 1:
|
||||||
payload['battery_health'] = str(round((health_calc['energy_full'] / health_calc['energy_full_design']) * 100,2)) + unit
|
payload['battery_health'] = str(round((health_calc['energy_full'] / health_calc['energy_full_design']) * 100,2)) + unit
|
||||||
else:
|
else:
|
||||||
payload['battery_health'] = round((health_calc['energy_full'] / health_calc['energy_full_design']) * 100,2)
|
payload['battery_health'] = round((health_calc['energy_full'] / health_calc['energy_full_design']) * 100,2)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if TIME_REMAINING == '1':
|
if TIME_REMAINING == 1:
|
||||||
unit = ' h' if SHOW_UNITS == '1' else ''
|
unit = ' h' if SHOW_UNITS == 1 else ''
|
||||||
try:
|
try:
|
||||||
for name in ['energy_now', 'power_now']:
|
for name in ['energy_now', 'power_now']:
|
||||||
with open(path + dir + '/' + name, 'r') as file:
|
with open(path + dir + '/' + name, 'r') as file:
|
||||||
time_remaining[name] = int(file.read().replace('\n',''))
|
time_remaining[name] = int(file.read().replace('\n',''))
|
||||||
if SHOW_UNITS == '1':
|
if SHOW_UNITS == 1:
|
||||||
payload['time_remaining'] = str(round((time_remaining['energy_now'] / time_remaining['power_now']),2) if round((time_remaining['energy_now'] / time_remaining['power_now']),2) < 24 else '> 24') + unit
|
payload['time_remaining'] = str(round((time_remaining['energy_now'] / time_remaining['power_now']),2) if round((time_remaining['energy_now'] / time_remaining['power_now']),2) < 24 else '> 24') + unit
|
||||||
else:
|
else:
|
||||||
payload['time_remaining'] = round((time_remaining['energy_now'] / time_remaining['power_now']),2) if round((time_remaining['energy_now'] / time_remaining['power_now']),2) < 24 else '> 24'
|
payload['time_remaining'] = round((time_remaining['energy_now'] / time_remaining['power_now']),2) if round((time_remaining['energy_now'] / time_remaining['power_now']),2) < 24 else '> 24'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user