Skip to content

Instantly share code, notes, and snippets.

@shamasis
Created May 23, 2024 15:52
Show Gist options
  • Save shamasis/abfec1a85fcf6bc2e33d3be0a8233d3d to your computer and use it in GitHub Desktop.
Save shamasis/abfec1a85fcf6bc2e33d3be0a8233d3d to your computer and use it in GitHub Desktop.
ESPHome Rain Sensor
# This is an ESPHome configuration for an Outdoor Weather Station.
# The configuration includes a raindrop coverage sensor and a rain sensor.
# The raindrop coverage sensor uses an ADC to measure water resistance,
# while the rain sensor is a digital sensor indicating whether it is raining or not.
#
# The configuration uses substitutions to define all the configurable parts,
# making it easy to adjust pins and other settings without modifying the entire file.
substitutions:
name: esph-29697f-ows-1
friendly_name: Outdoor Weather Station (ESPH-29697f)
board: nodemcuv2
pin_power: D7
pin_rs_analog: A0
pin_rs_digital: D2
# Define the name and friendly name for the ESPHome device
esphome:
name: ${name}
friendly_name: ${friendly_name}
name_add_mac_suffix: false
project:
name: esphome.web
version: '1.0'
# Specify the ESP8266 board being used
esp8266:
board: ${board}
# Enable logging, Home Assistant API, OTA updates, Improv Serial, and Web Server
logger: # Enable logging and other configuration
api: # Enable Home Assistant API
ota: # Allow Over-The-Air updates
improv_serial: # Allow provisioning Wi-Fi via serial
web_server: # To have a "next url" for improv serial
# Configure Wi-Fi settings
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Import the dashboard configuration from an example GitHub repo
dashboard_import:
package_import_url: github://esphome/example-configs/esphome-web/esp8266.yaml@main
import_full_config: true
# --------------------------------------------------------------------------------------------
# Define sensors
sensor:
- platform: adc
pin: ${pin_rs_analog} # Analog pin connected to the water resistance sensor
id: water_resistance # Internal ID for the water resistance sensor
internal: true # This sensor is used internally and won't be exposed to Home Assistant
filters:
- calibrate_linear: # Linear calibration to trim erroneous 5% edges
- 0.05 -> 0.0
- 0.95 -> 100.0
- platform: template
name: "Raindrop Coverage Sensor" # Name exposed to Home Assistant
id: raindrop_coverage_sensor # Internal ID for the raindrop coverage sensor
accuracy_decimals: 1 # Display one decimal place
unit_of_measurement: "%" # Unit of measurement as a percentage
icon: "mdi:water-percent" # Icon representing water percentage
# Define binary sensor
binary_sensor:
- platform: template
name: "Rain Sensor" # Name exposed to Home Assistant
id: rain_sensor # Internal ID for the rain sensor
device_class: moisture # Device class for moisture detection
icon: "mdi:umbrella" # Icon representing an umbrella
# Define interval to read data every 5 seconds and update the sensors
# This is done to save power and extend the sensor's longevity by only powering
# the sensors when necessary, instead of allowing each sensor to directly read continuously.
interval:
- interval: 5s
then:
- output.turn_on: sensor_power # Turn on the power to the sensor
- delay: 10ms # Wait for 10 milliseconds
- component.update: water_resistance # Update the water resistance sensor reading
- sensor.template.publish:
id: raindrop_coverage_sensor
state: !lambda |-
// Invert the water resistance state and constrain between 0 and 100
return std::max(0.0, std::min(100.0, 100.0 - id(water_resistance).state));
- lambda: |-
// Read the digital rain sensor state and publish it
bool rain_state = !digitalRead(${pin_rs_digital});
id(rain_sensor).publish_state(rain_state);
- delay: 10ms # Wait for 10 milliseconds
- output.turn_off: sensor_power # Turn off the power to the sensor
# Define the output to control sensor power
output:
- platform: gpio
pin: ${pin_power} # GPIO pin used to control the power to the sensor
id: sensor_power # Internal ID for the power control
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment