Dynamic timer with input_number

Hi guys, I’ve set up a simple automation which starts a timer, that reads an input_number as its duration, once I enable a device.
I’d like to change this timer duration dynamically, how could I achieve this? For example looking at the screenshot below, I’d like to move the slider and at the same time changing the timer’s duration without the need to stop the device and start it again.

Screenshot_20230106_214409

Thanks!

The only service call able to set a timer’s duration is the one that starts a timer: timer.start

In other words, changing the duration of an active timer will restart it with the new duration (i.e. the timer.restart event will be produced).

If that’s acceptable, then all you need is an automation with a State Trigger to monitor the Input Number that represents the timer’s desired duration (in seconds). When triggered, it executes timer.start and sets the timer entity’s duration to the Input Number’s value.

alias: example
trigger:
  - platform: state
    entity_id: input_number.timer_duration
condition: []
action:
  - service: timer.start
    target:
      entity_id: timer.example
   data:
     duration: "{{ trigger.to_state.state | int(0) }}"

If the Input Number’s value is in minutes, simply change the template to this:

     duration: "{{ trigger.to_state.state | int(0) * 60 }}"
3 Likes

Thank you Taras for the explaination! It works fine!

Automation to restart the timer while running:

- id: '1673437367970'
  alias: Timer riavvio in corsa
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_number.minutes
    - input_number.hours
    id: timer_termocop_restart
  condition:
  - condition: device
    type: is_on
    device_id: 84a0f117d9dfe632aa10123865117f47
    entity_id: switch.termocoperta
    domain: switch
  action:
  - service: timer.start
    data:
      duration: '{{ states(''input_number.hours'') | int }}:{{ states(''input_number.minutes'')
        | int }}:00'
    target:
      entity_id: timer.termocoperta
  mode: single

Input Number in configuration.yaml:

input_number:
  hours:
    name: ore
    icon: mdi:clock-start
    initial: 0
    min: 0
    max: 3
    step: 1
  minutes:
    name: minuti
    icon: mdi:clock-start
    initial: 25
    min: 0
    max: 60
    step: 5

While this is the initial automation that starts the timer once the device is turned on:

- id: '1672994283326'
  alias: Termocoperta attivazione
  description: ''
  trigger:
  - platform: state
    entity_id:
    - switch.termocoperta
    from: 'off'
    to: 'on'
    id: termocop_on
  condition: []
  action:
  - service: timer.start
    data:
      duration: '{{ states(''input_number.hours'') | int }}:{{ states(''input_number.minutes'')
        | int }}:00'
    target:
      entity_id: timer.termocoperta
  mode: single
1 Like

Hi @123 , this automation worked for me for couple of months until I updated my HA to 2023.10.1-5, and something happened to not work anymore. Even I change the input_number value, it seems the timer start the "timer.example to its default time . If I set the “input_number.timer_duration” to 20s the automation will trigger to start from 20s, but at the next start of the automation it start from it`s default value, 60s.; something hapening to not retain the last value. Have you facing the same issue? Have you find an solution? Please help

According to the documentation for timer.start

I also saw this but Is there any way to fix this temple to render corectly?

I have not used the timer.change service call yet but you may wish to experiment with it (in combination with timer.start) and see if it can provide the desired behavior.

Off topic but I’m curious on how did you make this to apear on your dashboard?
How did you get the entity of the remaining time of the timer?

The Entities card dynamically reports a timer entity’s remaining time. The calculation is performed in the front-end (i.e. in the browser).