How and why I read my apartment energy usage?
Let's start with why
I have no good answer other than that I want to. It's 99% for fun, since I live in an apartment with district heating and good insulation, and the energy consumption does not really correlate with heating or cooling.
Energy usage is tied mostly to cooking, the dishwasher and other appliances that we use consciously.
Energy usage is tied mostly to cooking, the dishwasher and other appliances that we use consciously.
I just want to get the wattage into Home Assistant in a way I can understand and/or manage myself.
But how do you measure it?
That's been quite the journey.
The go-to way of reading the energy usage (here in Norway at least) is using a device that plugs into the HAN-port in the fuse box, and Wifi. Tibber Pulse is a good example of this. My problem with this is that my HAN-port is in the basement, four concrete stories below my apartment, way out of reach of my Wifi.
There are HAN-devices that work over ZigBee, and I did contemplate sneakily replacing lightbulbs down the stairwell with ZigBee-bulbs, to extend my ZigBee-network all the way down that way.
I chose to not do that, for a bunch of reasons you can imagine.
I had to find a way to measure the energy usage inside my apartment. Even though the HAN-port is in the basement, my main fuse box is in my apartment. Inside of it, I found the two main wires that all my electricity flows through. Some googling later, I found the LeChacal RPIZ CT3V1. I liked the format, the RaspberryPi side of it, the documentation, the whole thing really. So I bought one, with one CT clamp.
It was plug 'n play, right?
Haha, no. Over the last 15 months I went through 5 different ways uf using it.
There are a few ways to view the data, something for everyone.
First attempt was a result after tinkering and testing, using the plain cat command and bash.
Read the line, replace space with semicolon, format as json, send it to Home Assistants API using curl. All in an infinite loop in bash in a docker container. Code here.
Home Assistant would then parse out the wattage from the line and round off the decimals. I finally had a live sensor in Home Assistant that showed the energy usage.
This worked for a few months, I don't really remember why it stopped working. I remember the line sometimes being cropped, as if the LeChacal wrote newline characters too soon or too early. But that brings me to my
Second attempt. This time going for a Python based approach because why not. I guess I thought serial reading using Python was better than the cat command. Code here. The principle was the same. Read the line, format it a bit, send it to the API. This worked just as "well".
Third attempt was basically giving up and using a solution someone else made. This did pretty much the same, but posted to MQTT which I then had to set up first.
This worked fine until it for some reason had trouble reading the serial port. I had no idea how to troubleshoot except for rebooting.
Fourth attempt was a fork of the third attempt which fixed a few problems. This had the same issues in my case, but also used a Node version that was too new for the Pi Zero I used. I'm on thin ice with the details here, at this point I was frustrated with all the compounding problems.
Fifth attempt is what is running right now, and so far so good. Code here. I'm basically back to first attempt, but with a new coat of paint.
Read the line (but with head instead of cat this time), clean up the line, post to MQTT.
I think this is the cleanest solution so far.
Fingers crossed, I don't really want to explore further ways of solving this problem. Next attempt might really be to buy new hardware that is much more plug'n'play. Perhaps a Shelly EM.
Thanks for reading, hit like and subscribe and all that jazz.
Comments
Post a Comment