I am not sure if you are familiar with CoRE or webCoRE but this is easy to do with either of those once you understand how to use them.
Here is a webCoRE piston that controls my TV. Based on a power range I can tell if it is idle and I turn it off.
webCoRE is still in beta, be warned.