How to listen to touch excluding one area?

I have a touchscreen (CYD) with a virtual button:

binary_sensor:
  - platform: touchscreen
    name: Night Mode
    id: night_mode
    x_min: 260
    x_max: 310
    y_min: 180
    y_max: 230
    page_id: main_page
    filters:
      - delayed_on_off: 10s
    on_release:
      then:
        - if:
            condition:
              - lambda: |-
                  return id(recent_touch);
            then:
              - switch.toggle: day_night
              - delay: 300s
              - display.page.show: time_page

I also have a on_touch event like this:

touchscreen:
    platform: xpt2046
    spi_id: touch
    cs_pin: GPIO33
    interrupt_pin: GPIO36
    update_interval: 50ms
    threshold: 400
    calibration:
      x_min: 180
      x_max: 3800
      y_min: 240
      y_max: 3860
    transform:
      mirror_y: false
      mirror_x: false
      swap_xy: true
  # When the display is touched, turn on the backlight,
  # store that we had a recent touch, and update the UI
    on_touch:
        then:
          - if:
              condition:
                and:
                  - lambda: |-
                      return id(recent_touch);
                  - binary_sensor.is_off: night_mode
              then:
                - display.page.show_next: esp_display
          - if:
              condition:
                display.is_displaying_page: main_page
              then:
                light.turn_on:
                  id: backlight
                  brightness: !lambda |- 
                    if( isnan(id(illuminance).state) ){
                      return 0.2;
                    } else{
                      return (max((id(illuminance).state)*0.02, 0.2));
                    }
          - lambda: |-
              id(recent_touch) = true;

However anytime I touch the binary sensor area the touchscreen on_touch is also activated.
I tried to prevent this with - display.page.show_next: esp_display but that doesn’t help.

How can exclude the touchbutton area from the on_touch event?

You can test for the coordinates of the touch in the on_touch: code block - then ignore if it’s within the binary sensor.

Found it myself:

on_touch:
        then:
          - if:
              condition:
                lambda: |-
                  return (
                    id(recent_touch) && 
                    (
                      touch.y < 160 ||
                      touch.x < 200 
                    )
                  );
              then:
                - display.page.show_next: esp_display
1 Like