I’m brand-new to ESPHome, but I have a working config for an esp32 board and HC-SR04 ultrasonic distance sensor that is measuring the level of salt pellets in my water softener. There are four different sensor entities exposed:
- The “raw” distance (the direct reading from the sensor position to the top of the salt)
- The “actual” distance from the max-fill line height to the top of the salt (because the sensor is mounted about 16cm above that height)
- The height of the remaining salt
- The percentage of the remaining salt compared to the max-fill line
This all seems to be working fine now, I’m getting readings that look accurate:
But it’s based on four separate sensors each taking their own readings at the same time:
esphome:
name: water-softener
esp32:
board: esp32dev
framework:
type: arduino
globals:
# distance between bottom of tank and sensor position is 0.91m
- id: sensor_height
type: float
initial_value: '0.91'
# distance between salt max fill line and sensor position is 0.16m
- id: distance_offset
type: float
initial_value: '0.16'
# distance between bottom of tank and max fill line is 0.75m
- id: max_fill_height
type: float
initial_value: '0.75'
sensor:
- platform: ultrasonic
trigger_pin: GPIO17
echo_pin: GPIO18
name: "Salt Level Distance"
update_interval: 10s
unit_of_measurement: "m"
- platform: ultrasonic
trigger_pin: GPIO17
echo_pin: GPIO18
name: "Salt Level Distance From Max Fill Line"
update_interval: 10s
filters:
# Calculates distance, in centimeters, of the difference between the max fill line height and the current level
- lambda: return (x - id(distance_offset)) * 100;
unit_of_measurement: "cm"
- platform: ultrasonic
trigger_pin: GPIO17
echo_pin: GPIO18
name: "Remaining Salt Level"
update_interval: 10s
filters:
# Calculates height, in centimeters, of salt pellets remaining in the tank
- lambda: return (id(sensor_height) - x) * 100;
unit_of_measurement: "cm"
- platform: ultrasonic
trigger_pin: GPIO17
echo_pin: GPIO18
name: "% Remaining Salt Level"
update_interval: 10s
filters:
# Calculates % of salt pellets remaining in the tank
- lambda: return (id(sensor_height) - x) * (100/id(max_fill_height));
unit_of_measurement: "%"
Is there a way to make this more concise and take a single reading, then split out the entity values?