Skip to content

Instantly share code, notes, and snippets.

@adonno
Last active October 7, 2024 21:03
Show Gist options
  • Save adonno/940d0865c4d4e2043566744393ecd67e to your computer and use it in GitHub Desktop.
Save adonno/940d0865c4d4e2043566744393ecd67e to your computer and use it in GitHub Desktop.
---
esphome:
name: salt_level_sensor
platform: ESP8266
board: d1_mini
# WiFi connection, replace these with values for your WiFi.
wifi:
ssid: !secret Wifi_SSID
password: !secret Wifi_PWD
# Enable logging
logger:
# Enable Home Assistant API
api:
# Enable over-the-air updates.
ota:
# Enable Web server.
web_server:
port: 80
# Sync time with Home Assistant.
time:
- platform: homeassistant
id: homeassistant_time
# Text sensors with general information.
text_sensor:
# Expose ESPHome version as sensor.
- platform: version
name: salt_level_sensor ESPHome Version
# Expose WiFi information as sensors.
- platform: wifi_info
ip_address:
name: salt_level_sensor IP
ssid:
name: salt_level_sensor SSID
bssid:
name: salt_level_sensor BSSID
# Exposed switches.
# Switch to restart the salt_level_sensor.
switch:
- platform: restart
name: "salt_level_sensor Restart"
sensor:
# Uptime sensor.
- platform: uptime
name: salt_level_sensor Uptime
# WiFi Signal sensor.
- platform: wifi_signal
name: salt_level_sensor WiFi Signal
update_interval: 60s
- platform: ultrasonic
trigger_pin: D1
echo_pin: D2
name: "Saltlevel in percent"
update_interval: 12h
filters:
# Calculates in %
# Replace 0.42 by the height of your container. From the sensor to the bottom.
# I used this website to know how I should multiply my values :https://www.skillsyouneed.com/num/percent-change.html
- lambda: return (0.42-x)*(100/0.42);
unit_of_measurement: "%"
- platform: ultrasonic
trigger_pin: D1
echo_pin: D2
name: "Saltlevel in cm"
update_interval: 12h
filters:
# Replace the 0.42 by the height of your container. From the sensor to the bottom.
# I multiplied by 100 in order to get CM since the sensor works in meters
- lambda: return (0.42-x)*100;
unit_of_measurement: "cm"
@mikedrews
Copy link

mikedrews commented Oct 7, 2024

Hi ... Just wondering if this code still works for you? I tried and can't seem to have 2 entities defined using "platform: ultrasonic".
What's really weird are the errors I get ... (pins 4 & 5)
image

I ended up having to do the following for the two sensors: In the case of my tank the sensor is located 83.5 cm from the bottom but I can only fill it to 72cm.

`sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Salt Level"
    update_interval: 60s
    filters:
    - lambda: return (0.835-x)*100;
    unit_of_measurement: "cm"
    id: salt_level
    
  - platform: template
    name: "Salt Level Percent"
    lambda: |-
      if (id(salt_level).state >= 0) {
        return (id(salt_level).state/72)*100;
      } else {
        return 0;
      }
    unit_of_measurement: "%"
    update_interval: 60s    `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment