Sensor displays unavailable after HA restart even with retain flag true

Hello. I have mqtt esphome sensor that is in deep sleep for 4 hours. It has retain flag: true

When I restart Home Assistant the sensor shows as unavailable until it wakes from deep sleep and sends value state. However, the mqtt broker mosquitto remembers last value and can show it when the sensor sleeps.

Can this be fixed that Home Assistant after restart reads last value sent by sensor which is currently in deep sleep? Possibly from mqtt broker?

Thanks, Jan

You need to set empty LWT message topics in ESPHome to prevent the broker noticing the device is no longer available.


Hello Tom, I am sorry I do not quite understand. From the documentation and from other posts here I understand I need to disable availability reporting by setting birth and will messages to empty and I have that:

    topic: mqpr03/status
    qos: 2
    topic: mqpr03/status

It is also consistent with documentation that you refer to, that if birth and will topics have empty message then availability reporting is disabled.

Isn’t that what I want to achieve, to not to report “Unavailable”? But it still does. In the mosquitto_sub I can see last state message even if sensor is offline. Looks like it is not pushed to Home Assistant.

Yep that’s good.

It’s up to the broker to push messages when clients subscribe to a topic containing retained messages. What broker are you using?

I use mosquitto add-on. Still, when I restart HA the sensor shows as unavailable until it wakes up and sends new state. The configuration in home assistant is following:

sensor 28:
  platform: mqtt
  unique_id: sensor.flower2_distance
  name: "Flower2_sensor_distance"
  state_topic: "mqtt_flower2/sensor/flower2_distance"
  force_update: true
  qos: 2
  unit_of_measurement: "mm"

and configuration in esphome like this:

  - platform: vl53l0x
    id: flower2_distance
    name: "Flower2 Distance"
    retain: true
    address: 0x29
    #update_interval: 59min
    accuracy_decimals: 0
    #force_update: true
    long_range: False
    unit_of_measurement: "mm"
      - multiply: 1000.0


  username: ...
  password: ...
    topic: mqtt_flower2/status
    qos: 2
    topic: mqtt_flower2/status
    - topic: mqtt_flower2/ota_mode
      payload: 'ON'
        - deep_sleep.prevent: deep_sleep_1
    - topic: mqtt_flower2/sleep_mode
      payload: 'ON'
        - deep_sleep.enter: deep_sleep_1

Try removing force_update: true

I will try but I think it needs to be there in order to correctly update Grafana raphs, I think I read it somewhere…

I did remove force_update and sensor still shows unavailable after HA restart.

That’s true, but if it solves your problem there are other methods around the Grafana issue.

Unfortunately removing force_update did not help. It show last value in history but still as unavailable. HA after restart:

I’m our of ideas. :man_shrugging:

I was thinking about setting the expire_after option to a value larger than your sleep time but that’s not going to help with HA restarts.