ESPHome fan controller with local rotary encoder

Hi,

I do have some issues I was unable to solve using my normal routine.
My new ESPHome project is a fan controller (PWM) which not only will be included in my Home Assistant, but will also have a local rotary encoder two change the speed. So far so good. Rotary encoder and pwm is working, but I face an issue with changing speed with the rotary encoder.

esphome:

  name: fan-controller

esp8266:

  board: d1_mini

# Enable logging

logger:

switch:

  - platform: restart

    name: Restart

binary_sensor:

# Reports if this device is Connected or not

  - platform: status

    name: Status

# globals

globals:

  - id: global_pwmfan0

    type: float

    restore_value: no

    initial_value: '0.0'

  - id: global_pwmfan1

    type: float

    restore_value: no

    initial_value: '0.0'

  - id: global_pwmfan2

    type: float

    restore_value: no

    initial_value: '0.0'

  - id: global_pwmfan3

    type: float

    restore_value: no

    initial_value: '0.0'

# fan

output:

  - platform: esp8266_pwm

    pin: D5

    frequency: 25000 Hz

    id: pwmfan0

  - platform: esp8266_pwm

    pin: D6

    frequency: 25000 Hz

    id: pwmfan1

  - platform: esp8266_pwm

    pin: D7

    frequency: 25000 Hz

    id: pwmfan2

  - platform: esp8266_pwm

    pin: D8

    frequency: 25000 Hz

    id: pwmfan3

fan:

  - platform: speed

    output: pwmfan0

    name: Fan 0

  - platform: speed

    output: pwmfan1

    name: Fan 1

  - platform: speed

    output: pwmfan2

    name: Fan 2

  - platform: speed

    output: pwmfan3

    name: Fan 3

# rotary encoder

sensor:

  - platform: rotary_encoder

    name: "Rotary Encoder"

    pin_a: D1

    pin_b: D2

    on_clockwise:

      - logger.log: "Increase fan speed"

      - lambda: |-

          if( id(global_pwmfan0) < 1 ) {

            id(global_pwmfan0) += 0.05;

            id(pwmfan0).set_level(return id(global_pwmfan0));

          }

      - logger.log:

          format: "Value %d"

          args: ['return id(global_pwmfan0)']

    on_anticlockwise:

      - logger.log: "Decrease fan speed"

      - lambda: |-

          if( id(global_pwmfan0) > 0 ) {

            id(global_pwmfan0) -= 0.05;

            id(pwmfan0).set_level(return id(global_pwmfan0));

          }

     

# Reports IP

text_sensor:

  - platform: wifi_info

    ip_address:

      name: IP

# Enable Home Assistant API

api:

  encryption:

    key: "WicZuNX02FLn5StHBFhb+vGtxAFpwtIhluPMbv8STKo="

ota:

  password: "871a922f9e1c283b3d6e1354a42eb179"

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails

  ap:

    ssid: "Fan-Controller Fallback Hotspot"

    password: "fizLVmJSOGnW"

captive_portal:

To be able to use the rotary encoder and the home assistant interface I will have to rely on a global variable as far as I know, because I cannot get the current level of the pwm. So my lambda for the level seems to be the problem. Does some have an idea whats wrong?
de-/increasing the global variable with the nob is no issue also changing speed in HA, but the global variable does not change the PWM level.

Any help is very much apprechiated.

BR,
bqzero

Hi @bqzero,

did you finish the project? Can you paste the config?

Thanks and regards

Jakob

Isn’t the level id(global_pwmfan0) ?

This is why you need to read the documentation instead of just copying and pasting someone else’s configuration.

You cant get the current pwm level?

Well, does this look like a clue?

Its configured to never restore pwm value and always start at 0. It explains this in the documentation plain as day.

The globals are really redundant anyway. You can configure those same options for the rotary_encoder but, it doesnt look like that documentation was bothered to be looked at either.

publish_initial_value (Optional, boolean): Controls whether the value is published upon start of ESPHome. By default, the value is only published when it changes, causing an “unknown” value at first. If you set this option to true, the value is published once after boot and when it changes. Defaults to false.

restore_mode (Optional): Control how the Rotary Encoder attempts to restore state on bootup.

RESTORE_DEFAULT_ZERO - (Default) Attempt to restore state and default to zero (0) if not possible to restore.

ALWAYS_ZERO - Always initialize the counter with value zero (0).