Unstable reading from rotary encoder

Hi everyone,
I’m trying to configure a rotary encoder as a dimmer for PWM LEDs. When testing on a protoboard everything was just fine, but when moving to the actual LEDs and encoders in a room, it fails.
In the “real world” condition, when I turn the encoder, the clockwise/counterclockwise logs seem random, no matter how I turn the encoder pin. I did test the same encoders with the protoboard setting and they work fine.
I’m guessing this might be an issue with the wiring (noise). I used about 7 m of unshielded CAT6 cable to connect the encoders in the room to the ESP32 in the attic. Switches using the same cable/controllers work fine; line integrity is OK, measured using a cable tester.

As I’m using on_clockwise / on_anticlockwise to dim the lights, using a filter does not affect the result, so debounce is useless.

Here is the code I use for this:

sensor:
  - platform: rotary_encoder
    name: "dim_Lucas"
    pin_a:
      number: 33
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: 32
      inverted: true
      mode:
        input: true
        pullup: true
    id: dim_lucas
    on_clockwise:
      - logger.log: "Turned Clockwise"
      - light.dim_relative:
          id: RGBLucas
          relative_brightness: 5% #increase brightness by 5%
      - light.dim_relative:
          id: white_lucas
          relative_brightness: 5% #increase brightness by 5%
    on_anticlockwise:
      - logger.log: "Turned Anticlockwise"
      - light.dim_relative:
          id: RGBLucas
          relative_brightness: -5% #decrease brightness by 5%
      - light.dim_relative:
          id: white_lucas
          relative_brightness: -5% #decrease brightness by 5%

Running a shielded cable would be expensive and quite laborious… The cable goes through conduits in a brick wall.
Any ideas?

It’s either noise or the distance or both really. How are you placing components in your configuration? For example, is the esp and encoder together and then your trying to control a light 7m away? Are the encoders and esp 7m away from each other? You should be able to send the encoder signals further if you step the voltage up but the pwm signals for the mosfet are a different story, those need to be fairly close or you need to likewise use a higher voltage which would require another mosfet

I started to do something similar but got distracted with my outside lighting. This is how i was doing the same thing. I found it better than using the dim_relative function and it allows you to send 0-255 values from elsewhere to adjust the lights. I prefer to have a way to control things in the event HA goes down or the hardware HA is on goes down. This way you could send number values via REST or MQTT. If you use multiple boards, it’s 100% doable but it will not be the same way.

  - platform: template
    name: ledchange
    id: my_number
    min_value: 0.0
    max_value: 100.0
    step: 5.0
    optimistic: true
    initial_value: 0.0        


sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder Dimmer"
    id: ledencoder
    min_value: 1
    max_value: 255
    resolution: 4
    

    pin_a: 
      number: D2
      inverted: true
      mode:
        input: true
        pullup: true     
        
    pin_b: 
      number: D7
      inverted: true
      
      mode:
        input: true
        pullup: true     
     
    filters:
      - or:
        - debounce: 0.1s
        - delta: 8        
      - lambda: |-
          if (x < 0.0) return 0.0;
          if (x > 255.0) return 255.0;
          return x;
    on_value:
      then:
        - light.turn_on:
            id: greenLED
            brightness: !lambda |-
              return x / 255;
        - logger.log:
            format: "pwm_output set to %.1f%s"
            args: [ 'x*100/256.0', '"%"' ]     

output:
  - platform: esp8266_pwm
    pin: D5
    id: dim_green   

light:
  - platform: monochromatic
    output: dim_green
    name: "Green LED"
    id: greenLED

switch:
  - platform: gpio
    id: encoder_push
    name: 'Rotary Push'
    pin: 
      number: D6
      mode:
        input: true      
        pullup: true  
      inverted: true      
 
    on_turn_on:
      if:
        condition:
           light.is_off: greenLED
        then:    
          - light.turn_on: 
              id: greenLED    
              brightness: 80%
        else:
           light.turn_off: greenLED    

I use these 4 channel mosfets or the single ones for a lot of stuff, they work great fyi. I finally finished my up/down landscape lighting.