ESP8266 PWM Output - Initial state 100%?

It’s a PWM computer fan speed controller. 12VDC Noctua fans with 5VDC PWM speed control. YAML code is providing five levels of fan:

  • OFF
  • LOW
  • MEDIUM
  • HIGH
  • MAXIMUM

Pretty much everything is working as expected, EXCEPT:
When the fan initially powers on at LOW speed, PWM is providing 100% of the speed_counter: 1000 value.

YAML code in ESPHome:

esphome:
  name: computer-rack-fans

esp8266:
  board: esp12e

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  password: !secret api_password

ota:
  password: !secret ota_password

wifi:
  ssid:     !secret wifi_ssid
  password: !secret wifi_password

switch:
  - platform: gpio
    id:       fan_power
    name:     "Fan Power"
    pin:      GPIO5
    inverted: false
    
  - platform: template
    id:       fan_speed_off
    turn_on_action:
      - logger.log:
          format: "The temperature sensor reports value %.1f, fans are OFF"
          args: [ 'id(rack_temp_f).state' ]
          level: INFO
      - lambda: |-
         id(rack_fan_speed).publish_state("Off");
      - switch.turn_off:
          id: fan_power
      - fan.turn_off:
          id: fan_pwm

  - platform: template
    id:       fan_speed_low
    turn_on_action:
      - logger.log:
          format: "The temperature sensor reports value %.1f, fans are LOW"
          args: [ 'id(rack_temp_f).state' ]
          level: INFO
      - lambda: |-
         id(rack_fan_speed).publish_state("Low");
      - fan.turn_on:
          id:    fan_pwm
          speed: 750
      - switch.turn_on:
          id:    fan_power

  - platform: template
    id:       fan_speed_medium
    turn_on_action:
      - logger.log:
          format: "The temperature sensor reports value %.1f, fans are MEDIUM"
          args: [ 'id(rack_temp_f).state' ]
          level: INFO
      - lambda: |-
         id(rack_fan_speed).publish_state("Medium");
      - fan.turn_on:
          id:    fan_pwm
          speed: 500
      - switch.turn_on:
          id:    fan_power

  - platform: template
    id:       fan_speed_high
    turn_on_action:
      - logger.log:
          format: "The temperature sensor reports value %.1f, fans are HIGH"
          args: [ 'id(rack_temp_f).state' ]
          level: INFO
      - lambda: |-
         id(rack_fan_speed).publish_state("High");
      - fan.turn_on:
          id:    fan_pwm
          speed: 250
      - switch.turn_on:
          id:    fan_power

  - platform: template
    id: fan_speed_max
    turn_on_action:
      - logger.log:
          format: "The temperature sensor reports value %.1f, fans are MAXIMUM"
          args: [ 'id(rack_temp_f).state' ]
          level: INFO
      - lambda: |-
         id(rack_fan_speed).publish_state("MAXIMUM");
      - fan.turn_on:
          id: fan_pwm
          speed: 1
      - switch.turn_on:
          id: fan_power

sensor:
  platform: dht
  model: dht22
  update_interval: 10sec
  pin:  GPIO14
  temperature:
    id: rack_temp_f
    unit_of_measurement: "°F"
    filters:
      lambda: |-
        return (x * 1.8) + 32.0;

    on_value_range:
      - below: 75.0
        then:
          switch.turn_on: fan_speed_off
          
      - above: 75.1
        below: 77.0
        then:
          switch.turn_on: fan_speed_low
          
      - above: 77.1
        below: 79.0
        then:
          switch.turn_on: fan_speed_medium
          
      - above: 79.1
        below: 80.0
        then:
          switch.turn_on: fan_speed_high
          
      - above: 80.1
        then:
          switch.turn_on: fan_speed_max

#
# Text sensor provides literal speed
# Off / Low / Medium / High / MAXIMUM
#
text_sensor:
  platform: template
  id:       rack_fan_speed
  name:     "Rack Fan Speed"
         
#
# fan_power  is a relay switching 12VDC fan power on and off
# pwm_output is a speed control line to the PWM fan
#
output:
  - platform:  esp8266_pwm
    pin:       GPIO12
    frequency: 25000 Hz
    id:        pwm_output

#
# The fan object
#
fan:
  - platform:    speed
    id:          fan_pwm
    name:        "Rack Fan PWM"
    output:      pwm_output
    speed_count: 1000
    restore_mode: ALWAYS_OFF

The ESPHome log output when it initially switches on to LOW speed:

[08:35:17][D][sensor:124]: 'rack_temp_f': Sending state 73.94000 °F with 1 decimals of accuracy
[08:35:27][D][dht:048]: Got Temperature=23.8°C Humidity=50.3%
[08:35:27][D][sensor:124]: 'rack_temp_f': Sending state 74.84000 °F with 1 decimals of accuracy
[08:35:37][D][dht:048]: Got Temperature=24.4°C Humidity=49.6%
[08:35:37][D][sensor:124]: 'rack_temp_f': Sending state 75.92000 °F with 1 decimals of accuracy
[08:35:37][D][switch:013]: 'fan_speed_low' Turning ON.
[08:35:37][I][main:046]: The temperature sensor reports value 75.9, fans are LOW
[08:35:37][D][text_sensor:067]: 'Rack Fan Speed': Sending state 'Low'
[08:35:37][D][fan:022]: 'Rack Fan PWM' - Setting:
[08:35:37][D][fan:025]:   State: ON
[08:35:37][D][fan:029]:   Speed: 1000
[08:35:37][D][fan:108]: 'Rack Fan PWM' - Sending state:
[08:35:37][D][fan:109]:   State: ON
[08:35:37][D][fan:111]:   Speed: 1000
[08:35:37][D][switch:013]: 'Fan Power' Turning ON.
[08:35:37][D][switch:037]: 'Fan Power': Sending state ON
[08:35:42][D][text_sensor:067]: 'Rack Fan Speed': Sending state 'Low'

After going through other speeds/states, the LOW setting works as expected:

[09:10:42][D][text_sensor:067]: 'Rack Fan Speed': Sending state 'Medium'
[09:10:47][D][dht:048]: Got Temperature=25.3°C Humidity=39.7%
[09:10:47][D][sensor:124]: 'rack_temp_f': Sending state 77.54000 °F with 1 decimals of accuracy
[09:10:57][D][dht:048]: Got Temperature=25.2°C Humidity=39.7%
[09:10:57][D][sensor:124]: 'rack_temp_f': Sending state 77.36000 °F with 1 decimals of accuracy
[09:11:07][D][dht:048]: Got Temperature=25.2°C Humidity=39.8%
[09:11:07][D][sensor:124]: 'rack_temp_f': Sending state 77.36000 °F with 1 decimals of accuracy
[09:11:17][D][dht:048]: Got Temperature=25.1°C Humidity=39.8%
[09:11:17][D][sensor:124]: 'rack_temp_f': Sending state 77.18000 °F with 1 decimals of accuracy
[09:11:27][D][dht:048]: Got Temperature=25.0°C Humidity=39.9%
[09:11:27][D][sensor:124]: 'rack_temp_f': Sending state 77.00000 °F with 1 decimals of accuracy
[09:11:27][D][switch:013]: 'fan_speed_low' Turning ON.
[09:11:27][I][main:046]: The temperature sensor reports value 77.0, fans are LOW
[09:11:27][D][text_sensor:067]: 'Rack Fan Speed': Sending state 'Low'
[09:11:27][D][fan:022]: 'Rack Fan PWM' - Setting:
[09:11:27][D][fan:025]:   State: ON
[09:11:27][D][fan:029]:   Speed: 750
[09:11:27][D][fan:108]: 'Rack Fan PWM' - Sending state:
[09:11:27][D][fan:109]:   State: ON
[09:11:27][D][fan:111]:   Speed: 750
[09:11:27][D][switch:013]: 'Fan Power' Turning ON.
[09:11:37][D][dht:048]: Got Temperature=25.0°C Humidity=39.9%
[09:11:37][D][sensor:124]: 'rack_temp_f': Sending state 77.00000 °F with 1 decimals of accuracy
[09:11:42][D][text_sensor:067]: 'Rack Fan Speed': Sending state 'Low'