Wifi smart plugs

4 replies [Last post]
Ayr, United Kingdom
Joined: 25 Sep 2011

I don't know if anyone else has tried these but I recently obtained a couple of wifi smart plugs from amazon:


There are quite a few around using various brand names and based on the ESP8266 wifi chip I think. While the RF plugs that I use are generally pretty reliable these give another option and it would seem could be integrated with the HAH using python code from here:


I've just used a lua app to send data to my raspberry pi which is on the network anyway running the fauxmo.py script for amazon echo integration. There's a bit of an issue regarding having the app open on your phone because the devices can only have one connection at a time but it seems to work on an experimental basis so far.

Just something else which could help to keep this excellent system alive.

MAIDSTONE, United Kingdom
Joined: 8 Jul 2013
Would be a great addition to the HAH

Allan I have just purchased a wifi smart plug and am quite impressed with its speed to respond "Alexa" commands.

I wish I had the skills to write the software for the Home Automation Hub and incorporate/integrate these devices into the HAH platform.

Ayr, United Kingdom
Joined: 25 Sep 2011
Software to integrate

I'm not sure that my software skills are quite up to it either!

You need to install the python-tuya stuff from here:


and also the xaplib.py stuff from here:


You're welcome to have a go with what I have so far.

I've abandoned the lua script and just run this python script on my raspi on the same network as my livebox.


from subprocess import *
import pytuya
import socket
import sys
import os
from xaplib import Xap
plugID = ['00','Xxxxx' ,'Xxxxx' ,'Xxxxx' ]
plugIP = ['00', 'Xxxxx', 'Xxxxx', 'Xxxxx']
plugKey = ['00','Xxxxx' ,'Xxxxx' ,'Xxxxx' ]
def hubTraffic(xap):
    xapmessage, port = xap.receive()
    parsed = xapmessage.split('\n')
    if parsed[5] == 'class=xAPBSC.cmd':
        for _ in range(3):

                      if parsed[6].find('tuya') > 0:
                        state = parsed[12][-2:]
                        if state == 'on':
                           state = ' 1'
                           state = ' 0'
                        tgt = parsed[6][-1:]
                        tgt = tgt+state
            except socket.error, msg:
                        print('Socket Error')
                        exitcode = Popen('bscmsg -t dbzoo.livebox.twitter -x Tuya_plug_failure -s on',shell=True, stdout=PIPE).stdout

def switchDevice(patt):
    data = patt
    t = int(data[:1])
    d = pytuya.OutletDevice(plugID[t], plugIP[t], plugKey[t])
    mess = 'Switching plug '+data[:1]
    if int(data[-1:]) == 1:
        ndata = d.set_status(True) # This requires a valid key
        ndata = d.set_status(False) # This requires a valid key


Obviously you need to put in your plugID, plugIP and plugKey in place of the Xxxxx fields. (leave the 00's in place at index 0)


With this script running I issus a "bscmsg -t dbzoo.livebox.tuya:plug.(1|2|3) -s (on|off)"

I haven't created these as bsc endpoints (although I suppose I could) I just rely on the script to pick up the xap message and react.

It switches the plugs pretty instantaneously when it works. As I said in my original posting there is a reliability issue with devices only able to maintain one connection at a time so if I am issuing frequent switching commands or if someone opens the app the script breaks :-(


So feel free to have a go and let me know how you get on.

Ayr, United Kingdom
Joined: 25 Sep 2011
here's a revised version

of the script. It's been running on a spare raspi for about a week now without breaking so it seems more stable and useable than the last one.

It attempts to reconnect if it fails to send the signal.

tuyacon.txt 2.3 KB
MAIDSTONE, United Kingdom
Joined: 8 Jul 2013
Many Thanks

Hi Allan many thanks for your instructions.

Hardware Info