One mqtt sensor of many reporting unavailable during sleep

morning all.

so i have a nodemcu with temp/humidity sensors and self battery level.
as its battery powered im using mqtt instead of the api.
the temp and humidty work fine and keep the last known values in sensor entity
the battery level however reverts to unavailable
the nodemcu yaml is as follows

esphome:
  name: nodemcutwo

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
#api:

ota:
  password: !secret ota_password
  id: my_ota
  
wifi:
  power_save_mode: HIGH
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  manual_ip:
    static_ip: 192.168.1.115
    gateway: 192.168.1.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nmcu2 Fallback Hotspot"
    password: !secret fallbackappassword
web_server:
  port: 80
  auth:
    username: admin
    password: !secret webservpass
captive_portal:

    

deep_sleep:
  run_duration: 4s
  sleep_duration: 15min
  id: battery_deep_sleep

mqtt:
  broker: 192.168.1.117
  username: !secret mqtt_username
  password: !secret mqtt_password

  birth_message:
    topic: nodemcuv2/status
    payload: ''
    retain: true
  will_message:
    topic: nodemcuv2/status
    payload: ''
    retain: true
  
  on_message:
    - topic: deep_sleep/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: battery_deep_sleep
  
    
    - topic: deep_sleep/ota_mode
      payload: 'OFF'
      then:
        - mqtt.publish:
            topic: deep_sleep/ota_mode
            payload: 'PASS'
            qos: 2
            retain: true
        - deep_sleep.enter: battery_deep_sleep






sensor:
  - platform: wifi_signal
    name: "nodemcutwo ssi"
    update_interval: 1s
  - platform: dht
    pin: D7
    temperature:
      name: "nodemcutwo Temperature"
    humidity:
      name: "nodemcutwo Humidity"
    update_interval: 1s    
    
    
  - platform: adc
    pin: A0
    retain: true
    name: "nodemcutwo Battery"
    update_interval: 1s
    expire_after: 1860s
    device_class: battery
    unit_of_measurement: "%"
    accuracy_decimals: 1
    filters:
      - multiply: 7.959902632416832
      - calibrate_linear:
            - 8.4 -> 100

            - 2.8 -> 0  
          
          
text_sensor:
  - platform: wifi_info
    ip_address:
      name: nodemcutwo IP Address
    ssid:
      name: nodemcutwo Connected SSID
    bssid:
      name: nodemcutwo Connected BSSID
    mac_address:
      name: nodemcutwo Mac Wifi Address
    scan_results:
      name: nodemcutwo Latest Scan Results
      



and here is a log of it waking up and transmitting

INFO Reading configuration /config/esphome/nodemcutwo.yaml...
INFO Starting log output from nodemcutwo/debug
INFO Connected to MQTT broker!
[12:33:01][I][deep_sleep:103]: Beginning Deep Sleep
[12:38:59][I][mqtt:215]: MQTT Connected!
[12:39:00][C][deep_sleep:035]: Setting up Deep Sleep...
[12:39:00][I][deep_sleep:040]: Scheduling Deep Sleep to start in 4000 ms
[12:39:00][I][app:062]: setup() finished successfully!
[12:39:00][D][text_sensor:067]: 'nodemcutwo Connected SSID': Sending state 'WiFi-5F80'
[12:39:00][D][sensor:124]: 'nodemcutwo ssi': Sending state -44.00000 dBm with 0 decimals of accuracy
[12:39:00][D][text_sensor:067]: 'nodemcutwo Connected BSSID': Sending state '34:E8:94:7B:5F:80'
[12:39:00][D][sensor:124]: 'nodemcutwo Battery': Sending state 28.01105 % with 1 decimals of accuracy
[12:39:00][D][text_sensor:067]: 'nodemcutwo IP Address': Sending state '192.168.1.115'
[12:39:00][I][app:102]: ESPHome version 2022.2.1 compiled on Feb 19 2022, 00:44:25
[12:39:00][C][wifi:491]: WiFi:
[12:39:00][C][wifi:353]:   Local MAC: 40:91:51:50:B6:88
[12:39:00][C][wifi:354]:   SSID: 'WiFi-5F80'[redacted]
[12:39:00][C][wifi:355]:   IP Address: 192.168.1.115
[12:39:00][C][wifi:356]:   BSSID: 34:E8:94:7B:5F:80[redacted]
[12:39:00][C][wifi:358]:   Hostname: 'nodemcutwo'
[12:39:00][C][wifi:360]:   Signal strength: -44 dB ▂▄▆█
[12:39:00][C][wifi:364]:   Channel: 10
[12:39:00][C][wifi:365]:   Subnet: 255.255.255.0
[12:39:00][C][wifi:366]:   Gateway: 192.168.1.1
[12:39:00][C][wifi:367]:   DNS1: 0.0.0.0
[12:39:00][C][wifi:368]:   DNS2: 0.0.0.0
[12:39:00][C][logger:233]: Logger:
[12:39:00][C][logger:234]:   Level: DEBUG
[12:39:00][C][logger:235]:   Log Baud Rate: 115200
[12:39:00][C][logger:236]:   Hardware UART: UART0
[12:39:00][C][dht:017]: DHT:
[12:39:00][C][dht:018]:   Pin: GPIO13
[12:39:00][C][dht:020]:   Auto-detected model: DHT11
[12:39:00][C][dht:027]:   Update Interval: 1.0s
[12:39:00][C][dht:029]:   Temperature 'nodemcutwo Temperature'
[12:39:00][C][dht:029]:     Device Class: 'temperature'
[12:39:00][C][dht:029]:     State Class: 'measurement'
[12:39:00][C][dht:029]:     Unit of Measurement: '°C'
[12:39:00][C][dht:029]:     Accuracy Decimals: 1
[12:39:00][C][dht:030]:   Humidity 'nodemcutwo Humidity'
[12:39:00][C][dht:030]:     Device Class: 'humidity'
[12:39:00][C][dht:030]:     State Class: 'measurement'
[12:39:00][C][dht:030]:     Unit of Measurement: '%'
[12:39:00][C][dht:030]:     Accuracy Decimals: 0
[12:39:00][C][adc:062]: ADC Sensor 'nodemcutwo Battery'
[12:39:00][C][adc:062]:   Device Class: 'battery'
[12:39:00][C][adc:062]:   State Class: 'measurement'
[12:39:00][C][adc:062]:   Unit of Measurement: '%'
[12:39:00][C][adc:062]:   Accuracy Decimals: 1
[12:39:00][C][adc:067]:   Pin: GPIO17
[12:39:00][C][adc:094]:   Update Interval: 1.0s
[12:39:00][C][wifi_info:013]: WifiInfo Mac Address 'nodemcutwo Mac Wifi Address'
[12:39:00][C][captive_portal:144]: Captive Portal:
[12:39:00][C][web_server:179]: Web Server:
[12:39:00][C][web_server:180]:   Address: 192.168.1.115:80
[12:39:00][C][mdns:084]: mDNS:
[12:39:00][C][mdns:085]:   Hostname: nodemcutwo
[12:39:00][C][ota:085]: Over-The-Air Updates:
[12:39:00][C][ota:086]:   Address: 192.168.1.115:8266
[12:39:00][C][ota:089]:   Using Password.
[12:39:00][C][mqtt:064]: MQTT:
[12:39:00][C][mqtt:065]:   Server Address: 192.168.1.117:1883 (192.168.1.117)
[12:39:00][C][mqtt:067]:   Username: 'mqtt3'[redacted]
[12:39:00][C][mqtt:068]:   Client ID: 'nodemcutwo-40915150b688'[redacted]
[12:39:00][C][mqtt:070]:   Discovery prefix: 'homeassistant'
[12:39:00][C][mqtt:071]:   Discovery retain: YES
[12:39:00][C][mqtt:073]:   Topic Prefix: 'nodemcutwo'
[12:39:00][C][mqtt:075]:   Log Topic: 'nodemcutwo/debug'
[12:39:00][C][mqtt:078]:   Availability: 'nodemcuv2/status'
[12:39:00][C][wifi_signal.sensor:009]: WiFi Signal 'nodemcutwo ssi'
[12:39:00][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[12:39:00][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[12:39:00][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[12:39:00][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[12:39:00][C][wifi_info:009]: WifiInfo IPAddress 'nodemcutwo IP Address'
[12:39:00][C][wifi_info:011]: WifiInfo SSID 'nodemcutwo Connected SSID'
[12:39:00][C][wifi_info:012]: WifiInfo BSSID 'nodemcutwo Connected BSSID'
[12:39:00][W][dht:169]: Requesting data from DHT failed!
[12:39:00][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[12:39:00][D][sensor:124]: 'nodemcutwo Temperature': Sending state nan °C with 1 decimals of accuracy
[12:39:00][D][sensor:124]: 'nodemcutwo Humidity': Sending state nan % with 0 decimals of accuracy
[12:39:00][C][wifi_info:010]: WifiInfo Scan Results 'nodemcutwo Latest Scan Results'
[12:39:00][C][mqtt:590]: MQTT Message Trigger:
[12:39:00][C][mqtt:591]:   Topic: 'deep_sleep/ota_mode'
[12:39:00][C][mqtt:592]:   QoS: 0
[12:39:00][C][mqtt:590]: MQTT Message Trigger:
[12:39:00][C][mqtt:591]:   Topic: 'deep_sleep/ota_mode'
[12:39:00][C][mqtt:592]:   QoS: 0
[12:39:00][C][mqtt.sensor:027]: MQTT Sensor 'nodemcutwo ssi':
[12:39:00][C][mqtt.sensor:031]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_ssi/state'
[12:39:00][C][mqtt.sensor:027]: MQTT Sensor 'nodemcutwo Temperature':
[12:39:00][C][mqtt.sensor:031]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_temperature/state'
[12:39:00][C][mqtt.sensor:027]: MQTT Sensor 'nodemcutwo Humidity':
[12:39:00][C][mqtt.sensor:031]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_humidity/state'
[12:39:00][C][mqtt.sensor:027]: MQTT Sensor 'nodemcutwo Battery':
[12:39:00][C][mqtt.sensor:029]:   Expire After: 1860s
[12:39:00][C][mqtt.sensor:031]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_battery/state'
[12:39:00][C][mqtt.text_sensor:023]: MQTT Text Sensor 'nodemcutwo IP Address':
[12:39:00][C][mqtt.text_sensor:024]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_ip_address/state'
[12:39:00][C][mqtt.text_sensor:023]: MQTT Text Sensor 'nodemcutwo Connected SSID':
[12:39:00][C][mqtt.text_sensor:024]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_connected_ssid/state'
[12:39:00][C][mqtt.text_sensor:023]: MQTT Text Sensor 'nodemcutwo Connected BSSID':
[12:39:00][C][mqtt.text_sensor:024]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_connected_bssid/state'
[12:39:00][C][mqtt.text_sensor:023]: MQTT Text Sensor 'nodemcutwo Mac Wifi Address':
[12:39:00][C][mqtt.text_sensor:024]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_mac_wifi_address/state'
[12:39:00][D][sensor:124]: 'nodemcutwo ssi': Sending state -43.00000 dBm with 0 decimals of accuracy
[12:39:00][C][mqtt.text_sensor:023]: MQTT Text Sensor 'nodemcutwo Latest Scan Results':
[12:39:01][C][mqtt.text_sensor:024]:   State Topic: 'nodemcutwo/sensor/nodemcutwo_latest_scan_results/state'
[12:39:01][C][deep_sleep:047]: Setting up Deep Sleep...
[12:39:01][C][deep_sleep:050]:   Sleep Duration: 900000 ms
[12:39:01][C][deep_sleep:053]:   Run Duration: 4000 ms
[12:39:01][D][sensor:124]: 'nodemcutwo Battery': Sending state 28.01105 % with 1 decimals of accuracy
[12:39:01][D][dht:048]: Got Temperature=29.2°C Humidity=90.0%
[12:39:01][D][sensor:124]: 'nodemcutwo Temperature': Sending state 29.20000 °C with 1 decimals of accuracy
[12:39:01][D][sensor:124]: 'nodemcutwo Humidity': Sending state 90.00000 % with 0 decimals of accuracy
[12:39:01][D][sensor:124]: 'nodemcutwo ssi': Sending state -43.00000 dBm with 0 decimals of accuracy
[12:39:02][D][sensor:124]: 'nodemcutwo Battery': Sending state 28.01105 % with 1 decimals of accuracy
[12:39:02][W][dht:169]: Requesting data from DHT failed!
[12:39:02][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[12:39:02][D][sensor:124]: 'nodemcutwo Temperature': Sending state nan °C with 1 decimals of accuracy
[12:39:02][D][sensor:124]: 'nodemcutwo Humidity': Sending state nan % with 0 decimals of accuracy
[12:39:02][D][sensor:124]: 'nodemcutwo ssi': Sending state -47.00000 dBm with 0 decimals of accuracy
[12:39:03][D][sensor:124]: 'nodemcutwo Battery': Sending state 28.01105 % with 1 decimals of accuracy
[12:39:03][D][dht:048]: Got Temperature=29.8°C Humidity=84.0%
[12:39:03][D][sensor:124]: 'nodemcutwo Temperature': Sending state 29.80000 °C with 1 decimals of accuracy
[12:39:03][D][sensor:124]: 'nodemcutwo Humidity': Sending state 84.00000 % with 0 decimals of accuracy
[12:39:03][D][sensor:124]: 'nodemcutwo ssi': Sending state -45.00000 dBm with 0 decimals of accuracy
[12:39:04][D][sensor:124]: 'nodemcutwo Battery': Sending state 27.87224 % with 1 decimals of accuracy
[12:39:04][I][deep_sleep:103]: Beginning Deep Sleep

the mcu sleeps for 15 mins and runs for 4 seconds…
i have tried setting retain on the sensor(not sure if this is a valid option but i get no errors and read that it fixed the problem for someone else)
i also set an expire after flag of 31 mins(to me this should make it keep the last known value after 2 cycles without an update and should then revert to unavailable)
this is driving me nuts… the temp and humidty are retained 100% of the time, but the battery level seems to revert to unavailable more than half the time it sleeps, never immediatly after a wake/reporting cycle … but when it happens it seems to happen half way through the sleep portion of the cycle

Hi
Why not keep it simpler and just take the one battery reading each time it comes out of deepsleep (the one you know works each time) You only are measuring to 1 decimal place and the battery shouldn’t change that much. Even increasing accuracy to 2 decimals might give you more smooth curve in graph if that looks better.

  - platform: adc
    pin: A0
    name: "nodemcutwo Battery"
    update_interval: 60s
    device_class: battery
    unit_of_measurement: "%"
    accuracy_decimals: 2
    filters:
      - multiply: 7.959902632416832
      - calibrate_linear:
            - 8.4 -> 100

            - 2.8 ->  0

thanks for your comment, but i set the interval for 1 second so that i would get a couple of readings each cycle, just an old habbit from temperature sensors that give a couple of glitchy readings…and the one decimal place was chosen because thats all i needed, i wasnt asking about smoothing out graphs and making it look better… i was asking why a sensor would revert to unavailable at random times

Hi
No problem. In my setup I set update interval to 60s so no chance of getting a second reading unless it hasn’t gone back into deep sleep. I’ve never had it go unavailable.Are you using a voltage divider to to A0 pin? If so what resistor are you using?

the physical setup is fine, i get good and only good readings… it just dissapears while the esp is sleeping

If the birth message and last will message have empty topics or topics that are different from each other, availability reporting will be disabled. This should stop them reporting unavailable.
This is a known problem for ESP going into deepsleep.