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.

See: https://esphome.io/components/mqtt.html#last-will-and-birth-messages

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:

  birth_message:
    topic: mqpr03/status
    payload:
    qos: 2
  will_message:
    topic: mqpr03/status
    payload:

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"
    filters:
      - multiply: 1000.0

...

mqtt:
  broker: 10.0.0.54
  username: ...
  password: ...
  birth_message:
    topic: mqtt_flower2/status
    payload:
    qos: 2
  will_message:
    topic: mqtt_flower2/status
    payload:
  on_message:
    - topic: mqtt_flower2/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: mqtt_flower2/sleep_mode
      payload: 'ON'
      then:
        - 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:
image

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.

Have you found a solution to this?

I have the same problem, but I have mosquitto in a separate container which do not help.

Not yet. I just dont make any config changes in yaml and I dont restart HA :slight_smile: Thats one advantage of running HA in VirtualBox, when I need to restart hostin OS, I save state of virtual box, restart computer and then restore state of HA. It even does not recognize that its host machine has been restarted.

I actually found a solution to that solved my problem. Discovery: True did the trick. I only thought that was needed for the initial discovery.

(I am using MQTT sensors from a esphome battery powered devices.)

Thanks, but if you mean discovery: true in mqtt: section then it does not work for me. I have it there from very beginning yet HA shows unavailable if it restarts while sensor is in deep sleep.