Using ESPhome to build a water flow rate meter

Hello @John_Kotrolos,

The ESPHome pulse counter sensor component is designed to count pulses continuously, regardless of the reporting interval (the update_interval setting). The update_interval simply determines how often the ESPHome device reports the counted pulses to Home Assistant, not how often it counts. Here’s how it works:

  1. Continuous Counting: The pulse counter in ESPHome will continuously monitor the pulses from the flow sensor. It does not stop counting between the updates that it sends to Home Assistant.
  2. Reporting Interval: When you set an update_interval of, say, 5 seconds, the ESPHome device will sum up all the pulses it has counted in that 5-second period and then send that number to Home Assistant.
  3. Accumulation: For your use case of tracking the total volume of water to determine when to change the filter, you would typically use an integration sensor in ESPHome that takes the flow rate and integrates it over time to calculate the total volume.
  4. Home Assistant Update: The update_interval in ESPHome corresponds to how often the sensor data is sent to Home Assistant. If you set it to 5 seconds, then Home Assistant’s entity state will refresh every 5 seconds with the latest data from the sensor.

For your specific use case, here’s what I would recommend:

  • Use an Integration Sensor: In ESPHome, define an integration sensor that uses the data from the pulse counter to calculate the total volume of water that has passed through the sensor.
  • Total Volume: You can maintain a running total of the volume of water that has passed through the filter. This would involve storing the accumulated volume in a variable that persists across reboots, which can be done using ESPHome’s Globals or by saving the state to flash.
  • Alerts: Set up an automation in Home Assistant that triggers an alert when the total volume reaches a certain threshold, indicating that it’s time to change the water filter.

Here’s a basic example of how you might define such a sensor in ESPHome:

sensor:
  - platform: pulse_counter
    pin: GPIO4
    unit_of_measurement: 'L/min'
    name: "Débit d'eau instantané"
    id: water_usage
    update_interval: 5s
    accuracy_decimals: 2
    filters:
      - lambda: return x / 98.0;

  - platform: total_daily_energy
    name: "Total Water Usage"
    power_id: water_usage
    unit_of_measurement: 'L'
    accuracy_decimals: 2

Note: The total_daily_energy sensor is typically used for energy measurement, but it can be repurposed for any kind of cumulative measurement like total water usage. You might need to adapt this to your specific requirements or use a different sensor type that’s more appropriate for water flow integration.

Remember to replace power_id with the correct ID if you’re not measuring power. This is just an example, and ESPHome might have different ways to handle cumulative totals for non-energy sensors.

In conclusion, the ESPHome setup you design should be able to handle the continuous tracking of water flow and integrate over time to calculate the total volume, independent of the reporting interval to Home Assistant.

Hi everyone,
Whenever I reset the esp8266 di mini, all the values ​​are set to 0.
Is there a solution for this?

sensor:

  - platform: pulse_counter
    name: "Wasserverbrauch/Min."  
    pin: GPIO2  
    update_interval: 5s  
    accuracy_decimals: 2  
    id: water_usage  
    unit_of_measurement: 'L/min' 
    filters:  
      - lambda: return (x / 690); 
  
  
  - platform: integration 
    name: "Wasserverbrauch m³"    
    unit_of_measurement: 'm³'   
    accuracy_decimals: 2  
    sensor: water_usage   
    device_class: water  
    state_class: total_increasing    
    time_unit: min   
    filters:    
      - lambda: return (x / 1000);
  
  
  
  - platform: total_daily_energy  
    name: "Wasserverbrauch total"   
    power_id: water_usage   
    unit_of_measurement: 'L'   
    accuracy_decimals: 2 
    device_class: water   
    state_class: total_increasing


image

This is where you need to setup your utility meter in configuration.yaml

utility_meter:
  consommation_main_hourly:
    source: sensor.utilisation_d_eau_total
    cycle: hourly
  consommation_main_daily:
    source: sensor.utilisation_d_eau_total
    cycle: daily
  consommation_main_weekly:
    source: sensor.utilisation_d_eau_total
    cycle: weekly
  consommation_main_monthly:
    source: sensor.utilisation_d_eau_total
    cycle: monthly
  consommation_main_yearly:
    source: sensor.utilisation_d_eau_total
    cycle: yearly

And then you can benefit from long-term data storage for these values.

1 Like

Thank you! I didn’t consider the utility meter for my water system, but it is perfect. You saved me sooooo much time. Merci!

1 Like

after latest esphome update this conf is not working anymore, state is always 0 (and i have 3 this sensors, none of them isnt working)

  - platform: pulse_counter
    pin: 32
    name: "Vesi 1. korrus_kook"
    id: vesi_1_korrus_kook
    update_interval: 5s
    filters:
    - lambda: return (x / 660.0);  # 660 = 11 * 60 sec
    unit_of_measurement: "L/sec"
    total:
      name: "Total Vesi 1. korrus_kook"
      unit_of_measurement: "L"
      accuracy_decimals: 3
      filters:
        - lambda: return (x / 660.0);  

help?

It’s a temporary glitch related to the latest ESPHome update. I’ve seen this problem not only with platform: pulse_counter but also with platform: dht and others too. This is not surprising that they pushed 2-3 updates close from each other, there is an ongoing issue but I managed to find a solution that worked for me.

  1. Note your API Encryption key and IP Address in logs:
    Capture d’écran, le 2023-12-23 à 09.15.17
    Capture d’écran, le 2023-12-23 à 09.17.07

  2. Delete the integration for the duplicated ESP-Node
    Capture d’écran, le 2023-12-23 à 09.13.02

Capture d’écran, le 2023-12-23 à 09.13.19
Capture d’écran, le 2023-12-23 à 09.13.26

  1. Manually add your ESPHome node
    Capture d’écran, le 2023-12-23 à 09.13.43

  2. Let Home Assistant add it after it rediscovers your node (optionnally)
    If you are not on a rush, juste give HA some time and the node will be discovered and you can add it by a single click.

im downgraded to 11.6 and working again.

Hey mate, thanks for this post it’s highly informative.
I aspire to have an off grid setup with a dam one day. I’m starting a nursary, esphome is so awesome. Thank you

Any update on this topic? I have a yf-b6 sensor that it measure always 0 pulse/min using ESPHOME 2023.12.5… any way to downgrade esphome in case? I have not backup to restore…

Is your HA Sensors shows as unavailable?
Or is your sensor incorrectly configured?

About the ESPHome bug where your entities are duplicated, I don’t know if it has been solved but my workaround worked for all duplicated entities.

The sensor was working before correctly… now the part related the pulse counter seems not working anymore…

@Destroyer1090, your calculations are made from an entity provided by ESPHome.
Because of the recent ESPHome bug there are duplications of entities provided by ESPHome to HA.

Check if your ESPHome Node entities are duplicated by going to your entities lists

I described the temporary solution in my comment here: Using ESPhome to build a water flow rate meter - #289 by donparlor

If you remove your ESPHome Node then add it back to HA, all your calculations should be back to normal.

It seems I have not duplicate entities… but even inside the esphome logs I see always 0 pulse/min

And if you look at the history of that sensor, do you see anything? Can you see when it last recorded data?

Seems from a certain time it stop to work…

I guess that I connected wrongly the wires because I recognize later the wires color was swapped… so here how I wrongly connected for a while…
Red: signal
Black: 5V
Signal: GND
After this, the device seems stop to working! Do you think I broke it? What I have to do now?:sob: any suggestion? Device is yf-b6.

There is a risk that reversing the wire colors on a flow sensor like the YF-B6 could indeed cause damage to the device, potentially rendering it inoperable.

  1. Reverse Polarity: Electronic sensors, like the YF-B6, are typically designed to operate with a specific polarity. Reversing the polarity (i.e., connecting the 5V where GND should be, and vice versa) can cause an internal short circuit, damaging sensitive electronic components.
  2. Inappropriate Signal: The red wire is typically used for the signal in many electronic devices. If you connect this wire to 5V instead of the signal, it could send an inappropriate voltage to the signal processing circuit, which could damage the sensor.
  3. Risk of Overload: Connecting the signal wire (which should normally receive a low voltage) directly to a 5V source could lead to an overload of the signal circuit, causing irreversible damage.

It is usually important to strictly follow the wiring instructions provided by the manufacturer to avoid any risk of damage to the device. If you are now sure about the wiring and it is still not updating, there are chances that your flow sensor is now unusable.

Hi @Destroyer1090, YES, there’s a way to recover, but it does cost some money.

Essentially, you can replace the board inside the sensor without having to remove the unit from the pipe you installed it into, which is the absolutely worst and most painful part of the job. Trust me, I have these in 50mm pipes.

I looked at your sensor online, it has screws on the outside. Take them out, and you’ll find a round circuit board, usually with a rubber gasket around it to keep it watertight. That part is cheap and you can get it anywhere; it’s inside almost all flow meters (really the only difference between them is what the number of rotations means).

If you buy another YF-B6, you can replace the part with confidence, but if you just look at the dead board, you can probably order it on Bangspressdu.

All flow meters are basically just hall sensors. There will be a magnet somewhere in the spinny bit, and the hall sensor just goes blip every time it comes around. You could actually make one, if you really, really needed to.

Like, if the movie takes place after the inevitable fall of society in the wake of late-stage capitalism, and the protagonist ventures into a ruined city, fighting mutants and so forth, and manages to scavenge a hall sensor in the very first place they break into, it would still be a pretty believable movie.

Best of luck. Let us know what happens.

EDIT: oh, and in case anyone else installs this stuff in big pipes, here’s some free advice: install your sensor with some kind of quick-release system on each side of it, and on the cable to the board.

Why? Because you will be doing service on it and/or replacing it at some point. You just will.

Groundwater/rainwater is pure yadda yadda but there’s often minerals and just plain old silt in it. After a time, the blades get stuck.

If you can take the sensor out, you can clean it pretty easily. Brush out what you can, and then rinse until clean. Little spray of something probably carcinogenic to make sure it’s smooth, and then clip it back in. Tadaaa.

2 Likes

Hi,

Thank you for sharing you code. The first code did i copy and paste in an ESP32 file in ESPHome. But i got an error “Mapping values are not allowed here”

Any idea?

Share your code first so we can look at it.