Combining multiple pins on ESP32 to show a percentage when triggered

So I’m trying to set up an ESP32 to show a percentage (25,50,75,100) as output to HA depending on whether certain pins are triggered. IE 32,33,27,14. I’m trying to do this one of two ways. either setting up a touchpad version and using those touch pins or just as a normal high/low GPIO switch. Say something triggers pin 32 it’s 25% then it triggers pin 33 it’s 50% and so on. My question is how do I go about writing this out? Plus is this configured at the ESPHOME interface or on the HA side of things. I have tried to add multiple binary sensors and it keeps throwing an error. any help is appreciated. My coding skills are a little on the rough side. but I’m trying to learn. This whole project is to monitor water level on a tank. so as the level rises it triggers the appropriate pins.

You can do either.

On the ESPHome side use a template sensor.

Or if you want to do it in Home Assistant use a… template sensor:

Pick which way you want to do it first. Share your ESPHome config with the binary inputs and then we can help with the templates.

Ok, I was able to get the coding right on the ESPHome side of things and was able to get the touch sensors added. So now each pin is showing in HA as a enitiy basically.

So I guess I am going to use templates in home assistant and just create a sensor to represent the current state of the pins of the ESP32. I tired to add a template sensor as a sensor and binary but i cant seem to find it in my enitites as it being added. Im sure im doing something wrong.

here are the entities listed that need to show as a single entity:
‘binary_sensor.water_25’
‘binary_sensor.water_50’
‘binary_sensor.water_75’
‘binary_sensor.black_level_full’

template:
  - sensor:
      - name: Tank Level
        unit_of_measurement: '%'
        state: >
          {% if is_state('binary_sensor.black_level_full', 'on') %}
            100
          {% elif is_state('binary_sensor.water_75', 'on') %}
            75
          {% elif is_state('binary_sensor.water_50', 'on') %}
            50
          {% elif is_state('binary_sensor.water_25', 'on') %}
            25
          {% else %}
            0
          {% endif %}

Show your ESPHome config if you want it done in ESPHome, so you don’t have all the extra unneeded binary sensors.

1 Like

Ok I had tried that type of template and I cant seem to have it show in-home assistant. what am I missing? What yaml do you put this in?

Let me grab the code and ill post it here for the ESPHome. I’m actually moving to another sensor type after some trial and error the last 24 hours. The touch-type wasn’t working as I wanted. so I’ll be going to 3 capacitive sensors running off 3 GPIO pins on the ESP32. most likely pins 32,33,25.

here is what I had for the touch.

binary_sensor:
  - platform: esp32_touch
    name: "Full"
    pin: GPIO32
    threshold: 1000
  - platform: esp32_touch
    pin: GPIO33
    name: "Water 75"
    threshold: 1000
    filters:
      - delayed_on: 500ms
  - platform: esp32_touch
    pin: GPIO27
    name: "Water 50"
    threshold: 1000
    filters:
      - delayed_on: 500ms
  - platform: esp32_touch
    pin: GPIO14
    name: "Water 25"
    threshold: 1000
    filters:
      - delayed_on: 500ms

Here is what I Will be using for the GPIO setup now. Once I get the rest of my sensors in next week this is how I will want it in ESPHome. or at least to output as maybe one sensor? to read 25,50,100
when triggered.

binary_sensor:
  - platform: gpio
    name: "Water 100"
    pin: GPIO32
  - platform: gpio
    pin: GPIO33
    name: "Water 50"
    filters:
      - delayed_on: 500ms
  - platform: gpio
    pin: GPIO25
    name: "Water 25"
    filters:
      - delayed_on: 500ms

Hi,
Like @SnowmanM16, I would like to add a sensor in ESPHome with a percentage from 4 binary sensors, so i can show a jauge in home assistant dashboard.
I did not understand how to use template sensor, any help is welcome.
Here is my ESPHome config file

esphome:
  name: esp32-niveau
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "************************************"

wifi:
  ssid: "Vortex"
  password: "**********"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Niveau d'eau"
    password: "********"

captive_portal:

text_sensor:
  - platform: template
    name: Uptime Human Readable
    id: uptime_human
    icon: mdi:clock-start

sensor:
  - platform: wifi_signal
    name: Signal WiFi
    update_interval: 60s
    
  - platform: uptime
    name: Uptime Sensor
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? String(days) + "d " : "") +
                (hours ? String(hours) + "h " : "") +
                (minutes ? String(minutes) + "m " : "") +
                (String(seconds) + "s")
              ).c_str();

binary_sensor:
  - platform: gpio
    name: Niveau plein
    pin: GPIO19

  - platform: gpio
    name: Niveau 66%
    pin: GPIO18

  - platform: gpio
    name: Niveau 33%
    pin: GPIO17
    
  - platform: gpio
    name: Niveau bas
    pin: GPIO16
    
switch:
  - platform: gpio
    name: Relais Pompe
    pin: GPIO25
    inverted: True
    
  - platform: gpio
    name: Relais Démarrage
    pin: GPIO26
    inverted: True

That goes in your configuration.yaml file.

Lets do it in ESPHome so you don’t have to.

Using your new level sensor, and preventing the binary sensors from showing up in home assistant (if you want them to, then just uncomment the names, could be useful for debugging):

binary_sensor:
  - platform: gpio
#    name: "Water 100"
    id: water_100
    pin: GPIO32
  - platform: gpio
    pin: GPIO33
#    name: "Water 50"
    id: water_50
    filters:
      - delayed_on: 500ms
  - platform: gpio
    pin: GPIO25
#    name: "Water 25"
    id: water_25
    filters:
      - delayed_on: 500ms

sensor:
  - platform: template
    name: "Tank Level"
    unit_of_measurement: "%"
    update_interval: 60s
    lambda: |-
      if (id(water_100).state) {
        return 100; 
      }
      else if (id(water_50).state) {
        return 50;
      }
      else if (id(water_25).state) {
        return 25;
      } 
      else {
        return 0;
      }

It would have been better if you had started your own topic rather than hijacking this one. But try this:

sensor:
  - platform: wifi_signal
    name: Signal WiFi
    update_interval: 60s
    
  - platform: uptime
    name: Uptime Sensor
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? String(days) + "d " : "") +
                (hours ? String(hours) + "h " : "") +
                (minutes ? String(minutes) + "m " : "") +
                (String(seconds) + "s")
              ).c_str();

  - platform: template
    name: "Niveau du Réservoir "
    unit_of_measurement: "%"
    update_interval: 60s
    lambda: |-
      if (id(niveau_100).state) {
        return 100; 
      }
      else if (id(niveau_66).state) {
        return 66;
      }
      else if (id(niveau_33).state) {
        return 33;
      } 
      else if (id(niveau_0).state) {
        return 0;
      } 
      else {
        return 'unknown';
      }

binary_sensor:
  - platform: gpio
    name: Niveau plein # comment out this name if you no longer need the binary sensor in Home Assistant
    id: niveau_100
    pin: GPIO19

  - platform: gpio
    name: Niveau 66% # comment out this name if you no longer need the binary sensor in Home Assistant
    id: niveau_66
    pin: GPIO18

  - platform: gpio
    name: Niveau 33% # comment out this name if you no longer need the binary sensor in Home Assistant
    id: niveau_33
    pin: GPIO17
    
  - platform: gpio
    name: Niveau bas # comment out this name if you no longer need the binary sensor in Home Assistant
    id: niveau_0
    pin: GPIO16

Apologies if I have murdered your language. Blame Google translate :slight_smile:

Thank you very much @tom_l , it worked. but a little problem. How could I remove the .0
and when all the binary sensors are off, it gives 1852798848.0% ???
i believe that this part is the probleme

      } 
      else {
        return 'unknown';
      }

What value do you want if there are no sensors on?

What value do you want if Niveau bas is on?

Hi, i changed the return to 0.
I just have to convert the 100.0 to 100, any idea ?
and thank you for your replies.

sensor:
  - platform: template
    name: "Niveau d'eau"
    unit_of_measurement: "%"
    update_interval: 10s
    lambda: |-
      if (id(niveau_4).state) {
        return 100; 
      }
      else if (id(niveau_3).state) {
        return 66;
      }
      else if (id(niveau_2).state) {
        return 33;
      } 
      else if (id(niveau_1).state) {
        return 10;
      } 
      else {
        return 0;
      }

binary_sensor:
  - platform: gpio
    name: Niveau plein
    id: niveau_4
    pin: GPIO19

  - platform: gpio
#    name: Niveau 66%
    id: niveau_3
    pin: GPIO18

  - platform: gpio
#    name: Niveau 33%
    id: niveau_2
    pin: GPIO17
    
  - platform: gpio
    name: Niveau bas
    id: niveau_1
    pin: GPIO16

Include this in your template sensor config:

accuracy_decimals: 0

It’s in the docs for sensor options: https://esphome.io/components/sensor/index.html#sensor-component

1 Like