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 ---------------------------------------------------------------------------------------------------------------------
esphome:
  name: f-6

esp8266:
  board: esp01_1m

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "XXX"

ota:
  password: "XXX"

wifi:
  networks:
  - ssid: XXX
    password: XXX

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

mqtt:
  broker: broker.duckdns.org
  discovery: true
  username: username
  password: password
  port: 1883
  topic_prefix: r1/f_6
  birth_message:
    topic: r1/f_6
    payload: online
    retain: TRUE
  will_message:
    topic: r1/f_6
    payload: offline
    retain: TRUE

dallas:
  - pin: GPIO4
    update_interval: 3s

sensor:
  - 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"

switch:
  - 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.

https://esphome.io/components/mqtt.html
quote from website

Blockquote

Warning

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.

Blockquote

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
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.