Tired of plugging/unplugging my mac to preserve the battery. I wanted to use my home automation system. batt2mqtt simply sends the battery status to an MQTT server. With Node-Red, my home automation system can automatically turn the plug on/off when it's time.
According to Battery University, the battery lasts longest when operating between 30 and 80 percent. With something very simple like Node-RED, you can adjust the limits as you want according to the information received on the topic MQTT_PUBLISH_TOPIC_BAT. You can also turn off the power when the mac goes to sleep and turn it back on when it has woken up (MQTT_PUBLISH_TOPIC_INFO).
Need cmake and pkg-config:
$ brew install pkg-config
$ brew install cmakedepends on:
CoreFoundationandIOKit(XCode)mosquitto
$ brew install mosquitto- Modify
batt2mqtt.plistto fit your needs (replace theXXXX). You can safely remove the sectionEnvironmentVariablesif you don't use a user/password to connect to your broker (MQTT server) - Modify what you want in
src/batt2mqtt.cbetweenBEGINandENDtags to set informations about your broker and topics.
Messages received on this topic are:
willsleepwhen the laptop will go to sleephaspoweredonwhen the laptop woke up
Message format: string
Message received on this topic is for example:
{"percentage":51, "state":0}
where percentage is the battery level and state is the power source status (0 = discharging (on Battery), 1 = charging (on Power AC))
Message format: JSON
You can send 2 commands to your mac (cf topic MQTT_SUBSCRIBE_TOPIC_CMD in src/batt2mqtt.c) :
displaysleepnow(causes display to go to sleep immediately)sleepnow(causes an immediate system sleep)
Message format: string
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make installThis will install batt2mqtt as an agent.
If you want to see the logs, use log.
Eg (like tail -f):
$ log stream --predicate 'subsystem contains "conurb.batt2mqtt"' --info --debug --style compactor, for example, show the logs from the past 2 hours:
$ log show --predicate 'subsystem contains "conurb.batt2mqtt"' --info --debug --style compact --last 2h