How to add a 'sensor' that tells me the time between wakeup and deepsleep?

Hi all,

Could someone point me in the right direction to add the following feature to this ESPHome device?

The ESP is in deep sleep most of the time. Every 5min it wakes up, connects to WiFi, updates the sensors, and goes back to sleep. Normally, the awake time is around 5-6s.

I’d like to add a sensor “timeawake” to log how long it’s awake every cycle.

So in modified pseudo-code, I want to do this:

esphome:
  name: esp-am2320
  platform: ESP8266
  board: d1_mini
  on_boot:
    - int t1 = millis();   <============================= TO ADD
    - wait_until: mqtt.connected
    - component.update: 'esp_am2320'
    - component.update: 'abshum_livingroom'
    - component.update: 'am2320_rssi'
    - delay: 1s
    - int t2 = millis();   <============================= TO ADD
    - int timeawake = t2 - t1;   <============================= TO ADD
    - //update a timeawake sensor   <============================= TO ADD
    - deep_sleep.enter:

Thanks! The full current code is added below.

esphome:
  name: esp-am2320
  platform: ESP8266
  board: d1_mini
  on_boot:
    - wait_until: mqtt.connected
    - component.update: 'esp_am2320'
    - component.update: 'abshum_livingroom'
    - component.update: 'am2320_rssi'
    - delay: 1s
    - deep_sleep.enter:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: x
    gateway: x
    subnet: x
    dns1: x
  fast_connect: true

logger:

mqtt:
  broker: x.x.x.x
  username: ''x"
  password: 'x!'
  birth_message:
  will_message:

ota:
  password: 'xxx'

deep_sleep:
  run_duration: 30s
  sleep_duration: 5min

# Example configuration entry
i2c:

sensor:
  - platform: am2320
    setup_priority: -100
    id: 'esp_am2320'
    
    temperature:
      name: "Living Room Temperature"
      id: "temp_livingroom"
    humidity:
      name: "Living Room Humidity"
      id: "humidity_livingroom"
    update_interval: never
    
    #filters:
      #- sliding_window_moving_average:
          #window_size: 15
          #send_every: 15
          #send_first_at: 1
          
  - platform: template
    name: "Living Room Absolute Humidity"
    id: "abshum_livingroom"
    lambda: |-
      const float mw = 18.01534;    // molar mass of water g/mol
      const float r = 8.31447215;   // Universal gas constant J/mol/K
      return (6.112 * powf(2.718281828, (17.67 * id(temp_livingroom).state) /
        (id(temp_livingroom).state + 243.5)) * id(humidity_livingroom).state * mw) /
        ((273.15 + id(temp_livingroom).state) * r); // in grams/m^3
    accuracy_decimals: 2
    update_interval: never
    icon: 'mdi:water'
    unit_of_measurement: 'g/m³'
  
  - platform: wifi_signal
    name: "AM2320 WiFi Signal"
    update_interval: never
    id: "am2320_rssi" 
  

Ok, after some fiddling, I got it to work like this. Not sure if this is the best or common way though. If someone has some feedback on it, it’d be appreciated.
Also, I wonder if the “- delay: 1s” is needed before going to sleep. Or will the ESP wait for the “component.update” to finish before it goes to sleep?

esphome:
  name: esp-am2320
  platform: ESP8266
  board: d1_mini
  on_boot:
    - globals.set:
            id: t1
            value: !lambda 'return millis();'  
    - wait_until: mqtt.connected
    - component.update: 'esp_am2320'
    - component.update: 'abshum_livingroom'
    - component.update: 'am2320_rssi'
    - globals.set:
            id: t2
            value: !lambda 'return millis();'
    - component.update: 'am2320_timeawake'
    - delay: 1s
    - deep_sleep.enter:
globals:
  - id: t1
    type: int
    restore_value: no
    initial_value: "0"
  - id: t2
    type: int
    restore_value: no
    initial_value: "0"
    
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: x
    gateway: x
    subnet: x
    dns1: x
  fast_connect: true

logger:

mqtt:
  broker: x
  username: x
  password: x
  birth_message:
  will_message:

ota:
  password: 'x'

deep_sleep:
  run_duration: 30s
  sleep_duration: 5min

# Example configuration entry
i2c:

sensor:
  - platform: am2320
    setup_priority: -100
    id: 'esp_am2320'
    
    temperature:
      name: "Living Room Temperature"
      id: "temp_livingroom"
    humidity:
      name: "Living Room Humidity"
      id: "humidity_livingroom"
    update_interval: never
    
    #filters:
      #- sliding_window_moving_average:
          #window_size: 15
          #send_every: 15
          #send_first_at: 1
          
  - platform: template
    name: "Living Room Absolute Humidity"
    id: "abshum_livingroom"
    lambda: |-
      const float mw = 18.01534;    // molar mass of water g/mol
      const float r = 8.31447215;   // Universal gas constant J/mol/K
      return (6.112 * powf(2.718281828, (17.67 * id(temp_livingroom).state) /
        (id(temp_livingroom).state + 243.5)) * id(humidity_livingroom).state * mw) /
        ((273.15 + id(temp_livingroom).state) * r); // in grams/m^3
    accuracy_decimals: 2
    update_interval: never
    icon: 'mdi:water'
    unit_of_measurement: 'g/m³'
  
  - platform: wifi_signal
    name: "AM2320 WiFi Signal"
    update_interval: never
    id: "am2320_rssi" 
    
  - platform: template
    name: "AM2320 TimeAwake"
    id: "am2320_timeawake"
    lambda: |-
      return (id(t2) - id(t1));
    accuracy_decimals: 0
    update_interval: never
    icon: 'mdi:water'
    unit_of_measurement: 'ms'
  

Capture