How to use mosquitto_pub to create new mqtt device? With answer

Hello. My OBD device does not send discovery topic, so instead of editing configuration.yaml and restarting server, I would like to use mosquitto_pub to create device. But is not working. My _pub command is:

mosquitto_pub -h 10.0.0.54 -u <user> -P <password> -t "homeassistant/sensor/AndrOBD/config" -m '{"name": "ECU voltage", "device_class": "voltage", "state_topic": "AndrOBD/ecu_voltage"}'

The message is sent correctly, I can see it in mosquitto_sub:

homeassistant/sensor/AndrOBD/config {"name": "ECU voltage", "device_class": "voltage", "state_topic": "AndrOBD/ecu_voltage"}

But no sensor is created. The actual message sent by device is

AndrOBD/ecu_voltage 8.994

Does anyone know how to register new device with mosquitto_pub?

I posted an answer few more messages down.

Try it with escaped quotes.

mosquitto_pub -h 10.0.0.54 -u <user> -P <password> -t "homeassistant/sensor/AndrOBD/config" -m '\"name\": \"ECU voltage\", \"device_class\": \"voltage\", \"state_topic\": \"AndrOBD/ecu_voltage\"}'

As an FYI, there are a couple of Forum threads that do this using HA’s MQTT publish service call in a script. Here is one such thread:

1 Like

Why so complicated with scripts? Simple command can do it, I have debugged it and it works:

mosquitto_pub -h <IP address> -u <user> -P <password> -t "homeassistant/sensor/AndrOBD/ecu_voltage/config" -m '{"name": "ECU voltage", "device_class": "voltage", "state_topic": "AndrOBD/ecu_voltage", "unit_of_measurement": "V"}'

Just remember one thing. After I restarted Home Assistant, entity definition is lost and I have to publish this event again. I created init-mqtt.sh script for that.

Where do you enter this command? it doesn’t explain it clearly in the Doc’s

Sorry I thought it is clear. I use linux and I installed clients with sudo apt-get install mosquitto-clients

Then you use linux terminal basically on any machine where you installed mosquitto-clients from which you can access HA and use IP address of mosquitto broker that is running in your HA instance (you get IP somewhere in system info).

I do not know how it works in non-linux environments. It could also be possible from mqtt explorer, which is graphical app.

Oh thank you, HA core has a ssh/terminal add-on I guess i could use that … Maybe?
And mqtt explorer, I have that so ill look into that one too.
Thanks for the info.

Actually, I think you can publish events straight from Home Assistant. With new update, it moved somewhere I cannot find it, but there is sumple GUI for listening and publishing straight inside HA. If you find it, please post the path, thanks.

Ok great thanks
Yes that’s in the mqtt integration configure menu.

Just remember one thing. After I restarted Home Assistant, entity definition is lost and I have to publish this event again. I created init-mqtt.sh script for that.

Thanks @janbenes
Can you show me where you save that file so it runs the script every boot.
And is there a down side to using the retain flag. Is not another way of retaining the definition of the enities?

I run that script manually on my laptop after I reboot HA computer. I am not familiar, I think RETAIN serves to populat value after you restart HA and before sensor sends its first reading. Then retain shows last valid value pulled out from mqtt broker.