Rotary Encoder: on valueand on direction

Here is the code for a rotary encoder used to control a thermostat:

  - platform: rotary_encoder
    id: myencoder
    min_value: 1
    max_value: 4
    pin_a:
      number: D6
      mode: INPUT_PULLUP
    pin_b:
      number: D7
      mode: INPUT_PULLUP
    # on_clockwise:
    #   - if:
    #       condition:
    #         switch.is_on: heat_adjust
    #       then:
    #         - climate.control:
    #             id: livingroom_thermostat
    #             target_temperature: !lambda "return id(livingroom_thermostat).target_temperature + (5.0/18);"
    # on_anticlockwise:
    #   - if:
    #       condition:
    #         switch.is_on: heat_adjust
    #       then:
    #         - climate.control:
    #             id: livingroom_thermostat
    #             target_temperature: !lambda "return id(livingroom_thermostat).target_temperature - (5.0/18);"
    on_value:
      - if:
          condition:
            switch.is_on: preset_adjust
          then:
            - if:
                condition:
                  - lambda: "return id(myencoder).state == 1;"
                then:
                  - climate.control:
                      id: livingroom_thermostat
                      custom_preset: Night
                else:
                  - if:
                      condition:
                        - lambda: "return id(myencoder).state == 2;"
                      then:
                        - climate.control:
                            id: livingroom_thermostat
                            custom_preset: Day
                      else:
                        - if:
                            condition:
                              - lambda: "return id(myencoder).state == 3;"
                            then:
                              - climate.control:
                                  id: livingroom_thermostat
                                  custom_preset: Away
                            else:
                              - if:
                                  condition:
                                    - lambda: "return id(myencoder).state == 4;"
                                  then:
                                    - climate.control:
                                        id: livingroom_thermostat
                                        custom_preset: Summer

The on_value section only works if the on_clockwise and on_anticlockwise sections are commented out. If those are uncommented, then they work but the on_value section does not. Can these commands be made to work together, or, if not, could anyone suggest another approach?

When you say it doesn’t “work” what do you mean? Doesn’t compile, doesn’t do what you want? Doesn’t do anything? Doesn’t log anything?

The button on the encoder turns on either heat_adjust or preset_adjust, depending on click length. Only one adjust can be on at a time, and the logs show that this is so. Turning the knob should either adjust the target temperature or the custom preset, depending on which adjust switch is on.

When the clockwise/anticlockwise section is commented out, as above, and preset_adjust is on, turning the rotary steps the custom presets up and down, setting each in turn. A log entry is made showing the updated value of the encoder and the new preset. In short, it behaves as expected.

When the clockwise section is commented in, and heat_adjust is on, turning the knob steps the target temperature up or down. But when preset_adjust is on, and heat_adjust is off, turning the knob has no effect. No log entry for the encoder or custom_preset is made. This is why I asked if on_value and on_clockwise/anticlockwise can ber used in the same encoder yaml.

Here is the button code. Thanks for your help.

binary_sensor:
  - platform: template
    id: fire
  - platform: gpio
    id: button
    pin:
      number: D4
      inverted: true
    on_click:
      - min_length: 50ms
        max_length: 200ms
        then:
          - switch.turn_off: preset_adjust
          - switch.turn_on: heat_adjust
          - switch.turn_on: display_on
          - delay: 30s
          - switch.turn_off: heat_adjust
          - switch.turn_off: display_on
      - min_length: 600ms
        max_length: 5000ms
        then:
          - switch.turn_off: heat_adjust
          - switch.turn_on: preset_adjust
          - switch.turn_on: display_on
          - delay: 30s
          - switch.turn_off: preset_adjust
          - switch.turn_off: display_on

Both sections suddenly began to function. Don’t understand why; the only change I made was in slightly changing the on_click times (longer). Well, anyway…

1 Like