Or simply use the command xprintidle
which returns the idle time in milliseconds.
It has been packaged for debian based systems. (the source is not available any more on the original site dtek.chalmers.se/~henoch but you can get it at packages.ubuntu.com)
more info on freshmeat.net