I am using luci collectd.
so in my case following code will be added to /usr/bin/stat-genconfig
function config_mqtt( c )
local str = ""
for s in pairs(sections) do
for key, type in pairs({ Sub="collectd_mqtt_sub", Pub="collectd_mqtt_pub" }) do
if sections[s][".type"] == type then
sname = OpenWrt4300
host = 192.168.1.10 (ip of the Homeassistant installation)
user = MQTT username
port = 1883
topic = collectd/4300
password = top-secret password
pname = what should be added here?
prefix = what should be added here
if sname then
if user then
str = str .. "\t" .. "<Subscribe " .."\"".. sname .."\"".." >" .. " \n"
str = str .. "\t" .. "host" .. " \"" .. host .. "\"\n"
str = str .. "\t" .. "port" .. " \"" .. port .. "\"\n"
str = str .. "\t" .. "user" .. " " .. user .. "\n"
str = str .. "\t" .. "password" .. " " .. password .. "\n"
str = str .. "\t" .. "topic" .. " \"" .. topic .. "\"\n"
str = str .. "\t" .. "CleanSession true" .. " \n"
str = str .. "\t" .. "</Subscribe>" .. " \n"
else
str = str .. "\t" .. "<Subscribe " .."\"".. sname .."\"".." >" .. " \n"
str = str .. "\t" .. "host" .. " \"" .. host .. "\"\n"
str = str .. "\t" .. "port" .. " \"" .. port .. "\"\n"
str = str .. "\t" .. "topic" .. " \"" .. topic .. "\"\n"
str = str .. "\t" .. "</Subscribe>" .. " \n"
end
else
str = str .. "\t" .. "<Publish " .."\"".. pname .."\"".." >" .. " \n"
str = str .. "\t" .. "host" .. " \"" .. host .. "\"\n"
str = str .. "\t" .. "port" .. " \"" .. port .. "\"\n"
str = str .. "\t" .. "prefix" .. " \"" .. prefix .. "\"\n"
str = str .. "\t" .. "</Publish>" .. " \n"
end
end
end
end
return str
end
and plugin section will look like this
section("collectd")
section("logfile")
mqtt = config_mqtt,
for plugin in pairs(plugins) do
if (plugin ~= "collectd") and (plugin ~= "logfile") then
section( plugin )
create a new file in /usr/lib/lua/luci/model/cbi/luci_statistics/mqtt.lua
-- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
-- Licensed to the public under the Apache License 2.0.
m = Map("luci_statistics",
translate("MQTT Subscriptions"),
translate(
"The MQTT will subscribe to MQTT Topics " ..
"to be displayled "
))
-- collectd_mqtt config section
s = m:section( NamedSection, "collectd_mqtt", "luci_statistics" )
-- collectd_mqtt_sub.enable
enable = s:option( Flag, "enable", translate("Enable this plugin") )
enable.default = 0
-- collectd_mqtt_sub config section (Sub)
sub = m:section( TypedSection, "collectd_mqtt_sub",
translate("MQTT Subscriptions"),
translate(
"This section defines on which subscription collectd will wait " ..
"for incoming data."
))
sub.addremove = true
sub.anonymous = true
-- collectd_mqtt_sub.sname
sub_name = sub:option( Value, "sname", translate("Subscription Name"))
sub_name.default = "Subscription1"
-- collectd_mqtt_sub.hosts
sub_hosts = sub:option( Value, "host", translate("MQTT hosts"))
sub_hosts.default = "127.0.0.1"
-- collectd_mqtt_sub.port
sub_port = sub:option( Value, "port", translate("MQTT port") )
sub_port.isinteger = true
sub_port.default = "1883"
-- collectd_mqtt_sub.user
sub_user = sub:option( Value, "user", translate("MQTT User") )
sub_user.isinteger = true
sub_user.default = ""
-- collectd_mqtt_sub.password
sub_password = sub:option( Value, "password", translate("MQTT password") )
sub_password.isinteger = true
sub_password.default = ""
-- collectd_mqtt_sub.topic
sub_topic = sub:option( Value, "topic", translate("Topic to Subscribe too") )
sub_topic.isinteger = true
sub_topic.default = "incoming/#"
-- collectd_mqtt_pub config section (Pub)
pub = m:section( TypedSection, "collectd_mqtt_pub",
translate("Publish MQTT"),
translate(
"This section defines to which MQTT servers it will publish MQTT " ..
"data to."
))
pub.addremove = true
pub.anonymous = true
-- collectd_mqtt_pub.pname
pub_name = pub:option( Value, "pname", translate("Publish Name"))
pub_name.default = "PublishCollectd"
-- collectd_mqtt_pub.host
pub_host = pub:option( Value, "host", translate("MQTT host") )
pub_host.default = "localhost"
-- collectd_mqtt_pub.port
pub_port = pub:option( Value, "port", translate("MQTT port") )
pub_port.default = ""
pub_port.isinteger = true
pub_port.optional = true
-- collectd_mqtt_pub.prefix
pub_prefix = pub:option( Value, "prefix", translate("Prefix") )
pub_prefix.default = "collectd"
return m
I don't have a file /usr/lib/lua/luci/controller/luci_statistics/luci_statistics.lua
edited /etc/config/luci_statistics and added following
config statistics 'collectd_mqtt'
option enable '1'