MQTT Config
So as I work to integrate MQTT into my automation environment, I find myself wishing for some sort of device discovery mechanism. Possibly also a device configuration mechanism.
Here’s the problem as I see it.
If I know the topic of a device, I can subscribe to it and I can even get its last data. I can also subscribe to all topics or a subset of topics. But then I have to wait for a device to publish something in order to discover it.
Unfortunately, I can’t ask Mosquitto what devices it knows about.
There are many use-cases where this just isn’t sufficient:
- A device that isn’t publishing when you reboot.
- A device that is turned off and only publishes when it gets turned on (like a device that uses a tilt sensor to power itself on).
- A device that doesn’t publish at all (a subscribe only device like a Relay).
It would also be nice to be able to re-configure some attributes of a device though I can see how this could be abused in an InternetOfThings scenario.
So I believe I’m going to define a ‘discovery’ record and experiment with that. I’m thinking something like this:
/Device_ID/config/[Name]/[Attribute]/[Value]
Examples:
/Kitchen/config/Temperature/type/int /Kitchen/config/Temperature/direction/input /Kitchen/config/Temperature/unit/celsius /Kitchen/config/Humidity/type/float /Kitchen/config/Humidity/direction/input /Kitchen/config/Humidity/unit/percent /Garage/config/Relay/type/bool /Garage/config/Relay/direction/output /Garage/config/RelayToggle/type/int /Garage/config/RelayToggle/direction/output /Garage/config/RelayToggle/unit/seconds /Garage/config/Relay/type/bool /Garage/config/Relay/direction/input
This tells me that is a sensor named “Kitchen” that publishes a temperature as an int in degrees celsius… There’s also a control named “Garage” that publishes a “Relay” state but it also has an output that it subscribes to called “Relay”… They both provide/take a boolean. There’s also a control named “Garage” that subscribes to “RelayToggle” that will conceivably cycle a relay on/off or off/on for some number of seconds.
I’ll experiment with that and see if I can make it useful… It will allow me to maintain an inventory of devices that are available… I will have to come up with some sort of expiry mechanism of course and if an attribute changes, I’ll need to override the old config with the new one presumably.