Skip to main content

MQTT agent for Xiaomi Lumi gateway

Description

The service allow controlling gateway LEDs, button, and illuminance sensor over MQTT

Default config should be located in /etc/lumimqtt.json or can be overridden with LUMIMQTT_CONFIG environment variable.

Interaction

Default devices

Action Topic Payload Expected values
Read light state lumi/<ID>/light {"state": "ON", "brightness": 255, "color": {"r": 255, "g": 0, "b": 0}}
Switch light lumi/<ID>/light/set {"state": "ON"}
Set light color lumi/<ID>/light/set {"color": {"r": 255, "g": 0, "b": 0}}
Set brightness lumi/<ID>/light/set {"brightness": 255}
Set color or brigthness with 10 seconds transition lumi/<ID>/light/set {"color": {"r": 255, "g": 0, "b": 0}, "brightness": 100, "transition": 10}
Read illuminance lumi/<ID>/illuminance 0-1000
Button lumi/<ID>/btn0/action single, double, triple, quadruple, many,
hold, double_hold, triple_hold, quadruple_hold, many_hold,
release

Binary sensors (soldered to GPIO points)

Action Topic Expected values
Read GPIO sensor lumi/<ID>/<SENSOR_NAME> ON/OFF

Run application

Example run command:

LUMIMQTT_CONFIG=./lumimqtt.json python3 -m lumimqtt

The configuration file is a JSON with the following content:

{
    "mqtt_host": "localhost",
    "mqtt_port": 1883,
    "mqtt_user": "",
    "mqtt_password": "",
    "topic_root": "lumi/{device_id}",
    "auto_discovery": true,
    "sensor_retain": false,
    "sensor_threshold": 50,
    "sensor_debounce_period": 60,
    "legacy_color_mode": true,
    "light_transition_period": 1.0
}

Every line is optional. By default, LumiMQTT will use the connection to localhost with the anonymous login.

device_id if not provided will be automatically replaced by a hex number representing a MAC address of the first network interface.

auto_discovery set to false to disable creating autodiscovery topics that are user by Home Assistant to discover entities.

sensor_retain is option to enable storing last sensor value on the broker

sensor_threshold is a threshold to avoid sending data to MQTT on small changes

sensor_debounce_period value in seconds to send data despite the threshold

light_transition_period value in seconds to set default transition for light switching or light change. Use 0 to remove the transition.

legacy_color_mode set to false to use with Home Assistant 2024.4 or newer

SSL/TLS connection

mqtt_tls (optional, boolean): Set to true to enable SSL/TLS connection. Defaults to false.

mqtt_verify_cert (optional, boolean): Set to false to disable verification of the server certificate, useful for self-signed certs. Defaults to true.

mqtt_ca (optional, string): Path to a file of CA certificate in PEM format.

mqtt_cert (optional, string): Path to a file of client certificate in PEM format.

mqtt_key (optional, string): Path to a file of client private key in PEM format.

Custom sensors

You can also use GPIO(s) as binary sensor(s). Add this to configuration:

{
    <your configuration>,
    "binary_sensors": {
        "<sensor name>": {
            "gpio": "<gpio number>",
            "device_class": "<device class>",
            "topic": "<sensor name>"
        }
    }
}

Values in <> must be replaced.

gpio is required, device_class and topic are optional. By default topic is sensor's name.

List of GPIOs. List of device classes.

Custom commands

You can add an extra section with custom commands that are executed with mqtt topics. By default, commands are exported as switch entities in Home Assistant. If json is passed to the set topic, the command will be interpolated with the values. Plain text is passed as {text} variable

{
    <your configuration>,
    "custom_commands": {
      "blink": "for i in 0 255 0 255 0 255 0; do echo $i > /sys/class/leds/{color}/brightness; sleep 1; done",
      "tts": "echo \"Test TTS without MPD component for home assistant\" | python3 -c 'from urllib.parse import quote_plus;from sys import stdin;print(\"wget -O /tmp/tts.mp3 -U Mozilla \\\"http://translate.google.com/translate_tts?q=\"+quote_plus(stdin.read()[:100])+\"&ie=UTF-8&tl=en&total=1&idx=0&client=tw-ob&prev=input&ttsspeed=1\\\" && amixer set Master 200 && mpg123 /tmp/tts.mp3\")' | sh 2> /dev/null",
      "tts_interpolate": "echo \"{text}\" | python3 -c 'from urllib.parse import quote_plus;from sys import stdin;print(\"wget -O /tmp/tts.mp3 -U Mozilla \\\"http://translate.google.com/translate_tts?q=\"+quote_plus(stdin.read()[:100])+\"&ie=UTF-8&tl=en&total=1&idx=0&client=tw-ob&prev=input&ttsspeed=1\\\" && amixer set Master {volume} && mpg123 /tmp/tts.mp3\")' | sh 2> /dev/null",
      "restart_lumimqtt": "/etc/init.d/lumimqtt restart",
      "reboot": "/sbin/reboot"
    }
}

Use an object to set command options. String commands are still supported:

{
    "custom_commands": {
        "doorbell": {
            "command": "mpg123 /etc/lumimqtt/doorbell.mp3",
            "entity_type": "button"
        },
        "restart_lumimqtt": "/etc/init.d/lumimqtt restart"
    }
}
  • command: required shell command.
  • entity_type: switch (default) or button.
  • entity_category: config or diagnostic (default: no category).
  • auto_discovery: false disables discovery for this command (default: true). MQTT commands still work; global discovery must be enabled to publish discovery.

Usage examples

Action Topic Payload
Run command "blink" lumi/<ID>/blink/set {"color": "red"}
Run command "tts" lumi/<ID>/tts/set <ANYTHING>
Run command "tts_interpolate" lumi/<ID>/tts_interpolate/set {"text": "Hi, it is a test", "volume": 200}
Run command "restart_lumimqtt" lumi/<ID>/restart_lumimqtt/set <ANYTHING>
Run command "reboot" lumi/<ID>/reboot/set <ANYTHING>

OpenWrt installation

opkg update 
opkg install python3-pip python3-asyncio python3-evdev
pip3 install -U lumimqtt

To upgrade you can just run

pip3 install -U lumimqtt

Example run command:

lumimqtt

or (in background):

lumimqtt &

Autorun:

To run lumimqtt on start you need a file /etc/init.d/lumimqtt with the following content:

#!/bin/sh /etc/rc.common
START=98
USE_PROCD=1
start_service()
{
    procd_open_instance

    procd_set_param env LUMIMQTT_CONFIG=/etc/lumimqtt.json
    procd_set_param command lumimqtt
    procd_set_param respawn
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_close_instance
}

To install this file on the gateway you can run

wget https://raw.githubusercontent.com/openlumi/lumimqtt/main/init.d/lumimqtt -O /etc/init.d/lumimqtt
chmod +x /etc/init.d/lumimqtt
/etc/init.d/lumimqtt enable
/etc/init.d/lumimqtt start

Metadata

Release files for lumimqtt 1.0.19

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

Source distribution (sdist)

Source distribution for lumimqtt 1.0.19
File Size Uploaded
lumimqtt-1.0.19.tar.gz 30.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lumimqtt 1.0.19
File Interpreter ABI Platform
lumimqtt-1.0.19-py3-none-any.whl Python 3 none any Details

Total release size: 59.8 kB

Release files / lumimqtt-1.0.19.tar.gz

Download URL lumimqtt-1.0.19.tar.gz
Size 30.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e9cd4affb80341d5db6222278f57b9e71e3e52f12aa811af3dd042e2fae72a18
BLAKE2b-256 checksum
How to use checksums
6de306203d13db1f22c2d204ac4edc0f1d575dd9b19822ef1995cbf0bbfefbef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.16

Release files / lumimqtt-1.0.19-py3-none-any.whl

Download URL lumimqtt-1.0.19-py3-none-any.whl
Size 29.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8b01d38f5fe5be6908cab14cf59697591083ce87022a0afcff649562a292190
BLAKE2b-256 checksum
How to use checksums
82e718e7150bb3b19a8dd1da35418250da9380c6ae30e619afda283578ec33d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.16

Release history Release notifications | RSS feed

This release

1.0.19 This release

2 release files

1.0.18

2 release files

1.0.16

2 release files

1.0.14

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

1 release file

1.0.0

1 release file

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