How to make deep sleep sensor show unavailable

I’d like my ESPhome sensor to show unavailable in HA if it’s disconnected, if the battery dies etc. Currently it just shows the last state reported.

I don’t want it to show unavailable if it’s just sleeping - only after an extended (ideally programmable) time.

This seems to be the opposite of what everyone is asking for. Am I missing something?

Show your code to see how you have gotten into this mess.

I will mention that it works sometimes i.e. that state is marked as unavailable. I know that I can write an automation to achieve the desired result, but I thought this would happen automatically. Here’s my code:

esphome:
  name: new32
  friendly_name: front_door_esp
  on_boot: 
    then:
      - switch.toggle: led
      - delay: 1s
      - switch.toggle: led
          

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "3KMh7sKH2alrQrzPo8NZ16kfgZwzGuY0R38N1QHb/ss="

ota:
  - platform: esphome
    password: "16afd332cfd5d03f6cb81bfa506309c8"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: True

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "New32 Fallback Hotspot"
    password: "gfCExsmT6C4F"

captive_portal:

deep_sleep:
  run_duration: 10s
  sleep_duration: 5min
  wakeup_pin: 
    number: GPIO4
    allow_other_uses: true

binary_sensor:
  - platform: gpio
    device_class: door
    pin:
      number: GPIO4
      allow_other_uses: true
      mode:
        input: true
        pullup: true
    name: esp_sensor_1
    id: front_door_esp  

switch:
  - platform: gpio
    pin:
      number: GPIO2 
      mode: output
    id: led

Home Assistant does not maintain a constant connection to its sensors, and so the HA dashboard shows the last value received - as you have observed. I see you are using ESP32 deep_sleep, which actually powers the ESP32 off … so disconnection is normal expected behaviour.

I found it helpful when trying to work out if a device was faulty to turn on the “Last updated” secondary value in most of the Entities lists in my dashboards, like


You can see the the result in the preview pane on the right hand side. I have now turned on the Last Updated secondary info almost everywhere to give me confidence that the values are indeed being received by HA.


I notice that your ESP is awake for only 10 seconds every 5 minutes. Ten seconds is not very long … must be difficult trying to catch that window for doing updates OTA :wink:

I wonder if sometimes 10 seconds is not long enough for the ESP to power on, ESPHome to start up and connect to wi-fi … and so the “unavailable” you see may be that the ESP32 is still waking up. I have noticed my HA dashboard sensors show “Unavailable” briefly while my ESP32 is waking up. Or maybe it is that HA hasn’t received several updates in a row and has timed out.

Going off on a tangent, you might like to add a battery voltage sensor to your ESP32. I think it’s better to have a value, and be able to react before your battery goes flat. And how do you intend to handle a flat battery ?

Other than that, I think the logic to determine if the ESP has been disconnected too long will need to be in HA.

1 Like

Thank you for the reply and the info.

I’ve found that if I open the door, the sensor is constantly triggered and awake so OTA works fine.

I would like to know the battery voltage but it isn’t critical for this application. I will have easy physical access and these only need them to work for a finite time which is much less than the capacity of my batteries.