Start PWM fan on ESPHome boot

Hi guys,
I have an ESP32 controlling a fan to control humidity in a nook in my house below/behind some cupboards. Together with a DHT22 I can keep an eye on what the environment does, and -for now by “hand”, through HA- control the fan speed.
However, when ESPHome reboots, it does not start the fan by itself. So, I thought, by adding an “on_boot” I might be able to accomplish this. However, it does not work. Does anyone know what I’m doing wrong here? This is the yaml:

esphome:
  name: keukenkast
  on_boot:
    priority: 600
    then:
      - fan.turn_on: keukenkast_fan
      - output.turn_on: ventilation_fan_pwm
      - output.set_level:
          id: ventilation_fan_pwm
          level: "40%"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret ha_api_key

time:
  - platform: homeassistant
    id: homeassistant_time

ota:
  password: "15563c9726f0e93ea26369287287e534"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  domain: ".intern.vanhalderen.net"
  use_address: 192.168.0.5

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Keukenkast Fallback Hotspot"
    password: !secret esp_ap_password

text_sensor:
  - platform: version
    name: ESPHome Keukenkast ESPHome Version
  - platform: wifi_info
    ip_address:
      name: ESPHome Keukenkast IP
    ssid:
      name: ESPHome Keukenkast SSID
    bssid:
      name: ESPHome Keukenkast BSSID
 
 
sensor:
  - platform: dht
    pin: GPIO21
    temperature:
      name: "Keukenkast Temperature"
      id: keukenkast_temperature
      accuracy_decimals: 1
    humidity:
      name: "Keukenkast Humidity"
      id: keukenkast_humidity
      accuracy_decimals: 1
    update_interval: 30s
  - platform: uptime
    name: ESPHome Keukenkast Uptime
  - platform: wifi_signal
    name: ESPHome Keukenkast WiFi Signal
    update_interval: 60s


output:
  - platform: ledc
    pin: GPIO27
    id: ventilation_fan_pwm

fan:
  - platform: speed
    output: ventilation_fan_pwm
    name: "Keukenkast Ventilatie"
    id: keukenkast_fan

switch:
  - platform: restart
    name: "ESPHome Keukenkast Restart"

mqtt:
  broker: kermit.intern.vanhalderen.net
  username: !secret mqtt_username
  password: !secret mqtt_password
  client_id: esphome_keukenkast

Someone got an idea? Thanks in advance!
Patrick

Priority 600 may still be a bit early, if you read the docs things like switches aren’t initialised until priority: 800.

  • 800.0: This is where all hardware initialization of vital components is executed. For example setting switches to their initial state.

I would probably add a delay as well:

esphome:
  name: keukenkast
  on_boot:
    priority: 800
    then:
      - delay: 1s
      - fan.turn_on: keukenkast_fan
1 Like

Can’t you work with restore_mode? Offers quite a few possibilities, see here:

restore_mode (Optional): Control how the fan attempts to restore state on boot.
  NO_RESTORE - Don’t restore any state.
  RESTORE_DEFAULT_OFF - Attempt to restore state and default to OFF if not possible to restore.
  RESTORE_DEFAULT_ON - Attempt to restore state and default to ON.
  RESTORE_INVERTED_DEFAULT_OFF - Attempt to restore state inverted from the previous state and default to OFF.
  RESTORE_INVERTED_DEFAULT_ON - Attempt to restore state inverted from the previous state and default to ON.
  ALWAYS_OFF (Default) - Always initialize the fan as OFF on bootup.
  ALWAYS_ON - Always initialize the fan as ON on bootup.
1 Like

In addition to the priority, the delay did the trick! Thanks.
A small sidetone; for some reason, the “output.set_level” to 40% does not work, the fan starts at the speed of before the restart, but I can live with that. I’ll try to have a look at that a later time.

I’ll have a look at this later, although it looks like something like the restore_mode is being used with the on_boot priority modification above, as the fan spins at the level set before the reboot, not the level set in the “output.set_level” part, for some reason.