Stopwatch with start/stop/resume, lap and reset

You have to include all the tictac stuff, which is in charge of updating the status.

So, you should keep:

  • input_boolean.tictac_stopwatch including the name TicTac.
  • Trigger wtih input_boolean.tictac_stopwatch in sensor.Stopwatch.
  • Complete code of sensor.tictac_stopwatch.
  • Complete automation.tic_tac_stopwatch.

I hope this helps.

Thank you for your reply. I realised that you mentioned this in the description or in a reply to someone a bit too late - sorry!

Very good, this is exactly what I was looking for!
The code is easy to understand and new instances of the StopWatch can be created with one click using “Search and Replace”.
Cool implementation, thanks!

1 Like

Great to hear your feedback.

Thank you for trying!

Hi! I am trying to create a second stopwatch. I followed your instructions and almost everything else. What does not work is just the visual part of when you toggle the start/stop button.

The code refers to the button.start entity. When is this one created? I need a separate one for the second timer. When I use the “input_boolean.start_stopwatch2” instead, the button works but the icon toggling does not. I suppose this is a very basic question but I do not know how to solve this.

So I am wondering when and how the button.start entity is created, why this one is used even though it is not referred to in the stopwatch.yaml and for resetting the code refers to “input_boolean.reset_stopwatch” and how I can create a new one for the stopwatch2, stopwatch3 and so on. Do I need to add a helper and an automation manually or is there an easier way to do it when creating a new stopwatch?

I would really appreciate your help. Thank you!

Hello,

I’m looking at the entities created and you’re right, I create a button with unique_id start_stop_stopwatch, but the name of the button afterwards is button.start which is one of the names given to the button depending it’s state, but afterwards changing the name really changes its friendly_name, but not its name.

I had to assign a unique_id for the button to work, as you can see in this thread:
https://community.home-assistant.io/t/template-button/443821/6

Can you try to change the name of the button of the second stopwatch and test if this works? (Now I cannot test it).

Try something like this:

 - button:
    - unique_id: 'start_stop_stopwatch2'
      name: >-
        {% if is_state('input_boolean.start_stopwatch2','off') %}
          {% if is_state('sensor.stopwatch2','00:00:00') %}
            Start2
          {% else %}
            Resume2
          {% endif %}
        {% else %}
          Stop/Pause2
        {% endif %}
      icon: >-
        {% if states('input_boolean.start_stopwatch2') == 'off' %}
          mdi:play-circle-outline
        {% else %}
          mdi:stop-circle-outline
        {% endif %}
      press:
        service: input_boolean.toggle
        target:
          entity_id: input_boolean.start_stopwatch2

Or maybe you can remove the name part of the button code and check the name given to the new button.

I hope this helps.

I’ve asked the question about template buttons in Configuration:

https://community.home-assistant.io/t/template-button-name/586900

Hi, thank you for your reply!

I changed the name of the button from the first time I started as you suggested above. I changed all the entity names to stopwatch2 at the end.
It is a bit strange because I can see a button.resume entity, not button.start as you wrote. At the same time, when I wrote my previous post, I am pretty sure that I could see a button.start entity since that’s why I wrote. What is stranger is that there is no button.start_stop_stopwatch2 or as a matter of fact, there are no other button entities related to the stopwatch at all. Just one.

In the code for the card, in the section for the button I have:

    cards:
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: toggle
        entity: input_boolean.start_stopwatch2
        name: Start/Stop daily stopwatch
        show_state: false

As I wrote before, the functionality is there after I changed the code to: input_boolean.start_stopwatch2 but the icon does not change with the state. Or to be precise it does but from mdi:stop-circle-outline to a circled tick icon or however I should call it, instead of changing from mdi:play-circle-outline to mdi:stop-circle-outline.
The only difference between the stopwatch and stopwatch2 code for the card is that the original one refers to button.start while the second one refers to input_boolean.start_stopwatch2.
When I change the button.start or now in my case button.resume to input_boolean.start_stopwatch (without the “2”) the first stopwatch also switches from the correct icons to the wrong ones.

Same behaviour here…

What I don’t know is whether it’s due to any change in latest versions of HA…

Let’s wait for an answer in the other thread.

Hello krkr,

I’ve created a new card using Mushroom integration (you can install it through HACS). You won’t need the button section of the stopwatch code, so you can safely remove and create several stopwatches without problems (I hope).

Here it is the card code:

type: vertical-stack
cards:
  - type: entity
    entity: sensor.stopwatch
  - type: horizontal-stack
    cards:
      - type: custom:mushroom-template-card
        primary: |-
          {% if is_state('input_boolean.start_stopwatch','off') %}
            {% if is_state('sensor.stopwatch','00:00:00') %}
              Start
            {% else %}
              Resume
            {% endif %}
          {% else %}
            Pause/Stop
          {% endif %}
        secondary: ''
        icon: |-
          {% if is_state('input_boolean.start_stopwatch','off') %}
            {% if is_state('sensor.stopwatch','00:00:00') %}
              mdi:play-circle-outline
            {% else %}
              mdi:pause-circle-outline
            {% endif %}
          {% else %}
            mdi:stop-circle-outline
          {% endif %}
        entity: input_boolean.start_stopwatch
        icon_color: |
          {% if is_state('input_boolean.start_stopwatch','off') %}
            {% if is_state('sensor.stopwatch','00:00:00') %}
              green
            {% else %}
              orange
            {% endif %}
          {% else %}
              red
          {% endif %}
        hold_action:
          action: none
        double_tap_action:
          action: none
      - type: custom:mushroom-template-card
        primary: Lap
        secondary: ''
        icon: mdi:camera-outline
        entity: input_boolean.lap_stopwatch
        icon_color: |-
          {% if is_state('input_boolean.start_stopwatch','off') %}
            grey
          {% else %}
            blue
          {% endif %}
        hold_action:
          action: none
        double_tap_action:
          action: none
      - type: custom:mushroom-template-card
        primary: Reset
        secondary: ''
        icon: mdi:close-circle-outline
        entity: input_boolean.reset_stopwatch
        icon_color: |-
          {% if is_state('input_boolean.start_stopwatch','off') %}
            {% if is_state('sensor.stopwatch','00:00:00') %}
              grey
            {% else %}
              red
            {% endif %}
          {% else %}
            red
          {% endif %}
        hold_action:
          action: none
        double_tap_action:
          action: none
  - type: horizontal-stack
    cards:
      - type: markdown
        content: |-
          **Laps:**
          {% for i in range(state_attr('sensor.stopwatch','laps')|length) %}
            {{ (i+1)|string + ': ' + state_attr('sensor.stopwatch','laps')[i]}}
          {% endfor %}
      - type: gauge
        entity: sensor.template_tictac_stopwatch
        min: 0
        max: 1
        needle: true
1 Like

Hi! I am sorry for such a late reply. I just tested it and it works perfectly. Thank you so much! :slight_smile:

1 Like

That’s great!

Hi @miguelpucela,
hope you might be able to help me.
when i start the stop watch via input_boolean.start_stopwatch the tic tac starts. but the stop watch only moves from Unknown to Unavailable?

if i template

{{states('sensor.stopwatch')}}

all i get is unavailable. not any number what so ever. perhaps i have made a mistake in setup?

your code was pasted under a package like this:

homeassistant:
  packages:
    stop_watch:
      input_boolean:
        start_stopwatch:
          # It triggers stopwatch to start/stop(pause)
          name: Start/Stop Stopwatch
        #    initial: off
        reset_stopwatch:
          # It triggers stopwatch to reset
....................................

etc.

any chance you could help me?

Have you tried to press “Reset” button? It should reset its state to “00:00:00”, as is stated here:

    sensor:
      - name: "Stopwatch"
        state: >-
          {% if is_state('input_boolean.reset_stopwatch','on') %}
              {{ '00:00:00' }}
...

No i did not. And that of course worked…

Thank you so much! :slight_smile:

Great to hear that!

I’m adding it to the initial post.

Thanks very much for this. I’ve been looking for a way to create a job-timer thing with a physical button and whilst I can sort the electronic and Tasmota side out, I was struggling with how to do it in HA. This does the trick and I shall have fun integrating it. - All the best! :slight_smile:

1 Like

Great to read this!
Thank you!

hello, and thank you for this work. I was looking for a timer to start work, be able to take breaks and finally stop. the goal was to count the working time over the day…
I’m a little new to HA and I would like to be able to switch the daily data into influxdb (I already have an HA database in there with consumption reports for example). And also know if once transferred to influx the HA database can be cleaned? in any case, the timer and what I was looking for!

I cannot help you here. I’m not expert in dababase and I don’t know anything about influxdb. I don’t know if anybody else can help you, or you can try creating a new topic about your question in configuration thread, as this is not directly related with the stopwatch, but with database management.