ESP32 touch sensor display the value

I’d like to make use of the actual touch sensor value rather than configure it as binary on or off.
The config only supports “binary_sensor:” for - platform: esp32_touch, trying as simply “sensor:” it will not compile.
That in itself doesn’t matter too much but I don’t know how to access the reading as displayed in the logs.
For example the logs show

[16:21:45][D][esp32_touch:122]: Touch Pad 'GPIO27' (T7): 1458

and it is the end of this string, the value “1458” that I’d like to display.
At the moment my display shows 0.00000
This is my code, any help appreciated on how to format the last line to display the proper value:

esphome:
  name: status_display
  platform: ESP32
  board: heltec_wifi_lora_32

wifi:
  ssid: 
  password: 

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

esp32_touch:
  setup_mode: True

binary_sensor:
  - platform: esp32_touch
    name: "GPIO27"
    id: GPIO27
    pin: GPIO27
    threshold: 1000

font:
  - file: "Arial.ttf"
    id: arial
    size: 15
  - file: 'Frutiger.ttf'
    id: frut
    size: 20

i2c:
  sda: GPIO4
  scl: GPIO15
  scan: false
  frequency: 700kHz
  
display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    update_interval: 5s
    reset_pin: GPIO16
    address: 0x3c
    lambda: |-
      it.print(2, 5, id(arial), "Sensor reading is");
      it.printf(2, 35, id(frut), "%f",id(GPIO27).state);     

It is a binary sensor except during setup. Are you looking for a pressure sensor?

I’d like to use the capacitive values for a non-contact fluid level sensor. The values are present in the log file yet I can’t find a way to give them an id and make use of them in esphome, its just assumed you only want a binary result.

I just figure this out a couple days ago but I don’t know if I needed to go to the development version or not (at the time I thought I did). I found it in the discussion regarding adaptive threshold.

binary_sensor:
# ESP32_Touch Sensors
  - platform: esp32_touch
    name: "espTouch Bed Top Left"
    id: esptouch_bed_top_left
    pin: GPIO2
    threshold: 56
    filters:
     - delayed_on_off: 5s

sensor:
#Readings calculated from espTouch sensors
  - platform: template
    name: "Bed Top Left Readings"
    id: "bed_top_left_readings"
    update_interval: 10s
    accuracy_decimals: 0
    lambda: |-
      return ((uint32_t) id(esptouch_bed_top_left)->get_value());
3 Likes

Thanks so much, that looks exactly right for what I’m trying to do, although when I try to compile I now get the error:

src/main.cpp: In lambda function:
src/main.cpp:335:34: error: 'class esphome::esp32_touch::ESP32TouchBinarySensor' has no member named 'get_value'

What version of esphome are you on?

Good shout (again!) I’ve just updated to the latest esphome and its now working perfectly. Thanks so much! :smiley:

1 Like

Can you give us a full write-up of your project and how it is working? Cheers.

I’ll happily do a full write-up although at the moment its some way off working. Having been helped to solve the above I now find I’m stuck again with the display data.

What I’d like to do is use the sensor and display different text based upon the reading,
eg if the sensor reading is below 1000 I’d like to display LOW, if >1000 and <2000 display MEDIUM and if >3000 display HIGH for example.

These aren’t the exact figures but I can tweak with that once I have a system. I don’t want to use the HA template builder as I want this data to be displayed on the ESP and for it to be able to function stand-alone.
I’ve been down several dead ends looking at how to change variables but keep getting stuck. My last example attempt is below (just for LOW and HIGH) but I can’t seem to find the right format for it to compile:

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
#    update_interval: 100ms
    reset_pin: GPIO16
    address: 0x3c
    lambda: |-
      it.print(2, 5, id(arial), "Sensor reading is");
      it.printf(2, 35, id(frut), "%.0f",id(touchValue).state);    
      if (return id(touchValue).state) <= 1000) {
        it.print(2, 55, id(frut), "state: LOW");
      } else {
        it.print(2, 55, id(frut), "state: HIGH");
      }

Any help appreciated!

Could use a separate template text sensor ?

Use one for my washing machine:

text_sensor:
  - platform: template    
    name: "Washer Status"
    lambda: |-
      if ((id(washer_wattage).state >= 5) and (id(washer_wattage).state < 400)) {
        return {"Washing"};
      }
      if ((id(washer_wattage).state >= 400) and (id(washer_wattage).state < 1500)) {
        return {"Drying"};
      }
      if ((id(washer_wattage).state >= 1500) and (id(washer_wattage).state < 1800)) {
        return {"Running"};
      }
      if ((id(washer_wattage).state >= 1800)) {
        return {"Heating"};
      } else {
        return {"Idle"};
      }
    update_interval: 5s

Thanks for the suggestion. I’ve added a text sensor as follows:

text_sensor:
  - platform: template    
    name: "Litres"
    id: "Litres"
    lambda: |-
      if ((id(touchValue).state >= 5) and (id(touchValue).state < 400)) {
        return {"LOW"};
      }
      if ((id(touchValue).state >= 400) and (id(touchValue).state < 1500)) {
        return {"MEDIUM"};
      }
      if ((id(touchValue).state >= 1500) and (id(touchValue).state < 1800)) {
        return {"HIGH"};
      }
      if ((id(touchValue).state >= 1800)) {
        return {"VERY HIGH"};
      } else {
        return {"OUT OF RANGE"};
      }
    update_interval: 5s

And that works in that I can see the correct message in the log file. But where it falls over at the moment is in trying to get that to display, there’s clearly some error in the least line but it eludes me at the moment.

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
#    update_interval: 100ms
    reset_pin: GPIO16
    address: 0x3c
    lambda: |-
      it.print(2, 5, id(arial), "Sensor reading is");
      it.printf(2, 35, id(frut), "%.0f",id(touchValue).state);    
      it.printf(2, 55, id(arial), "Volume is: %s",id(Litres).state);

it does compile but gives the warning

src/main.cpp:412:60: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' [-Wformat=]
       it.printf(2, 55, frut, "volume is: %s" ,Litres->state);

and displays just a solid block rather than the expected LOW, HIGH etc

In your lambda where you have this, you probably need .c_str():

      it.printf(2, 55, id(arial), "Volume is: %s",id(Litres).state);

needs to be:

      it.printf(2, 55, id(arial), "Volume is: %s",id(Litres).state.c_str());