EspHome Controlling fan using potenziometer and automations or lambdas

Hi to all sirs,
my name is Emilio and I am new to HASS and EspHome.
I am trying to control a fan using a potentiometer as input to ESP32. I am making test to undestund…

  • I declared a LEDC output component to drive with a PWM signal a MOSFET that feed a DC fan.
    I can now control the fan inside Home assistant moving the fan control slider.
  • I have also connected a potentiometer to one analog input of the ESP32 and i can read the voltage.
  • Now my next step is to take the values caming from the potentiometer (0-3.3 Volts) multiply these values for 30.3 in order to have a 0-100 range and feed the ledc output in order to control the fan turning manually the potentiometer.
    I understood it can be done using automation or lambda, but i cannot undestund how…
  • the automation is to be done inside the analog component (that controls the output) or inside the output that receive the values from the analog component. ???..
    Can someoe give me a very simple example abot this ?
    Thank you so much,
    Emilio.

My actual code (not working…)

esphome:
  name: tester

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "tester Fallback Hotspot"
    password: "xxxxxxxxxx"

captive_portal:
    
# =======================================
# Web server
# =======================================
web_server:
  port: 80

# =======================================
# Status led
# =======================================
status_led:
  pin:
    number: GPIO13
    inverted: yes

# =======================================
# One Wire devices
# =======================================
dallas:
  - pin: GPIO27
    update_interval: 1s

# =======================================
# Sensori
# =======================================
sensor:
  - platform: dallas
    address: 0x02051691e093ff28
    name: Temp1
    id: temp1
  
  - platform: dallas
    address: 0xb405168640fbff28
    name: Temp2
    id: temp2
  
  - platform: dht
    pin: GPIO26
    temperature:
      name: "DHT11-T"
    humidity:
      name: "DHT11-RH"
    update_interval: 1s

  # =======================================
  # Analog IN
  # =======================================
  - platform: adc
    pin: GPIO34
    attenuation: auto
    name: "Pot"
    update_interval: 1s
    id: pot
    on_value_range:
      - above: 0.0
        then:
        - output.set_level: 
            id: pwm_peltier
            level: !lambda |- 
              return(id(pot).value;)

# =======================================
# PWM Output
# =======================================
output:
  - platform: ledc
    pin: GPIO33
    frequency: 4882
    id: pwm_peltier
    inverted: true
  
  - platform: ledc
    pin: GPIO32
    frequency: 4882
    id: pwm_fan_hot
    inverted: true
    
  - platform: ledc
    pin: GPIO25
    frequency: 4882
    id: pwm_fan_cold
    inverted: true

# =======================================
# FAN
# =======================================
fan:
  - platform: speed
    output: pwm_peltier
    name: "Peltier"
    id: peltier

  - platform: speed
    output: pwm_fan_hot
    name: "Fan Hot"
    id: fan_hot
  
  - platform: speed
    output: pwm_fan_cold
    name: "Fan Cold"
    id: fan_cold
        - output.set_level: 
            id: pwm_peltier
            level: !lambda |- 
              return(id(pot).value * 33 | float(0);) # or * 100 - see below

Depending on your platform your ADC may return the voltage (between or-3.3) or a value between 0 and 1.

If you haven’t confirmed what yours does then read the note in the ADC sensor for more info

Actually - set level in lambda can accept a value in the range 0-1 as a float - so something more like:

    on_value_range:
      - above: 0.0
        then:
          id(pwm_peltier).set_level(id(pot).value);

Also you are only setting the speed on a value above zero, does this mean you never want it to stop?

Hi to all and thank you for your answers,
I tried the two modifications but i had no good results.
I also commented the lines about the fan settings in order to not interfere with the control by potentiometer, but again the output is not modified by the potentiometer.
The values the potentiometer reports are float number from 0.00 to 3.15 Volt.

I tried this code but the output stays off:

esphome:
  name: sun-water

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "sun-water Fallback Hotspot"
    password: "xxxxxxxx"

captive_portal:
    
# =======================================
# Web server
# =======================================
web_server:
  port: 80

# =======================================
# Status led
# =======================================
status_led:
  pin:
    number: GPIO13
    inverted: yes

# =======================================
# One Wire devices
# =======================================
dallas:
  - pin: GPIO27
    update_interval: 1s

# =======================================
# Sensori
# =======================================
sensor:
  - platform: dallas
    address: 0x02051691e093ff28
    name: Temp1
    id: temp1
  
  - platform: dallas
    address: 0xb405168640fbff28
    name: Temp2
    id: temp2
  
  - platform: dht
    pin: GPIO26
    temperature:
      name: "DHT11-T"
    humidity:
      name: "DHT11-RH"
    update_interval: 1s

  # =======================================
  # Analog IN
  # =======================================
  - platform: adc
    pin: GPIO34
    attenuation: auto
    name: "Pot"
    update_interval: 1s
    id: pot
    on_value_range:
      - above: 0.0
        then:
        - output.set_level: 
            id: pwm_peltier
            level: !lambda |- 
              return(id(pot).state * 0.317);

# =======================================
# PWM Output
# =======================================
output:
  - platform: ledc
    pin: GPIO33
    frequency: 4882
    id: pwm_peltier
    inverted: true
  
  - platform: ledc
    pin: GPIO32
    frequency: 4882
    id: pwm_fan_hot
    inverted: true
    
  - platform: ledc
    pin: GPIO25
    frequency: 4882
    id: pwm_fan_cold
    inverted: true

# =======================================
# FAN
# =======================================
fan:
  # - platform: speed
  #   output: pwm_peltier
  #   name: "Peltier"
  #   id: peltier

  - platform: speed
    output: pwm_fan_hot
    name: "Fan Hot"
    id: fan_hot
  
  - platform: speed
    output: pwm_fan_cold
    name: "Fan Cold"
    id: fan_cold

I have to use “state” instead of “value” for ADC readings