Skip to main content

MQTT client triggering & reporting shutdown on systemd-based systems

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

systemctl_mqtt-3.0.0.tar.gz (96.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

systemctl_mqtt-3.0.0-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file systemctl_mqtt-3.0.0.tar.gz.

File metadata

  • Download URL: systemctl_mqtt-3.0.0.tar.gz
  • Upload date:
  • Size: 96.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for systemctl_mqtt-3.0.0.tar.gz
Algorithm Hash digest
SHA256 43dc12d95df0ccac357ecf50e6b0914352a029ab374a4d0cc25c43b08a28fdd3
MD5 96afecb458eff5857e449ef65c3807b9
BLAKE2b-256 9482660d0611c464eeb2645628ffcba037d22a751f30516fbe1cebdda4cd8b01

See more details on using hashes here.

File details

Details for the file systemctl_mqtt-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: systemctl_mqtt-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for systemctl_mqtt-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 363e5b38e18aca9909eb08225a4e1b90e7ce83598b9cb193737c2521f30e21f9
MD5 0c993d2d4eb80250a5b1d5416e9a8f17
BLAKE2b-256 c09444de9fdc63234090a3b4fb2089e299968e27350e3e84d7d1e1f54832c32c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page