How to measure time of energy sensor

How to write the yaml for template sensor to measure time (like stopwatch) when power of energy sensor is >0kWh?

here you have an example: