Thomas shares makes

2025-04-18

Rainwatertank Usage meter

empty

Home assistant graph of water usage, the lightest shade is rain water measured by DIY solution, the darkest shade reported by the tap water utility meter over P1 connection.

I integrated a 7€ YF-B7 water flow sensor into my home assistant setup using an off the shelve 35€ modbus based pulse counter.

The sensor

flow sensor picture

The flow sensor connected to the rain water pump.

I bought from a "YF-B7 Water Flow Sensor with Temperature Sensor - Brass - G1/2" from tinytronics.nl. This device uses a magnetic propeller sitting in the water feed to trigger pulses in a hall effect sensor.

A summary of its specifications:

Water flow sensor:
    Supply voltage: 5-18V DC
    Signal voltage: 4.7V (with 5V DC power supply)
    Connector: JST-SM 3p female
    Pulse frequency per L/min: 11Hz*
    Duty cycle pulse: 50% ±10%
    Measuring range: 1-25L/min
    Accuracy: ±3%*
Temperature sensor:
    Type: NTC
    Resistance: 50KΩ
    Beta value: 3950
    Connector: JST-SM 2p female
    Thread: M8
Water connection: G1/2"
Maximum water pressure: 17.5bar
Working temperature: 1-100°C
Suitable liquids: Water**
Material: Brass
Dimensions: See images

Pinout (water flow sensor):

Red: Vcc/plus (supply voltage)
Black: GND/min
Yellow: Signal

Pulse counter

I have several sensors in my garden shed, and they are all read via a 2 wire RS485 serial bus.

I figured I could use an off the shelve pulse counter to keep track of the water use. This way the more complex real-time tracking part can be kept close to the sensor since modbus is probably too slow and it keeps things simple.

The €34.89 "WJ150-485 1 Way Encoder Pulse Signal Counter or 2 Way DI High-speed Counter Modbus RTU Module" from Aliexpress was a good fit, it had good documentation and I got it to work in an hour.

rain water level sensor principle

Mechanical sensor parts assembled with a very short sensor tube to fit the picture.

In set-up mode, which is enabled by connecting a wire to one of the module's screw terminals, the module can receive ASCII commands, so a modbus RTU client is not even necessary.

I sent this command to it using a serial terminal to set module address from '01' to '05', module type '00', baudrate at 9600 ('6'):

%01050006

After a powercycle I checked if I could read the pulse count, and after blowing on the water flow meter, the count register increased!

thomas@roper ~ % modbus  -b 9600 -s 1 /dev/ttyUSB0  h@32/\<I
Parsed 0 registers definitions from 1 files
32: 2819 0xb03

Home assistant setup

I added this modbus.yaml entry:

- name: water_counter
  unique_id: water_counter
  slave: 5
  address: 32
  swap: word
  data_type: uint32

And a template sensor to convert the pulse count to liters (template.yaml):

- trigger:
    - platform: time_pattern
      minutes: "/1"
    - platform: state
      entity_id:
        - sensor.water_counter
      not_from:
        - "unknown"
        - "unavailable"
      not_to:
        - "unknown"
        - "unavailable"
  sensor:
    - name: regenwater
      unique_id: regenwater
      unit_of_measurement: L
      device_class: water
      state_class: total_increasing
      state: >
        {% set count = states('sensor.water_counter') | float %}
        {{ '%0.3f' | format(count*6.0/(4*660)) }}
      attributes:
        dummy: "{{ now().minute }}"

Now I can view pretty bar charts in the Home Assistant Energy overview.

The accuracy might be rough, but I did some napkin math and it seems to be ballpark-accurate.


Liked something? Worked on something similar? Let me know what you think on Mastodon!
You can use your Mastodon account to reply to this post.

Reply to post

You can respond to this post with an account on the Fediverse or Mastodon. Since Mastodon is decentralized, you can use your existing account or create your account on a server of your choice.

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

Learn how @carlschwan wrote the code that loads Mastodon posts into this webpage here.

Follow me on Mastodon!