There are many ways to do the above…but these give you an idea.
Every state change has a time attribute…that includes automations and scripts. One way to check the time between button pushes would be to have the button state change automation action run a script. Both have a time stamp. The next time the automation runs, your time stamp for now - the timestamp of the last time you called the script file is the elapsed time:
I actually accomplished this by using NodeRED. I created a blank script, then created an event watcher that watched for that script to be called. When that happens, it routes to a node that timestamps a sensor registered in HA, marking UNIX time. I also capture the previous recorded time and store it to the new sensor as an attribute previous.
Then, I created an alternate sensor to do some math on the values of the first sensor to get the time since last expressed in seconds.
From there, I can use the historical sensor component or Grafana (theoretically) to create any calculations.
My apologies for necroing this old thread, but I couldn’t find a simple solution to this question posted anywhere, so I wanted to add my solution:
I created a script with the name I wanted (I’ll just refer to it as script.time_tracker), and then I used a Mushroom entity card on the dashboard. The script does nothing, it has no actions.
Here’s the code for the card, it works PERFECTLY. Tells me how long it’s been since the last time someone pushed that button.
The purpose of this was so my wife can easily keep track of when the last time our son was given tylenol.
This works excellent. I was looking for a “count up” timer, and have racked my brain trying to figure out how to create one - everything I tried didn’t work properly - often the counters would stop when Home Assistant was restarted. This workaround works perfectly, thanks for “necroing”!
EDIT: This method resets whenever home assistant restarts