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.3.1.tar.gz (50.0 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.3.1-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mqttactions-0.3.1.tar.gz
Algorithm Hash digest
SHA256 8fdd75658b298961f04585d109b8f5282a701e7b8f49a725ec37991af25bd8a2
MD5 bc8effa60ec3d862737b06e57e8074ce
BLAKE2b-256 8e92daa058a5a49d02c1c4456ad8ecbb3fade585e31b80e0fe6495a78c3d01cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mqttactions-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2aca8699e637f8e45eba2c3b0a364abfc2577c261314a0251f23a08141cf665a
MD5 ac64cef109c9f0ab838ed61539238c7b
BLAKE2b-256 854c89fffdefb4690c2df9becb804b5c7fdf98ada239fb789953fc48281f393a

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