Skip to main content

systemctl-mqtt

Code style: black CI Pipeline Status Coverage Status Last Release Compatible Python Versions DOI

MQTT client triggering & reporting shutdown on systemd-based systems

Setup

Via Pip

$ pip3 install --user --upgrade systemctl-mqtt

On debian-based systems, a subset of dependencies can optionally be installed via:

$ sudo apt-get install --no-install-recommends python3-jeepney python3-paho-mqtt

Follow instructions in systemd-user.service to start systemctl-mqtt automatically via systemd.

Via Docker Compose 🐳

  1. Clone this repository.
  2. Load AppArmor profile: sudo apparmor_parser ./docker-apparmor-profile
  3. sudo docker-compose up --build

Pre-built docker image are available at https://hub.docker.com/r/fphammerle/systemctl-mqtt/tags

Annotation of signed tags docker/* contains docker image digests: https://github.com/fphammerle/systemctl-mqtt/tags

Usage

$ systemctl-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS

systemctl-mqtt --help explains all available command-line options / parameters.

MQTT via TLS

TLS is enabled by default. Run systemctl-mqtt --mqtt-disable-tls … to disable TLS.

MQTT Authentication

systemctl-mqtt --mqtt-username me --mqtt-password-file /run/secrets/password …
# or for testing (unsafe):
systemctl-mqtt --mqtt-username me --mqtt-password secret …

Schedule Poweroff

Schedule poweroff by sending a MQTT message to topic systemctl/hostname/poweroff.

$ mosquitto_pub -h MQTT_BROKER -t systemctl/hostname/poweroff -n

Adapt delay via: systemctl-mqtt --poweroff-delay-seconds 60 …

Shutdown Report

systemctl-mqtt subscribes to logind's PrepareForShutdown signal.

systemctl halt|poweroff|reboot triggers a message with payload true on topic systemctl/hostname/preparing-for-shutdown.

Lock Screen

Lock screen by sending a MQTT message to topic systemctl/hostname/lock-all-sessions.

$ mosquitto_pub -h MQTT_BROKER -t systemctl/hostname/lock-all-sessions -n

Suspend

$ mosquitto_pub -h MQTT_BROKER -t systemctl/hostname/suspend -n

Monitor ActiveState of System Units

$ systemctl-mqtt --monitor-system-unit foo.service

enables reports on topic systemctl/[hostname]/unit/system/[unit_name]/active-state.

Control System Units

$ systemctl-mqtt  --control-system-unit <unit_name>

enables that a system unit can be started, stopped, restarted and isolated by a message on topic systemctl/[hostname]/unit/system/[unit_name]/start, …/stop, …/restart, …/isolate.

Home Assistant 🏡

Note: Home Assistant 2025.10+ is required for MQTT Discovery of systemctl-mqtt entities.

When MQTT Discovery is enabled (default in Home Assistant ≥0.117.0), the following entities will be added automatically:

  • binary_sensor.[hostname]_logind_preparing_for_shutdown
  • button.[hostname]_logind_lock_all_sessions
  • button.[hostname]_logind_poweroff
  • button.[hostname]_logind_suspend
  • sensor.[hostname]_unit_system_[unit_name]_active_state for --monitor-system-unit [unit_name]
  • button.[hostname]_unit_system_[unit_name]_start for --control-system-unit [unit_name]
  • button.[hostname]_unit_system_[unit_name]_stop for --control-system-unit [unit_name]
  • button.[hostname]_unit_system_[unit_name]_restart for --control-system-unit [unit_name]
  • button.[hostname]_unit_system_[unit_name]_isolate for --control-system-unit [unit_name], when the unit allows isolation (e.g., targets)

homeassistant entities_over_auto_discovery

Pass --homeassistant-discovery-prefix custom-prefix to systemctl-mqtt when using a custom discovery topic prefix.

Release files for systemctl-mqtt 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for systemctl-mqtt 3.0.0
File Size Uploaded
systemctl_mqtt-3.0.0.tar.gz 96.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for systemctl-mqtt 3.0.0
File Interpreter ABI Platform
systemctl_mqtt-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 128.7 kB

Release files / systemctl_mqtt-3.0.0.tar.gz

Download URL systemctl_mqtt-3.0.0.tar.gz
Size 96.3 kB
Tags Source
SHA-256 checksum
How to use checksums
43dc12d95df0ccac357ecf50e6b0914352a029ab374a4d0cc25c43b08a28fdd3
BLAKE2b-256 checksum
How to use checksums
9482660d0611c464eeb2645628ffcba037d22a751f30516fbe1cebdda4cd8b01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / systemctl_mqtt-3.0.0-py3-none-any.whl

Download URL systemctl_mqtt-3.0.0-py3-none-any.whl
Size 32.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
363e5b38e18aca9909eb08225a4e1b90e7ce83598b9cb193737c2521f30e21f9
BLAKE2b-256 checksum
How to use checksums
c09444de9fdc63234090a3b4fb2089e299968e27350e3e84d7d1e1f54832c32c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page