Start a timer with a given number of minutes / dynamic duration

Hi!

I’am trying to create a timer by adding a number input with a slider to ajust the minutes and a button to start the countdown. The number input is typ float and therefore i get 22.0 (e g.) instead of 22. So i have to convert the input to int. This is my approch, which does not work:

The timer works fine when i hardcode the duration. What do i missunderstand?

- type: custom:mod-card
  style: |
    ha-card {
      margin-left: 7px;
      margin-right: 7px;
    }
  card:
    type: vertical-stack
    cards:
      - type: markdown
        title: Music Timer
        content: Test
      - type: button
        name: Start Stop Timer
        icon: mdi:power
        show_name: false
        show_icon: true
        show_state: true
        tap_action: 
          action: call-service
          service: timer.start
          service_data: 
            entity_id: timer.musik_timer

            # Conversation to String "00:30:00" fails:
            duration: "00:{{ (states.input_number.timer_minutes.state | int ) }}:00"

            #  hardcoding works:
            # duration: "00:30:00" 

      - type: entities
        entities:
          - entity: input_number.timer_minutes
            name: Countdown in Minuten
          - entity: timer.musik_timer
            name: Timer

I am using this approach for my dynamic timer. From the input_number in minutes, convert it to seconds and using the timestamp expression to convert it to the duration time format. See if you can use the same approach to get yours to work.

              - service: timer.start
                data_template:
                  entity_id: timer.vann
                  duration: >
                    {% set ctime = states.input_number.vann_timer.state %}
                    {% set ctime = ctime|float*60 %}
                    {{ ( ctime) | timestamp_custom('%H:%M:%S', false) }}
2 Likes

Thank you for your help!

Tested that right away but i get the message:

Fehler beim Aufrufen des Diensts timer/start. offset {% set ctime = states.input_number.timer_minutes.state %} {% set ctime = ctime|float*60 %} {{ ( ctime) | timestamp_custom('%H:%M:%S', false) }} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['duration']

I will do further tests with that code part, if you or someone here around has another hint: any help will be appreciated!

This works. You can set the duration in seconds so you don’t need to format HH:MM:SS. If your input_number represents minutes, you just have to multiply it by 60

service: timer.start
data:
  entity_id: timer.musik_timer
  duration: "{{states('input_number.timer_minutes')|int * 60}}"
3 Likes

Thats driving me nuts.

I get the exact same error message with your example. It feels like the template is not rendered / intepreted and it hands only the complete string over to the service and not the calculated result…

You cant use templates in the core version of Lovelace.

baunan’s and obaldius’ examples work because they are calling the service with a template in the backend (an automation or script), not the frontend (Lovelace).

You have two options:

  1. Put your service call and template in a script. Call the script from Lovelace. Or
  2. use a custom Lovelace template card. This uses JavaScript templates, not jinja.
2 Likes

Ahhh! This is the root cause and explains why my approch also failed to work. I will double check this.

Thank you for this background information!

Could not test earlier:

I’ve now created

Input number entity

type: entity
entity: input_number.sz_musik_dauer

Timer Entity

type: entity
entity: timer.sz_musik_timer
name: Countdown
attribute: remaining

start button

type: button
tap_action:
  action: call-service
  service: script.sz_start_music_countdown
  service_data: {}
  target: {}
icon: 'mdi:power'
name: Start

Script

service: timer.start
data:
  duration: '{{states(''input_number.sz_musik_dauer'')|int * 60 + 99}}'
target:
  entity_id: timer.sz_musik_timer

When i press the button i get count down time display 01:01:39 which is correct. But the countdown does not count down… it frozen to that value…

What else do i miss here? Oh mann…

But: the timer switches also from idle / unknown to active on button press.

But i can’t see a count down… and the finish time does not make sense to me (button press around post of my last question / post) )

grafik

that’s something that has been happening for a long time. The timer works although the remaining time doesn’t update. There are workarounds in the forum if you search, but nothing straightforward afaik.

EDIT:

1 Like

Another bunch of grey hairs done by HA :rofl:

Thanks again, also for the hints earlier today. It’s great to receive competent “just in time” help. @baunan @tom_l @obaldius

This one too if you are into Node red. It persists HA restarts, which the other doesn’t, and has a countdown that updates… It’s not difficult to set up, it’s actually very easy

1 Like

Well, i’am also on node RED and integrated some features already in HA… therefore great additional hint! Thanks for that!

btw, if you set the timer in lovelace through a lovelace badge instead of a card, it will update.

Hmm.

I’am on HKI Framework / Theme to setup the front end. I tested my problems in love lace to prove that it is not related to Homekit infused theme and not to mess up things or to make it even more complicated to analyze whats wrong.

Therefore i’am unsure how to realize that. I will check that out!

1 Like

Guess what?!

In hki it just works! I did not change anything… awesome.

And for the records, the code (in hki):

- type: custom:mod-card
  style: |
    ha-card {
      margin-left: 7px;
      margin-right: 7px;
    }
  card:
    type: vertical-stack
    cards:
      - type: markdown
        title: Music  Timer
        content: Alles nur ein Test
      - type: button
        name: Start Stop Timer
        icon: mdi:power
        show_name: false
        show_icon: true
        show_state: true
        tap_action: 
          action: call-service
          service: script.sz_start_music_countdown
          service_data: {}
          target: {}
      - type: entities
        entities:
          - entity: input_number.sz_musik_dauer
            name: Countdown in Minuten
          - entity: timer.sz_musik_timer
            name: Timer

Countdown counts down. Solved! Thanks again!

1 Like