MQTT discovery message #150
Replies: 2 comments
-
I am not aware on the possibility of sending special mqtt messages to HA for discovery (but I also do not use HA). What I understand is that you have to define the sensor in a yaml file.You can find examples of this here: https://github.com/johanmeijer/grott/tree/master/examples/Home%20Assistent I am always want to learn more and if it can be done by sending a MQTT discovery message then we can try to add that to Grott. But I need more information about that then. |
Beta Was this translation helpful? Give feedback.
-
Here is some reading material about mqtt auto discovery. I am not sure if this belongs to the mqtt spec, or that it is HA specific. My guess is that it belongs to the mqtt spec. Here is some code from my BMS MQTT project, code says more then a 100 words ;) import paho.mqtt.client as mqttClient
...
#
# a class for initializing mqtt
#
class MQTTInitializer():
def __init__(self, filename):
self.AUTO_DISCOVER = "homeassistant/sensor/bms/battery_{topic}/config"
self.initialized = False
d = {}
if os.path.isfile(filename):
with open(filename, 'r') as f:
d = json.load(f)
# only top level items become properties
for key, value in d.items():
self.__dict__[key] = value
def formTopic(self, key, value):
return self.AUTO_DISCOVER.format(topic=key), json.dumps(value)
def initialize(self, mqtt, log = None):
try:
mqtt.username_pw_set(self.username, password=self.password)
mqtt.connect(self.host, self.port, 60)
mqtt.loop()
for key, value in self.topics.items():
topic, jsonValue = self.formTopic(key, value)
mqtt.publish(topic, jsonValue)
if log != None:
log.debug(f"published to mqtt: {topic} -> {jsonValue}")
time.sleep(1)
self.initialized = True
return self.initialized
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
if log:
log.error(f'exception during mqtt discovery publisch: {str(e)} line: {exc_tb.tb_lineno}')
return False What I do here is build a key/value dict with keys being the topic/sensor name and value being read from a json file. {
"username": "johnberg",
"password": "ha ha .. my secret",
"host": "localhost",
"port": 1883,
"topics": {
"voltage": {
"state_topic": "bms/battery_voltage",
"icon": "mdi:battery",
"name": "BMS Battery Voltage",
"unique_id": "bms_battery_voltage",
"unit_of_measurement": "V"
},
"current": {
"state_topic": "bms/battery_current",
"icon": "mdi:battery",
"name": "BMS Battery Current",
"unique_id": "bms_battery_current",
"unit_of_measurement": "A"
},
... I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Due to recent problems with the Growatt integration in Home Assistant I decided to switch to Grott. I've reconfigured my 2 inverters, setup a server on my Debian server and configured MQTT.
Everything is working fine, and I can see the messages in the MQTT broker in Home Assistant.
I wrote a BMS / MQTT interface myself, and I seem to recall, that for auto discovery, a special mqtt message needs to be send on start up, so mqtt entities can be discovered. Works perfectly on my BMS software.
However, as far as I can tell, no such message is send with grott? Must I configure all the sensors manually in Home Assistant?
Edit: Answering my own question: there is no such message, and all sensors must be configured manually. (that is a pain in the .. )
However there is a example (that is outdated) in the examples/homeassistant folder and a pending pull request for the new format.
Beta Was this translation helpful? Give feedback.
All reactions