Input_datetime.set_datetime not Working

I’m following the docs and trying to create a script to set a datetime input to the current date/time but it’s giving me an error:

  - service: input_datetime.set_datetime
    entity_id: input_datetime.date_and_time
    data_template:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

Failed to call service input_datetime/set_datetime. Invalid datetime specified: {{ now().strftime('%Y-%m-%d %H:%M:%S') }} for dictionary value @ data['datetime']

What you have done is correct according to the docs:

Screenshot_2020-04-13 Input Datetime

You should raise a new issue. https://github.com/home-assistant/core/issues

1 Like

Did you create the input_datetime.date_and_time object? Does it have both date and time?

Since your variables so closely match the docs, did you name it correctly?

They named theirs ‘both_date_and_time’ in the config, but then call input_datetime.date_and_time in the example for some reason.

# Example configuration.yaml entry
input_datetime:
  both_date_and_time: # <----
    name: Input with both date and time
    has_date: true
    has_time: true
  # Sets date and time to date and time from datetime object (current date and time in this example)
  - service: input_datetime.set_datetime
    # But in the e
    entity_id: input_datetime.date_and_time  # <-----
    data_template:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

how about this

- service: input_datetime.set_datetime
  data_template:
    entity_id: input_datetime.date_and_time
    datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

Has anyone been able to reproduce this error? I can’t; it works for me.

Testing Procedure

I created this input_datetime:

input_datetime:
  dt1:
    name: Date Time 1
    has_date: true
    has_time: true
    initial: "2019-05-01 07:30"

and these two scripts:

#script:
  set_dt1_a:
    alias: version a
    sequence:
      service: input_datetime.set_datetime
      entity_id: input_datetime.dt1
      data_template:
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

  set_dt1_b:
    alias: version b
    sequence:
      service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.dt1
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

Executing either of the two scripts correctly sets input_datetime.dt1 to the current date and time. The system log contains no warnings or errors.

Screenshot from 2020-04-13 08-03-35


EDIT
Tested with Home Assistant Core 0.108.0 and 0.108.3.

Looks like it was due to the fact I was using the GUI to build the automation. If I use automation.yaml and use data_template it works.

Thanks all for looking at this!

I’d suggest to open a new issue on Github if you use a reasonably new HA and can reproduce the issue with Automation editor.

1 Like

It seems that I have the same issue, even I edit the script in Script.yaml. Can anybody help me? I got the same error message:

home assistant Failed to call service script Invalid time specified: for dictionary value @ data[‘time’]

while my definition is:


input_datetime:
  get_up_time:
    name: get_up_time
    has_date: false
    has_time: true



test:
  alias: Test
  sequence:
  - service: input_datetime.set_datetime
    data:
      time: >
        {% if is_state('WorkdayInDE', 'on') %}
          '08:00:00'
        {% else %}
          '08:30:00'
        {% endif %}
    entity_id: input_datetime.get_up_time
  mode: single

after the change below, it works


test:
  alias: Test
  sequence:
  - service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.get_up_time
      time: '{% if is_state("WorkdayInDE", "on") %}08:00:00{% else %}08:30:00{% endif %}'
  mode: single