Introduction:
Some community members have approached me with requests to consolidate and detail the code Iād developed into a singular post for easier comprehension. I must admit, itās been several months since I last revisited this project. Today, Iām pleased to finally share it in its entirety. Please note, this post is intended for informational purposes, and if it assists or inspires other enthusiasts in the community, it would bring me immense satisfaction.
Entities Overview:
Hereās a summary of the entities that will be discussed and their respective functionalities:
- Instantaneous Water Flow Rate (
sensor.debit_d_eau_instantane
): Monitors the real-time flow rate of water.
- Total Water Volume in Cubic Meters (
sensor.eau_total_en_m3
): Tracks the cumulative water usage in mĀ³.
- Total Water Volume in Liters (
sensor.utilisation_d_eau_total
): Tracks the cumulative water usage in liters.
- Duration of the Last Water Usage (
sensor.duree_ecoulement_eau_principale
): Measures how long water has been flowing in the most recent usage.
- Volume of the Last Water Usage (
sensor.volume_derniere_utilisation
): Calculates the volume of water used in the last flow.
- Cost of the Last Shower (
sensor.cout_de_la_derniere_douche
): Estimates the cost associated with the water used during the last shower.
- Water Flow Binary Sensor (
binary_sensor.debit_d_eau_on_off
): A binary sensor indicating whether water is currently flowing.
- Starting Water Volume (
input_number.volume_de_depart_debit
): A helper entity to store the starting volume for specific duration calculations.
Here is the card I created to have an overview of the entities:
Now, letās delve into the specifics of the implementation.
Using ESPhome to Build a Water Flow Rate Meter for Shower Analysis
In this guide, weāll explore a solution Iāve implemented to monitor and analyze water usage during showers using the Home Assistant platform. This setup harnesses the capabilities of a NPT YF-B5 water flow sensor, though you can adapt it to work with other similar sensors. The goal here is twofold:
- Create an automation that provides contextual data via Google Home Mini after each shower.
- Design a Lovelace card displaying key metrics related to water consumption.
This setup not only records the duration and volume of water consumed during showers but also provides contextual feedback on the shower duration compared to the national average. Such insights aim to promote awareness and sustainable water usage.
Now, letās delve into the implementation:
1. Instantaneous Water Flow Rate (in ESPHome)
Capture real-time water flow rate using the NPT YF-B5 sensor (or your chosen water flow sensor).
sensor:
- platform: pulse_counter
pin:
number: 4
mode:
input: true
pullup: true
unit_of_measurement: 'L/min'
accuracy_decimals: 2
id: water_usage
name: "DƩbit d'eau instantanƩ"
update_interval: 5s
filters:
- lambda: return (x / 396); # 396 = 6.6 * 60
2. Total Water Volume Measurements (in ESPHome)
Determine the overall water volume in both cubic meters and liters:
a. Total volume in cubic meters:
sensor:
- platform: integration
name: "Eau total en M3"
unit_of_measurement: 'mĀ³'
accuracy_decimals: 2
sensor: water_usage
time_unit: min
filters:
- lambda: return (x / 1000);
b. Total volume in liters:
sensor:
- platform: integration
name: "Utilisation d'eau total"
unit_of_measurement: 'L'
accuracy_decimals: 2
sensor: water_usage
time_unit: min
3. Setting up Helper Entities (in the Home Assistant UI or configuration.yaml
)
Helpers in Home Assistant are entities that store temporary values. In this project, weāll utilize an input_number
helper to keep track of the starting water volume for our calculations.
a. Setting up input_number
for Starting Water Volume:
Helpers in Home Assistant are entities that store temporary values. They can be used in automations, scripts, or Lovelace dashboards. In this project, weāll utilize an input_number
helper to keep track of the starting water volume for our calculations.
a. Setting up input_number
for Starting Water Volume:
If youāre using the Home Assistant UI:
- Navigate to Configuration > Helpers.
- Click on the ā+ Add Helperā button.
- Select āNumberā.
- Provide a name such as āStarting Water Volumeā and set the desired range and step values. Note down the entity ID; it will look something like
input_number.starting_water_volume
.
If you prefer to set up using the configuration.yaml
file:
input_number:
volume_de_depart_debit:
name: Starting Water Volume
initial: 0
min: 0
max: 100000
step: 1
This input_number
entity will store the water volume at the start of our monitoring period and can be used in template calculations to determine water usage for a specific duration.
b. Automation to Set the Starting Water Volume:
alias: "Set Starting Water Volume"
trigger:
- platform: state
entity_id:
- binary_sensor.debit_d_eau_on_off
to: "on"
action:
- service: input_number.set_value
target:
entity_id: input_number.volume_de_depart_debit
data:
value: "{{ states('sensor.utilisation_d_eau_total') | float }}"
This automation sets the starting water volume to the current total water volume whenever the water starts flowing.
4. Template Sensors (in configuration.yaml)
a. Binary Sensor for Water Flow Detection:
template:
- binary_sensor:
- name: DĆ©bit d'eau (ON/OFF)
state: "{{ states('sensor.debit_d_eau_instantane') | float(0) > 0 }}"
b. Duration of the Last Water Usage:
sensor:
- platform: template
sensors:
duree_ecoulement_eau_principale:
friendly_name: "Duration of Last Water Flow"
unit_of_measurement: "sec"
value_template: >
{% set t = states('sensor.duree_ecoulement_eau_principale') | float if states('binary_sensor.debit_d_eau_on_off') == 'off' else now().timestamp() - states.binary_sensor.debit_d_eau_on_off.last_changed.timestamp() %}
{{ t | round(2) }}
c. Volume of the Last Water Usage:
sensor:
- platform: template
sensors:
volume_derniere_utilisation:
friendly_name: "Last Water Usage Volume"
unit_of_measurement: 'L'
value_template: >
{{ (states('sensor.utilisation_d_eau_total') | float - states('input_number.volume_de_depart_debit') | float) | round(2) }}
d. Cost of the Last Shower:
sensor:
- platform: template
sensors:
cout_de_la_derniere_douche:
friendly_name: "Cost of the Last Shower"
unit_of_measurement: '$'
value_template: >
{{ (states('sensor.volume_derniere_utilisation') | float * 0.01) | round(2) }} #change the multiplier (0.01) by your local cost of energy to heat up 1 litre of water to your tank set temperature
5. Prerequisites
To ensure the automation runs smoothly, ensure you have the binary_sensor.debit_d_eau_on_off
set up to detect water flow.
6. Automations
a. Voice Feedback After Shower (Using Google Home Mini):
This automation is particularly interesting as it provides an integration of contextual data after a shower. It gives insights into the duration of the shower, the volume of water used, and how this compares with the national average.
alias: "Shower Feedback - Duration and Volume"
trigger:
- platform: state
entity_id:
- binary_sensor.debit_d_eau_on_off
to: "off"
from: "on"
condition:
- condition: numeric_state
entity_id: sensor.duree_ecoulement_eau_principale
above: "240" #this condition ensure that not all water usage will trigger the automation. A continuous 4 minutes and above will be considered a shower, below can be a toilet or a dishwaher. You can adjust according to your own house usage.
- condition: not
conditions:
- condition: state
entity_id: sensor.presence_iphone_lydia #My girlfriend has a beauty salon in the basement. If she uses the water for her salon and it goes above 240 seconds, it's not a shower, so I have a condition that check if her phone is located in her beauty salon (I use ESPresense but you can set your own method of detection if you have such exceptions)
attribute: distance
state: ess
action:
- delay: "00:00:25"
- service: media_player.volume_set
data:
volume_level: 0.5
target:
entity_id: media_player.salle_de_bain_rdc
- service: tts.speak
data:
cache: true
media_player_entity_id: media_player.salle_de_bain_rdc
message: >
{% set t = states('sensor.duree_ecoulement_eau_principale') | int %}
{% set m = t // 60 %}
{% set s = t - m * 60 %}
Your shower lasted {{ m }} minutes and {{ s }} seconds, consuming {{ states('sensor.volume_derniere_utilisation') | int }} liters of water.
{% if t > 480 %}
This duration is {{ ((t - 480) / 480) | round(1) * 100 }}% longer than the national average.
{% endif %}
Please be mindful of your water consumption. Thank you and have a great day.
target:
entity_id: tts.google_translate_say
mode: single
This comprehensive setup offers a deep dive into understanding water consumption patterns and, when combined with Home Assistantās automation capabilities, can be a powerful tool for promoting water conservation.