#Stopwatch Timer

1 messages · Page 1 of 1 (latest)

timid wave
#

I have automations that automatically turn certain devices on and off during the day and it is important for me to know how long they have been on. This would normally be a typical stopwatch type application. Start the stopwatch when it turns on and paue it when it turns off.

I know HA doesn't have a stopwatch so I thought a very easy way around this would be setting a timer of 24:00:00 and then turning this timer on and off during the day. To see how long the item was on, simply sutract the remaining time on the timer from 24:00:00 and you can see how long the item was on. To do this, I thought I would use a script which is triggered every second to do the subtractions ... except I can't figure out how to actually do the calculation.

I was thinking something like this:

timers:
mine_max is set to 24:00:00 as duration.
mineml_on_time_today is the timer I keep turning on and off.

  • service: input_datetime.set_datetime
    metadata: {}
    data:
    time: "{{ state_attr('timer.mine_max', 'duration') } - { state_attr('timer.mineml_on_timer', 'remaining') }}"
    target:
    entity_id: input_datetime.mineml_on_time_today

Any suggestions?

tight windBOT
fierce pelican
#

See if this works for your usecase. I don't think you really want a script running every second.

jagged rain
#

If you are dead set on doing this outside of the history_stats integration, your best bet would be a trigger-based template sensor that is triggered off the state change of the device you are tracking, and uses its state to keep the total of the time on. You’ll also need a second trigger at "00:00" to reset it daily. And you’ll want to consider what to do when the sensor is unknown/unavailable.

timid wave
timid wave
jagged rain
# timid wave Agreed ... Again comes down to adding input_datetime stored values.

With karwost’s suggestion you don’t need a script at all, nor do you need any input_datetime values. Did you read the linked documentation? Copy the 7 lines of yaml code from the example and replace the sensor entity_id with yours and you’re done.

My suggestion is for more advanced use cases and I’d avoid it unless there’s some compelling reason that history_stats don’t work for you. You don’t need input_datetime with mine either; you store everything you need in the sensor’s state or attributes.

timid wave
#

Yes, I am working throug the options. Thank you @jagged rain.