Turning on sensor power on boot priority not working properly

Hi!
I’m building battery powered sensors that should wake up, turn on power output for the sensors, take reading, publish to MQTT, turn off power to the sensor and then deep sleep.

While it does read the sensor eventually and post to MQTT, it still seems to be trying to read it too early ignoring the on_boot priority.

  on_boot: 
    priority: 800
    then:
      - output.turn_on: dht_sensor_power
      - component.update: dht_sensor
      - output.turn_off: dht_sensor_power

output:
  - platform: gpio
    pin: GPIO03
    inverted: True
    id: dht_sensor_power

sensor:
  - id: dht_sensor
    platform: dht
    model: AM2302
    pin: GPIO02
    update_interval: 1s
    temperature:
      name: "Temperature"
      id: ${name}_temperature
    humidity:
      name: "Humidity"
      id: ${name}_humidity

In logs I get:

[14:00:06][I][app:029]: Running through setup()...
[14:00:06][I][wifi:313]: WiFi Connecting to 'XXX'...
[14:00:06][W][dht:174]: Requesting data from DHT failed!
[14:00:06][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[14:00:06][W][component:157]: Component dht.sensor set Warning flag: unspecified
[14:00:06][W][component:157]: Component wifi set Warning flag: associating to network
[14:00:07][W][component:170]: Component dht.sensor cleared Warning flag
[14:00:08][I][wifi:617]: WiFi Connected!
[14:00:08][W][component:157]: Component mqtt set Warning flag: unspecified
[14:00:08][I][mqtt:250]: Connecting to MQTT...
[14:00:08][W][component:170]: Component wifi cleared Warning flag
[14:00:08][W][component:170]: Component mqtt cleared Warning flag
[14:00:08][I][mqtt:291]: MQTT Connected!
[14:00:08][I][app:062]: setup() finished successfully!
[14:00:08][I][app:100]: ESPHome version 2024.11.3 compiled on Dec 10 2024, 13:57:34
[14:00:08][I][app:102]: Project esphome.web version dev
[14:00:08][I][deep_sleep:060]: Beginning Deep Sleep
[14:00:08][I][deep_sleep:062]: Sleeping for 9000000us
[14:00:08][W][wifi_esp8266:513]: Event: Disconnected ssid...

First thing first: Is your DHT working alone, w/o MQTT, deep sleep, power, … ?

IMHO try to use DEBUG logging to see more details, especially about dht_sensor_power.

Sensor is wired correctly and works fine. I forgot to add that to the OP.
I do get readings out of it and it gets posted to MQTT, just not as it’s supposed to.

Keep in mind you can have multiple on_boot

According to ESPHome doc:

    800.0: This is where all hardware initialization of vital components is executed. For example setting switches to their initial state.
    600.0: This is where most sensors are set up.
    250.0: At this priority, WiFi is initialized.
    200.0: Network connections like MQTT/native API are set up at this priority.
    -100.0: At this priority, pretty much everything should already be initialized.

So it seems 800 is pretty late to power the DHT.
Try maybe to power it at 300, update it at 800 (or -100) and power it down at on_shutdown?

You seem to have skimmed the manual: A higher value means a high priority and thus also your code being executed earlier.

It seems so indeed.
Point remains that the powering up and forced reasing at the same time is a likely cause for your issue.