MQTT DiscoveryStream integration

This is an “extension” (and compatible replacement) of the builtin mqtt_statestream integration.
Besides the functionalities of the here-above, it also allows to publish and handles an MQTT “disvovery” setup.

This is specifically useful in “Master-Slave” HA scenarios.

  • A “slave”, by instance a specific HA instance handling zigbee and / or Bluetooth, would use this component to publish states and discovery via MQTT.
  • A “master” would use the built-in MQTT discovery mechanism to subscribe to state topics, and get commands (e.g. turn on light) information

Changelog

0.5

  • Add device support
  • FIX color support
  • Add availability support

0.4

  • Add device_tracker
  • Add light transitions
  • Initial HACS release

0.3

  • Manage color temperature

0.3

  • Fix binary_sensors

0.1

  • Initial release:
    Handles:
    • sensors
    • switches
    • lights (partial)

Pre-requistes

  1. MQTT configured

Installation

HACS

  1. Launch HACS
  2. Navigate to the Integrations section
  3. “+ Explore & Add Repositories” button in the bottom-right
  4. Search for “MQTT DiscoveryStream”
  5. Select “Install this repository”
  6. Restart Home Assistant

Home Assistant

The integration is configured via YAML only.

Example:

mqtt_discoverystream:
  base_topic: test_HA
  publish_attributes: false
  publish_timestamps: true
  publish_discovery: true
  include:
    entities:
      - sensor.owm_hourly_humidity
      - sensor.jellyfin_cloud
      - light.wled_esp
  exclude:
    entities:
      - sensor.plug_xiaomi_1_electrical_measurement

Repository

6 Likes

Does this handle wildcarded entities in the include: and exclude: sections ?

It has exactly the same way of working as mqtt_statestream, on which this is based.

Ahh yes - domains will be fine for my needs - seems to be working well.

I missed the significance of this … very nice

This is excellent and has helped considerably in my setup. I understand you’ll not want to develop too far from the core statestream integration but I was wondering if a discovery_prefix could be added so I can store the mqtt sensors in their own base topic but still use the default homeassistant discovery location.

~Brian

Hi Chris,

Is there any chance you can add a discovery_prefix?

I’m trying to investigate why the retained flag is being removed from mqtt ha discovery messages and having the base topic also set to homeassistant so sensors write there as well is causing problems for me.

~B

Not sure what you mean. This component pushes discovery messages to the topic of your choice.
The actual discovery happens on the builtin MQTT integration.

I have 2 HA instances for collecting z-wave, zigbee and ble sensors which are then published to an MQTT server for the main HA instance to use. I use your integration to publish the ble sensors.

With zigbee2mqtt and Z-Wave JS2MQTT the sensors are published to the base topics zigbee2mqtt and zwave and the auto discovery config is published in the base topic homeassistant.

With your discoverystream integration the config messages are published in whatever topic is set as the base topic - there is no separate discovery_topic configuration option as there is with the zigbee2mqtt and zwave2mqtt addons. This means I have to set the base_topic to homeassistant for HA auto discovery to work.

I want to publish the sensor data in its own base_topic and have the auto_discovery messages published in the homeassistant base topic.

Example:

discoverystream message published under ha_sensor_1 - HA fails to auto discovery sensor

I may be mis-understanding something with the config but I hope that makes sense.

~B

Ah, makes sense. Will look into it.

gonna test this out soon, if i include a domain “lights” for example, will it exlude automaticly all the other domains? or do i need to exclude them manually? dont want an overkill of entitiies on mqtt

Include/Exclude works the same way as mqtt_statestream

ok, i installed this custom with hacs, and setup domain “light” for now only
some question, for now i’m having just HA as standalone, the idea is to push the entities to OpenHab, so i can turn on/off lights/switches

  1. i see in mqtt explorer the lights coming in, but in the “mqtt broker” integration, i dont see any mqtt lights? should i not see the there also? as an mqtt duplicate of my normal light?

  2. also , whats the payload that i need to send for example in mqtt explorer to turn an light on/off?

thnx in advance

  1. No, unless you create a discovery on the exact same root topic, which doesn’t make sense
  2. See MQTT Discovery - Home Assistant for the structure of the “config” topic, and adapt for openhab

Ok, will have a look. It seems I need to send a command topic , not a state , right?

Or is the state actually the command?

Because a state in HA is actually not the on/off command , it just represents the state of a light…

On that page above, I don’t see an example of a command topic to turn something on, or I’m confused… Sorry mqtt a bit new to me

The command topic is “generated” by the custom component.
Please post the formatted content of one of your “config” topic for a practical example

this one for ex:

{
  "uniq_id": "mqtt_light.wasplaats",
  "name": "Wasplaats",
  "stat_t": "ha_mqtt/light/wasplaats/state",
  "avty_t": "ha_mqtt/light/wasplaats/availability",
  "cmd_t": "ha_mqtt/light/wasplaats/set_light",
  "schema": "json",
  "color_mode": true,
  "supported_color_modes": [
    "onoff"
  ]
}

Here, the topic to switch the light is ha_mqtt/light/wasplaats/set_light (cmd_t or command topic)
The payload to send is the default (MQTT Light - Home Assistant), so “ON” or “OFF”

So, to switch the light on, use the equivalent of the below in openhab

mosquitto_pub -h <your_mqtt_broker> -p 1883 -t "ha_mqtt/light/wasplaats/set_light" -m ON

aha, was testing it with mqtt_explorer, instead of command line, like below

but it doesnt turn on a light :slight_smile:
i send the “ON” in raw format, maybe thats not ok?

hmm, tested with command line like your ex, i see this incoming, but the light doesnt go on/off

image

what should happen? should the mqtt compont send a command to my light.wasplaats ?

Ah sorry, it’s been a while.
Lights use the JSON form: MQTT Light - Home Assistant

So you have to send

{ "state": "ON" }