Monochromatic light and rotary encoder

I am making a dimmer with ESPHome.
An ESP12e with rotary encoder.
I can dim the LED with the rotary encoder, I can dim with the slider in the menu.
The only problem is that the slider does not move when I turn the rotary encoder.
I once made this with push buttons and the slider does move with it.
How can I fix this?
This is my code:

globals:
   - id: x
     type: int
     restore_value: no
     initial_value: '0'

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D1
    pin_b: D2
    min_value: 0
    max_value: 52
    resolution: 4
    filters:
      - or:
        - debounce: 0.1s
        - delta: 10
      - lambda: |-
          if (x < 0.0) return 0.0;
          if (x > 52.0) return 52.0;
          return x;
    on_value:
      then:
        - output.set_level:
            id: pwm_output
            level: !lambda "return x/53.0;"
            
        - logger.log:
            format: "pwm_output set to %.1f%s"
            args: [ 'x*100/53.0', '"x"' ]
          
    
output:
  - platform: esp8266_pwm
    pin: D6
    inverted: true
    frequency: 1000 Hz
    id: pwm_output
    
light:
  - platform: monochromatic
    output: pwm_output
    name: "LED-Lamp"
2 Likes

which slider are you referring to?

A couple of things in your config, which don’t answer your question, but you should probably fix anyway:

  • You should remove your global x as this is defined automatically by ESPHome for each sensor.
  • Your first lambda does nothing so you should remove it completely. This is because your rotary encoder min and max values already limit the output values between 0 and 52. You don’t need to “return x;” either.
  • I don’t think you’ll ever see 52 because you’ve used “delta 10”. You’ll only see 0, 10, 20, 30, 40 and 50.

I want to use the slider that automatically has used with a monochromatic light.

  • You should remove your global x as this is defined automatically by ESPHome for each sensor.

If I make an id for example id(global_dim) it doesn’t work.

35 - id: global_dim
56 level: !lambda "return id(global_dim)/52;"

  • Your first lambda does nothing so you should remove it completely. This is because your rotary encoder min and max values already limit the output values between 0 and 52. You don’t need to “return x;” either.

I have remove the first lambda (didn’t nothing) , if I remove the . "return x/52;" then did’t work dimming the led with the rotary.
I don’t know what else I must place in “level”

  • I don’t think you’ll ever see 52 because you’ve used “delta 10”. You’ll only see 0, 10, 20, 30, 40 and 50.

I removed delta: 10

code is now : (and it does the same as before)

globals:
   - id: x
     type: int
     restore_value: no
     initial_value: '0'

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D1
    pin_b: D2
    min_value: 0
    max_value: 52
    resolution: 4
    filters:
      - or:
        - debounce: 0.1s
     
    on_value:
      then:
        - output.set_level:
            id: pwm_output
            level: !lambda "return x/52;"
    
output:
  - platform: esp8266_pwm
    pin: D6
    inverted: true
    frequency: 1000 Hz
    id: pwm_output
    
light:
  - platform: monochromatic
    output: pwm_output
    name: "LED-Lamp"

I think I must the output from the rotary encoder place somewhere in the output level but I don’t know how.

Have you tried changing the brightness of the light instead of changing the output level ?

As @patfelst says, you can delete the whole globals section and leave the “return x/52;” in your lambda as is

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D1
    pin_b: D2
    min_value: 0
    max_value: 52
    resolution: 4
    filters:
      - debounce: 0.1s
    on_value:
      then:
      - light.turn_on:
          id: light_1
          brightness: !lambda "return x/52;"

output:
  - platform: esp8266_pwm
    pin: D6
    inverted: true
    frequency: 1000 Hz
    id: pwm_output
    
light:
  - platform: monochromatic
    output: pwm_output
    name: "LED-Lamp"
    id: light_1

Thanks that’s the solution.
Now I can finetune the code and toggle on/off.
Do you know how it’s possible that I can’t change " x " in id(global_dim)

from the docs:

lambda Filter

filters: - lambda: return x * (9.0/5.0) + 32.0;

lambda : Perform a simple mathematical operation over the sensor values. The input value is x and the result of the lambda is used as the output (use return ).