mirror of
				https://github.com/Tediore/battery2mqtt.git
				synced 2025-11-04 12:46:36 +01:00 
			
		
		
		
	add time remaining calculation
This commit is contained in:
		@@ -35,6 +35,7 @@ services:
 | 
				
			|||||||
    - INTERVAL=60
 | 
					    - INTERVAL=60
 | 
				
			||||||
    - MONITORED_CONDITIONS=status,capacity
 | 
					    - MONITORED_CONDITIONS=status,capacity
 | 
				
			||||||
    - BATTERY_HEALTH=1
 | 
					    - BATTERY_HEALTH=1
 | 
				
			||||||
 | 
					    - TIME_REMAINING=1
 | 
				
			||||||
    volumes:
 | 
					    volumes:
 | 
				
			||||||
    - /sys/class/power_supply:/sys/class/power_supply:ro
 | 
					    - /sys/class/power_supply:/sys/class/power_supply:ro
 | 
				
			||||||
    restart: unless-stopped
 | 
					    restart: unless-stopped
 | 
				
			||||||
@@ -59,8 +60,10 @@ You can specify only those conditions that you'd like to track. The default is t
 | 
				
			|||||||
```
 | 
					```
 | 
				
			||||||
alarm,capacity,capacity_level,present,status,voltage_now
 | 
					alarm,capacity,capacity_level,present,status,voltage_now
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
# Battery health calculation
 | 
					
 | 
				
			||||||
 | 
					# Battery health and time remaining calculations
 | 
				
			||||||
The default is to also provide a battery health percentage calculation by dividing `energy_full` by `energy_full_design`. This can be disabled by setting `BATTERY_HEALTH` to `0` in your `docker-compose.yaml`. 
 | 
					The default is to also provide a battery health percentage calculation by dividing `energy_full` by `energy_full_design`. This can be disabled by setting `BATTERY_HEALTH` to `0` in your `docker-compose.yaml`. 
 | 
				
			||||||
 | 
					Similiarly, an estimate of time remaining on battery (in hours) is calculated by dividing `energy_now` by `power_now`. This can be disabled by setting `TIME_REMAINING` to `0` in your `docker-compose.yaml`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Example Home Assistant configuration
 | 
					# Example Home Assistant configuration
 | 
				
			||||||
```yaml
 | 
					```yaml
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,7 @@ 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','alarm,capacity,capacity_level,present,status,voltage_now')
 | 
					MONITORED_CONDITIONS = os.environ.get('MONITORED_CONDITIONS','alarm,capacity,capacity_level,present,status,voltage_now')
 | 
				
			||||||
BATTERY_HEALTH = os.getenv('BATTERY_HEALTH', 1)
 | 
					BATTERY_HEALTH = os.getenv('BATTERY_HEALTH', 1)
 | 
				
			||||||
 | 
					TIME_REMAINING = os.getenv('TIME_REMAINING', 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)
 | 
				
			||||||
@@ -24,6 +25,7 @@ dirs = os.listdir(path)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
payload = {}
 | 
					payload = {}
 | 
				
			||||||
health_calc = {}
 | 
					health_calc = {}
 | 
				
			||||||
 | 
					time_remaining = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
while True:
 | 
					while True:
 | 
				
			||||||
    for dir in dirs:
 | 
					    for dir in dirs:
 | 
				
			||||||
@@ -44,11 +46,19 @@ while True:
 | 
				
			|||||||
                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',''))
 | 
				
			||||||
                json.dumps(health_calc)
 | 
					 | 
				
			||||||
                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':
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                for name in ['energy_now', 'power_now']:
 | 
				
			||||||
 | 
					                    with open(path + dir + '/' + name, 'r') as file:
 | 
				
			||||||
 | 
					                        time_remaining[name] = int(file.read().replace('\n',''))
 | 
				
			||||||
 | 
					                payload['time_remaining'] = round((time_remaining['energy_now'] / time_remaining['power_now']),2)
 | 
				
			||||||
 | 
					            except:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    client.connect(MQTT_HOST)
 | 
					    client.connect(MQTT_HOST)
 | 
				
			||||||
    client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), qos=MQTT_QOS, retain=False)
 | 
					    client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), qos=MQTT_QOS, retain=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user