ESPHome controlling a PWM Fam

Good Evening

Is anyone able to help me out, I’m trying to control a 12v DC fan using a Mosfet Driver (https://www.ebay.co.uk/itm/IRF520-MOSFET-Driver-Breakout-Board-Module-IRF520N-MOS-FET-Switch-PWM-Arduino-Pi/232518999508) Connected to a ESP8266, with a DHT211 connected to the same esp8266 controller.

What I want to archive is using esphome is turn the fan on at different speeds (High, Medium or Low) depending on the temperature on the DHT22

25.0 below (off)
25.0 to 27.0 (Low)
27.0 to 29.0 (Medium)
29 above (high)

really would appreciate some help.

esphome:
  name: pool_controller
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret WiFi_SSID
  password: !secret WiFi_Password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Pool Controller Fallback Hotspot"
    password: !secret AP_Password

captive_portal:

# Enable logging
logger:

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

ota:
  password: !secret OTA_Password
  
# These are for the manual buttons and to trigger the switches 
binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: "Manual Mode Toggle Button"
    on_press:
      then:
        - switch.toggle: relay1
        
output:
# PC Fan Controller
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 16000 Hz
    id: fridge_fans

fan:
  - platform: speed
    output: fridge_fans
    name: "Ventilation Fan's"
    id: fridge1
    speed:
      low: 0.33
      medium: 0.66
      high: 1

sensor:
# Temperature Sensor
  - platform: dht
    pin: GPIO2
    update_interval: 5s 
    temperature:
      name: "Pool Controller Temp"
      on_value_range:
        - above: 25.0
          then:
            - fan.turn_on:
                id: fridge1
                speed: low
        - above: 27
          then:
            - fan.turn_on:
                id: fridge1
                speed: medium
        - above: 29
          then:
            - fan.turn_on:
                id: fridge1
                speed: high
        - below: 25
          then:
            - fan.turn_off:
                id: fridge1
    humidity:
      name: "Pool Controller Hum"
 
# Power 
switch:
  - platform: gpio
    pin: GPIO16
    name: "Relay #1"
    id: relay1
    interlock: [relay1]

  - platform: gpio
    pin: GPIO4
    name: "Relay #2"
    id: relay2
    interlock: [relay2]

Are you positive it’s a esp01?
This is a esp01:
esp01

Good spot I will need to change that. As for the code it kind of works. As the temp increases so does the speed of the fan. But say the temp is above 30 and the fan speed is high the fan stays high until it gets below 25.0 where it switches off.

I’m a terrible coder.
That said this bit of code you have seems to be at fault:

    temperature:
      name: "Pool Controller Temp"
      on_value_range:
        - above: 25.0
          then:
            - fan.turn_on:
                id: fridge1
                speed: low
        - above: 27
          then:
            - fan.turn_on:
                id: fridge1
                speed: medium
        - above: 29
          then:
            - fan.turn_on:
                id: fridge1
                speed: high
        - below: 25
          then:
            - fan.turn_off:
                id: fridge1

this looks as if it well only change speed in a upward scale then off.

I think you well need to insert some more “Below” values to distinguish the pwm speed.

This should help:

Hope it’s of help.

Thank sfor your help so far, i’m not a coder either but I will have a go.
so it’s kind of helped but I cant have multiple options, so I come up with this but it still doesn’t work. anyone able to point me in the right direction please

fan:
  - platform: speed
    output: fridge_fans
    name: "Ventilation Fan's"
    id: fridge1

# PC Fan Controller
output:
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 16000 Hz
    id: fridge_fans
    
sensor:
# Temperature Sensor
  - platform: dht
    pin: GPIO2
    update_interval: 5s 
    temperature:
      name: "Pool Controller Temp"
      id: sensor_temp
      on_value_range:
        - below: 25.0
          then:
            - fan.turn_off:
               id: fridge1
        - above: 25.0
          then:
          - if:
              condition:
                lambda: 'return id(sensor_temp).state < 27.0;'
              then:
                - switch.turn_on: fan_low
        - above: 27.0
          then:
          - if:
              condition:
                lambda: 'return id(sensor_temp).state < 29.0;'
              then:
                - switch.turn_on: fan_med
        - above: 29.0
          then:
            - fan.turn_on:
                id: fridge1
                speed: high
    humidity:
      name: "Pool Controller Hum"
      

# Power 
switch:
  - platform: gpio
    pin: GPIO16
    name: "Relay #1"
    id: relay1
    interlock: [relay1]

  - platform: gpio
    pin: GPIO4
    name: "Relay #2"
    id: relay2
    interlock: [relay2]

  - platform: template
    id: fan_low
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: LOW
          
  - platform: template
    id: fan_med
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: MEDIUM
          
  - platform: template
    id: fan_high
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: HIGH 

Evening, So I have manage to finally work this out with a friend. Thought I would upload the code for anyone else who might have something similar.


fan:
  - platform: speed
    output: fridge_fans
    name: "Ventilation Fan's"
    id: fridge1

# PC Fan Controller
output:
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 16000 Hz
    id: fridge_fans

sensor:
# Temperature Sensor
  - platform: dht
    pin: GPIO2
    update_interval: 5s 
    temperature:
      name: "Pool Controller Temp"
      id: sensor_temp
      on_value_range:
        - below: 25.0
          then:
            - fan.turn_off: fridge1
        - above: 25.1
          below: 27.0
          then:
            - switch.turn_on: fan_low
        - above: 27.1
          below: 29.0
          then:
            - switch.turn_on: fan_med
        - above: 29.1
          then:
            - switch.turn_on: fan_hig
    humidity:
      name: "Pool Controller Hum"

# Power 
switch:
  - platform: gpio
    pin: GPIO16
    name: "Relay #1"
    id: relay1
    interlock: [relay1]

  - platform: gpio
    pin: GPIO4
    name: "Relay #2"
    id: relay2
    interlock: [relay2]


  - platform: template
    id: fan_low
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: LOW
          
  - platform: template
    id: fan_med
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: MEDIUM
        
  - platform: template
    id: fan_hig
    turn_on_action:
      - fan.turn_on:
          id: fridge1
          speed: HIGH
2 Likes

Dude, still have similar problem. Whats your second relay for? Are both phisical rellays? Im trying to run 2 fans on 2 pwms output. But to not waiste energy I have connect 12v sipplay to switch (240v). Looks like switch never tufns on…

Found this very helpful. Finally got my fans working as I wanted!! Thank you pmiles156👍