YAML syntax to set timer.start duration: with an input_datetime entity?

Hi, I’m struggling to identify the correct YAML syntax in a service timer.start block to set the duration from an input_datetime entity.
I’ve read all the related Documentation pages and searched the Community items, but not yet found the solution. I’ve tried using:
A)

    - service: timer.start
      data:
        duration: input_datetime.test_time
      target:
        entity_id: timer.test_timer

which results in the error “Stopped because an error … offset input_datetime.test_time should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘duration’]”

B) as A) but with duration line > duration: “{{input_datetime.test_time}}”
resulting in “Error rendering data template: Undefined error: ‘input_datetime’ is undefined”

C) as A) but with > duration: “{{ ‘input_datetime.test_time’ }}”
results in error same as A).

What is the correct YAML syntax to use for setting the duration from an input_datetime?

Many thanks…

This is how I have done it for a bathroom fan: I have set it to 180 seconds but you could use an input number. Datetime will contain the string containing a time, date or date and time but is not a duration.

- id: '1612134477709'
  alias: Half Bath Timer Activation
  description: Activate the timer for the half bath
  trigger:
  - platform: state
    entity_id: switch.half_bath_light
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: timer.start
    data:
      duration: '180'
    target:
      entity_id: timer.halfbath_timer
  mode: single

- id: 'Half Bath Fan Run'
  alias: Half Bath Fan Run
  description: Turn on Half bath fan if light was turned after after being on for
    at least 3 minutes
  trigger:
  - platform: state
    entity_id: switch.half_bath_light
    from: 'on'
    to: 'off'
  condition:
  - condition: state
    entity_id: timer.halfbath_timer
    state: idle
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.half_bath_fan
  - delay:
      seconds: "{{ states('input_number.fan_runtime')| int(30) }}"
  - service: switch.turn_off
    target:
      entity_id: switch.half_bath_fan
  mode: single

The first starts a timer and the second automation turns on the fan if the timer is at or below 0.

Hope this helps.

Hi, and thanks for replying.
Your first part, as you say, uses "duration: ‘180’ ". I’ve tried it with the ‘180’ replaced by an input_number, but that still fails - seems to want something giving format ‘HH:MM’, ‘HH:MM:SS’.
An input_datetime with a Time value ought to be a duration, since it’s a ‘HH:MM:SS’ value.
Seems I’m missing the correct formatting syntax.
I don’t understand the jinja parts, if that’s what’s needed, and what {{ states(…) }} is doing. Cannot find that documented in HA Docs so far.

The issue is that you are not using valid templates. I prefer to use Input number helpers because they tend to be easier.

For seconds you can use a template directly as the value for duration

    - service: timer.start
      data:
        duration: "{{ states('input_number.test_time') }}"
      target:
        entity_id: timer.test_timer

You can also supply hours, minutes, or seconds (or a combination of any of those) using a dictionary for duration.

service: timer.start
data:
  duration: 
    minutes: "{{ states('input_number.test_time')}}"
target:
  entity_id: timer.test_timer

You can also use Input datetime (time-only) helpers if that is your preference.

    - service: timer.start
      data:
        duration: "{{ states('input_datetime.test_time') }}"
      target:
        entity_id: timer.test_timer
1 Like

Hi and thank you Didgeridrew. That’s what I needed. I’ll give them a try.

Is this "{{ states( …etc… usage and it’s variations/syntax/options documented somewhere? I’ve yet to find it.

Again, my thanks.

Somehow this as well as similar solutions just aren’t working for me. I keep getting the “Failed to call service timer/start. offset {{ states(‘input_datetime.aquarium_small_timer_input’) }} should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘duration’]” error message even doing (as far as I know) exactly what @Didgeridrew described.

This is the code for the button that I’m trying to use to start the timer.

              - show_name: true
                show_icon: true
                type: button
                tap_action:
                  action: call-service
                  service: timer.start
                  target:
                    entity_id: timer.aquarium_small_light_timer
                  data:
                    duration: '{{ states(''input_datetime.aquarium_small_timer_input'') }}'
                entity: timer.aquarium_small_light_timer
                show_state: true
                hold_action:
                  action: call-service
                  service: timer.cancel
                  target:
                    entity_id: timer.aquarium_small_light_timer
                  data: {}
                name: Start Timer "small"

And if I put this into the Template thingy under Developer tools

{{ states("input_datetime.aquarium_small_timer_input") }}

I get the correct result

00:05:00

Am I doing something wrong?

The issue is that you are trying to use templating in the action block of a Dashboard card. Most core cards do not accept templating at all, and even custom cards that do accept templating, often do not allow it in actions. The most reliable workaround is to move your action with templated logic into a script, then have the button action call the script.

Ahh I see. I’ll try the script workaround then. Thanks a lot :slight_smile:

There is also a relatively new custom feature for Tile cards that adds service calls and allows templating… you may be able to use that, but I haven’t experimented with it much yet.