Skip to main content

Home Assistant - MQTT Auto-Discovery :: Automation

Guangzhou, China

WIP

MQTT Autodiscovery

The discovery of MQTT devices will enable one to use MQTT devices with only minimal configuration effort on the side of Home Assistant. Two parts are required on the device side: The configuration topic which contains the necessary device type and unique identifier, and the remaining device configuration without the device type.

In Part I I looked into the configuration API and described the topic payloads needed to have Home Assistant add your camera as a new device when you connect it to the MQTT broker. Now I want to make sure that Home Assistant does not forget the device configuration if my broker "looses" those configuration topics.

I will try to use an automation that is triggered when my camera comes alive and feeds all those topics to my MQTT broker.

Last Will Trigger

To use the automation I first need to define something that can trigger it - e.g. I can use the System Start event. But a much cleaner solution is the Last-Will of my camera status/testament that I configured to use the 2 strings alive and dead:

Home Assistant MQTT Auto-Discovery

I now need to create a MQTT Sensor that reads the state of my camera's last-will:

mqtt:
sensor:
- device:
identifiers: in9408_garden
manufacturer: INSTAR Deutschland GmbH
model: INSTAR 2k+ IN-9408 WLAN
name: IN-9408 2k+ Garden
configuration_url: "http://192.168.2.115:80"
availability:
topic: cameras/115/status/testament
payload_available: '{"val":"alive"}'
payload_not_available: '{"val":"dead"}'
object_id: in9408_garden_testament
unique_id: in9408_garden_testament
name: "LWT Garden"
state_topic: "cameras/115/status/testament"
value_template: "{{ value_json.val }}"
icon: mdi:coffin
qos: 1

Home Assistant MQTT Auto-Discovery

Automation

Now we can create the Automation that will publish all configuration topics for us:

Home Assistant MQTT Auto-Discovery

And select the state of our camera's last-will as the Trigger:

Home Assistant MQTT Auto-Discovery

As Action we select Call Service and to execute the Python Script I will use the Shell Script option in HA. Start by creating a shell directory inside your config path and create a shell script mqtt_autodiscover_9408_garden.sh:

mkdir config/shell
nano config/shell/mqtt_autodiscover_9408_garden.sh

The shell script only needs to direct the Python binary to the location you used for the Python Script:

mqtt_autodiscover_9408_garden.sh

#!/bin/bash
python /config/python_scripts/mqtt5_client.py

To activate the Shell Extension and expose our script to HA we need to add the following lines to the configuration.yml:

# Auto configure 9408 with mqtt
shell_command:
mqtt_autodiscover_9408_garden: /bin/ash /config/shell//mqtt_autodiscover_9408_garden.sh

Restart HA and you should be able to find your shell script in the Call Service category:

Home Assistant MQTT Auto-Discovery

You can test the automation by deleting all of the configuration topics you might already have added to your MQTT broker. Then take your camera offline. In my case the camera is also my broker - so HA will also loose the MQTT connection. After turning the camera back, after a while, you should see your script kicking into action and registering your camera:

Home Assistant MQTT Auto-Discovery