mqtt-executor

module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2020 License: MIT

README

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

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

Or you can download the release binaries: here

Configuration

Create a configuration file named "config.json"

{
  "availability": {
    "topic": "tele/__DEVICE_ID__/status",
    "interval": "10s",
    "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",
    "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]*"
      ]
    }
  }]
}

Usage

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.json

Enable the Homeassitant discovery support

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

Trigger command execution

To 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/RESULT
Get the trigger results

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/RESULT

Get the sensor results

Read the trigger state:

mosquitto_sub -t tele/+/memory/free

Directories

Path Synopsis
cmd
internal
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL