Rotary encoder to control low and high temperature target in HA

Hi everyone, i am hoping if someone could help with the esphome code.

  1. I would like to switch between pages when i press rotary encoder button. with the current code, it is not switching between pages on my ST7735 display.

  2. i have climate control entity imported in esphome. I would like to adjust HA climate control low and high temperature target when i turn the dial in either direction. with the code below, i can only see value changes in the display but not in HA climate entity.

any help greatly appreciated as i am planning to build standalone esp device to control my central heating low and high temperature as needed. thanks

 
binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO14
      inverted: true
    on_click:
      - display.page.show_next: oled1
      - display.page.show_previous: oled1

sensor:
  - platform: homeassistant
    id: downstair_temp_high
    entity_id: climate.boiler_downstair
    attribute: target_temp_high

  - platform: homeassistant
    id: downstair_temp_low
    entity_id: climate.boiler_downstair
    attribute: target_temp_low

  - platform: rotary_encoder
    id: encoder
    pin_a:
      number: GPIO13
      mode: INPUT_PULLUP
    pin_b:
      number: GPIO12
      mode: INPUT_PULLUP
    on_clockwise:
      then:
      - logger.log: "Turned clockwise" 
      - lambda: |-
          id(downstair_temp_high).state + 0.5;

    on_anticlockwise:
      then:
      - logger.log: "Turned Anticlockwise" 
      - lambda: |-
          id(downstair_temp_high).state -= 0.5;        
   
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: st7735
    id: oled1
    model: "INITR_BLACKTAB"
    reset_pin: GPIO04
    cs_pin: GPIO15
    dc_pin: GPIO02
    rotation: 0
    device_width: 128
    device_height: 160
    col_start: 0
    row_start: 0
    eight_bit_color: true
    update_interval: 1.0s
    pages:
      - id: page1
        lambda: |-
          it.print(10, 10, id(my_font), "HELLO from page 1!");
          
      - id: page2
        lambda: |-
          it.print(10, 10, id(my_font), "HELLO from page 2!");

For the button you are showing the next page and then immediately returning to previous page so it appears the page is not changing. The .show_previous can be removed, .show_next will wrap around to toggle between the 2 pages.

For the temp limits, use .publish_state() to output the new value. Your lambda logic gets the current state and adds/subtracts 0.5 okay. Basically that value then needs passed inside the .publish_state() function.

id(downstair_temp_high).publish_state(id(downstair_temp_high).state + 0.5);

id(downstair_temp_high).publish_state(id(downstair_temp_high).state - 0.5);

Thank you for taking time and replying to it.
The screen is now switching between pages. thank you for the advice
However, climate target temp high and low are not changing in HA. I can see on the display it is changing correctly. Issue was that i like to change it on the HA climate entity when i press button, reduce target low temperature when rotary encoder is turned anticlockwise and increase target temperature when move clockwise in HA. will it be something like
homeassistant service instead of using publish.state option? I i tried code shared in last reply and - homeassistant.service & service: climate.set_temperature but unable to make it work.

my updated codes are as follows,

substitutions:
  device_name: dsmrreader
  friendly_name: DSMR
  entity_downstair: climate.boiler_downstair    ### Change for your 'Heating' Entity
  entity_upstair: climate.boiler_upstair    ### Change for your 'Cooling' Entity
 
esphome:
  name: '${device_name}'
  comment: '${device_description}'

esp32:
  board: esp32dev


binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO14
      inverted: true
    on_click:
      - display.page.show_next: oled1
#      - display.page.show_previous: oled1

  - platform: template
    id: fire
  - platform: template
    id: cool

sensor:
  - platform: homeassistant
    id: upstair_temp_high
    entity_id: $entity_upstair
    attribute: target_temp_high

  - platform: homeassistant
    id: upstair_temp_low
    entity_id: $entity_upstair
    attribute: target_temp_low

  - platform: rotary_encoder
    id: encoder
    pin_a:
      number: GPIO13
      mode: INPUT_PULLUP
    pin_b:
      number: GPIO12
      mode: INPUT_PULLUP
    on_clockwise:
      then:
      - logger.log: "Turned clockwise" 
#      - lambda: |-
#          id(upstair_temp_high).state + 0.5;
      - lambda: |-
          id(upstair_temp_high).publish_state(id(upstair_temp_high).state + 0.5);

    on_anticlockwise:
      then:
      - logger.log: "Turned Anticlockwise" 
      - lambda: |-
          id(upstair_temp_high).publish_state(id(upstair_temp_high).state - 0.5);      
   
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: st7735
    id: oled1
    model: "INITR_BLACKTAB"
    reset_pin: GPIO04
    cs_pin: GPIO15
    dc_pin: GPIO02
    rotation: 0
    device_width: 128
    device_height: 160
    col_start: 0
    row_start: 0
    eight_bit_color: true
    update_interval: 1.0s
    pages:
      - id: page1
        lambda: |-
          it.print(10, 10, id(my_font), "HELLO!");

          it.printf(0, 64, id(font3), TextAlign::BASELINE_LEFT, "L:%.1f°", id(upstair_temp_low).state);
          it.printf(128, 64, id(font3), TextAlign::BASELINE_RIGHT, "H:%.1f°", id(upstair_temp_high).state);
          
                
      - id: page2
        lambda: |-
          it.print(10, 10, id(my_font), "HELLO!");

animation:
  - file: "_icons/thermostat/fan.gif"
    id: ani_fan
  - file: "_icons/thermostat/fire.gif"
    id: ani_fire

image:
  - file: "_icons/thermostat/home-thermometer.png"
    id: home_thermometer
  - file: "_icons/thermostat/arrow-left-circle-outline.png"
    id: arrow_left
  - file: "_icons/thermostat/arrow-right-circle-outline.png"
    id: arrow_right
  - file: "_icons/thermostat/lightbulb-on-outline.png"
    id: bulb

font:
  - file: "fonts/Comic Sans MS.ttf"
    id: my_font
    size: 20
  - file: "_fonts/nasalization.ttf"
    id: font1
    size: 12
  - file: "_fonts/refsan.ttf"
    id: font2
    size: 42
  - file: "_fonts/refsan.ttf"
    id: font3
    size: 13
  - file: "_fonts/refsan.ttf"
    id: font4
    size: 10

You have sensors with the upstair and downstairs temperature, they are read only.
You need to send the command to the climate entity.
I can see how you think but you need to use a home assistant service call from ESP-Home or you expose the turning of the ESP to HA and create an automation in HA.

Service calls here:

I’m not sure which I would choose. Doing the service calls from EAP-Home would be very slick but it will probably be harder since you want to toggle between two climate entities.

Sorry, the publish_state() isn’t going to work with the climate entity.

Using the sensor homeassistant platform seems like a good way to keep track of the current settings in HA. The climate service calls could then be setup something like this to add/subtract from current state in a short lambda block.

on_clockwise:
  then:
  - logger.log: "Turned clockwise" 
  - homeassistant.service:
      service: climate.set_temperature
      data:
        target_temp_high: !lambda |-
          return id(downstair_temp_high).state + 0.5;
      target:
        entity_id: climate.$entity_upstair   

thanks for taking time. unfortunately, it is not working and giving an error.

[target] is an invalid option for [homeassistant.service].

when i remove target before entity_id, I am getting following error and values are not updating in HA.

All i can see is the following log.

[23:34:14][D][main:632]: Turned clockwise
[23:34:15][D][main:632]: Turned clockwise
[23:34:16][D][main:632]: Turned clockwise
[23:34:16][D][main:632]: Turned clockwise
[23:34:17][D][main:140]: Turned Anticlockwise
[23:34:17][D][main:140]: Turned Anticlockwise
[23:34:17][D][main:140]: Turned Anticlockwise
[23:34:17][D][main:140]: Turned Anticlockwise
[23:34:18][D][main:140]: Turned Anticlockwise
[23:34:19][D][main:632]: Turned clockwise
[23:34:19][D][main:632]: Turned clockwise
[23:34:19][D][main:632]: Turned clockwise
[23:34:19][D][main:632]: Turned clockwise

target: was cut and paste from HA developer tools. Putting the entity_id in the data worked okay in my tests for calls using HA. For the climate generic_thermostat, I didn’t see any attributes for target_temp_high or target_temp_low, just temperature for the target temperature. Try the clockwise / anticlockwise event with just static data instead of the lambda to see if that shows up in HA.

hvac_modes:
  - heat
  - 'off'
min_temp: 45
max_temp: 95
target_temp_step: 1
current_temperature: 68
temperature: 85
hvac_action: 'off'
friendly_name: Study
supported_features: 1
service: climate.set_temperature
data:
  target_temp_high: 100
  target_temp_low: 50
  temperature: 85
  entity_id: climate.study

Just try:

on_clockwise:
  then:
  - logger.log: "Turned clockwise" 
  - homeassistant.service:
      service: climate.set_temperature
      data:
        target_temp_high: !lambda |-
          return id(downstair_temp_high).state + 0.5;
        entity_id: climate.$entity_upstair