BTHome Electricity Meter for Home Assistent

I wanted to track my electric usage in real-time, and I’ve already have Home Assistant as my main smart home management system, so I wanted to make use of the Energy tracking feature that it provides.

Because my meter is located outside our house, I had to find a solution that runs on batteries.

I was looking into the Frient electricity meter, but couldn’t find any available anywhere. So I searched and found this DIY smart meter

And to prolong the battery-life, I’m using 2 x D batteries instead of the small CR2032 battery.

UPDATE: After a couple of month using this. It became very unstable. It keept loosing connection to Home Assistant.
I ended up buying the Frient Electricity Meter Interface 2 – and this has been working perfectly.

What you need

  • Puck.js
  • LDR sensor (GL5537)
  • 2 x 15mm ring magnets
  • D battery holder
  • 2 x D batteries
  • wire / 3d-printer
  • optional: jst connector for the wire

3D printed casing

I started with 3d printing the casing for the LDR sensor and the Puck.JS

I divided it in 3 part, for easier print.
– Casing for the LDR sensor and Puck.JS
– Magnet holder
– Meter alignment-piece, to block out any outside ligth when attached to the meter


I started with removing the battery clip and the button from the Puck, for it to be more low-profile

– Put doublesided tape on one of the magnets, and attach it on the meters led.
– Solder the LDR and battery-connecter / wires together.
– Assemble the 3 3d printed pieces by glueing them together.

I added a red cap, so that the Puck’s led could shine throgh.


Get the code for the puck from github:

You need to adjust the imp variable to match your meters. It should be printed on your meter. Something like: 1000 imp/kWh

If your Home assistant is setup correctly, it will find the device automatically.

Uptime robot-robot

So.. 3 years with no posts .. wow..


My better half works with web sites and hosting. They rely on the service of UptimeRobot to notify them if any of their sites is down.

I though it would be fun, to create something physical, that could maybe blink or something.

Introducing.. The UptimeTimeRobot-robotstill working on the name 😉

When the “robot” is connected to wifi, it will just sit there and blink every once in a while.

But whenever a server is down, we get this.

At the top, there’s a button to pause the alert until next cycle, where it calls the uptime robot api.

Click: Pause the alert until next cycle.
5 sec. press: Reset wifi settings.
10 sec. press: Checks for new firmware OTA

The body is 3d printed, and the interior is
– 1 x 5110 lcd display
– 1 x wemos d1
– 1 x push button
– 2 x leds (red and blue)

And here’s how it’s wired together:

Source code can be found here:

Homebridge with Sonoff


I bought two of those cheap wifi remote controlled switches called Sonoff.
These switches allow you to turn them on and off with an app on your phone.

Inside these switches are a ESP8266 , which means that we can flash the chip with our own custom firmware.
And with the help of a Raspberry pi we can enable homekit.

Firstly I reflashed the sonoff with my custom firmware, using the Arduino IDE. (Google how to flash ESP8266)
(This disables the use of the official sonoff app)

Remember to change the hostname, if you have multiple sonoff.

#define HOSTNAME        "sonoff_00"

Raspberry Pi – Homebridge

Download and install Raspbian on the Raspberry Pi.

When this is done we can install Homebridge, which is the service that sits between your apple devices and the sonoff switch.

The full install guide can be found here:

TLDR; (Raspberry Pi 3)

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git make
sudo apt-get install g++
#install node
curl -sL | sudo -E bash -
sudo apt-get install -y nodejs
#install Avahi and other dependencies
sudo apt-get install libavahi-compat-libdnssd-dev


Next we need to install Script2, which is a homebridge plugin that allows you to run bash-scripts though the homebridge

npm install -g homebridge-script2

Start at bootup

Lastly we need to start Homebridge whenever the Raspberry pi boots up.
And the following method was found here:

We need to create the following two files.


# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/lib/homebridge


Description=Node.js HomeKit Server


#could be /usr/local/bin/homebridge - check where the homebridge folder exists

ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS


And to enable the newly created service. (first time only)

sudo systemctl daemon-reload
sudo systemctl enable homebridge
sudo systemctl start homebridge

We also need to create a new user and the folder for the config.json file

#create a new user
sudo useradd -M --system homebridge

#creates a new folder
sudo mkdir /var/lib/homebridge

#changes the owner of the folder to new homebridge-user
sudo chown homebridge:homebridge /var/lib/homebridge


To configure the script2, we need to create a config.json file in the /var/lib/homebridge folder.

Mine looks like this:

    "bridge": {
        "name": "Sonoff",
        "username": "AA:BB:CC:DD:EE:FF", (THIS NEEDS TO BE CHANGED)
        "port": 40500,

    "description": "Sonoff Homebridge",

    "accessories": [
                    "accessory": "Script2",
                    "name": "Bedroom lamp",
                    "on": "curl http://sonoff_00.local/on",
                    "off": "curl http://sonoff_00.local/off",
                    "state": "curl --max-time 1 http://sonoff_00.local",
                    "fileState": "",
                    "on_value": "on"
                    "accessory": "Script2",
                    "name": "Living room lamp",
                    "on": "curl http://sonoff_01.local/on",
                    "off": "curl http://sonoff_01.local/off",
                    "state": "curl --max-time 1 http://sonoff_01.local",
                    "fileState": "",
                    "on_value": "on"

    "platforms": []

Restart the homebridge service

sudo systemctl restart homebridge

Now you should be able to add the sonoff homebridge to your Home-app on the iphone/ipad, and control the switches.

GIF frame – IOS app

My first idea was to add a ESP8266 to the GIF frame, so that I could control it remotely. But I haven’t had much success when tinkering the it. I can not get it to run a webserver without it freezing up.

But I had a Bluetooth Low Energy (BLE) laying around – Yes, I have a lot a stuff laying around 🙂
I had bought it a while ago, but didn’t really got into code in Cocoa Touch for iOS- also I didn’t want to pay 100$ for the developer subscription – which was needed for you to get the app on the iPhone.

But with the latest release of xcode, you can install your own apps on your iPhone without the subscription! So everything kinda came together for this project 🙂

The BLE device communicates with a serial port, and can talk directly to the Arduino.
I just need some small adjustments to the menu code – adding software serial. (and I included a power on/off option, while I was at it)

Download: GIF frame

And here it is:

GIF frame – software

The software for the animated gif frame, consist of 3 parts.

  1. code for the Arduino, that reads the sd-card and displays it on the ledstrips (main board)
  2. code for the Arduino, that runs the LCD menu (menu)
  3. code on the PC, which converts a GIF into a text file, with the color codes of the GIF.

Arduino sketch

The two Arduinos communicates via the serial ports.
At startup the main board, request the menu for the current settings. (it does this by sending a request_signal)

Serial.write(REQUEST_DATA);       // request for new data    - value is 155

The menu, then sends the settings in the following format

Serial.write(START_SIGNAL);       // 111
Serial.write(loadNext ? 1 : 0);   // next                    - values 0 or 1
Serial.write(menu.mode);          // mode (single/slideshow) - values 0 or 1
Serial.write(menu.repeat);        // slideshow duration      - values 1 to 50
Serial.write(menu.speed);         // speed                   - values 10 to 100
Serial.write(menu.brightness);    // brightness              - values 10 to 60
Serial.write(END_SIGNAL);         // 222

Whenever a change in the menu happens, it will also send this byte array to the main board.
The menu also,saves the changes to EPROM, so that the settings can be restored when power off and on.

Download the sketch + xcode here: GIF frame

PC software

On the PC side, I used this Processing sketch, but I think i really is just Java. Kinda like Arduino sketch is C/C++.

I modified the original code, so I only outputs the color codes, and not the Arduino sketch code.
Here is my version: GIF_Converter.pde

16×16 animated gif frame

I once saw a Kickstarter project, where they made a frame with a animated gif.
So I wanted to try that out.

My main concern, was how a Arduino would read a GIF file, and convert it into a color code that the ledstrip could understand.
Luckily I found this site, which helped me with converting a GIF into a text file containing the colorcodes.

I customized the code to fit my needs, and coded the Arduino from scratch – actually I coded it twice, as I accidentally delete me sketch during development…
(now I have Carbon Copy Cloner, which backups to my network drive!)

And here is the end result:


Heres a list of the component used for this build – all found and bought through eBay.

  • A square frame (mines from IKEA)
  • RGB LED strip (256 leds)
  • 2 x Arduino
    • One for the running the ledstrip
    • One for the lcd menu system
  • SD card reader (and a SD card)
  • Nokia 5110 lcd
  • Power regulator to turn 12V to 5V and 5V to 3.3V (for the lcd)
  • Laptop 12v power supply.

During developing stage, I used an Arduino Duemilanove I already had. But for the finished product, I made “DIY” Arduino from a guide I found online. On afterthought, maybe I could’ve bought a cheap knockoff online, for less money… but where the fun in that 😉

At first I try using only one Arduino, but I ran out of memory, when I added the code for the lcd. It used a lot of memory to store all the characters.
So instead, I’m using two Arduino. And they are communicating via the serial port.

As a side effect of having two Arduino, is that the menu system is quick and responsive, as it does not need to wait for the ledstrip to be processed.

My next plan is to add a ESP8266, so that I can upload a new image file remotely.
But so far I havn’t had any succes using the ESP8266 as a webserver. I can’t get it to run without it freezing.

More about the software later…

  • The LED strip is glued to a piece of cardboard

Raspberry Pi RF remote

So I have one of these cheap remote controlled outlets, and I just finished my IR remote control project with the Raspberry Pi, so why not try to control these with the Raspberry Pi also?
Remote Control Outlet

How does it work?

The way these outlets work, is you set some dip-switches on the remote, and on the outlets so they match.

RF RemoteRF outlet

Creating RF transmitter / receiver


I bought my RF transmitter and receiver from eBay a about 1$.
Connecting RF transmitter and receiver to the Raspberry Pi is pretty simple.
Both the reciver and transmitter has a VCC, GND and DATA pins.
And all you do is connect it to the Raspberry Pi GPIO pins.

Install RPi_utils


Start by installing RPI_utils

sudo apt-get update

#Installs GIT
sudo apt-get install git
sudo apt-get install git-core

#Installs 433util
git clone git://
cd 433Utils/RPi_utils/

#Installs wiringPI
git clone git://
cd wiringPi
git pull origin

#Builds RPI_util
cd ..
make all

Testing and decoding RF signal

To capture and decode the RF signal from the remote, I ran the program RFSniffer.

sudo ./RFSniffer

When I push the A (on) button on the remote, I get the following code on the screen.

Received 5260625
Received 5260625
Received 5260625
Received 5260625
Received 5260625

To test if the transmitter works you can send the received value with this command.

sudo ./codesend 5260625

If you convert 5260625 to binary, you get 10100000100010101010001.
And looking at the binary value of all the buttons, you start to see a pattern.

Button Decimal value Binary value
A (On) 5260625 10100000100010101010001
A (Off) 5260628 10100000100010101010100
B (On) 5263697 10100000101000101010001
B (Off) 5263700 10100000101000101010100
C (On) 5264465 10100000101010001010001
C (Off) 5264468 10100000101010001010100
D (On) 5264657 10100000101010100010001
D (Off) 5264660 10100000101010100010100

They all starts with 1010000010, and all the ON buttons ends with 0001 and all the OFF buttons ends with 0100.
The 1010000010 matches with the dip-switches; 10 = down and 01 = up.
The middle part, is a value that identifies A,B,C and D.
You can see the pattern here:

Dip switch Button Id On / Off
A = 10 10 00 00 10 + 00 10 10 10 1 + 0001 / 0100
B = 10 10 00 00 10 + 10 00 10 10 1 + 0001 / 0100
C = 10 10 00 00 10 + 10 10 00 10 1 + 0001 / 0100
D = 10 10 00 00 10 + 10 10 10 00 1 + 0001 / 0100


I then created the following bash script.


BTN=$(echo $BTN | tr '[:lower:]' '[:upper:]')
STATUS=$(echo $STATUS | tr '[:lower:]' '[:upper:]')

DIP_SWITCH="DDUUD"  #Change dip switches to match the remote

#hardcoded values

DIP_SWITCH=$(echo $DIP_SWITCH | sed 's/D/10/g' | sed 's/U/00/g')

case $BTN in
    A )
        BTN=$BTN_A ;;
    B )
        BTN=$BTN_B ;;
    C )
        BTN=$BTN_C ;;
    D )
        BTN=$BTN_D ;;
    * )
        echo "Please define the button [A-D]";exit;
case $STATUS in
    ON )
        STATUS="0001" ;;
    OFF )
        STATUS="0100" ;;
    * )
        echo "Please define the button state [ON/OFF]";exit;


#echo $DEC
sudo /home/pi/codesend $DEC

And to emulate a click, I just call

./ A ON

Control via web

Because the codesend uses wiringpi, it is required to call with sudo.
But if you need to call it from a webserver, it creates some problem, because sudo requires a password.

So we need to edit visudo, so it doesn’t require a password.
(it may not be a secure thing to do, but in my case the webserver is not exposed to the internet, and is only for “at home” use)

sudo visudo

Add the following line to the file.
#NOTE: use TAB and not space, except for the last one – after NOPASSWD:

www-data        ALL=(root) NOPASSWD: /home/pi/433Utils/RPi_utils/codesend

Save the file and reboot the Raspberry Pi, and you’re good to go.

Now the local PHP webserver can call the script with:

  shell_exec("/home/pi/433Utils/RPi_utils/ A ON");

Raspberry Pi IR remote

I wanted to use the Raspberry Pi as a remote control for my TV, so I searched the internet and found a pretty good guide by Alex Bain.
So all credits goes to him!

I then created a TV-guide webapp, where I could click on a channel to switch to that channel on the TV.
It’s webbased but I haven’t exposed it to the internet, so it only works when I’m home 🙂

Images of the final assembly

(Ignore the circuit connected with the green and red wire, its a RF tranmitter, which I will post about later)

Video demo

Installing the distro

I had a old spare 1GB SD card laying around, so first thing I did was install Moebius which is a very small Raspberry Pi distro, that fits the 1GB nicely 🙂
I don’t know what class the card is, but I don’t think it really matter, as performance is not a issue here.

To install the image to a SD-card, I followed the guide from RPi Easy SD Card Setup. (I used the program Pi-Writer.)

Creating IR transmitter

I made the electronic from Alex’ schematic, and soldered the components on a stripboard.
I bought my parts from my local electronics store:
2 x IR-LED
2 x LED holder
1 x IR receiver
1 x NPN transistor
1 x 10 kΩ resistor
1 x 2×13 pin connector
1 x stripboard

Installing LIRC

Follow the guide here.

Installing a web server

Just for good measure reboot the Raspberry Pi

sudo reboot

When it’s back up and running, we now install the webserver and PHP

sudo apt-get -y install lighttpd
sudo apt-get -y install php5-common php5-cgi php5

#enable fastcgi module included with lighttpd
sudo lighttpd-enable-mod fastcgi fastcgi-php

#The following restarts the webserver
sudo service lighttpd force-reload

Now we need to setup some permissions for the webserver.

#change the owner of /var/www to www-data (user and group)
sudo chown www-data:www-data /var/www

#gives user and group read/write/execute rights to /var/www
sudo chmod 775 /var/www

#adds the user pi to the group www-data
sudo usermod -a -G www-data pi