How to get "last known value" while sensor is in deep sleep

I’ve had the same issue and just won with it.

TL;DR set entire birth_message and will_message to be empty and it all works, retaining last sensor values.

As per Esphome documentation: https://esphome.io/components/mqtt.html#last-will-and-birth-messages the birth and will messages should have empty topics, not payloads. It’s actually impossible because Esphome complains then about empty string where it can’t be. There’s a fix in discussion in this Esphome issue: https://github.com/esphome/issues/issues/352 , it will work if entire will_message and birth_message are empty, without any sub-keys.
No need for any sensor templates in Homeassistant, I’m actually still running a pretty vanilla installation with autogenerated Lovelace dashboard and almost zero handwritten yaml.

My working Esphome yaml fragment:

mqtt:
  broker: ...
  port: ...
  username: ...
  password: ...
  client_id: $devicename
  topic_prefix: $devicename
  birth_message:
  will_message:
  on_message: 
    topic: ${devicename}/ota_mode 
    then: 
      - deep_sleep.prevent: deep_sleep_1

This should definitely be updated in Esphome documentation :wink: