Files
battery2mqtt/README.md

132 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2020-12-04 19:42:41 -06:00
# battery2mqtt
2020-12-04 20:25:39 -06:00
*Push information about batteries in your system to MQTT*
2021-08-18 09:57:44 -05:00
I thought of this project when I switched to using my old laptop as my Home Assistant server. I wanted to track its battery level in Home Assistant to use in automations. Hopefully others can find it useful as well.
2020-12-04 20:25:39 -06:00
# Summary
2021-08-18 20:24:43 -05:00
`battery2mqtt` can monitor current battery percentage, charging status, etc. for any batteries present at `/sys/class/power_supply`. The MQTT topic format is `battery2mqtt/$TOPIC/$NAME` where `$TOPIC` is the topic you define and `$NAME` is the name of each battery. For example, if `/sys/class/power_supply/BAT0` is present in your system and you choose `server` to be the topic, the full topic will be `battery2mqtt/server/BAT0`. The topic for sensor availability would be `battery2mqtt/server/status`.
2020-12-04 20:25:39 -06:00
# Instructions
2020-12-04 21:13:05 -06:00
2021-08-18 00:31:52 -05:00
**Option 1: Manual build**
2020-12-04 20:25:39 -06:00
1. Clone repo: `git clone https://github.com/Tediore/battery2mqtt`
2. Enter directory: `cd battery2mqtt`
3. Build image: `docker build . -t battery2mqtt`
4. Customize `docker-compose.yaml` to fit your needs
5. `docker-compose up -d`
2021-08-18 00:32:05 -05:00
**Option 2: Docker Hub**
2021-08-18 00:31:52 -05:00
1. Follow steps 4 and 5 above using `tediore/battery2mqtt:latest` as the image.
2022-02-15 21:17:35 -06:00
Example docker-compose.yaml with all possible environmental variables listed:
2020-12-04 20:25:39 -06:00
```yaml
version: '3'
services:
battery2mqtt:
container_name: battery2mqtt
image: battery2mqtt:latest
environment:
- MQTT_HOST=10.0.0.2
- MQTT_PORT=1883
- MQTT_USER=user
- MQTT_PASSWORD=password
- MQTT_TOPIC=server
- MQTT_QOS=1
- INTERVAL=60
2021-08-18 00:09:08 -05:00
- MONITORED_CONDITIONS=status,capacity,energy_now,energy_full,energy_full_design,power_now,voltage_now
2020-12-05 10:15:10 -06:00
- BATTERY_HEALTH=1
2020-12-05 10:52:54 -06:00
- TIME_REMAINING=1
2020-12-05 16:18:42 -06:00
- SHOW_UNITS=1
2021-08-16 15:12:49 -05:00
- AC_ADAPTER=1
2021-08-17 23:42:56 -05:00
- LOG_LEVEL=info
2020-12-04 20:25:39 -06:00
volumes:
- /sys/class/power_supply:/sys/class/power_supply:ro
restart: unless-stopped
```
2022-02-15 21:17:35 -06:00
Example `docker run` command with all possible environmental variables:
```
docker run --name battery2mqtt \
-e MQTT_HOST=10.0.0.2 \
-e MQTT_PORT=1883 \
-e MQTT_USER=user \
-e MQTT_PASSWORD=password \
-e MQTT_TOPIC=server \
-e MQTT_QOS=1 \
-e INTERVAL=60 \
-e MONITORED_CONDITIONS=status,capacity,energy_now,energy_full,energy_full_design,power_now,voltage_now \
-e BATTERY_HEALTH=1 \
-e TIME_REMAINING=1 \
-e SHOW_UNITS=1 \
-e AC_ADAPTER=1 \
-e LOG_LEVEL=info \
-v /sys/class/power_supply:/sys/class/power_supply:ro \
tediore/battery2mqtt:latest
```
2020-12-04 20:25:39 -06:00
# Configuration
| Variable | Default | Required | Description |
|----------|---------|----------|-------------|
| `MQTT_HOST` | `None` | True | IP address or hostname of the MQTT broker to connect to. |
| `MQTT_PORT` | `1883` | False | The port the MQTT broker is bound to. |
| `MQTT_USER` | `None` | False | The user to send to the MQTT broker. |
| `MQTT_PASSWORD` | `None` | False | The password to send to the MQTT broker. |
| `MQTT_TOPIC` | `server` | True | The topic prefix to send the payload to. |
| `MQTT_QOS` | `1` | False | The MQTT QoS level. |
| `INTERVAL` | `60` | False | How often (in seconds) battery2mqtt polls for battery info. |
| `MONITORED_CONDITIONS` | (See below) | True | Battery properties to send to MQTT (must be a comma-separated string). |
| `BATTERY_HEALTH` | `1` | False | Enable/disable battery health percentage calculation. Set to 0 to disable. |
| `TIME_REMAINING` | `1` | False | Enable/disable time remaining estimate (in hours). Set to 0 to disable. |
| `SHOW_UNITS` | `1` | False | Enable/disable power units in the MQTT payload. Set to 0 to disable. |
| `AC_ADAPTER` | `0` | False | Enable/disable AC adapter status. Set to 1 to enable. |
| `LOG_LEVEL` | `info` | False | Set minimum log level. Valid options are `debug`, `info`, `warning`, and `error`. |
2020-12-04 20:25:39 -06:00
2022-02-15 21:14:18 -06:00
# Multiple instances
If you plan on using `battery2mqtt` on more than one machine, it is very important that you use a **different MQTT_TOPIC for each instance**; otherwise, you _will_ experience issues with LWT.
2021-08-18 01:00:44 -05:00
2020-12-04 20:25:39 -06:00
# Monitored conditions
2020-12-05 16:18:42 -06:00
You can specify only those conditions that you'd like to track. The default is to track `status, capacity, energy_now, energy_full, energy_full_design, power_now, voltage_now`. You can add more conditions (found at `/sys/class/power_supply/$NAME`) or choose only those you want to track. The variable in your `docker-compose.yaml` must follow this comma-separated format:
2020-12-04 21:02:45 -06:00
```
2020-12-05 16:18:42 -06:00
status,capacity,energy_now,energy_full,energy_full_design,power_now,voltage_now
2020-12-04 21:02:45 -06:00
```
A summary of these conditions is below.
| Condition | Description | Unit |
|-----------|-------------|------|
| Status | Battery status (charging, discharging, full) | None |
| Capacity | Current battery percentage | % |
| Energy now | Current battery capacity | Wh (watt-hours) |
| Energy full | Battery capacity when full | Wh |
| Energy full design | Original battery capacity when full* | Wh |
| Power now | Current power consumption | W |
| Voltage now | Current battery voltage | V |
\* Batteries lose capacity with each charge cycle. *Energy full* shows the actual full capacity of the battery due to wear; *Energy full design* shows the capacity the battery was able to hold when factory fresh.
2020-12-05 10:52:54 -06:00
# 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`.
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`.
2020-12-04 20:25:39 -06:00
2021-08-16 15:30:43 -05:00
# AC adapter monitoring
You can monitor the status of the AC adapter (online or offline) by setting `AC_ADAPTER` to `1`. This is disabled by default.
2020-12-04 20:25:39 -06:00
# Example Home Assistant configuration
```yaml
sensor:
- platform: mqtt
name: Server battery
2020-12-05 16:28:58 -06:00
state_topic: &server_battery_topic "battery2mqtt/server/BAT0"
2020-12-04 20:25:39 -06:00
value_template: "{{ value_json.capacity }}"
unit_of_measurement: '%'
2020-12-05 16:28:58 -06:00
json_attributes_topic: *server_battery_topic
2021-08-18 20:24:43 -05:00
availability_topic: "battery2mqtt/server/status"
device_class: battery
2020-12-04 20:25:39 -06:00
```
2021-08-16 15:26:34 -05:00
2021-08-16 15:28:25 -05:00
# TODO
2021-08-18 00:08:09 -05:00
1. ~~Implement LWT~~
2. ~~Add proper logging~~
2021-08-16 21:31:59 -05:00
3. Add Home Assistant MQTT autodiscovery?
4. ???
5. Profit