Lovelace input time for timer in automation

Hello All,

I have an automation that works how I want for the most part but I would love to be able to edit the timer length from love lace. I can’t seem to get the right formatting to make the lovelace input work for my automation.

For this action:

  action:
  - data: {}
    entity_id: timer.garagelight
    service: timer.start

I would like to reference the input_datetime input I made for love lace.

I have tried the following with no luck:

entity_id: timer.garagelight
duration: {{ states.input_datetime.gltime.state }}

*************************************************************

entity_id: timer.garagelight
data_template:    
  duration: >
    {{ states.input_datetime.gltime.state }}

*************************************************************
data:
  entity_id: timer.garagelight
data_template:
  duration: |
    {{ states.input_datetime.gltime.state }}
service: timer.start

Any help would be appreciated.

you can’t (easily?..) use an input datetime for the duration.

you probably should use two input numbers - one for the minutes & another for the seconds:

then you can do this:

- service: timer.start
  data_template:
    entity_id: timer.your_timer
    duration: "00:{{ states('input_number.timer_minutes') | int }}:{{ states('input_number.timer_seconds') | int }}"
2 Likes

OMG! Thank you!!! I have been banging my head against the wall for awhile now! Just using minutes is perfect. For my education why doesn’t input_datetime work? I don’t understand all these rules. I am wanting to create a scheduler for turning on and off lights at specific times during the day. I am guessing I will have to create an input number for each segment like hour and minute?

You can use input_datetimes for a schedule (time on/time off).

And just to check, I ran a test setup to see if a timer duration can accept an input_datetime from an automation and indeed it can. So I was wrong. :slightly_frowning_face:

here is the code I used for it if you want to try it or just use my code in your setup:

timer:
  test_timer:

input_boolean:
  testing:

input_datetime:
  test_time:
    name: test timer duration
    initial: "00:13:00"
    has_date: false
    has_time: true
  
automation:
  alias: test time
  trigger:
    platform: state
    entity_id: input_boolean.testing
    to: 'on'
  action:
    service: timer.start
    entity_id: timer.test_timer
    data_template:
      duration: "{{ states.input_datetime.test_time.state }}"

Hmm… So it seems I was just missing the quotes from what I was trying. Thanks again for the help!

Kind of…

In the first attempt you are missing "data_template " And quotes.

In the third attempt you have two “data” entries (data and data_template) and I’m not sure that will work.

The second attempt should have worked I think. The syntax is at least correct.

Unless you were using a datetime with date in it. Then it wouldn’t work without templating out the date part.

Hi, Im trying to follow your code, cos I would like to do the same, but I’m experiencing no result at all, probably cos there are errors in the log…let me share if you don’t mind, this is the code for the simple automation:

- id: '1594636020436'
  alias: Cambio Horas depuración
  description: ''
  trigger:
  - entity_id: input_datetime.tiempo_depuracion
    platform: state
  condition: []
  action:
    entity_id: timer.swiming_pool
    service: timer.start
    data_template:
      duration: "{{states(input_datetime.tiempo_depuracion)}}"

When I execute it or change the hours in the UI the automation is executed, but nothing happens (the timer does not change). And I saw this error in the logs:

jinja2.exceptions.UndefinedError: 'input_datetime' is undefined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 132, in async_prepare_call_from_config
    template.render_complex(config[CONF_SERVICE_DATA_TEMPLATE], variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 72, in render_complex
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 72, in <dictcomp>
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 74, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 230, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'input_datetime' is undefined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 153, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 656, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 252, in _prep_call_service_step
    return async_prepare_call_from_config(self._hass, self._action, self._variables)
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 135, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'input_datetime' is undefined

Any idea how to resolve it?

There was a syntax error, the entity_id inside the states must be with ’ ':

- id: '1594636020436'
  alias: Cambio Horas depuración
  description: ''
  trigger:
  - entity_id: input_datetime.tiempo_depuracion
    platform: state
  condition: []
  action:
    entity_id: timer.swiming_pool
    service: timer.start
    data_template:
      duration: "{{states('input_datetime.tiempo_depuracion)'}}"

I’d love to change the start duration from UI too, and googling showed me a lot other people would love it too.
Is it possible to allow the change of the duration parameter of the timer?

It should work as given above. You can’t change the duration of the timer using the UI directly, but you can achieve the wanted configuration if you always use a mechanism that gives the duration as parameter to the timer, so the duration value always gets ignored.

You need:

  1. The timer
    (example: timer.rolladen_terrasse_delay)
  2. A datetime-helper (time) for the duration which you can edit in the UI
    (example: input_datetime.rolladen_terrasse_delay_dauer)
  3. An automation or script that starts the timer

As I want to delay the closing of the door for someone to be able to come inside before the blinds close, I trigger this whenever blinds “Wohnzimmer” (living room) get closed.

If you do a script for this, you won’t need a trigger and call the script whenever you need to start the timer.

alias: Zu Terrasse Delay starten
description: Verzögertes Schließen Terrasse für Anzeige "Geht runter"
triggers:
  - event_type: call_service
    event_data:
      domain: scene
      service: turn_on
      service_data:
        entity_id: scene.rolladen_geschlossen_wohnzimmer
    trigger: event
conditions: []
actions:
  - action: timer.start
    target:
      entity_id: timer.rolladen_terrasse_delay
    data_template:
      duration: "{{ states('input_datetime.rolladen_terrasse_delay_dauer') }}"
mode: single

Didn’t run it by now, but I’m quite confident it will work this way.

Edit: It makes no sense to give HH:MM for this purpose, will change it to number of seconds helper, but it should be similar. input_number instead of input_datetime and cast the thing to int should do fine.