Mqtt autodiscovery

I completly confused about mqtt auto-discovery. What does it means an how does it works?

I’ve several shelly switches an sensors in use, all with mqtt, but none of them appear automatic as entity in HA. That’s what I understand under “auto-discovery”

As I read here (MQTT non-autodiscovery lights/switches as devices? - Configuration - Home Assistant Community (home-assistant.io)) other use automations to “auto-discover” the mqtt entities. (what ist auto, when I have to put all information for each single entity in an automation)?

For the moment I’ve set up all my mqtt devices in a normal config.yaml file, and this works without any problem and then and then I see these entities in the entitiy list.

I’m confused… :woozy_face:

my config:

configuration.yaml

homeassistant:
  packages: !include_dir_named integrations/

sensor: !include_dir_merge_list sensors/
light: !include_dir_list lights/

mqtt-config in directory integrations/

mqtt:
  discovery: true
#  discovery_prefix: homeassistant

shelly_sensor.yaml in directroy sensors/

# configuration Shelly Sensors
  - platform: mqtt
    name: "shelly-button-wohnen-batt"
    state_topic: shellies/shellybutton1-D8F15B154F21/sensor/battery
    unit_of_measurement: "%"
  - platform: mqtt
    unique_id: shelly_sensor_win_keller1
    name: "shelly-window-keller1-status"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/state"
    icon: "mdi:window-open-variant"
  - platform: mqtt
    name: "shelly-window-keller1-vibration"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/vibration"
    icon: "mdi:vibrate"
  - platform: mqtt
    name: "shelly-window-keller1-lux"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/lux"
    icon: "mdi:globe-light"
    unit_of_measurement: "lux"
  - platform: mqtt
    name: "shelly-window-keller1-batt"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/battery"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-window-keller1-temp"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/temperature"
    unit_of_measurement: "°C"
  - platform: mqtt
    name: "shelly-window-keller1-tilt"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/tilt"
    icon: "mdi:arrow-top-right"
  - platform: mqtt
    name: "shelly-window-keller1-illumination"
    state_topic: "shellies/shellydw2-A4CF12F42D61/sensor/illumination"
    icon: "mdi:brightness-6"
  - platform: mqtt
    name: "shelly-water-heizung-batt"
    state_topic: "shellies/shellyflood-C8A7C2/sensor/battery"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-water-heizung-temp"
    state_topic: "shellies/shellyflood-C8A7C2/sensor/temperature"
    unit_of_measurement: "°C"   
  - platform: mqtt
    name: "shelly-water-heizung-status"
    state_topic: "shellies/shellyflood-C8A7C2/sensor/flood"
    icon: "mdi:water-alert"
  - platform: mqtt
    name: "shelly-ht-hasenstall-temp"
    state_topic: "shellies/shellyht-957402/sensor/temperature"
    unit_of_measurement: "°C"    
  - platform: mqtt
    name: "shelly-ht-hasenstall-hum"
    state_topic: "shellies/shellyht-957402/sensor/humidity"
    icon: "mdi:water-percent"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-ht-hasenstall-batt"
    state_topic: shellies/shellyht-957402/sensor/battery
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-ht-keller-temp"
    state_topic: "shellies/shellyht-955504/sensor/temperature"
    unit_of_measurement: "°C"    
  - platform: mqtt
    name: "shelly-ht-keller-hum"
    state_topic: "shellies/shellyht-955504/sensor/humidity"
    icon: "mdi:water-percent"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-ht-keller-batt"
    state_topic: shellies/shellyht-955504/sensor/battery
    unit_of_measurement: "%"  
  - platform: mqtt
    name: "shelly-ht-schlafzimmer-temp"
    state_topic: "shellies/shellyht-F4466F/sensor/temperature"
    unit_of_measurement: "°C"    
  - platform: mqtt
    name: "shelly-ht-schlafzimmer-hum"
    state_topic: "shellies/shellyht-F4466F/sensor/humidity"
    icon: "mdi:water-percent"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "shelly-ht-schlafzimmer-batt"
    state_topic: shellies/shellyht-F4466F/sensor/battery
    unit_of_measurement: "%"

2 example files in directory lights/

  platform: mqtt
  name: "shelly_1pm_buero_mqtt"
  state_topic: "shellies/shelly1pm-A4CF12F3FB52/relay/0"
  command_topic: "shellies/shelly1pm-A4CF12F3FB52/relay/0/command"
  payload_on: "on"
  payload_off: "off"
  retain: true
  platform: mqtt
  name: "shelly_1pm_carmen_zimmer_mqtt"
  state_topic: "shellies/shelly1pm-A4CF12F3FA2C/relay/0"
  command_topic: "shellies/shelly1pm-A4CF12F3FA2C/relay/0/command"
  payload_on: "on"
  payload_off: "off"
  retain: true

and this is in my mqtt-broker:

Home assistant MQTT discovery is a custom protocol for Home Assistant.

Basically it says “any device that publishes stuff to /homeassistant/<sensor_type>/<entity_id> will generate a custom device based on the payload if it conforms to the API”

Having it enabled means anyone on the MQTT network (and I do mean anyone) can create new entities if they want by following the API.

So, you have to turn that on in the devices. For the shelly, you would have to tell it to do home assistant mqtt discovery. And you have to hope there is a button to do that. Such configs exist for things like Tasmota, zigbee2mqtt, etc. But you still have to tell those devices to do that.

It is an independent system, and can live with both auto discovered devices and manually config devices.

For Shelly devices you can use Shellies Discovery https://github.com/bieniu/ha-shellies-discovery

@Bieniu This is exactly what I have been looking for but I can’t quite get it to work. I am using the Shelly HT Plus Sensor, and followed the instructions in this video. In MQTT Explorer I can see the sensor sending the temperature and humidity.

However the MQTT integration does not pick up the new MQTT sensors, despite having made the addition to configuration.yaml and the new folder. I can see the Shelly script.


Digging a bit deeper, I saw this error:

I just can’t figure out what I could have done wrong. Any thoughts?

Please read the documentation. You made a very common mistake.

Yes, indeed, Maciek, I read about the error message, but I can’t figure out where I went off track.

  • correct MQTT configuration in Home Assistant with discovery enabled

Done

  • same discovery_prefix in Home Assistant configuration and in script configuration

I used your script.

  • Shellies firmware updated to current version

Done

  • Home Assistant updated to current version

Done

  • enabled MQTT in Shellies configuration

Done

  • you can’t manually run the shellies_discovery.py script ('dict object' has no attribute 'payload_json' error)

Understood but the sensors still don’t show up in HA.

What am I missing? :thinking:

Run the announce automation or restart HA. If this not help show me debug log for python_script.

ht is battery powered, so announce will not help if the device is in deep sleep.
Also HT plus is gen2 device, isn’t it? If so, Use gen2 dedicated Benio’s script

1 Like

Did both. Here is the excerpt of the log:

2022-09-18 15:18:42.141 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies announce: Running automation actions
2022-09-18 15:18:42.142 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies announce: Executing step call service
2022-09-18 15:18:42.143 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Initialized trigger Shellies announce
2022-09-18 15:18:42.480 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Initialized trigger Shellies discovery
2022-09-18 15:20:31.108 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies announce: Running automation actions
2022-09-18 15:20:31.109 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies announce: Executing step call service

Have you the device woken up at time of running announce script?

1 Like

@maxym is right, I didn’t notice you are trying to configure gen2 device. You have to use Shellies Discovery Gen2.

1 Like

I use this one: ha-shellies-discovery/python_scripts at master · bieniu/ha-shellies-discovery · GitHub

Ah, just seeing the Gen2 script. Let me make the changes…

Works like a charm. Thank you both very much!

1 Like