MQTT Auto-Discovery - Use Node-RED to register Smarthome Devices
Previously I wrote a Python Script that allowed me to register MQTT devices with Home Assistant using the HA internal auto-discovery service. The same script can be used to register those devices with OpenHAB using the homie convention. Now I want to use Node-RED to dynamically register devices, instead of providing fixed configuration files for each device.
All flows can be downloaded from Github.
Home Assistant
As a convention I am grouping all devices by prefix according to the device type. E.g. all MQTT cameras are in the cameras
group. I identify each device by it's IP address - e.g. 192.168.2.120 would give the device ID 120
:
To be able to register all my cameras with a Node-RED script I first have to inject the prefix and ID of the camera I want to target and then walk my way through the MQTT API to extract all the information I need from this camera:
By writing those information into flow variables I can then use them to build the Configuration Topics needed by the Home Assistant Auto-discovery:
Now all I have to do is to inject the MQTT ID of the camera I want to register with Home Assistant (which has to be connected to the same MQTT Broker) and all the configuration topics will fire. This event can be coupled to the Last-Will topic of the device to have it register itself as soon as it connects with the broker.
OpenHAB (homie)
OpenHAB uses the homie convention to do the same thing. Again, I first need to extract all information through the MQTT API:
Now I can continue building the configuration topics according to the homie convention:
First we have to define the device by registering the $homie
version it is using, the $name
of the device, some more administrative work with the $extensions
and the online $state
that I have coupled to the camera Last-Will & Testament. The last point is a comma separated list of all the $nodes
we want to register to group $properties
that directly translate into Thing Channels and can be used as Items like switches or sliders:
The $node
then lists all the $properties
it contains and then defines each of those - that directly correspond to a MQTT command topic on my camera:
Every property here is, thanks to Node-RED, directly coupled with the camera API and will receive change updates:
In OpenHAB you should now see a new Thing in your Inbox:
Select your camera from the inbox and add it:
All homie &properties
should now show up as Channels and are ready to use as dashboard Items or for automation:
To toggle a value use the /set
command - e.g. the state topic defined to turn the alarm on is homie/120/alarm/alarm-armed
. You can toggle it by publishing an update to homie/120/alarm/alarm-armed/set
. Make sure that all subscriptions are active and send an update to the set-command:
Updating the command topic homie/120/alarm/alarm-armed/set
will now also update the value of the state topic homie/120/alarm/alarm-armed
and of course, since it is coupled with the device API, it will actually update the state on your device as well: