Vary Deep Sleep Time Based On Sensor Help Please

Hi, I’m using the code shown below for a solar powered light level sensor.

I would like to adjust the sleep time based on the lux level:
Under 1 Lux = sleep for 15mins
1 - 2000 Lux = sleep for 5 mins
2000+ Lux = don’t sleep

I’ve tried a number of methods shown online but had no success - any help welcome.

Thanks

esphome:

  name: lux-sensor

  includes:

    - MAX17048_component.h

esp32:

  board: firebeetle32

  framework:

    type: arduino

# Enable logging

logger:

# Enable Home Assistant API

api:

  encryption:

    key: ""

ota:

  password: ""

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails

#  ap:

#    ssid: "Lux-Sensor Fallback Hotspot"

#    password: ""

deep_sleep:

  run_duration: 4s

  sleep_duration: 4min

i2c:

  scan: true

  id: bus_a

sensor:

  - platform: bh1750

    name: "Outdoor Lux"

    address: 0x23

    update_interval: 1s

  - platform: custom

    lambda: |-

      auto max17048_sensor = new MAX17048Sensor();

      App.register_component(max17048_sensor);

      return {max17048_sensor->voltage_sensor, max17048_sensor->percentage_sensor};

    sensors:

      - name: "Voltage"

        unit_of_measurement: V

        accuracy_decimals: 2

      - name: "Percentage"

        unit_of_measurement: '%'

 

captive_portal:

Invoke the deep_sleep.enter action, that sleep_duration is templatable.