Skip to main content

Toolkit to interact with MQTT to build simple action rules

Project description

mqttactions

mqttactions is a simple CLI to build simple interactive behavior with MQTT. It's particularly useful for build simple rules for home automation without going to full in on a complex solution like Home Assistant or similar.

For example, mqttactions makes it super simple to listen to an MQTT topic from a switch (e.g., from Zigbee2MQTT) and then publish a message to a different topic to, for example, turn on a light.

Since the automation scripts are just Python, you can do arbitrarily complex things. Want to have a button (via Zigbee2MQTT) that checks if you have a new email and if so, flashes a light 3 times? No problem, just requires some more Python code.

Installation

mqttactions is available on PyPI and can be installed with pip:

$ pip install mqttactions

Especially if you plan to use complex scripts that require additional packages, it's advisable to install mqttactions in a virtualenv.

Usage

MQTT Actions provides two main commands:

Discover MQTT Devices

Discover devices exposed via the Home Assistant MQTT discovery protocol:

$ mqttactions --host mqtt.example.com discover

This can be used to help figure out what the command and/or state topics of your devices are if they support Home Assistant's MQTT discovery protocol.

Use the --filter option to do a substring match on any of the fields in case you have many devices.

Run Automation Scripts

Run automation scripts that respond to MQTT messages:

$ mqttactions --host mqtt.example.com run script1.py script2.py

The scripts you pass are imported by the CLI which then runs until you terminate it. Scripts can subscribe to topics and publish messages but also anything else an ordinary Python module could do.

Writing Automation Scripts

Scripts use a simple decorator-based API for reacting to MQTT messages:

from mqttactions import on, publish

@on("some-switch/action", payload="press_1")
def turn_on_light():
    publish("some-light/set", {"state": "ON"})

@on("some-switch/action", payload="press_2")
def turn_off_light():
    publish("some-light/set", {"state": "OFF"})

Check the examples/ directory for more example scripts that demonstrate common automation patterns.

The API is currently a very rough first attempt, expect it to evolve in breaking ways for some time.

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

mqttactions-0.2.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

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

mqttactions-0.2.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file mqttactions-0.2.0.tar.gz.

File metadata

  • Download URL: mqttactions-0.2.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for mqttactions-0.2.0.tar.gz
Algorithm Hash digest
SHA256 74c48c5a2860008855ee91c3fcb7988810e3664eb4bb842c9df0e8a843890671
MD5 deb7386f589f2231d6848e79a0fe5c2b
BLAKE2b-256 d8a3b12368aa5a391e6d105b6be55b6195d0cd4f0f4669029797e92a5d978112

See more details on using hashes here.

File details

Details for the file mqttactions-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mqttactions-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4a2777dc8d804f12416dd3b9a169ea053be62aff8b68116c62f63504a63b464
MD5 aeb42de262a0e4d1b425acdef65a6687
BLAKE2b-256 f78572fdb5c9c2ed67fb6f84d77eb368e61d4ef1981686a32ddaa78997637977

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