Logging a Time When Pressing a Button

I’m not exactly sure where to start on this project, but here’s what I’d like to be able to do:

  • Use a button in lovelace to log the current time and display it in a sensor next to the button.
  • Press that button again and measure the time between button presses and display that interval next to the button and current time depressed sensor.
  • Show an average interval time and a max and min interval time.
  • A reset button to clear everything.

Have the current displayed information be persistent.

Any geniuses out there with a few ideas?

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:

{ now().timestamp() - states.script.smoker_status.last_updated.timestamp() }

For current date/time…set up a sensor in your configuration.yaml:

sensor:
       - platform: time_date
         display_options:
         - 'time'

and then sensor.time has the current date/time.

You’re on your own on averages

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.

type: custom:mushroom-entity-card
entity: script.time_tracker
tap_action:
  action: more-info
primary_info: name
name: Baby Tylenol
icon: mdi:pill
secondary_info: last-changed
hold_action:
  action: call-service
  service: script.time_tracker
  target: {}

2 Likes

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 :frowning:

Can you share the script? I want to do the same thing but for bottles

Sure, here it is:

The script in my system looks like this in the “scripts.yaml” file:

script_name:
sequence:

That’s it. Nothing else.