Homeassistant integration

Can Openwrt developers help with this issue https://community.home-assistant.io/t/openwrt-dd-wrt-and-freshtomato-link-and-signal-sensors/422272.

In it's current state https://www.home-assistant.io/integrations/luci/

Basically, OpenWrt should expose more sensors to the Homeassistant integration, including

  • Traffic meter
  • Signal strength
  • Link rate
  • Link type sensors.
  • Load average
  • temperature sensors.
1 Like

One could probably make a shell script / C application which uses mosquitto_pub or another MQTT client to publish data to the HomeAssistant MQTT broker every minute or so...
like the Inverter guys are doing, except we have procd and Luci :slight_smile:

2 Likes

that could be one solution, but since Mqtt requires another add-on to function, I think submitting a change to the LUCI integration might be a better idea.

What's stopping you doing this?

coding skills.
I am a noob when it comes to programming.
If I had the skills, I could have posted the solution for discussion instead of requesting Honroable OpenWrt developers.

@MangoMan It seems we have some trick on our hands.

working example

1 Like

if you want to control mqtt through luci

@sash99 thanks, but how do I set up the collectd to publish data to Mosquito on HA?

https://collectd.org/wiki/index.php/Plugin:MQTT

it mentions adding the following to /etc/collectd.conf

<Plugin "mqtt">
   <Publish "name1">
     Host "localhost"
     Prefix "collectd"
     Retain true
   </Publish>
</Plugin>

Currently, I have Mosquitto as broker/server on HA.

Another integration via HACS

1 Like

if you are using Luci collectd graghing then you need to do what i mentioned above. as Luci will overwrite collectd.conf. if you do not use luci collectd then just add to collectd.conf

<Plugin "mqtt">
   <Publish "name1">
     Host "localhost"
     Prefix "collectd"
     Retain true
   </Publish>
</Plugin>

collectd publishes and subs in a specific format
what database does HA use you could use to publish it to your HA data base using server in the example below to influxdb

LoadPlugin network
<Plugin network>
        Server "localhost" "25826"
        Forward false
</Plugin>

or you write a simple script that translates collectd mqtt to HA mqtt format

example this one looking for mqtt topic called grid - when it receives one it translate to mqtt that compatible with collectd mqtt and publishes it in your case you are looking for collectd Mqtt and you want to translate to HA

#!/usr/bin/perl -w
open(SUB, "/usr/bin/mosquitto_sub -t /grid ");
while ($MQTT = <SUB>) {
system("mosquitto_pub -t 'incoming/OpenWrt/mqtt-Energy/power-grid' -m 'N:$MQTT'");
}

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'

The only method which I got to work with is UPNP, still waiting on other methods

How is your mileage on that one? I tried it but looks like the connection regular drops and ha shows unavailable for the sensor for some seconds over and over again (alternating between the values and unavailable) :slightly_frowning_face:

Initially it was same as your, but has improved recently don't know why, consist connection, and so far.
I think Kvj intergration can work, if we don't use Https.