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.1.0.tar.gz (17.7 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.1.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mqttactions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1b567874c20b6ca2b82916190767e9aa843ade994e735bee04fb9035927338b7
MD5 31dd92d5a188c5b03a9d395fb346c081
BLAKE2b-256 86b9ad965f7837e204c8d7efeb2391f11091cc88421e3c48cea1d80775b7e826

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mqttactions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff4e977bc1256e77eef4bee86fdcf7dc86f4108a2d5cc5ed453f15283874dafe
MD5 d4a425487a760b0f86cb86abb60799e5
BLAKE2b-256 bb3ea5c9eea7449fb0927204cb624c01936a1562c0f4c7b82c6954e29a2a6352

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