After looking some more I just realized that your healthpotion variable is incorrect. you need your pet to find the item, not your hero.
healthpotion = pet.findNearestItem()
not
healthpotion = hero.findNearestItem()
Also, I believe Hellenar has the right idea. Try putting:
pet.on("spawn",onSpawn)
after your onSpawn function if the fix I gave you above does not work.