Using ESPhome to build a water flow rate meter

@DeeBeeKay connected my today and it’s working, thanks. Did you tried to have it counting the liters used per day?

I used this Uxcell G1/2" Thread Hall Effect Liquid Water Flow Sensor Switch Flowmeter for this setup

Hi @saboaua, that would be hard for me because I don’t have a flow meter on the outlet - so I can’t just count it.

I suppose it could be calculated, but it would be hard to do in ESPHome itself. I would prefer Node-RED or something similar.

If you take a reading of the reservoir level at the beginning of the day (x), and then add how much you pumped in during the day (y), then measure the reservoir actual at the end of the day (z), then (x+y)-z should give you the total litres you went through. I think. I am not clever.

But I can’t even do that because I have an old diesel pump down by the spring that doesn’t have a flow meter, so it would throw off my calculation.

I was trying to find a way to have it done thru esphome itself. So have to look and try with Node-RED or something similar. As my setup will include a NO valve to shut off the water, if it notice a waterleak for x amount of time.

You work has got me started on the project, thanks.

1 Like

@saboaua That’s very interesting! Controlling for leaks is a great idea.

ESPHome can have lambdas, so, it should actually be possible to do it if you can write the code in C++. My coding ability is very limited. I see the difficulty more as being that you might practically want more advanced responses to the actual problem than just shutting off the water.

I’ll give you an example of what I mean. I’m using ESPHome to actually control the borehole pump itself, using a relay to switch a 26A contactor which in turn switches on the heavy load of the pump. The ESPHome device that controls the pump and the one that monitors the flow are half a kilometer apart, so it’s not practical to have them work off the same board, but they are on the same network.

Node-RED controls the schedule. It turns the pump on, and then it monitors the flow of water. If the flow rate begins to drop suddenly, it means that the water level in the borehole has dropped below the pump intake, and we are now sucking (some) air. That’s bad for the pump (and also sad because we have so little water), so Node-RED shuts the pump down immediately. But then it sends me an email to let me know there’s a problem. My next goal is to have Node-Red tweak the pump run and rest times in response to these problems, so that it can run the pump a little less next time, or let it rest a little longer.

I think you need something similar. Checking for leak condition is a great idea! but you also need a response to that situation that makes you aware there’s an issue - like with a push notification, or a flashing light, or whatever. Otherwise, you will only know there is a leak when you notice that your automatic system (which you designed so you can ignore it) has stopped pumping. Just a thought.

Please continue to post here as you develop your system. I’m glad I got you started, but you also gave me a great idea about detecting leaks. If we keep sharing our ideas, our systems will be amazing.

Ok, I have implemented the valve to my water line coming to the house. The only issue i have now, is to show it correctly in home assistant and getting it working correctly with ESPHOME.

I got only the toggle option working, but as i turn it on the toggle will switch to on and then off. But the valve is still close.

Any ideas? i tried the switch.turn.on option and the toggle in home assistant will do the same, but the relay will stay on. But as i click the toggle to turn off the relay, nothing happends to the relay and stays on.

switch:

  • platform: gpio
    pin: GPIO05 #D1
    id: relay
    restore_mode: ALWAYS_OFF

  • platform: template
    name: Water Valve Control
    icon: mdi:valve-closed
    turn_on_action:

    • switch.toggle: relay
      turn_off_action:
    • switch.toggle: relay

You’re over complicating it.
Remove the template switch and put the name and icon to the GPIO one

Note that esphome does not support per state icon

I did it before, but it didn’t remember the state. As this morning i change it back to the simple switch and it remembers the state.

switch:

  • platform: gpio
    pin: GPIO05 #D1
    id: relay
    name: “Water Valve Control”
    icon: “mdi:valve-closed”
    restore_mode: ALWAYS_OFF

Thanks.

1 Like

It took me a while to figure out the calculations. So I will write here for others to use. Hopefully I am not wrong. First thing is to check your water flow sensor for the Flow Pulse. In DeeBeeKay’s case it was 0.45. I use a a YF-21 model so I have 0.7 from this datasheet.
This is a constant set in the datasheet of the sensor that tells us the relation between flowrate (Q) and frequency aka pulses per second the sensor is sending (Hz). Check this formula written in the DeeBeeKay’s datasheet (Flow Pulse: F(Hz)=(0.45xQ) +/-3% Q=L/min). That also means that F(Hz) / 0.45 = Q (L/m). So if we have 10 Hz (pulses per second) we get 10 / 0.45 = 22.22 (L/m). Or is it L/s?

This is the only thing bothering me right now. That F(Hz) is measured in seconds as in pulses per second and Q is said to be in minutes as in Litres per minute. So how is it?

This? Option A

filters:
    - lambda: return (x / 0.45) * 60.0 * 60.0;
    unit_of_measurement: "L/hr"

(Pulses per sec divided by 0.45 to get L/sec, multiplied by 60 seconds to get L/minute, and multiplied again by 60 minutes to get L/hour. I let you see all the calculations so you understand what is going on. For example 0.45 * 60 = 27 :wink: )

Or this? Option B

filters:
    - lambda: return (x / 0.45) * 60.0;
    unit_of_measurement: "L/hr"

(Pulses per sec divided by 0.45 to get L/min !!!, multiplied by 60 minutes to get L/hour.)

BTW, in ESPHome the pulse counter sensor defaults to measuring its values using a unit of measurement of “pulses/min”.

So DeeBeeKay, are you sure your calculations are now off by 60? :slight_smile:

Let me know if and what I did wrong and if you are getting good results from your setup.
Thank you!
ygreq

1 Like

I am a little bit … no, I am a lot confused by what you have written. You appear to be trying to re-do the work that ESPHome is already doing.

ESPHome measures the rate of pulses by itself and reports this as Pulses Per Minute. You don’t have to write any code for that. ESPHome just does it for you.

Converting that data into Litres per Minute or per hour depends on how many times your specific sensor will spin per litre. For my sensor, I know that 27 pulses = 1L of water. So I divide PPM by 27, and that gives me litres per minute. But I want to know the rate per hour, because that is more useful to me, and because there are 60 minutes in an hour, I multiply L per min by 60.

I can do this because the datasheet for my sensor explicitly states that 27 pulses = 1L. The datasheet for your sensor does not explicitly state how many pulses equate to 1L of water. However, it seems that your sensor is also called YF-S201 (based on the file name of the PDF you linked to). Some googling reveals that YF-S201 is quite a small sensor, and turns 450 times for 1L.

So for you, the filter looks like this:

    filters:
    - lambda: return (x / 450.0) * 60.0;

That’s it. You don’t need to do anything else.

2 Likes

Thank you for your reply, DeeBeeKay! But can you explain this formula? It’s for your sensor. The previous message is based on this

And based on that formula, it makes sense for your sensor. Because ESPHome gives you F(Hz) as PPS and you know the constant (0.45). And if you multiply the constant to 60 sec, it actually gives you 27 like you already know and have used in your calculations ( 1L water= 27Pulse).

I am curious where you found info for my sensor as I couldn’t find how many pulses for 1 litre. Can you share the link? Because otherwise, the same formula will get me somewhere else completely for my sensor. The formula for my sensor is F(Hz)=(0.7xQ) +/-5% Q=L/min. That would mean:

filters:
     - lambda: return (x / 42.0) * 60.0;

(meaning 0.7 * 60sec = 42)

Thank you!

1 Like

And btw, I didn’t write code for that. Indeed I made you really confused. :smiley:

Life, the Universe and Everything!

1 Like

Hi @DeeBeeKay,

Thank you, this is a great tread!
I use the YF-B10 and I have got a problem with the datasheet I found.
http://www.sunwoald.com/en/productshow.asp?ArticleID=R8S7WRQTY5
It does not give the number of pulses for one liter, it gives only the formula between F and Q.
F=(6*Q-8)
What surprises me is the 8. It means that when there is no water flow, there is still a pulse!?
I did the experimentation by typing this inton ESPhome:

  - platform: pulse_counter
    name: "Flow rate 1"
    pin: D5
    update_interval: 60s
    filters:
    - lambda: return (( x + 480 ) / 360) * 60; #Flow pulse: F=(6*Q-8)±3% with Q=L/min
    unit_of_measurement: "L/h" 
    id: flow_rate_1

So of course, when putting no water in the sensor, I can read 80 L/h.
Any clue?

1 Like

Surely the formula is

((x+8)/6)*60

Which can be reduced further to

((x+8)*10

Which is weird because then it shows 80 L/min when there is no water going in the sensor…

1 Like

So the vendor replied giving me the value of 420 pulses per liter.
After experimentation with a 1.5 liter bottle, it seems correct.
However, after repeating the experimentation 10 times, I find an error margin of 13% and not 3%… In other words, the value I get is between 1.45 and 1.7 liters.
Maybe my experimentation isn’t good enough as I am simply pouring water in the sensor…

Hi. I’m also having problems on getting my flow meter sensor working with esphome always getting 0 values on the readings.

I have this water flow sensor: [SEN-HZ43WB]

Specifications:
Product Name: Water Flow Sensor
Material: Copper(Shell)
Liquid Temperature: Not More Than 60C
Start Flow Range: 1.5L/min
Flow Range: 2-45L/min
Working Voltage Range: DC4.5-18V
Max Current: 10mA
Maximum Water Pressure: 1.75MPa
Connector: Male SM-3P Plug
Cable Length: 35cm
Size: 44mm x 32mm x 30mm(LWH)
External Thread Diameter: G3/4 inch
Tube Length: 44mm
Color: Gold Tone
Net Weight: 112g
6. Instantaneous Flow Pulse Characteristic F=8.1 x Q(flow)-5 +/-10%
7. The cumulative flow pulse conversion ratio 1L Water=477 pluse +/- 10%

Sow for 1L my sensor gives 477 pulses.

And i have it connect to a esp8266 node

Here’s the code that i’m using:

esphome:
  name: cmf_contador_agua
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "**********"
  password: "**********"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Cmf Contador Agua"
    password: "*********"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "**********"

ota:
  password: "**********"

sensor:
  - platform: pulse_counter
    id: water_pulse
    internal: true
    name: "Flow rate"
    pin:
      number: D1
      mode: INPUT_PULLUP
    update_interval: 1s
    filters:
      #- delta: 1.0
      - lambda: |-
          return x / (477.0 * 60.0);
          static float total_pulses = 0.0;
          total_pulses += x * 1 / 60.0;
          id(lifetime_counter).publish_state(total_pulses / 318);
          return x ;
          
  - platform: template
    id: lifetime_counter
    name: "Water Total"
    unit_of_measurement: "L"

  - platform: template
    name: "Water Flow Rate"
    id: water_flow_rate
    accuracy_decimals: 1
    unit_of_measurement: "L/min"
    icon: "mdi:water"
    lambda: return (id(water_pulse).state /318);
    update_interval: 3s

What am i doing rong ???
Hope anyone could help me on this.

Hi @mrc-core. ESPHome already returns the sensor value as pulses per minute. So you do not need to recalculate that the way you seem to be doing.

All you need to know is how many pulses = 1L, and we know that this is 477. Therefore, your Lambda need be no more complex than:

     filters:
       - lambda: return (x / 477.0) * 60.0;

x is pulses per minute, provided by ESPHome reading the sensor. x divided by 477 gives you the number of litres per minute. There are 60 minutes in an hour, so, that L/min value multiplied by 60 gives you the speed in litres per hour.

I hope that helps.

Hi @DeeBeeKay and thanks for your replay.
I have made the changes on the code and finaly i can see some data appearing on the sensor.
But now i have a new problem it dosen’t store the data the sensor always stays at zero…
Could you help me on this? How to get the readings store on home assistant.

Once again thanks.

what is the easyest way to build a water meter with ha ?