Hi! Thank you for sharing your code. It is great! I am trying to time the ON time for my garden pump. I would create two timers - one would be reset every 24h and the other would never be reset.
Since I will not need the tic tac timer and the lap timer, I was trying to remove that part of the code. I am relatively new to this and I am not sure if I did it correctly. I actually do not even need any buttons since the start, pause and reset of the stopwatch would be handled by simple automations.
Does the below make sense? I am asking because when I start the timer, I do not see the status updating. It only shows me the current stopwatch time when I pause it.
input_boolean:
start_stopwatch:
# It triggers stopwatch to start/stop(pause)
name: Start/Stop Stopwatch
# initial: off
reset_stopwatch:
# It triggers stopwatch to reset
name: Reset
# initial: off
tictac_stopwatch:
template:
- trigger:
# Stopwatch sensor with Start, Stop/Pause, Reset and Lap features. Hundreds of second precission
- platform: state
entity_id: input_boolean.start_stopwatch
from: "off"
to: "on"
- platform: state
entity_id: input_boolean.start_stopwatch
from: "on"
to: "off"
- platform: state
entity_id: input_boolean.reset_stopwatch
from: "off"
to: "on"
sensor:
- name: "Stopwatch"
state: >-
{% if is_state('input_boolean.reset_stopwatch','on') %}
{{ '00:00:00' }}
{% elif is_state('input_boolean.start_stopwatch','off') and is_state('input_boolean.lap_stopwatch','off') %}
{% set value = as_timestamp(now()) - state_attr('sensor.stopwatch','initial_time') + state_attr('sensor.stopwatch','elapsed_time') %}
{{ value|float|timestamp_custom("%H:%M:%S", False) + '.' + ((value|float*100)%100)|round(0)|string }}
{% elif is_state_attr('sensor.stopwatch','running','on') %}
{% set value = as_timestamp(now()) - state_attr('sensor.stopwatch','initial_time') + state_attr('sensor.stopwatch','elapsed_time') %}
{{ value|float|timestamp_custom("%H:%M:%S", False) |string }}
{% else %}
{{ states('sensor.stopwatch') }}
{% endif %}
icon: mdi:timer
attributes:
initial_time: >-
{% if is_state('input_boolean.start_stopwatch', 'on') and is_state_attr('sensor.stopwatch','running','off') %}
{{ as_timestamp(now()) }}
{% else %}
{{ state_attr('sensor.stopwatch','initial_time') }}
{% endif %}
elapsed_time: >-
{% if is_state('input_boolean.reset_stopwatch','on') %}
{{ 0 }}
{% elif is_state('input_boolean.start_stopwatch','off') and is_state('input_boolean.lap_stopwatch','off') %}
{{ as_timestamp(now()) - state_attr('sensor.stopwatch','initial_time') + state_attr('sensor.stopwatch','elapsed_time') }}
{% else %}
{{ state_attr('sensor.stopwatch','elapsed_time') }}
{% endif %}
running: >-
{{ states('input_boolean.start_stopwatch') }}
laps: >-
{% if is_state('input_boolean.reset_stopwatch','on') %}
{{[]}}
{% elif is_state('input_boolean.lap_stopwatch','on') and is_state_attr('sensor.stopwatch','running','on') %}
{% set data = namespace(laps=state_attr('sensor.stopwatch','laps')) %}
{% set value = as_timestamp(now()) - state_attr('sensor.stopwatch','initial_time') + state_attr('sensor.stopwatch','elapsed_time') %}
{% set data.laps = (data.laps + [value|float|timestamp_custom("%H:%M:%S", False) + '.' + ((value|float*100)%100)|round(0)|string]) %}
{{ data.laps }}
{% else %}
{{ state_attr('sensor.stopwatch','laps')}}
{% endif %}
# Start/Stop(Pause) button
- button:
- unique_id: 'start_stop_stopwatch'
name: >-
{% if is_state('input_boolean.start_stopwatch','off') %}
{% if is_state('sensor.stopwatch','00:00:00') %}
Start
{% else %}
Resume
{% endif %}
{% else %}
Stop/Pause
{% endif %}
icon: >-
{% if states('input_boolean.start_stopwatch') == 'off' %}
mdi:play-circle-outline
{% else %}
mdi:stop-circle-outline
{% endif %}
press:
service: input_boolean.toggle
target:
entity_id: input_boolean.start_stopwatch
automation:
- id: reset_stopwatch
alias: "Reset Stopwatch"
description: "It reset input_booleans when input_boolean.reset_stopwatch is set to on"
trigger:
- platform: state
entity_id: input_boolean.reset_stopwatch
from: "off"
to: "on"
action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.start_stopwatch
- service: input_boolean.turn_off
target:
entity_id: input_boolean.tictac_stopwatch
- service: input_boolean.turn_off
target:
entity_id: input_boolean.reset_stopwatch
mode: single