Display total usage of a device between one on and off period

My goal is to display total usage of a device between one on and off period - basically need to know for how long this device was switched on. I’am wathing the device IP, if ping returns it’s on.

I found this topics, similar problem with mine, but I could not adapt the code, would somebody help me?

Thanks!