A simple MQTT client written in go that subscribes to a configurable list of MQTT topics on the specified broker and executes a given shell script/command whenever a message arrives. Furthermore you can define commands that are executed in a configurable interval. This can be useful for sensor data. Also this application supports Homeassitant mqtt descovery.
You can build it on your own (you will need golang installed):
go build -a -installsuffix cgo ./cmd/mqtt-executor/Or you can download the release binaries: here
Create a configuration file named "config.json"
{
"availability": {
"topic": "tele/__DEVICE_ID__/status",
"payload": {
"available": "On",
"unavailable": "Off"
}
},
"trigger": [{
"name": "Touch file",
"topic": "cmnd/touch/file",
"command": {
"name": "/usr/bin/touch",
"arguments": ["/tmp/example"]
}
}],
"sensor": [{
"name": "Free Memory",
"topic": "tele/__DEVICE_ID__/memory/free",
"retained": false,
"unit": "kB", //used for hassio
"icon": "hass:eye", //used for hassio
"interval": "10s",
"command": {
"name": "/bin/sh",
"arguments": [
"-c",
"cat /proc/meminfo | grep MemFree | cut -d\\: -f2 | sed 's/ //g' | grep -o [0-9]*"
]
}
}],
"multi_sensor": [{
"topic": "tele/__DEVICE_ID__/stats",
"retained": false,
"interval": "10s",
"command": {
"name": "/bin/sh",
"arguments": [
"echo",
"{\"mem\":1, \"cpu\":2}"
]
},
"values": [{
"name": "Memory",
"unit": "kB", //used for hassio
"icon": "hass:eye", //used for hassio
"template": "{{value_json.mem}}" //used for hassio
},{
"name": "CPU",
"unit": "%", //used for hassio
"icon": "hass:eye", //used for hassio
"template": "{{value_json.cpu}}" //used for hassio
}]
}]
}Start the tool with the path to the config file and the URL of the MQTT broker
mqtt-executor -broker tcp://127.0.0.1:1883 -config /path/to/config.jsonEnable the Homeassitant discovery support
mqtt-executor -broker tcp://127.0.0.1:1883 -config /path/to/config.json -home-assistantTo execute a trigger:
mosquitto_pub -t cmnd/touch/file -m "START"To interrupt a trigger
mosquitto_pub -t cmnd/touch/file -m "STOP"Read the trigger state:
mosquitto_sub -t cmnd/touch/file/STATE- RUNNING -> the command is running
- STOPPED -> the command has (not) been executed
Read the trigger result (command's output):
mosquitto_sub -t cmnd/touch/file/RESULTRead the trigger state:
mosquitto_sub -t cmnd/touch/file/STATE- RUNNING -> the command is running
- STOPPED -> the command has (not) been executed
Read the trigger result (command's output):
mosquitto_sub -t cmnd/touch/file/RESULTRead the trigger state:
mosquitto_sub -t tele/+/memory/freeThe optional number section accepts plain numeric MQTT payloads. With
-home-assistant, each entry is discovered as a Number entity (for example,
a 0–100% volume slider). The configured command receives the validated
value as its last, separate argument. The payload is never interpolated
into a shell command. state_command must print the actual numeric value to
stdout; it runs at startup and after a successful write. Set
state_interval to also refresh values changed outside Home Assistant.
Example for a PipeWire/PulseAudio user session with wpctl available:
{
"number": [{
"name": "Volume",
"topic": "cmnd/__DEVICE_ID__/volume",
"icon": "mdi:volume-high",
"unit": "%",
"min": 0,
"max": 100,
"step": 1,
"state_interval": "10s",
"command": {
"name": "/bin/sh",
"arguments": ["-c", "wpctl set-volume @DEFAULT_AUDIO_SINK@ \"$1%\"", "sh"]
},
"state_command": {
"name": "/bin/sh",
"arguments": ["-c", "wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print $2 * 100}'"]
}
}]
}Publish 35 to cmnd/<device-id>/volume to request 35%. The observed
value appears on cmnd/<device-id>/volume/VALUE; errors appear on
cmnd/<device-id>/volume/ERROR. On a read failure the value topic publishes
UNKNOWN, which resets the discovered Home Assistant entity to unknown.
Malformed, non-finite, out-of-range, or off-step requests are rejected without
running the write command. Write failures do not publish the requested value
as an observed value.
For boolean controls that need real state feedback, add a number entry
with "as_switch": true, min: 0, max: 1, and step: 1. The MQTT
command and state payloads remain numeric 0/1, but Home Assistant
discovers a stateful Switch instead of a Number. This keeps mute separate
from a 0% volume value. For a PipeWire default sink, wpctl set-mute
accepts 0 or 1, and wpctl get-volume includes [MUTED] when muted.
The command runs as the mqtt-executor process user. For desktop volume
control that user needs access to the active audio session. Keep command
topics protected by broker authentication/ACLs; they can execute configured
programs. Do not retain command messages, or a stale volume request may run
again after reconnect.