A script that retrieves power value (W) from the SAP Power Monitor app for macOS and publishes it to an MQTT broker.
- Power value (W) retrieval from SAP Power Monitor
- Automatic MQTT publishing
- Test mode, single-run mode, and continuous monitoring mode
- Visit the SAP Power Monitor releases page
- Download the latest
.pkgfile - Install the downloaded file
- Visit the Homebrew page
- Copy the installation command and run it in your terminal
- Clone the repository:
git clone git@github.com:rewse/power-monitor-mqtt.git
cd power-monitor-mqtt- Install dependencies:
This will install
mosquittoandjq.
make deps- Install the script:
The script will be installed to
~/.local/binand configuration files to~/.config/power-monitor-mqtt.
make install- Edit the configuration file:
vim ~/.config/power-monitor-mqtt/configAvailable Makefile commands:
make help # Show help message
make deps # Install dependencies (mosquitto, jq)
make install # Install script and config file
make test # Run script in test mode
make clean # Remove config file
make uninstall # Remove installed scriptNote: Make sure you have completed the Prerequisites section above before proceeding.
- Clone the repository:
git clone git@github.com:rewse/power-monitor-mqtt.git
cd power-monitor-mqtt- Install dependencies manually:
brew install mosquitto jq- Create configuration file:
mkdir -p ~/.config/power-monitor-mqtt
cp config.example ~/.config/power-monitor-mqtt/config- Edit the configuration file:
vim ~/.config/power-monitor-mqtt/configConfigure the following items in the configuration file (~/.config/power-monitor-mqtt/config):
MQTT_HOST: MQTT broker hostnameMQTT_PORT: MQTT broker port (default:1883)MQTT_USERNAME: MQTT authentication username (default:pub_client)MQTT_PASSWORD: MQTT authentication passwordTOPIC_PREFIX: MQTT topic prefix (default:power-monitor)DEVICE_NAME: Device name (default: Mac's hostname)INTERVAL: Data transmission interval for continuous mode (default:60seconds)
Retrieves data from SAP Power Monitor only, without sending to MQTT broker.
./power-monitor-mqtt.sh --testSends power value to MQTT broker once. Use with helper tool like Keyboard Maestro, LaunchAgent, or cron for periodic execution.
./power-monitor-mqtt.sh --onceRuns persistently at INTERVAL intervals.
./power-monitor-mqtt.sh./power-monitor-mqtt.sh --configThe script publishes data to the following topics:
{TOPIC_PREFIX}/{DEVICE_NAME}/power/current- Power value (W){TOPIC_PREFIX}/{DEVICE_NAME}/power/average- Average power value (W){TOPIC_PREFIX}/{DEVICE_NAME}/status- Status information
{
"value": 45.2,
"unit": "W",
"timestamp": "2024-12-16T16:30:00+09:00"
}{
"measurements": 150,
"country_code": "unknown",
"precise_location": false,
"carbon_footprint": -1,
"timestamp": "2024-12-16T16:30:00+09:00"
}Note: As of now, country_code, precise_location, and carbon_footprint do not return correct values from SAP Power Monitor.
Logs are written to ~/Library/Logs/power-monitor-mqtt/power-monitor-mqtt.log by default. The log file is automatically rotated when it reaches the maximum size.
- Download Execute-power-monitor-mqtt.kmmacros
- Open Keyboard Maestro
- Import the macro:
- Go to
File > Import > Import Macros Safely... - Select the downloaded
.kmmacrosfile
- Go to
- Review and adjust the interval if needed
- Open Keyboard Maestro
- Create a new macro with a descriptive name (e.g., "Execute power-monitor-mqtt")
- Set the trigger:
- Type: "Periodically while logged in"
- Interval: "Repeating every 1 Minutes" (adjust as needed)
- Add an action:
- Type: "Execute a Shell Script"
- Script content:
PATH=/opt/homebrew/bin:$PATH ~/.local/bin/power-monitor-mqtt.sh --once
- Save the macro
Note: The PATH environment variable must include /opt/homebrew/bin (or /usr/local/bin for Intel Macs) to ensure mosquitto_pub and jq commands are accessible.
Example configuration for Home Assistant:
mqtt:
sensor:
- name: "My Mac Power Current"
state_topic: "power-monitor/my-mac/power/current"
value_template: "{{ value_json.value }}"
unit_of_measurement: "W"
device_class: power
- name: "My Mac Power Average"
state_topic: "power-monitor/my-mac/power/average"
value_template: "{{ value_json.value }}"
unit_of_measurement: "W"
device_class: powerImportant: The power values reported by macOS internal sensors are typically 15-25% lower than actual AC power consumption measured by external devices (smart plugs, power meters). This difference is due to:
- AC-DC conversion efficiency: Power adapter losses (10-20%)
- Internal power conversion: Motherboard and component efficiency losses
- Measurement scope: Internal sensors may not account for all components and external devices
- Measurement methodology: Internal values are often estimates based on component usage
For more accurate power consumption calculations (e.g., electricity cost estimation), consider using a correction factor.
template:
- sensor:
- name: "My Mac Power Current Corrected"
unit_of_measurement: "W"
device_class: power
state: "{{ (states('sensor.my_mac_power_current') | float(0) * 1.2) }}"
- name: "My Mac Power Average Corrected"
unit_of_measurement: "W"
device_class: power
state: "{{ (states('sensor.my_mac_power_average') | float(0) * 1.2) }}"Power Consumption (kWh) Calculation with Integral Sensor
sensor:
# Using raw internal sensor values
- platform: integration
source: sensor.my_mac_power_current
name: My Mac Energy Total Internal
unit_prefix: k
round: 6
method: trapezoidal
max_sub_interval:
minutes: 5
# Using corrected values for more accurate consumption
- platform: integration
source: sensor.my_mac_power_current_corrected
name: My Mac Energy Total Corrected
unit_prefix: k
round: 6
method: trapezoidal
max_sub_interval:
minutes: 5