MQTT Sensor not showing up in HA

Hi. I have a temp sensor that broadcasts over MQTT. Using a dallas temp sensor with ESP8266 Wemos D1 mini.

I have the identical setup for several other units and they work perfectly. I can also see that the message is being relayed to the MQTT server when I check with MQTT-explorer. The other entities (wifi, led switches) work nicely with this config. it’s just the the temp sensor that HA doesn’t pick up for some strange reason.

Any ideas what the problem could be?

Code for my sensor:

#--------------------------------------------------------------------------------------------------------------------- BASIC ---------------------------------------------------------------------------------------------------------------------
  name: f-6

  board: esp01_1m

# Enable logging
  level: DEBUG

# Enable Home Assistant API
    key: "XXX"

  password: "XXX"

  - ssid: XXX
    password: XXX

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "f-6 Fallback Hotspot"
    password: "XXX"

  discovery: true
  username: username
  password: password
  port: 1883
  topic_prefix: r1/f_6
    topic: r1/f_6
    payload: online
    retain: TRUE
    topic: r1/f_6
    payload: offline
    retain: TRUE

  - pin: GPIO4
    update_interval: 3s

  - platform: dallas
    address: 0xa83c86f649dfb128
    name: "f_6"
    id: "f_6_temp"
    unit_of_measurement: "°C"

  - platform: wifi_signal
    name: "wifi_f_6"
    update_interval: 10s
    unit_of_measurement: "dB"

  - platform: gpio
    id: "red_led_switch_f_6"
    name: "red led_f_6" 
    pin: GPIO14
    inverted: FALSE
  - platform: gpio
    id: "green_led_switch_f_6"
    name: "green led_f_6" 
    pin: GPIO13
    inverted: FALSE

I see 2 errors i see is you have API: in the setup but are using mqtt.
the documenation says you need to remove api: from your config if using mqtt.
quote from website



If you enable MQTT and you do not use the “native API” for Home Assistant, you must remove the api: line from your ESPHome configuration, otherwise the ESP will reboot every 15 minutes because no client connected to the native API.


the second error is you topic_prefix, home assistant watches for the prefix homeassistant… unless you have defined otherwise.

to make this work you would have to manually go into MQTT in settings, and add r1/f_6 to “listen to a topic”

Thanks. I forgot to include the api line but I have set it to reboot:0, which prevents it from rebooting every 15 min.

# Enable Home Assistant API
  password: "123"
  reboot_timeout: 0s

This actually works fine on my other sensors together with mqtt. I use the above as a template. And the rest of the sensors above work.

Regarding the topic, I’m not really sure why it doesn’t work. I use the same base topic for the rest of the sensors and I haven’t defined them manually and they pop up automatically. So for example, I have f_5 that works as intended.

The only other thing I can think of is that you need to restart home assistant, as ESPHome only can auto discover a new device on restart!

Thanks. Did as well but nothing happened. I guess I’ll just have to manually define it in my mqtt sensor list then.