ESPHome with MQTT autodiscovery

Hey,

I am relatively new to ESPHome and HA, but I have recently been trying to setup my system.

I went for MQTT way, i.e. my ESP8266s do not have API tag in their config. I managed to setup ESP8266 and MQTT explorer shows the device and messages coming in. I also see retained configuration messages of homeassistant with its prefix topic. This is my device config.

esphome:
  name: esp8266-1
  friendly_name: scd4x-air-quality-1

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
#api:

sensor:
  - platform: scd4x
    co2:
      name: "co2"
    temperature:
      name: "temp"
    humidity:
      name: "humidity"
    i2c_id: bus_a  

i2c:
  sda: 2
  scl: 14
  scan: true
  id: bus_a

mqtt:
  broker: IP
  username: user
  password: user

ota:
  
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ""
    password: ""

captive_portal:

One problem I saw right away is that no entities were created in homeassistant although retained config messages are seen in MQTT explorer.

image

Do I need additional setup for HA to see this device and somehow to autodetect it and add entities? Do I have to modify configuration.yaml to do that only? What is the typical workflow for MQTT ESPHome devices/entities to appear? I have reviewed manual several times, but it is really hard to read and understand sometimes, because it mostly works as a reference only…

Moreover, I recently reinstalled homeassistant machine and thus all of my devices are gone now. But I do not understand, how can I readd my devices to ESPHome dashboard that are based on MQTT integration only? They do not have API integration. At the moment, HA does not add them via devices section (with an IP) and states that “it does not have API tag”. Also, there is no option to readd on ESPHome section. I am simply lost, I do not want to reflash all my devices incase I reinstall homeassistant OS…

Thank you.

Is MQTT added as an integration here Open your Home Assistant instance and show your integrations.

And is Discovery enabled in the same place?

I do not know, why I missed that, but yes, I found that there is that MQTT autodiscovery option that I should have enabled. For some reason I was not aware of this… Thanks, now it seems that entities are created, appreciate that.

But still, the second problem exists. I do not know how to readd that device to ESPHome dashboard in order to enable OTA upgrades. Adding device through HA simply requires API, but… in this case, it is not preset.

Do you have any ideas?

Thanks.

So you ghave the ESPHome add-on installed but did not use it to create the device?

I have addon installed. I added ESPHome device, configured it successfully, I was able to install firmware OTA, but then I deleted the device. And I am no longer able to add it back. It does not have API enabled, only MQTT works for data transfer, so I am not really sure how to get it back to ESP Dashboard (if that is possible at all) without actually reflashing from scratch.

If that’s the yaml for it above, create a new device with the same name as the existing. Before saving copy & paste that code into it.

It should then be fine - you don’t need to reflash.

if you ever lose the yaml code for a device that isn’t on the dashboard - it’s gone forever. It is in no way recoverable from the device.

1 Like

Thanks, I will give it a try this evening. Appreciate your help.

Hi,

I would like to ask you guys for some explanation as I am getting slightly lost with the ESP & mqtt and please do excuse my basic question - if I get an ESP device into HA is it automatically discovered by mqtt and broadcasted? Example - I have an ESP32 with BMP and INA sensors with few entities. No mqtt configured in ESP yaml code. The device is visible in the Explorer and I have access to the sensors’ entities also in my Homey that gets mqtt communication. I can not however see these sensors entities under mqtt integration…

Your kind clarification for a noob would be highly appreciated…

TIA.

Then it won’t be using MQTT, nor will the sensors appear on your MQTT broker.

The only way (mostly) for an ESPHome device to connect to a broker is if you define the MQTT broker in the YAML, and turn off the api for good measure.

I was thinking same way, but :slight_smile: - it appears in the MQTT Explorer (?) and as I wrote - I can get it from HA to my Homey, which do communicate with each other using matt… I have intentionally deleted the mqtt config in yaml as I had duplicated sensors when I have added device to HA, now I only have single entities but still mqtt explorer sees it… I can not understand that…

My yaml (all mqtt setup is not active):


#enable mqtt

#captive_portal:

#mqtt:
# broker: 192.168.1.84
# username: !secret mqtt_username
# password: !secret mqtt_password
# reboot_timeout: 0s //UNUSED LINE//
# will_message:
#    topic: 
#      Temperature/topic
#      Pressure/topic
#      Current/topic
#      Power/topic
#      bus_voltage/topic
#      shunt_voltage/topic
#    payload: 
#      Temperature
#      Pressure
#      Current
#      Power
#      bus_voltage
#      shunt_voltage
# shutdown_message:
#    topic: 
#      Temperature/topic
#      Pressure/topic
#      Current/topic
#      Power/topic
#      bus_voltage/topic
#      shunt_voltage/topic
#    payload: 
#      Temperature
#      Pressure
#      Current
#      Power
#      bus_voltage
#      shunt_voltage

i2c:
    sda: GPIO08
    scl: GPIO09 
    scan: True

sensor:
  - platform: bmp085
    address: 0x77
    temperature:
       name: "Temperature"
    pressure:
       name: "Pressure"
    update_interval: 30s
  - platform: ina219
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      name: "Current"
    power:
      name: "Power"
    bus_voltage:
      name: "Bus Voltage"
    shunt_voltage:
      name: "Shunt Voltage"
    max_voltage: 32.0V
    max_current: 3.2A
    update_interval: 360s

If retain was set to true then the broker will cache that forever. Delete the topic in Explorer and see if it comes back. Click on the top level of the topic - then top right of Explorer in the Topic pane there is a little bin symbol to remove it.

I guess there was no retain setting but I think it defaults to true. After deleting this topic from explorer it is not coming back. Thank you for helping me understand. Best. JU.