Water Metering With Home Assistant

I’ve pretty much settled on Home Assistant as my DIY automation platform. While it is not perfect, it has the right mix of features, simplicity and customization for my needs. I also like that it is written in Python and includes support for custom dashboards using HADashboard. I’ll be adding additional posts about home automation platforms in general, but for now I want to concentrate on how to connect a water flow meter to Home Assistant. In addition to measuring usage, having water flow information in Home Assistant allows you to come up with some unique automations. For instance, if the system knows you are not home and water flow is detected, it can send you an alert or even shut off the main valve. This is much more efficient than trying to place water leak detectors all over the place. I also want to see real time and historical usage on my various dashboards. Here is one dashboard I have running on a small LCD display in my kitchen:

The water utility meter already installed in most residences is probably not something you can pull data from directly. If it has a data output, it is likely closed and/or proprietary. It may also be too far from the rest of your equipment to be practical, as it was in my case. (A recent article on rtl-sdr.com explains how you can intercept and decode wireless utility meter data, but only a few meters are supported. It is worth checking out though to see if yours is one of them.) This leaves most of us in a position to require a plumber to install our own meter that has some kind of data output. (It also allows us to independently verify utility readings.) I found some advanced meters that simply clamp onto the exterior of the water main and read the flow inside the pipe through ultrasonic analysis, but these were very expensive ($1,200+) and did not have enough sensitivity to measure small flows. More research uncovered a selection of meters in the $150 range that fit the bill. Here are the main things to look for:

  • Make sure the meter is designed for the size of your incoming water main. Common sizes are 3/4″ and 1″. If unsure, look on your water bill for the pipe size or ask a plumber.
  • The meter should be made from a sturdy but safe material. Keep in mind that all of your household water will be passing through this device before it reaches you!
  • Don’t get a cheap meter designed for garden hoses and install on your water main. You want something that will last and not leak.
  • Make sure that the data resolution or definition is sufficient. To detect leaks and small flows, you want to be able to measure at least every 1/10 gallon of flow.
  • What format is the data output? Some are ZWave which integrate easily and others have a simple pulse output you’ll need to interface yourself (see below for more)

I settled on a stainless steel high definition meter with pulsed output from EKM Metering. (They were helpful in answering my questions and had prompt shipping.) Once you have selected a meter, you will likely need a plumber to install it. Unfortunately that will probably cost more than the meter itself. If you have the skill, you could save a lot of money doing it yourself but it is definitely not a project for amateur plumbers!

Once the meter is installed, the next step is to interface the data output with Home Assistant. Almost all the meters in the $150 range work by having a photocell detect the movement of the mechanical dial on the face of the meter. (In fact, if you have an existing meter with dials like those shown in the photo, you may be able to just purchase and snap on the optical detector and avoid the costly steps outlined so far.) This type of metering provides a “pulsed” output which is basically just a switch closure every time a certain amount of water passes through the meter. (In the case of the meter referenced above, it is one pulse every 0.075 gallons) We need to count these pulses and somehow push the data to Home Assistant. Some meters have an external ZWave device that does the counting and data transfer for you. If you get one of those then you are basically set. However, for us DIY hardware tinkerers, that would be too easy. I wanted more control, so I decided to build a pulse counter and data node using a Raspberry Pi.

I suggest using a non-zero Pi with Ethernet built in, but any Pi can work as long as you place it on the same network as your Home Assistant installation. I do not recommend using the same Pi that is running Home Assistant, since we don’t want anything slowing down the measurement activity. Counting pulses is a pretty basic task for the Pi and the core counting code has been posted below. Note that the water meter output is basically a pulse width modulation (PWM) waveform with a duty cycle that varies with the water flow rate. Also note that due to the nature of the mechanical source of the data, when water flow stops, the pulse can be either high or low and remain that way until the next flow activity. For that reason, I decided to only count the rising edge of the signal.

The Python script counts the pulses and calculates the flow rate. The data is transferred as sensors to Home Assistant using MQTT. Originally I used HTTP sensors but the overhead was higher than it needed to be and MQTT seems to be a more natural fit for the tiny real time data being transferred. In addition to flow rate, I included some binary sensors for sustained flow and high flow as well as daily cost, which can be configured through variables. You’ll want to run the script in the background continuously as a service. I decided to get overly fancy and installed the Nginx web server as a front end for meter configuration settings and SQLite for the back end. In fact, every pulse is saved in the database so that intricate usage analysis can be performed if desired. To make the Pi’s environment easier to replicate, the major functions are running inside two Docker containers. For a full tutorial on building the hardware and installing the software, see part two.

Water usage data is a great addition to Home Assistant and provides a lot of insight into how your household and its appliances use (and waste) water. The more utility data you integrate into the system, the better the potential automations become.


import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
global pcount # variable for counted pulses
pcount = 0
count1 = 0
client = mqtt.Client()
client.connect(192.168.1.100, 1883, 60) #IP address of your Home Assistant (HA)
client.loop_start()
def reportstatus(channel):
# will execute on every rising pulse
global pcount
pcount += 1
GPIO.add_event_detect(3, GPIO.RISING, callback=reportstatus, bouncetime=120)
while True:
# loop updates flow in HA every 3 seconds
count1 = pcount
count1 = count1 * 9.6 #convert pulses to ounces
pcount = 0
sleep(3)
# push the flow data to HA using MQTT
client.publish(topicname, round(float(count1)/3,0))

view raw

gistfile1.txt

hosted with ❤ by GitHub

5 thoughts on “Water Metering With Home Assistant

  1. When publishing to Hassio the mqtt broker uses a username and password,how can i alter it to my user name and password

  2. exactly what i was looking for, unable to use it, as Hassio needs a password and user name, big pity,:( as i used mqtt explorer and it works like a dream, would you beadle to add that into the settings page?
    just a username and password, i would ask for current readings ect but suppose that is pushing it 🙂

    1. managed to figure this one out, working like a charm
      added this line under connect, in watermeter.py

      client.username_pw_set(username=”example steve”,password=”password”)

      and it connects:)

Leave a Reply

Your email address will not be published. Required fields are marked *