As a first project, I’m monitoring the electricity consumption of my boiler. For this, I’m using Broadlink SP3s wifi Power plugs (the ‘s’ series have power monitor). They come with a simple app for both Android and iOS, which I used for the wifi config of the plugs. I’ll be using Zabbix to monitor the outlets.
Configuring the wifi on the power plugs
First we’ll configure the Broadlink power plug to connect to the wifi. The app is pretty self explanatory; swipe right to show the menu and tap ‘Add device’. Next fill in your wifi SSID and password (mine is called ‘localhost’) and tap ‘configure’.
That should pretty much do it. Now you should be able to control it through the app:
That’s the easy (and boring) part. Next up, getting the energy usage into Zabbix. I already have a working zabbix server, so I won’t go over the installation.
Connecting to the power plug form the server
First we’ll get the server to communicate with plug. For this, we need the python broadlink module from github.
apt-get -y install python-dev
apt -y install python-pip
apt-get -y install git
git clone https://github.com/mjg59/python-broadlink.git
cd python-broadlink/
python -m pip install pycrypto
python -m pip install netaddr
wget https://bootstrap.pypa.io/get-pip.py
python setup.py install
If both the zabbix server and the power plug are on the same subnet, you should now be able to discover it:
./broadlink_discover
Which should return something like this:
We’ll need to make note of the type, host and mac address. A very important note: the mac address is inversed. The actual mac address as shown in my ARP tables is 34:ea:34:75:b8:25.
With this information, we can query it:
As we can see here, my power plug is currently not consuming any power.
So far so good, let’s automate it
For the sake of simplicity, I created a DHCP reservation for the power plug, and wrote the following bash script to query the plug:
#!/bin/bash
mac=$1
ip=$2
command=$3
val=$(/root/python-broadlink/cli/broadlink_cli –type 0x947a –host $ip –mac $mac –$command)
echo ${val%.*} # returns an integer instead of a string
To run it:
bash broadlink.sh 25b87534ea34 192.168.6.67 energy
which in this case would return ‘0’.
Configuring the zabbix agent
To enable zabbix to run this script we need to set up a userparameter.
nano /etc/zabbix/zabbix_agentd.d/userparameter_broadlink.conf
add the following line and restart the zabbix agent
UserParameter=broadlink[*], /bin/bash /root/broadlink.sh $1 $2 $3
This creates the key ‘broadlink’, which accepts variables being sent from the zabbix server (the ‘[*]’), and when called, will execute ‘/bin/bash /root/broadlink.sh’ with the three variables it receives from the server.
Configuring the zabbix server
Create a new item for the client. Set the key to the one we created in our config file: broadlink[mac,ip,command], in this case that would be
broadlink[25b87534ea34,192.168.6.67,energy]
Click on ‘update’ and give it a minute. If we go to ‘monitoring’ > ‘latest data’ and select the client, we can show a graph.
Dear sir, I tried to follow your different steps but when I try to connect on my device I face authentication failed. Do you know if there is any security enabled now on the BroadLink Smart Plug SP4L-EU ? Thank you for your support
Kind Regards
Sebastien
Hi Sebastien,
Unfortunately, it looks like the SP4L-EU does not have the energy monitoring functionality.
The steps above will not work for this type Broadlink plug.
kind regards,
vanderheyde