Home automation using simple tools

I bought a smart power plug for €6,49 at the Action today.

Lots of home automation stuff assumes you want to use Home Assistant. I do not want Home Assistant, I just want to use a bunch of simple and industry standard tools like curl, jq and rrdtool.

These are the steps I ultimately followed after consulting multiple sources and are mostly some notes for myself with the hope that it will inspire people. Do not blindly follow this, you have been warned!

Install esphome using pip3 install esphome.

Esphome is arguably not a simple tool, but it’s currently the only option that is easy to use. Looking at all the build dependencies, there is certainly lots of room for improvements here…

I mostly copy pasted a YAML file from someone else (in true DevOops fashion). I added a static IP address because I had no idea what lease it would get and I don’t have access to the DHCP server here :)

esphome compile LSC2.yaml

This will install a whole bunch of complicated things, but it seems to work.

bk7231tools write_flash -d /dev/ttyUSB0 -s 0x11000 -S 0 ./.esphome/build/lsc-powerplug1/.pioenvs/lsc-powerplug1/image_bk7231n_app.0x011000.rbl

To flash the firmware, you have to attach some wires to the UART of the chip. This requires disassembly of the power plug, but nothing too bad in my view. At least you can “just” overwrite the firmware, no locked firmware :)

The name lsc-powerplug1 is a defined string in the YAML file.

Writing the full UF2 firmware file resulted in an error due to the lack of flash space. You could upload the application partition at the 0x011000 offset, which worked!

The following script fetches the data:

#!/bin/bash

curl -sN http://10.42.46.200/events | \
while IFS= read -r line; do
    if [[ "$line" == *\"sensor-power\"* ]]; then
        json="${line#data: }"
        echo "$json" | jq '.state' | cut -d'"' -f2 | sed 's/ W//' | xargs -I{} rrdtool update power.rrd "$(date +%s):{}"

    fi
done

This is in no way reliable, but it’s a cool quick proof of concept.

Here is the data after I was first charging my phone and then my laptop:

rrdtool graph with my phone drawing a few watts and then my laptop around 45