项目作者: rainu

项目描述 :
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.
高级语言: Go
项目地址: git://github.com/rainu/mqtt-executor.git
创建时间: 2020-05-03T15:33:27Z
项目社区:https://github.com/rainu/mqtt-executor

开源协议:MIT License

下载


mqtt-executor

Go

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.

Get the Binary

You can build it on your own (you will need golang installed):

  1. go build -a -installsuffix cgo ./cmd/mqtt-executor/

Or you can download the release binaries: here

Configuration

Create a configuration file named “config.json”

  1. {
  2. "availability": {
  3. "topic": "tele/__DEVICE_ID__/status",
  4. "payload": {
  5. "available": "On",
  6. "unavailable": "Off"
  7. }
  8. },
  9. "trigger": [{
  10. "name": "Touch file",
  11. "topic": "cmnd/touch/file",
  12. "command": {
  13. "name": "/usr/bin/touch",
  14. "arguments": ["/tmp/example"]
  15. }
  16. }],
  17. "sensor": [{
  18. "name": "Free Memory",
  19. "topic": "tele/__DEVICE_ID__/memory/free",
  20. "retained": false,
  21. "unit": "kB", //used for hassio
  22. "icon": "hass:eye", //used for hassio
  23. "interval": "10s",
  24. "command": {
  25. "name": "/bin/sh",
  26. "arguments": [
  27. "-c",
  28. "cat /proc/meminfo | grep MemFree | cut -d\\: -f2 | sed 's/ //g' | grep -o [0-9]*"
  29. ]
  30. }
  31. }],
  32. "multi_sensor": [{
  33. "topic": "tele/__DEVICE_ID__/stats",
  34. "retained": false,
  35. "interval": "10s",
  36. "command": {
  37. "name": "/bin/sh",
  38. "arguments": [
  39. "echo",
  40. "{\"mem\":1, \"cpu\":2}"
  41. ]
  42. },
  43. "values": [{
  44. "name": "Memory",
  45. "unit": "kB", //used for hassio
  46. "icon": "hass:eye", //used for hassio
  47. "template": "{{value_json.mem}}" //used for hassio
  48. },{
  49. "name": "CPU",
  50. "unit": "%", //used for hassio
  51. "icon": "hass:eye", //used for hassio
  52. "template": "{{value_json.cpu}}" //used for hassio
  53. }]
  54. }]
  55. }

Usage

Start the tool with the path to the config file and the URL of the MQTT broker

  1. mqtt-executor -broker tcp://127.0.0.1:1883 -config /path/to/config.json

Enable the Homeassitant discovery support

  1. mqtt-executor -broker tcp://127.0.0.1:1883 -config /path/to/config.json -home-assistant

Trigger command execution

To execute a trigger:

  1. mosquitto_pub -t cmnd/touch/file -m "START"

To interrupt a trigger

  1. mosquitto_pub -t cmnd/touch/file -m "STOP"

Read the trigger state:

  1. 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):

  1. mosquitto_sub -t cmnd/touch/file/RESULT

Get the trigger results

Read the trigger state:

  1. 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):

  1. mosquitto_sub -t cmnd/touch/file/RESULT

Get the (multi) sensor results

Read the trigger state:

  1. mosquitto_sub -t tele/+/memory/free