If you live by yourself, it's really easy to set up a sensor to turn a light on and off when you leave a room. But it's a problem if there you have a guest still sitting in the room. Instructables user RPisces came up with a solution that does a simple head count.
When you walk into a room with Pisces' sensor, his system tallies up how many people are inside and only turns off the lights once everyone has left. To accomplish this feat, Pisces built a circuit, attached a couple of IR distance sensors, a relay circuit, and then ran everything through some code. It's not the easiest setup in the world, but the Instructable is incredibly detailed on every step of the process.
Room Visitor Counter with Relay Control [Instructables]