Help with return status sensor to HA

hello, I have a rain sensor, connected to a pin dell’esp32. I would like to say:

  • if the pin is on, it returns the “it’s raining” value directly to the home assistant (so I don’t create other sensors)
    Now on home assistant I only see on/off.
    I would like to convert this value directly to esphome
esphome:
  name: esphome-sensore-pioggia
  platform: ESP32
  board: esp-wrover-kit

api:
logger: 
ota:
web_server:

wifi:
  ssid: "ssid"
  password: "pass"
  manual_ip:
    static_ip: 192.168.2.198  # Inserisci l'indirizzo IP statico desiderato
    gateway: 192.168.2.1  # Inserisci l'indirizzo IP del gateway
    subnet: 255.255.255.0  # Inserisci la subnet corretta
    dns1: 8.8.8.8  # Inserisci l'indirizzo IP del server DNS primario
    dns2: 8.8.4.4  # Inserisci l'indirizzo IP del server DNS secondario


# Imposta il nome del tuo dispositivo ESPHome
substitutions:
  device_name: esphome-sensore-pioggia

# Aggiungi altri componenti o configurazioni ESPHome necessarie

binary_sensor:
  - platform: gpio
    pin:
      number: 16  # Sostituisci con il numero del pin corretto
      mode: INPUT_PULLUP
    name: "Sensore Pioggia"
    id: sensore_pioggia
    on_state:
    ????

Thank you !

Can you explain this a bit more? Do you want to output text instead of on/off?

Right now the gpio state is already converted to a binary sensor which is indeed on/off (actually true/false). When you want to send texts like “it’s raining” and “it’s not raining” to home assistant, you can use a text sensor. For example:

text_sensor:
  - platform: template
    id: sensore_pioggia
    name: Sensore Pioggia

binary_sensor:
  - platform: gpio
    pin:
      number: 16  # Sostituisci con il numero del pin corretto
      mode: INPUT_PULLUP
    id: sensore_pioggia_interno
    internal: True
    on_state:
      then:
        - lambda: |-
            id(sensore_pioggia).publish_state(x ? "Sta piovendo" : "Non sta piovendo");

The binary sensor is set to internal so that you will only see the text sensor in Home Assistant.
While at it: it is advised to debounce gpio inputs which can simple be done with the delay filter.

1 Like

EDIT: Beaten by @ckxsmart :smiley:

Assuming your sensor stays latched (on) for the entire time it is raining:

binary_sensor:
  - platform: gpio
    pin:
      number: 16  # Sostituisci con il numero del pin corretto
      mode: INPUT_PULLUP
    id: sensore_pioggia
    internal: true

text_sensor:
  - platform: template
    name: "Raining"
    lambda: |-
      if (id(my_binary_sensor).state) {
        return {"It's raining"};
      } else {
        return {"It's NOT raining"};
      }
    update_interval: 30s
2 Likes

Thank you…

you are the best!