Simple stopwatch?

Hi everybody,

do any of you have a simple stopwatch script that I could borrow? I’d like to

  • start keeping track of time once <state change from/to> happens
  • stop tracking once happens
  • have the amount of time that has passed in an input_datetime entity (or similar)

This way, I could display the amount of time our vacuum took for the last cleaning cycle in Home Assistant. My current work around is

  • input_datetime.llh_sheena_starttime
  • input_datetime.llh_sheena_stoptime

Each of these entities gets the current timestamp written to when the device starts/stops. I am currently trying to create a template sensor that will display the time difference input_datetime.llh_sheena_stoptime - input_datetime.llh_sheena_starttime (but haven’t quite figured it out yet).

But perhaps there is a simpler way that I just have not found yet…?

Thank you in advance for your ideas :slight_smile:

UPDATE: the time difference could be determined like this, but I still think it is a bit too complicated {{ ((state_attr('input_datetime.llh_sheena_stoptime', 'timestamp')) - (state_attr('input_datetime.llh_sheena_starttime', 'timestamp'))) | timestamp_custom("%H:%M:%S", True) }}

Your “current work around” with input_datetimes is probably the best way, with trigger-based template sensors as an equally-valid alternative.

Your template is also fine, although I think you want False in timestamp_custom to avoid adjusting the absolute result to your timezone. Looks to me like you’ve solved your own problem.

Although your question is old, maybe you can find this useful: