So what I think is happening
In your final selection set, sel2, you are cleaning up the drawing a bit and then deleting items. If this selection set doesn't exist (ie. it didn't find anything), then the erase is erasing nil I think something to this effect:
(command _erase "" "")
Nil acting like an enter / space, the second "" is again like another enter / space which is repeat the last command line input "HPO" - and since HPO is a LISP and not a command, HPO is an unknown command.
If you do something to fix that it should work, something like this at the end:
(if (= sel2 nil)
() ; if sel2 is a 'nil' selection, do nothing
(command "_erase" sel2 "") ; if sel2 is a selection, then erase it
)