Eshome sleep befor connecting to home assita

INFO Reading configuration /config/rtank-level.yaml...
INFO Starting log output from 192.168.XX.X15 using esphome API
WARNING Can't connect to ESPHome API for 192.168.XX.X15: Error connecting to ('192.168.XX.X15', 6053): [Errno 113] Connect call failed ('192.168.XX.X15', 6053)
INFO Trying to reconnect to 192.168.XX.X15 in the background
INFO Successfully connected to 192.168.XX.X15
[11:36:19][I][app:102]: ESPHome version 2023.4.4 compiled on May  8 2023, 10:06:39
[11:36:19][C][status_led:019]: Status LED:
[11:36:19][C][status_led:020]:   Pin: GPIO2
[11:36:19][C][wifi:505]: WiFi:
[11:36:19][C][wifi:363]:   Local MAC: XX:5B:XX:E9:XX:XX
[11:36:19][C][wifi:364]:   SSID: [redacted]
[11:36:19][C][wifi:365]:   IP Address: 192.168.XX.X15
[11:36:19][C][wifi:366]:   BSSID: [redacted]
[11:36:19][C][wifi:368]:   Hostname: 'rtank-level'
[11:36:19][C][wifi:370]:   Signal strength: -69 dB ▂▄▆█
[11:36:19][C][wifi:374]:   Channel: 4
[11:36:19][C][wifi:375]:   Subnet: 255.255.255.0
[11:36:19][C][wifi:376]:   Gateway: 192.168.XX.1
[11:36:19][C][wifi:377]:   DNS1: 0.0.0.0
[11:36:19][C][wifi:378]:   DNS2: 0.0.0.0
[11:36:19][C][logger:294]: Logger:
[11:36:19][C][logger:295]:   Level: DEBUG
[11:36:19][C][logger:296]:   Log Baud Rate: 115200
[11:36:19][C][logger:297]:   Hardware UART: UART0
[11:36:19][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Water-Tank Level'
[11:36:19][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[11:36:19][C][ultrasonic.sensor:045]:   Unit of Measurement: 'm'
[11:36:19][C][ultrasonic.sensor:045]:   Accuracy Decimals: 2
[11:36:19][C][ultrasonic.sensor:045]:   Icon: 'mdi:arrow-expand-vertical'
[11:36:19][C][ultrasonic.sensor:046]:   Echo Pin: GPIO4
[11:36:19][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO5
[11:36:19][C][ultrasonic.sensor:048]:   Pulse time: 10 µs
[11:36:19][C][ultrasonic.sensor:049]:   Timeout: 11661 µs
[11:36:19][C][ultrasonic.sensor:050]:   Update Interval: 1.0s
[11:36:19][D][ultrasonic.sensor:040]: 'Water-Tank Level' - Got distance: 0.05 m
[11:36:19][D][sensor:109]: 'Water-Tank Level': Sending state 0.05076 m with 2 decimals of accuracy
[11:36:19][C][captive_portal:088]: Captive Portal:
[11:36:19][C][web_server:151]: Web Server:
[11:36:19][C][web_server:152]:   Address: 192.168.XX.X15:80
[11:36:19][C][mdns:108]: mDNS:
[11:36:19][C][mdns:109]:   Hostname: rtank-level
[11:36:19][C][ota:093]: Over-The-Air Updates:
[11:36:19][C][ota:094]:   Address: 192.168.XX.X15:8266
[11:36:19][C][ota:097]:   Using Password.
[11:36:19][C][api:138]: API Server:
[11:36:19][C][api:139]:   Address: 192.168.XX.X15:6053
[11:36:19][C][api:141]:   Using noise encryption: YES
[11:36:19][C][homeassistant.binary_sensor:039]: Homeassistant Binary Sensor 'disable_deep_sleep'
[11:36:19][C][homeassistant.binary_sensor:040]:   Entity ID: 'input_boolean.disable_deep_sleep'
[11:36:19][C][sntp:053]: SNTP Time:
[11:36:19][C][sntp:054]:   Server 1: '0.pool.ntp.org'
[11:36:19][C][sntp:055]:   Server 2: '1.pool.ntp.org'
[11:36:19][C][sntp:056]:   Server 3: '2.pool.ntp.org'
[11:36:19][C][sntp:057]:   Timezone: 'IST-5:30'
[11:36:20][C][deep_sleep:049]: Setting up Deep Sleep...
[11:36:20][C][deep_sleep:052]:   Sleep Duration: 298000 ms
[11:36:20][D][ultrasonic.sensor:040]: 'Water-Tank Level' - Got distance: 0.05 m
[11:36:21][D][ultrasonic.sensor:040]: 'Water-Tank Level' - Got distance: 0.05 m
[11:36:22][D][api:102]: Accepted 192.168.XX.X00
[11:36:22][I][deep_sleep:116]: Beginning Deep Sleep
WARNING 192.168.XX.X15: Connection error occurred: Ping response not received after 90.0 seconds
INFO Processing unexpected disconnect from ESPHome API for 192.168.XX.X15
WARNING Disconnected from API
WARNING Can't connect to ESPHome API for 192.168.XX.X15: Error connecting to ('192.168.XX.X15', 6053): [Errno 113] Connect call failed ('192.168.XX.X15', 6053)
INFO Trying to reconnect to 192.168.XX.X15 in the background

How to make sure that, home-assistant [11:36:22][D][api:102]: Accepted 192.168.XX.X00 receive sensor data: that is
how can I write script which wait for home-assistant to connect, and then, to read the value of sensor data after that go_to sleep for 5min or 298sec,

Can use the on_boot event. Use priority 200 so that it’s invoked after native API (HA) is set up.

let me check and then I will report you.

[I][logger:259]: Log initialized
[C][status_led:014]: Setting up Status LED...
[C][ota:469]: There have been 8 suspected unsuccessful boot attempts.
[I][app:029]: Running through setup()...
[C][ultrasonic.sensor:011]: Setting up Ultrasonic Sensor...
[C][wifi:038]: Setting up WiFi...
[C][wifi:039]:   Local??MAC: 5C:CF:7F:FD:8A:D2
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:00 - 01-01-1970'
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:05 - 01-01-1970'
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:15 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:25 - 01-01-1970'
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:35 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:45 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[W][wifi:586]: Restarting WiFi adapter...
[D][text_sensor:064]: 'Ora Text': Sending state '00:00:55 - 01-01-1970'
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[I][wifi:142]: Starting fallback AP!
[C][wifi:211]: Setting up AP...
[C][wifi:213]:   AP SSID: 'nodemcu2-test'
[C][wifi:214]:   AP Password: 'nodemcu2-test'
[C][wifi:223]:   IP Address: 192.168.4.1
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:01:05 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:01:15 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[D][text_sensor:064]: 'Ora Text': Sending state '00:01:25 - 01-01-1970'
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[I][wifi:258]: WiFi Connecting to 'mi-fi'...
[D][text_sensor:064]: 'Ora Text': Sending state '00:01:35 - 01-01-1970'
[W][wifi_esp8266:482]: Event: Disconnected ssid='mi-fi' bssid=AA:DA:0C:6E:6C:4E reason='Auth Expired'
[W][wifi:550]: Error while connecting to network.
[D][ultrasonic.sensor:036]: 'Water-Tank Level' - Distance measurement timed out!
[I][wifi:258]: WiFi Connecting to 'mi-fi'...

Unable to connect wifi, time not synced, there is some problem with password